Skip to content
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

Decouple start date logic #1461

Merged
merged 2 commits into from
Nov 4, 2024
Merged

Conversation

jonathangoulding
Copy link
Collaborator

https://eaflood.atlassian.net/browse/WATER-4687

As part of work to introduce quarterly returns we have noticed some logic leaking out of the start date functionality.

Outside of this service we do not care how the start date is calculated.

This change introduces a variable 'returnVersionStartDate' to the session for services outside of start to use.

Start date has been left as intact as possible and this change has been added when the start date saves the session so the internal workings have been left alone.

https://eaflood.atlassian.net/browse/WATER-4687

As part of work to introduce quarterly returns we have noticed some logic leaking out of the start date functionality.

Outside of this service we do not care how the start date is calculated.

This change introduces a variable 'returnVersionStartDate' to the session for services outside of start to use.

Start date has been left as intact as possible and this change has been added when the start date saves the session so the internal workings have been left alone.
@jonathangoulding jonathangoulding added the housekeeping Refactoring, tidying up or other work which supports the project label Nov 1, 2024
@jonathangoulding jonathangoulding self-assigned this Nov 1, 2024
@jonathangoulding jonathangoulding requested review from Jozzey, robertparkinson and rvsiyad and removed request for robertparkinson, Jozzey and rvsiyad November 1, 2024 15:45
robertparkinson
robertparkinson previously approved these changes Nov 1, 2024
They are not used / broken
@jonathangoulding jonathangoulding merged commit b02e8b2 into main Nov 4, 2024
6 checks passed
@jonathangoulding jonathangoulding deleted the refactor-return-versions-start-date branch November 4, 2024 11:21
Cruikshanks added a commit that referenced this pull request Nov 8, 2024
https://eaflood.atlassian.net/browse/WATER-4687

In [Decouple start date logic](#1461) we made a change to support adding quarterly returns.

It was determined the logic for working out the start date from the values entered by the user was being duplicated in other places. The change ensured the logic stays in just the `SubmitStartDateService`, and added a new property `returnVersionStartDate` that other services could use instead.

Unfortunately, an error was introduced.

```javascript
  // app/services/return-versions/setup/submit-start-date.service.js

  // Reminder! Because of the unique qualities of Javascript, Year and Day are literal values, month is an index! So,
  // January is actually 0, February is 1 etc. This is why we deduct 1 from the month.
  session.returnVersionStartDate = new Date(`${payload['start-date-year']}-${payload['start-date-month'] - 1}-${payload['start-date-day']}`).toISOString().split('T')[0]
```

This way of creating the date was added. It uses a [template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to combine the values provided by the user and pass it as an argument o JavaScript's `new Date()`.

But it also applies a common fix for the fact JavaScript expects month to be provided as an index, not literally! So, January = 0, February = 1 etc.

The problem is `new Date('2024-04-01')` is not the same as `new Date(2024, 4, 1)`. The first _will_ return you 1 April 2024, the second will return 1 March 2024.

JavaScript understands that 'month' is literal when you provide a string argument to `new Date()`. It is only when passing month as a separate value that you have to treat it as an index.

The result for the return versions setup journey is that a user is entering, for example, `13 May 2019` on the start page, but we're storing (and would eventually persist!) `13 April 2019`.

This change fixes the issue.
jonathangoulding pushed a commit that referenced this pull request Nov 8, 2024
* Fix broken return version start date page

https://eaflood.atlassian.net/browse/WATER-4687

In [Decouple start date logic](#1461) we made a change to support adding quarterly returns.

It was determined the logic for working out the start date from the values entered by the user was being duplicated in other places. The change ensured the logic stays in just the `SubmitStartDateService`, and added a new property `returnVersionStartDate` that other services could use instead.

Unfortunately, an error was introduced.

```javascript
  // app/services/return-versions/setup/submit-start-date.service.js

  // Reminder! Because of the unique qualities of Javascript, Year and Day are literal values, month is an index! So,
  // January is actually 0, February is 1 etc. This is why we deduct 1 from the month.
  session.returnVersionStartDate = new Date(`${payload['start-date-year']}-${payload['start-date-month'] - 1}-${payload['start-date-day']}`).toISOString().split('T')[0]
```

This way of creating the date was added. It uses a [template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to combine the values provided by the user and pass it as an argument o JavaScript's `new Date()`.

But it also applies a common fix for the fact JavaScript expects month to be provided as an index, not literally! So, January = 0, February = 1 etc.

The problem is `new Date('2024-04-01')` is not the same as `new Date(2024, 4, 1)`. The first _will_ return you 1 April 2024, the second will return 1 March 2024.

JavaScript understands that 'month' is literal when you provide a string argument to `new Date()`. It is only when passing month as a separate value that you have to treat it as an index.

The result for the return versions setup journey is that a user is entering, for example, `13 May 2019` on the start page, but we're storing (and would eventually persist!) `13 April 2019`.

This change fixes the issue.

* Simplify licence start date handling

We don't need to call `toISOString()` and `split()`. It works perfectly, as it has done in other places in the code without these additions.

* Oops!

* Fix test that masked the issue

The user has entered '26 11 2023' in the UI. Therefore, they should expect to see `26 November 2023` when `returnVersionStartDate` is used or displayed anywhere in the UI.

The test wasn't highlighting something was wrong because it was asserting this fact.

* Fix the issue

* I like spacing, but not that much!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
housekeeping Refactoring, tidying up or other work which supports the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants