-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Determine licence start date 4 returns reqs setup #655
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://eaflood.atlassian.net/browse/WATER-4261 > This change supports the return requirements setup work Tickets WATER-4263 and WATER-4266 cover adding controls and validation to the start date page in the set-up journeys (returns required and not required). Users will be offered the choice to choose an existing start date or enter one manually. The existing start date needs to come from the current licence version record. So, this change adds functionality to the `InitiateReturnRequirementSessionService` to add the start date to the session for use later in the page.
With this change we should return only the 'current' version for a licence which will give us the start date to use in the `/start-date` page. > This will fail the unit test. More in the next commit!
Well this one had us puzzled! The type for `water.licence_versions.start_date` is `DATE` in the DB. Ideally you want all dates to be held as a timestamp else PG will default to the local systems time zone when returning a value. A timestamp ensures you get to set what the time zone should be (UTC in our case). But we are where we are. We still expect this to come back from the DB when queried as a JavaScript `Date`. But we were getting a string; `'2022-05-01T00:00:00.000Z'`. We went down a rabbit hole of first assuming pg was returning the value as a string and therefore we needed to add some custom parsing to `db/db.js` ```javascript pg.types.setTypeParser(pg.types.builtins.DATE, (value) => { return value === null ? null : new Date(value) }) ``` But that didn't work. Then we wondered whether Objection.js was doing something when parsing the values from Knex.js. So we plugged into the model hooks and confirmed all was well. ```javascript $parseDatabaseJson (json) { // Remember to call the super implementation. json = super.$parseDatabaseJson(json) // Remember to always check if the json object has the particular // field. It may not exist if the user has used `select('id')` // or any other select that excludes the field. if (json.startDate !== undefined) { console.log('🚀 ~ LicenceVersionModel ~ $parseDatabaseJson ~ startDate:', json.startDate) } return json } $formatJson(json) { // Remember to call the super class's implementation. json = super.$formatJson(json) // Do your conversion here. // Remember to always check if the json object has the particular // field. It may not exist if the user has used `select('id')` // or any other select that excludes the field. if (json.startDate !== undefined) { console.log('🚀 ~ LicenceVersionModel ~ $formatJson ~ startDate:', json.startDate) } return json } ``` In all cases the value was presenting as a `Date` and not a string. But when we output `session` immediately after creating it we go this. ```javascript 🚀 ~ _createSession ~ session: SessionModel { data: { journey: 'returns-required', licence: { id: '8674134c-98b0-45ed-ac90-443dc739434b', startDate: '2022-05-01T00:00:00.000Z', licenceRef: '01/426', licenceHolder: 'Licence Holder Ltd' } }, id: '49e601ac-9342-420c-94a0-0adb3dbb104f', createdAt: 2024-01-11T13:16:23.348Z, updatedAt: 2024-01-11T13:16:23.348Z } ``` You can see `startDate:` is a string! 🤬😩. More out of desperation we amended the `returning()` statement to just specify the `id` (as that is all we really wanted back). Hazzah! This worked. ```javascript 🚀 ~ _createSession ~ session: SessionModel { data: { licence: { id: '9032aa77-47b0-450a-81db-43cd7e5056f3', licenceRef: '01/615', licenceHolder: 'Licence Holder Ltd', startDate: 2022-05-01T00:00:00.000Z }, journey: 'returns-required' }, id: '985c5a14-07ca-426c-bc1d-2712377c9dcc' } ``` So, we have to conclude something 'extra' is going on when `returning('*')` is used. We need to try and be aware of this in future. But now we have a solution we've not going digging to find out what and whether its **Knex.js** or **Objection.js** at the root.
robertparkinson
approved these changes
Jan 11, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
https://eaflood.atlassian.net/browse/WATER-4261
Tickets WATER-4263 and WATER-4266 cover adding controls and validation to the start date page in the set-up journeys (returns required and not required).
Users will be offered the choice to choose an existing start date or enter one manually. The existing start date needs to come from the current licence version record.
So, this change adds functionality to the
InitiateReturnRequirementSessionService
to add the start date to the session for use later in the page.Note about using
returning(*)
In our new test we compared
startDate
in the result withnew Date('2022-05-01')
. It kept failing becausestartDate
was coming back as a string;'2022-05-01T00:00:00.000Z'
.Well, this one had us puzzled!
The type for
water.licence_versions.start_date
isDATE
in the DB. We still expect this to come back from the DB when queried as a JavaScriptDate
. But we were getting a string;'2022-05-01T00:00:00.000Z'
.We went down a rabbit hole of first assuming pg was returning the value as a string and therefore we needed to add some custom parsing to
db/db.js
But that didn't work. Then we wondered whether Objection.js was doing something when parsing the values from Knex.js. So we plugged into the model hooks and confirmed all was well.
In all cases, the value was presented as a
Date
and not a string. But when we outputsession
immediately after creating it we go this.You can see
startDate:
is a string! 🤬😩. More out of desperation we amended thereturning()
statement to just specify theid
(as that is all we wanted back). Hazzah! This worked.So, we have to conclude something 'extra' is going on when
returning('*')
is used. We need to try and be aware of this in future. But now we have a solution we're not going digging to find out what and whether its Knex.js or Objection.js at the root.