-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathuser.ts
More file actions
184 lines (161 loc) · 5.71 KB
/
user.ts
File metadata and controls
184 lines (161 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import type { TZDate } from "@date-fns/tz"
import { schemas } from "@dotkomonline/db/schemas"
import { getCurrentUTC, slugify } from "@dotkomonline/utils"
import { isAfter, isBefore } from "date-fns"
import { z } from "zod"
import { buildSearchFilter } from "./filters"
export const MembershipSpecializationSchema = schemas.MembershipSpecializationSchema
export type MembershipSpecialization = z.infer<typeof MembershipSpecializationSchema>
export type MembershipType = z.infer<typeof MembershipTypeSchema>
export const MembershipTypeSchema = schemas.MembershipTypeSchema
export const MembershipSchema = schemas.MembershipSchema.extend({})
export type MembershipId = Membership["id"]
export type Membership = z.infer<typeof MembershipSchema>
export const MembershipWriteSchema = MembershipSchema.pick({
type: true,
start: true,
end: true,
specialization: true,
semester: true,
})
export type MembershipWrite = z.infer<typeof MembershipWriteSchema>
export const UserFlagSchema = schemas.UserFlagSchema.extend({})
export type UserFlag = z.infer<typeof UserFlagSchema>
export type UserFlagId = UserFlag["id"]
export const UserFlagWriteSchema = UserFlagSchema.pick({
name: true,
description: true,
imageUrl: true,
})
export type UserFlagWrite = z.infer<typeof UserFlagWriteSchema>
export const UserSchema = schemas.UserSchema.extend({
memberships: z.array(MembershipSchema),
flags: z.array(UserFlagSchema),
})
export type User = z.infer<typeof UserSchema>
export type UserId = User["id"]
export type UserProfileSlug = User["profileSlug"]
export const UserFlagWithUsersSchema = schemas.UserFlagSchema.extend({
users: UserSchema.pick({
id: true,
name: true,
profileSlug: true,
imageUrl: true,
}).array(),
})
export type UserFlagWithUsers = z.infer<typeof UserFlagWithUsersSchema>
export const NAME_REGEX = /^[\p{L}\p{M}\s'-]+$/u
export const PHONE_REGEX = /^[0-9-+\s]*$/
export const PROFILE_SLUG_REGEX = /^[a-z0-9-]+$/
// These max and min values are arbitrary
export const UserWriteSchema = UserSchema.pick({
workspaceUserId: true,
}).extend({
profileSlug: z
.string()
.min(2, "Brukernavnet må være minst 2 tegn lang")
.max(64, "Brukernavnet kan ikke være lengre enn 64 tegn")
.regex(PROFILE_SLUG_REGEX, "Brukernavnet kan bare inneholde små bokstaver, tall og bindestrek")
.refine((value) => slugify(value) === value, {
message: "Brukernavnet kan bare inneholde små bokstaver, tall og bindestrek",
}),
name: z
.string()
.min(2, "Du må skrive inn et navn")
.max(128, "Navnet kan ikke være lengre enn 128 tegn")
.regex(NAME_REGEX, "Du kan bare bruke bokstaver, bindestrek, apostrof og mellomrom i navnet")
.nullable(),
email: z
.string()
.email("Ugyldig e-post")
.min(1, "Du må skrive en e-post")
.max(128, "E-posten kan ikke være lengre enn 128 tegn")
.nullable(),
phone: z
.string()
.regex(PHONE_REGEX, "Ugyldig telefonnummer")
.max(32, "Telefonnummeret kan ikke være lengre enn 32 tegn")
.nullable(),
imageUrl: z.string().url("Ugyldig URL").max(500, "Bildelenken kan ikke være lengre enn 500 tegn").nullable(),
biography: z.string().max(2000, "Biografien kan ikke være lengre enn 2000 tegn").nullable(),
gender: z.string().nullable(),
dietaryRestrictions: z.string().max(200, "Kostholdsrestriksjoner kan ikke være lengre enn 200 tegn").nullable(),
})
export type UserWrite = z.infer<typeof UserWriteSchema>
export const PublicUserSchema = UserSchema.pick({
id: true,
profileSlug: true,
name: true,
imageUrl: true,
biography: true,
})
export type PublicUser = z.infer<typeof PublicUserSchema>
export const UserFilterQuerySchema = z
.object({
byName: buildSearchFilter(),
byEmail: buildSearchFilter(),
})
.partial()
export type UserFilterQuery = z.infer<typeof UserFilterQuerySchema>
export function isMembershipActive(
membership: Membership | MembershipWrite,
now: TZDate | Date = getCurrentUTC()
): boolean {
if (isAfter(membership.start, now)) {
return false
}
if (membership.end && isBefore(membership.end, now)) {
return false
}
return true
}
/**
* Get the most relevant active membership for a user. Most relevant is defined as the membership with the highest
* semester.
*
* This will always deprioritize KNIGHT (Ridder) memberships in favor of student or social memberships, because they are
* easier to work with for our attendance systems.
*/
export function findActiveMembership(user: User): Membership | null {
const now = getCurrentUTC()
// This orders active memberships by semester descending with null values last
const orderedMemberships = user.memberships
.filter((membership) => isMembershipActive(membership, now))
.toSorted((a, b) => {
if (a.semester === null && b.semester === null) {
return 0
}
if (a.semester !== null && b.semester !== null) {
return b.semester - a.semester
}
return b.semester !== null ? 1 : -1
})
return orderedMemberships.at(0) ?? null
}
export function getMembershipTypeName(type: MembershipType) {
switch (type) {
case "BACHELOR_STUDENT":
return "Bachelor"
case "MASTER_STUDENT":
return "Master"
case "SOCIAL_MEMBER":
return "Sosialt medlem"
case "KNIGHT":
return "Ridder"
}
}
export function getSpecializationName(specialization: MembershipSpecialization) {
switch (specialization) {
case "ARTIFICIAL_INTELLIGENCE":
return "Kunstig intelligens"
case "DATABASE_AND_SEARCH":
return "Database og søk"
case "INTERACTION_DESIGN":
return "Interaksjonsdesign"
case "SOFTWARE_ENGINEERING":
return "Programvareutvikling"
case "UNKNOWN":
return "Ukjent spesialisering"
}
}
export const USER_IMAGE_MAX_SIZE_KIB = 512