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

Add a biweekly period implementation #3276

Merged
Merged
Show file tree
Hide file tree
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
35 changes: 25 additions & 10 deletions client/src/periodUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const PERIOD_FORMAT = {
END_LONG: "D MMMM YYYY"
}

const weekType = "isoWeek"

const refMondayForBiweekly = "2021-01-04" // lets select 1st monday of 2021

export const PERIOD_FACTORIES = {
[RECURRENCE_TYPE.DAILY]: (date, offset) => ({
start: date.clone().subtract(offset, "days").startOf("day"),
Expand All @@ -46,17 +50,28 @@ export const PERIOD_FACTORIES = {
start: date.clone().subtract(offset, "weeks").startOf("week"),
end: date.clone().subtract(offset, "weeks").endOf("week")
}),
// FIXME: biweekly calculation should be changed, first agree on what it means
[RECURRENCE_TYPE.BIWEEKLY]: (date, offset) => ({
start: date
.clone()
.subtract(2 * offset, "weeks")
.startOf("week"),
end: date
[RECURRENCE_TYPE.BIWEEKLY]: (date, offset) => {
cemalettin-work marked this conversation as resolved.
Show resolved Hide resolved
// every biweekly period's start is even number of weeks apart from reference monday
const refMonday = moment(refMondayForBiweekly).startOf(weekType)
const curWeekMonday = date.clone().startOf(weekType)

const diffInWeeks = refMonday.diff(curWeekMonday, "weeks")
// current biweekly period's start has to be even number of weeks apart from reference monday
const curBiweeklyStart =
diffInWeeks % 2 === 0
? curWeekMonday
: curWeekMonday.clone().subtract(1, "weeks")

const curBiweeklyEnd = curBiweeklyStart
.clone()
.subtract(2 * offset, "weeks")
.endOf("week")
}),
.add(1, "weeks")
.endOf(weekType)

return {
start: curBiweeklyStart.clone().subtract(2 * offset, "weeks"),
cemalettin-work marked this conversation as resolved.
Show resolved Hide resolved
end: curBiweeklyEnd.clone().subtract(2 * offset, "weeks")
cemalettin-work marked this conversation as resolved.
Show resolved Hide resolved
}
},
// for more context read: https://github.com/NCI-Agency/anet/pull/3272#discussion_r515826676
[RECURRENCE_TYPE.SEMIMONTHLY]: (date, offset) => {
// With first half we mean first half of a month
Expand Down
42 changes: 42 additions & 0 deletions client/tests/utils/periodUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const EXAMPLE_INPUT_DATES_AND_OFFSETS = [
semiAnnually: {
start: "2019-07-01",
end: "2019-12-31"
},
biweekly: {
start: "2019-12-09",
end: "2019-12-22"
}
}
},
Expand All @@ -31,6 +35,10 @@ const EXAMPLE_INPUT_DATES_AND_OFFSETS = [
semiAnnually: {
start: "2018-07-01",
end: "2018-12-31"
},
biweekly: {
start: "2019-12-09",
end: "2019-12-22"
}
}
},
Expand All @@ -46,6 +54,10 @@ const EXAMPLE_INPUT_DATES_AND_OFFSETS = [
semiAnnually: {
start: "2019-07-01",
end: "2019-12-31"
},
biweekly: {
start: "2020-02-03",
end: "2020-02-16"
}
}
},
Expand All @@ -61,6 +73,10 @@ const EXAMPLE_INPUT_DATES_AND_OFFSETS = [
semiAnnually: {
start: "2020-07-01",
end: "2020-12-31"
},
biweekly: {
start: "2020-03-02",
end: "2020-03-15"
}
}
},
Expand All @@ -76,6 +92,10 @@ const EXAMPLE_INPUT_DATES_AND_OFFSETS = [
semiAnnually: {
start: "2021-01-01",
end: "2021-06-30"
},
biweekly: {
start: "2021-01-04",
end: "2021-01-17"
}
}
},
Expand All @@ -91,6 +111,10 @@ const EXAMPLE_INPUT_DATES_AND_OFFSETS = [
semiAnnually: {
start: "2019-07-01",
end: "2019-12-31"
},
biweekly: {
start: "2020-05-25",
end: "2020-06-07"
}
}
},
Expand All @@ -106,13 +130,31 @@ const EXAMPLE_INPUT_DATES_AND_OFFSETS = [
semiAnnually: {
start: "2021-07-01",
end: "2021-12-31"
},
biweekly: {
start: "2020-07-20",
end: "2020-08-02"
}
}
}
]

// test with index so that if it fails we know which date from the array
describe("For period creation utility", () => {
it("We should get the correct biweekly periods given example input", () => {
EXAMPLE_INPUT_DATES_AND_OFFSETS.forEach((input, index) => {
const period = PERIOD_FACTORIES[RECURRENCE_TYPE.BIWEEKLY](
moment(input.date),
input.offset
)
expect(prefix(index) + period.start.format(FORMAT)).toEqual(
prefix(index) + input.expectedPeriods.biweekly.start
)
expect(prefix(index) + period.end.format(FORMAT)).toEqual(
prefix(index) + input.expectedPeriods.biweekly.end
)
})
})
it("We should get the correct semimonthly periods given example input", () => {
EXAMPLE_INPUT_DATES_AND_OFFSETS.forEach((input, index) => {
const period = PERIOD_FACTORIES[RECURRENCE_TYPE.SEMIMONTHLY](
Expand Down