diff --git a/src/app/garfile/arrival/index.njk b/src/app/garfile/arrival/index.njk index 6d1ddef1..fe457ba9 100644 --- a/src/app/garfile/arrival/index.njk +++ b/src/app/garfile/arrival/index.njk @@ -131,8 +131,18 @@
+ type="radio" + value="Yes" + {% set IATA_CODE_LENGTH = 3 %} + {% set ICAO_CODE_LENGTH = 4 %} + {{ + 'checked' if cookie.getGarArrivalVoyage().arrivalPort === '' + or cookie.getGarArrivalVoyage().arrivalPort.length === IATA_CODE_LENGTH + or cookie.getGarArrivalVoyage().arrivalPort.length === ICAO_CODE_LENGTH + }} + data-aria-controls="portChoiceCodeContainer" + aria-labelledby="portChoiceCode-label" + >
@@ -154,8 +164,34 @@
+ type="radio" + value="No" + {# + In some instances when a user submits an empty input field e.g. Latitude: [] [28] [] [], + Not-A-Number (NaN) will be returned because user input is converted to a floating-point number + with parseFloat(). + #} + + {% set DECIMAL_POINT = '.' %} + {% set NOT_A_NUMBER = 'NaN' %} + {# + If both LAT and LONG values exist. The "No" port choice will be selected if both the LAT and LONG + values contain a decimal point or either the LAT or LONG values are NaN. + #} + {{ + 'checked' if cookie.getGarArrivalVoyage().arrivalPort != undefined + and ( + cookie.getGarArrivalVoyage().arrivalLat.includes(DECIMAL_POINT) + and cookie.getGarArrivalVoyage().arrivalLong.includes(DECIMAL_POINT) + or ( + cookie.getGarArrivalVoyage().arrivalLat == NOT_A_NUMBER + or cookie.getGarArrivalVoyage().arrivalLong == NOT_A_NUMBER + ) + ) + }} + data-aria-controls="portChoiceCoordinatesContainer" + aria-labelledby="portChoiceCoordinates-label" + >
@@ -200,8 +236,6 @@ -
{# End arrival coords #}
diff --git a/src/app/garfile/arrival/post.controller.js b/src/app/garfile/arrival/post.controller.js index 10bcf18f..0a96ccb2 100644 --- a/src/app/garfile/arrival/post.controller.js +++ b/src/app/garfile/arrival/post.controller.js @@ -86,7 +86,7 @@ const buildValidations = (voyage) => { const validations = [ [new ValidationRule(validator.realDate, 'arrivalDate', arriveDateObj, realDateMsg)], - [new ValidationRule(validator.currentOrFutureDate, 'arrivalDate', arriveDateObj, __('field_arrival_date_too_far_in_future'))], + [new ValidationRule(validator.currentOrPastDate, 'arrivalDate', arriveDateObj, __('field_arrival_date_too_far_in_future'))], [new ValidationRule(validator.dateNotTooFarInFuture, 'arrivalDate', arriveDateObj, __('field_arrival_date_too_far_in_future'))], [new ValidationRule(validator.validTime, 'arrivalTime', arrivalTimeObj, timeMsg)], [new ValidationRule(validator.notEmpty, 'portChoice', voyage.portChoice, portChoiceMsg)], diff --git a/src/app/garfile/departure/get.controller.js b/src/app/garfile/departure/get.controller.js index 33833940..bbf25a04 100644 --- a/src/app/garfile/departure/get.controller.js +++ b/src/app/garfile/departure/get.controller.js @@ -8,9 +8,6 @@ module.exports = (req, res) => { garApi.get(cookie.getGarId()).then((apiResponse) => { const parsedResponse = JSON.parse(apiResponse); - if (parsedResponse.departurePort === 'ZZZZ') { - parsedResponse.departurePort = 'YYYY'; - } cookie.setGarDepartureVoyage(parsedResponse); return res.render('app/garfile/departure/index', { cookie }); diff --git a/src/app/garfile/departure/index.njk b/src/app/garfile/departure/index.njk index a93d8009..d5567468 100644 --- a/src/app/garfile/departure/index.njk +++ b/src/app/garfile/departure/index.njk @@ -134,8 +134,18 @@
+ type="radio" + value="Yes" + {% set IATA_CODE_LENGTH = 3 %} + {% set ICAO_CODE_LENGTH = 4 %} + {{ + 'checked' if cookie.getGarDepartureVoyage().departurePort === '' + or cookie.getGarDepartureVoyage().departurePort.length === IATA_CODE_LENGTH + or cookie.getGarDepartureVoyage().departurePort.length === ICAO_CODE_LENGTH + }} + data-aria-controls="portChoiceCodeContainer" + aria-labelledby="portChoiceCode-label" + >
@@ -157,8 +167,34 @@
+ type="radio" + value="No" + {# + In some instances when a user submits an empty input field e.g. Latitude: [] [28] [] [], + Not-A-Number (NaN) will be returned because user input is converted to a floating-point number + with parseFloat(). + #} + + {% set DECIMAL_POINT = '.' %} + {% set NOT_A_NUMBER = 'NaN' %} + {# + If both LAT and LONG values exist. The "No" port choice will be selected if both the LAT and LONG + values contain a decimal point or either the LAT or LONG values are NaN. + #} + {{ + 'checked' if cookie.getGarDepartureVoyage().departurePort != undefined + and ( + cookie.getGarDepartureVoyage().departureLat.includes(DECIMAL_POINT) + and cookie.getGarDepartureVoyage().departureLong.includes(DECIMAL_POINT) + or ( + cookie.getGarDepartureVoyage().departureLat == NOT_A_NUMBER + or cookie.getGarDepartureVoyage().departureLong == NOT_A_NUMBER + ) + ) + }} + data-aria-controls="portChoiceCoordinatesContainer" + aria-labelledby="portChoiceCoordinates-label" + >
@@ -187,8 +223,6 @@ -
@@ -211,8 +245,6 @@ -
{# End departure coords #}
diff --git a/src/app/garfile/departure/post.controller.js b/src/app/garfile/departure/post.controller.js index e27d190f..5e929421 100644 --- a/src/app/garfile/departure/post.controller.js +++ b/src/app/garfile/departure/post.controller.js @@ -4,7 +4,7 @@ const validator = require('../../../common/utils/validator'); const CookieModel = require('../../../common/models/Cookie.class'); const garApi = require('../../../common/services/garApi'); const ValidationRule = require('../../../common/models/ValidationRule.class'); -const airportValidation = require('../../../common/utils/airportValidation'); +//const airportValidation = require('../../../common/utils/airportValidation'); const createValidationChains = (voyage) => { @@ -46,7 +46,7 @@ const createValidationChains = (voyage) => { const validations = [ [new ValidationRule(validator.realDate, 'departureDate', departDateObj, __('field_departure_real_date_validation'))], - [new ValidationRule(validator.currentOrFutureDate, 'departureDate', departDateObj, __('field_departure_date_too_far_in_future'))], + [new ValidationRule(validator.currentOrPastDate, 'departureDate', departDateObj, __('field_departure_date_too_far_in_future'))], [new ValidationRule(validator.dateNotTooFarInFuture, 'departureDate', departDateObj, __('field_departure_date_too_far_in_future'))], [new ValidationRule(validator.validTime, 'departureTime', departureTimeObj, __('field_departure_real_time_validation'))], [new ValidationRule(validator.notEmpty, 'portChoice', voyage.portChoice, __('field_port_choice_message'))], @@ -114,7 +114,6 @@ module.exports = async (req, res) => { // Define voyage const voyage = req.body; delete voyage.buttonClicked; - // TODO: Update this once the intended 'unknown' port code is discovered. if (voyage.portChoice === 'No') { logger.debug("Testing departure Lat and Long values..."); diff --git a/src/common/utils/validator.js b/src/common/utils/validator.js index 4d1ee7da..7278dfae 100644 --- a/src/common/utils/validator.js +++ b/src/common/utils/validator.js @@ -196,29 +196,37 @@ function validGender(value) { return validValues.includes(value); } -function currentOrFutureDate(dObj) { +/** + * Checks that the supplied date is not a past date + * @param {Object} dObjh Date - can be js Date object or the {d:,m:,y} type object that is used in the UI + * @returns {Boolean} true if not past date, false if past date + */ +function currentOrPastDate(dObj) { const currDate = new Date(); + const currMonth = currDate.getMonth() + 1; + + /* + Returns true if all dates fields are empty to avoid duplicate error messages being displayed. + The dateNotTooFarInFuture function will cover this error. + */ + if([dObj.d,dObj.m,dObj.y].includes('')) { + return true; + } if (dObj.y < currDate.getFullYear()) { return false; } - if (dObj.y > currDate.getFullYear()) { - return true; - } if (dObj.y == currDate.getFullYear()) { - if (dObj.m < currDate.getMonth() + 1) { + if (dObj.m < currMonth) { return false; } - if (dObj.m > currDate.getMonth() + 1) { - return true; - } - if (dObj.m == currDate.getMonth() + 1) { + if (dObj.m == currMonth) { return dObj.d >= currDate.getDate(); } } - return false; -} + return true; +} /** * Check that supplied date is within an acceptable range (currently within 1 month from Date.now()) @@ -725,7 +733,7 @@ module.exports = { realDateInFuture, bornAfter1900, realDateFromString, - currentOrFutureDate, + currentOrPastDate, validTime, validFlag, validPort, diff --git a/src/test/garfile/arrival/post.controller.test.js b/src/test/garfile/arrival/post.controller.test.js index ed1a0cb6..c6224cdf 100644 --- a/src/test/garfile/arrival/post.controller.test.js +++ b/src/test/garfile/arrival/post.controller.test.js @@ -175,7 +175,7 @@ describe('Arrival Post Controller', () => { expect(garApi.patch).to.not.have.been.called; expect(res.render).to.have.been.calledWith('app/garfile/arrival/index', { cookie, - errors: [new ValidationRule(validator.currentOrFutureDate, 'arrivalDate', { d: "30", m: "5", y: "2010" }, 'Arrival date must be today and cannot be more than 1 month in the future')], + errors: [new ValidationRule(validator.currentOrPastDate, 'arrivalDate', { d: "30", m: "5", y: "2010" }, 'Arrival date must be today and cannot be more than 1 month in the future')], }); }); }); diff --git a/src/test/validators.test.js b/src/test/validators.test.js index 4050182d..e02d0621 100644 --- a/src/test/validators.test.js +++ b/src/test/validators.test.js @@ -201,11 +201,11 @@ describe('Validator', () => { const nextMonth = currDate.getMonth() + 1; const year = currDate.getFullYear(); const nextYear = currDate.getFullYear() + 1; - expect(validator.currentOrFutureDate(genDateObj(day, nextMonth, year))).to.be.true; - expect(validator.currentOrFutureDate(genDateObj(day, nextMonth, nextYear))).to.be.true; - expect(validator.currentOrFutureDate(genDateObj(nextDay, month, nextYear))).to.be.true; - expect(validator.currentOrFutureDate(genDateObj('22', '12', '2055'))).to.be.true; - expect(validator.currentOrFutureDate(genDateObj('01', '01', '2020'))).to.be.true; + expect(validator.currentOrPastDate(genDateObj(day, nextMonth, year))).to.be.true; + expect(validator.currentOrPastDate(genDateObj(day, nextMonth, nextYear))).to.be.true; + expect(validator.currentOrPastDate(genDateObj(nextDay, month, nextYear))).to.be.true; + expect(validator.currentOrPastDate(genDateObj('22', '12', '2055'))).to.be.true; + expect(validator.currentOrPastDate(genDateObj('01', '01', '2020'))).to.be.true; clock.restore(); }); @@ -213,10 +213,10 @@ describe('Validator', () => { it('Should return true for a date after, edge cases', () => { const clock = sinon.useFakeTimers(new Date(2011, 11, 31).getTime()); - expect(validator.currentOrFutureDate(genDateObj(1, 1, 2011))).to.be.false; - expect(validator.currentOrFutureDate(genDateObj(31, 1, 2011))).to.be.false; - expect(validator.currentOrFutureDate(genDateObj(1, 1, 2012))).to.be.true; - expect(validator.currentOrFutureDate(genDateObj(31, 12, 2011))).to.be.true; + expect(validator.currentOrPastDate(genDateObj(1, 1, 2011))).to.be.false; + expect(validator.currentOrPastDate(genDateObj(31, 1, 2011))).to.be.false; + expect(validator.currentOrPastDate(genDateObj(1, 1, 2012))).to.be.true; + expect(validator.currentOrPastDate(genDateObj(31, 12, 2011))).to.be.true; clock.restore(); }); @@ -229,9 +229,9 @@ describe('Validator', () => { const month = currDate.getMonth() + 1; const previousMonth = currDate.getMonth() - 1; const year = currDate.getFullYear(); - expect(validator.currentOrFutureDate(genDateObj(day, month, year))).to.be.false; - expect(validator.currentOrFutureDate(genDateObj(currDate.getDate(), previousMonth, year))).to.be.false; - expect(validator.currentOrFutureDate(genDateObj('22', '12', '1999'))).to.be.false; + expect(validator.currentOrPastDate(genDateObj(day, month, year))).to.be.false; + expect(validator.currentOrPastDate(genDateObj(currDate.getDate(), previousMonth, year))).to.be.false; + expect(validator.currentOrPastDate(genDateObj('22', '12', '1999'))).to.be.false; clock.restore(); });