Skip to content

Commit 4d68e51

Browse files
erikekbergerikekberg
andauthored
Add default mandate dates (Closes #151) (#967)
* Added default values for mandate periods * Added default null value for memberId in form * Fixed default mandate dates * Added comment explaining the add month logic * Fixed formatting --------- Co-authored-by: erikekberg <er7030ek-s@student.lu.se>
1 parent 4a2f949 commit 4d68e51

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- AlterTable
2+
ALTER TABLE "positions" ADD COLUMN "end_month" INTEGER NOT NULL DEFAULT 11,
3+
ADD COLUMN "start_month" INTEGER NOT NULL DEFAULT 0;

src/database/prisma/schema.prisma

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ model Position {
603603
boardMember Boolean @default(false) @map("board_member")
604604
descriptionSv String? @map("description_sv")
605605
descriptionEn String? @map("description_en")
606+
startMonth Int @default(0) @map("start_month")
607+
endMonth Int @default(11) @map("end_month")
606608
committee Committee? @relation(fields: [committeeId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "positions_committee_id_foreign")
607609
emailAliases EmailAlias[]
608610
mandates Mandate[]

src/database/schema.zmodel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ model Position {
723723
boardMember Boolean @default(false) @map("board_member")
724724
descriptionSv String? @map("description_sv")
725725
descriptionEn String? @map("description_en")
726+
startMonth Int @default(0) @map("start_month")
727+
endMonth Int @default(11) @map("end_month")
726728
committee Committee? @relation(fields: [committeeId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "positions_committee_id_foreign")
727729
emailAliases EmailAlias[]
728730
mandates Mandate[]

src/database/seed/.snaplet/dataModel.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7668,6 +7668,34 @@
76687668
"isId": false,
76697669
"maxLength": null
76707670
},
7671+
{
7672+
"id": "public.positions.end_month",
7673+
"name": "end_month",
7674+
"columnName": "end_month",
7675+
"type": "int4",
7676+
"isRequired": true,
7677+
"kind": "scalar",
7678+
"isList": false,
7679+
"isGenerated": false,
7680+
"sequence": false,
7681+
"hasDefaultValue": true,
7682+
"isId": false,
7683+
"maxLength": null
7684+
},
7685+
{
7686+
"id": "public.positions.start_month",
7687+
"name": "start_month",
7688+
"columnName": "start_month",
7689+
"type": "int4",
7690+
"isRequired": true,
7691+
"kind": "scalar",
7692+
"isList": false,
7693+
"isGenerated": false,
7694+
"sequence": false,
7695+
"hasDefaultValue": true,
7696+
"isId": false,
7697+
"maxLength": null
7698+
},
76717699
{
76727700
"name": "committees",
76737701
"type": "committees",

src/routes/(app)/positions/[id]/+page.server.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import authentik from "$lib/server/authentik";
1212
import type { Actions, PageServerLoad } from "./$types";
1313
import * as m from "$paraglide/messages";
1414
import { languageTag } from "$paraglide/runtime";
15+
import dayjs from "dayjs";
16+
import utc from "dayjs/plugin/utc";
17+
18+
dayjs.extend(utc);
1519

1620
export const load: PageServerLoad = async ({ locals, params }) => {
1721
const { prisma } = locals;
@@ -48,9 +52,40 @@ export const load: PageServerLoad = async ({ locals, params }) => {
4852
if (!position) {
4953
throw error(404, m.positions_errors_positionNotFound());
5054
}
55+
56+
//Logic for startMonth and endMonth that can wrap over new years
57+
58+
//If the mandateperiod is within a year the endMonth will be greater than the startMonth which gives the difference:
59+
//(position.endMonth - position.startMonth) to be added.
60+
61+
//If the mandateperiod wraps into a new year (and is a year or less) the endMonth will be smaller than the startMonth.
62+
//The months that need to be added in this case is a full year minus the difference between the months:
63+
//12 - Math.abs(position.endMonth - position.startMonth)
64+
65+
const addMandateMonthDifference =
66+
position.endMonth > position.startMonth
67+
? position.endMonth - position.startMonth
68+
: 12 - Math.abs(position.endMonth - position.startMonth);
69+
5170
return {
5271
updateForm: superValidate(position, zod(updateSchema)),
53-
addMandateForm: superValidate(zod(addManadateSchema)),
72+
addMandateForm: superValidate(zod(addManadateSchema), {
73+
defaults: {
74+
memberId: "",
75+
startDate: dayjs()
76+
.month(position.startMonth)
77+
.utc()
78+
.startOf("month")
79+
.toDate(),
80+
endDate: dayjs()
81+
.month(position.startMonth)
82+
.utc()
83+
.startOf("month")
84+
.add(addMandateMonthDifference, "months")
85+
.endOf("month")
86+
.toDate(),
87+
},
88+
}),
5489
updateMandateForm: superValidate(zod(updateMandateSchema)),
5590
deleteMandateForm: superValidate(zod(deleteMandateSchema)),
5691
position,

0 commit comments

Comments
 (0)