diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..ab1f4164 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..03d9549e --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..6adae737 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/opptaksside.iml b/.idea/opptaksside.iml new file mode 100644 index 00000000..c956989b --- /dev/null +++ b/.idea/opptaksside.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/SESSION_NOTES.md b/SESSION_NOTES.md new file mode 100644 index 00000000..22227ee3 --- /dev/null +++ b/SESSION_NOTES.md @@ -0,0 +1,186 @@ +# Date Standardization — Session Notes + +**Branch:** `refactor/standardize-date-objects` +**Session:** 2026-04-27 — 2026-05-02 +**Commit:** `b0a3a7b` — _refactor: standardize date objects across project_ +**PR URL:** https://github.com/appKom/opptak/pull/new/refactor/standardize-date-objects + +> This file is a working memo of what happened in the session. It is **not** committed to git (kept locally only). + +--- + +## 1. Context at session start + +The branch had a large in-progress refactor: period / applicant / committee date fields changed from `string` to `Date` end-to-end. Already in the working tree (uncommitted) before the session: + +- `lib/types/types.ts` — types flipped from `string` → `Date`. +- `lib/utils/parseDates.ts` (new) — central client-side parsing helpers (`parsePeriodDates`, `parseApplicantDates`, `parseCommitteeDates`). +- `lib/api/{applicantApi,committeesApi,periodApi}.ts` — fetchers run `parseDates` on JSON responses. +- `pages/api/...` — POST/PUT handlers run `parseDates` on `req.body`. +- `lib/utils/{validators,validateApplication,convertIsoToScheduleFormat,dateUtils}.ts` — adapted to handle Dates. +- `pages/{apply,admin,committee,committees}` — direct Date comparisons replacing string-vs-Date hacks. +- `lib/mongo/{periods,applicants}.ts` — mongo queries now pass Date objects. +- `scripts/migrate-dates.js` — in-place migration script (string → BSON Date) for any DB the URI points to. + +--- + +## 2. Pre-test analysis — bugs identified by reading the diff + +### Bug 1 — `validateCommittee` rejected parsed `Date` objects +`lib/utils/validators.ts` (in `validateCommittee`): + +```ts +data.availabletimes.every( + (time: { start: string; end: string }) => + typeof time.start === "string" && typeof time.end === "string", +); +``` + +Called *after* `parseCommitteeDates(req.body)` ran, so `start`/`end` were already `Date` objects → `typeof === "string"` was always `false` → 400 "Invalid data format" on every committee submission. + +**Fix:** check for `Date` instances instead. + +### Bug 2 — `isApplicantType(req.body, ...)` saw `date: undefined` +`pages/api/applicants/index.ts`: + +```ts +const requestBody = parseApplicantDates(req.body); // new object, not mutation +requestBody.date = new Date(); +... +if (!isApplicantType(req.body, period)) { // ← raw req.body, no date Date +``` + +Pre-refactor `requestBody = req.body` was an alias, so `requestBody.date = new Date()` mutated `req.body.date`. Post-refactor `parseApplicantDates` returns a fresh object via spread, so `req.body.date` stayed `undefined`. Validator's `date instanceof Date` check failed → 400. + +**Fix:** call `isApplicantType(requestBody, period)`. + +### Two pre-existing concerns logged, not regressions of this PR: +- Two `periods` rows with corrupted year data (`Fredrik Hansteen` year 1, `Testeeeeeee` year 567). +- React warning `