Skip to content

Commit

Permalink
feat: BE: Hyväksymispäätöksen kuulutusaikalaskuri (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
haapamakim committed Aug 10, 2022
1 parent 0c0f5da commit 6c363ec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
21 changes: 15 additions & 6 deletions backend/src/endDateCalculator/endDateCalculatorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { LaskePaattymisPaivaQueryVariables } from "../../../common/graphql/apiModel";
import { LaskePaattymisPaivaQueryVariables, LaskuriTyyppi } from "../../../common/graphql/apiModel";
import { dateTimeToString, dateToString, ISO_DATE_FORMAT, parseDate } from "../util/dateUtil";
import { Dayjs } from "dayjs";
import { bankHolidaysClient } from "./bankHolidaysClient";
import { config } from "../config";

export async function calculateEndDate({
alkupaiva,
tyyppi: _tyyppi,
}: LaskePaattymisPaivaQueryVariables): Promise<string> {
// Calculator currectly has only two types (KUULUTUKSEN_PAATTYMISPAIVA, NAHTAVILLAOLON_KUULUTUKSEN_PAATTYMISPAIVA) and they are calculated with equal formula
alkupaiva,
tyyppi,
}: LaskePaattymisPaivaQueryVariables): Promise<string> {
switch (tyyppi) {
case LaskuriTyyppi.KUULUTUKSEN_PAATTYMISPAIVA:
case LaskuriTyyppi.NAHTAVILLAOLON_KUULUTUKSEN_PAATTYMISPAIVA:
return calculateOnePlusGivenDaysSkipHolidays(alkupaiva, 30);
case LaskuriTyyppi.HYVAKSYMISPAATOKSEN_KUULUTUSAIKA:
return calculateOnePlusGivenDaysSkipHolidays(alkupaiva, 7 + 30); // julkaisupäivä + 7 päivän tiedoksisaantiaika + 30 päivän muutoksenhakuaika
}
}

async function calculateOnePlusGivenDaysSkipHolidays(alkupaiva: string, days: number) {
let start: Dayjs;
// Only accept dates in prod, but allow datetimes in other environments
const isDateOnly = config.isProd() || alkupaiva.length == ISO_DATE_FORMAT.length;
Expand All @@ -23,7 +32,7 @@ export async function calculateEndDate({
throw new Error("Alkupäivän pitää olla muotoa YYYY-MM-DDTHH:mm");
}
}
let endDate: Dayjs = start.add(30, "day");
let endDate: Dayjs = start.add(days, "day");
const bankHolidays = await bankHolidaysClient.getBankHolidays();
while (bankHolidays.isBankHoliday(endDate)) {
endDate = endDate.add(1, "day");
Expand Down
6 changes: 6 additions & 0 deletions backend/test/endDateCalculator/endDateCalculator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ describe("Api", () => {
).to.be.equal("2022-12-05");
});

it("should calculate correct Hyväksymispäätöksen kuulutusaika end date over weekend", async () => {
expect(
await calculateEndDate({ alkupaiva: "2022-11-03", tyyppi: LaskuriTyyppi.HYVAKSYMISPAATOKSEN_KUULUTUSAIKA })
).to.be.equal("2022-12-12");
});

it("should manage list of bank holidays correctly", () => {
const bankHolidays = new BankHolidays(["2022-12-24", "2022-12-25"]);
expect(bankHolidays.isBankHoliday(parseDate("2022-12-24"))).to.be.true;
Expand Down
1 change: 1 addition & 0 deletions graphql/types.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ type LatausTiedot {
enum LaskuriTyyppi {
KUULUTUKSEN_PAATTYMISPAIVA
NAHTAVILLAOLON_KUULUTUKSEN_PAATTYMISPAIVA
HYVAKSYMISPAATOKSEN_KUULUTUSAIKA
}

enum Kieli {
Expand Down

0 comments on commit 6c363ec

Please sign in to comment.