|
1 | 1 | import type { TZDate } from "@date-fns/tz" |
2 | 2 | import { schemas } from "@dotkomonline/db/schemas" |
3 | 3 | 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" |
5 | 5 | import { z } from "zod" |
6 | 6 | import { buildSearchFilter } from "./filters" |
7 | 7 |
|
@@ -31,7 +31,7 @@ export type UserId = User["id"] |
31 | 31 | export type UserProfileSlug = User["profileSlug"] |
32 | 32 |
|
33 | 33 | 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]*$/ |
35 | 35 | export const PROFILE_SLUG_REGEX = /^[a-z0-9-]+$/ |
36 | 36 |
|
37 | 37 | // These max and min values are arbitrary |
@@ -93,25 +93,30 @@ export function findActiveMembership(user: User): Membership | null { |
93 | 93 | } |
94 | 94 |
|
95 | 95 | 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)) |
98 | 100 |
|
99 | 101 | switch (membership.type) { |
100 | 102 | case "KNIGHT": |
101 | 103 | case "PHD_STUDENT": |
102 | 104 | return 5 |
| 105 | + |
103 | 106 | case "SOCIAL_MEMBER": |
104 | 107 | return 1 |
| 108 | + |
105 | 109 | case "BACHELOR_STUDENT": { |
106 | 110 | // 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 |
108 | 112 | } |
| 113 | + |
109 | 114 | 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 |
114 | 118 | } |
| 119 | + |
115 | 120 | case "OTHER": |
116 | 121 | return null |
117 | 122 | } |
@@ -161,3 +166,25 @@ export function getNextAcademicStart(): TZDate { |
161 | 166 | const isBeforeAugust = isBefore(now, firstAugust) |
162 | 167 | return isBeforeAugust ? firstAugust : addYears(firstAugust, 1) |
163 | 168 | } |
| 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