Skip to content

Commit 2b41e3f

Browse files
committed
fix: update getMembershipGrade helper
1 parent 02a246b commit 2b41e3f

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

apps/rpc/src/modules/user/user-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export function getUserService(
215215
},
216216

217217
async register(handle, userId) {
218-
// NOTE: The registrer function here has a few responsibilities because of our data strategy:
218+
// NOTE: The register function here has a few responsibilities because of our data strategy:
219219
//
220220
// 1. The database is the source of truth, and is ALWAYS intended to be as such.
221221
// 2. Unfortunately, there was a period in time where Auth0 was the source of truth, most notably right after we

packages/types/src/user.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { TZDate } from "@date-fns/tz"
22
import { schemas } from "@dotkomonline/db/schemas"
33
import { getCurrentUTC, slugify } from "@dotkomonline/utils"
4-
import { addYears, differenceInYears, isAfter, isBefore, setMonth, startOfMonth } from "date-fns"
4+
import { addYears, isAfter, isBefore, setMonth, startOfMonth } from "date-fns"
55
import { z } from "zod"
66
import { buildSearchFilter } from "./filters"
77

@@ -31,7 +31,7 @@ export type UserId = User["id"]
3131
export type UserProfileSlug = User["profileSlug"]
3232

3333
export const NAME_REGEX = /^[\p{L}\p{M}\s'-]+$/u
34-
export const PHONE_REGEX = /^[0-9+-\s]*$/
34+
export const PHONE_REGEX = /^[0-9-+\s]*$/
3535
export const PROFILE_SLUG_REGEX = /^[a-z0-9-]+$/
3636

3737
// These max and min values are arbitrary
@@ -93,25 +93,30 @@ export function findActiveMembership(user: User): Membership | null {
9393
}
9494

9595
export function getMembershipGrade(membership: Membership): 1 | 2 | 3 | 4 | 5 | null {
96-
// Take the difference, and add one because if `startYear == currentYear` they are in their first year
97-
const delta = differenceInYears(getAcademicStart(getCurrentUTC()), getAcademicStart(membership.start)) + 1
96+
const now = getCurrentUTC()
97+
98+
// Make sure we clamp the value to a minimum of 1
99+
const delta = Math.max(1, getAcademicYearDelta(membership.start, now))
98100

99101
switch (membership.type) {
100102
case "KNIGHT":
101103
case "PHD_STUDENT":
102104
return 5
105+
103106
case "SOCIAL_MEMBER":
104107
return 1
108+
105109
case "BACHELOR_STUDENT": {
106110
// Bachelor students are clamped at 1-3, regardless of how many years they used to take the degree.
107-
return Math.max(1, Math.min(3, delta)) as 1 | 2 | 3
111+
return Math.min(3, delta) as 1 | 2 | 3
108112
}
113+
109114
case "MASTER_STUDENT": {
110-
// Master students must be clamped at 4-5 because they can only be in their first or second year, but are always
111-
// considered to have a bachelor's degree from beforehand.
112-
const yearsGivenBachelors = delta + 3
113-
return Math.max(4, Math.min(5, yearsGivenBachelors)) as 4 | 5
115+
// Master students are clamped at 4-5, and are always considered to have a bachelor's degree from beforehand.
116+
const yearsWithBachelors = delta + 3
117+
return Math.min(5, yearsWithBachelors) as 4 | 5
114118
}
119+
115120
case "OTHER":
116121
return null
117122
}
@@ -161,3 +166,25 @@ export function getNextAcademicStart(): TZDate {
161166
const isBeforeAugust = isBefore(now, firstAugust)
162167
return isBeforeAugust ? firstAugust : addYears(firstAugust, 1)
163168
}
169+
170+
/**
171+
* Calculates how many academic years have passed since the start date.
172+
* If start is "last August" (current academic year), returns 1.
173+
* If start was the August before that, returns 2.
174+
*/
175+
function getAcademicYearDelta(startDate: Date | TZDate, now: Date | TZDate = getCurrentUTC()): number {
176+
const currentYear = now.getFullYear()
177+
const currentMonth = now.getMonth() // 0-indexed (Jan=0, Aug=7)
178+
179+
// If we are in Jan-July (0-6), the academic year started in the PREVIOUS calendar year
180+
// If we are in Aug-Dec (7-11), the academic year started in THIS calendar year
181+
const academicYearCurrent = currentMonth >= 7 ? currentYear : currentYear - 1
182+
183+
// We do the same normalization for the membership start date
184+
// (Handling cases where a member might join in Jan/Feb)
185+
const startYear = startDate.getFullYear()
186+
const startMonth = startDate.getMonth()
187+
const academicYearStart = startMonth >= 7 ? startYear : startYear - 1
188+
189+
return academicYearCurrent - academicYearStart + 1
190+
}

0 commit comments

Comments
 (0)