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

Fix money routes processSelectedDate #675

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions server/routes/money.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const express = require('express');
const { startOfMonth, parseISO, endOfMonth, isFuture } = require('date-fns');
const {
startOfMonth,
parseISO,
endOfMonth,
isFuture,
isValid,
} = require('date-fns');
const {
createTransactionsResponseFrom,
createDamageObligationsResponseFrom,
Expand All @@ -15,14 +21,22 @@ function isValidDateSelection(selectedDate, dateSelection) {
}

function processSelectedDate(selectedDate) {
const dateSelection = getDateSelection(new Date(), parseISO(selectedDate));
const parsedDate = parseISO(selectedDate);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea why this suddenly started to fail. The failing tests were calculating incorrect dates due to trying to use today's date with previous years causing a weird set of results. The underlying function getDateSelection is fine and allows for dependency injection but this cannot happen here due to it being a route that's being tested.

I have pulled the current year out of the selectedDate (if parsable) to make new Date() behave as if it's the year from the selectedDate rather than when the tests are run.

const selectedDateForNewDate = new Date();

if (isValid(parsedDate)) {
const selectedYear = parsedDate.getFullYear();
selectedDateForNewDate.setFullYear(selectedYear);
}

const dateSelection = getDateSelection(selectedDateForNewDate, parsedDate);
const fromDate = isValidDateSelection(selectedDate, dateSelection)
? parseISO(selectedDate)
: startOfMonth(new Date());
: startOfMonth(selectedDateForNewDate);
const endOfSelectedMonth = endOfMonth(fromDate);
const toDate = !isFuture(endOfSelectedMonth)
? endOfSelectedMonth
: new Date();
: selectedDateForNewDate;

return {
dateSelection,
Expand Down
Loading