|
2 | 2 |
|
3 | 3 | import { type z } from "zod"; |
4 | 4 |
|
5 | | -import { apiServer } from "@/api/server"; |
| 5 | +import { db } from "@echo-webkom/db"; |
| 6 | +import { |
| 7 | + answers, |
| 8 | + registrations, |
| 9 | + users, |
| 10 | + type AnswerInsert, |
| 11 | + type SpotRange, |
| 12 | +} from "@echo-webkom/db/schemas"; |
| 13 | + |
| 14 | +import { revalidateRegistrations } from "@/data/registrations/revalidate"; |
| 15 | +import { isUserBannedFromBedpres } from "@/lib/ban-info"; |
6 | 16 | import { getUser } from "@/lib/get-user"; |
7 | 17 | import { type registrationFormSchema } from "@/lib/schemas/registration"; |
8 | 18 |
|
@@ -34,7 +44,136 @@ export const register = async (id: string, payload: z.infer<typeof registrationF |
34 | 44 | }) |
35 | 45 | .json<{ success: boolean; message: string }>(); |
36 | 46 |
|
37 | | - return resp; |
| 47 | + const canSkipSpotRange = doesIntersect( |
| 48 | + hostGroups, |
| 49 | + user.memberships.map((membership) => membership.group.id), |
| 50 | + ); |
| 51 | + |
| 52 | + /** |
| 53 | + * Get correct spot range for user |
| 54 | + * |
| 55 | + * If user is not in any spot range, return error |
| 56 | + */ |
| 57 | + const userSpotRange = getCorrectSpotrange(user.year, spotRanges, canSkipSpotRange); |
| 58 | + |
| 59 | + if (!userSpotRange) { |
| 60 | + console.error("User is not in any spot range", { |
| 61 | + userId: user.id, |
| 62 | + happeningId: id, |
| 63 | + }); |
| 64 | + return { |
| 65 | + success: false, |
| 66 | + message: "Du kan ikke melde deg på dette arrangementet", |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + const data = await registrationFormSchema.parseAsync(payload); |
| 71 | + |
| 72 | + /** |
| 73 | + * Check if all questions are answered |
| 74 | + */ |
| 75 | + const allQuestionsAnswered = happening.questions.every((question) => { |
| 76 | + const questionExists = data.questions.find((q) => q.questionId === question.id); |
| 77 | + const questionAnswer = questionExists?.answer; |
| 78 | + |
| 79 | + return question.required ? !!questionAnswer : true; |
| 80 | + }); |
| 81 | + |
| 82 | + if (!allQuestionsAnswered) { |
| 83 | + console.error("Not all questions are answered", { |
| 84 | + userId: user.id, |
| 85 | + happeningId: id, |
| 86 | + }); |
| 87 | + return { |
| 88 | + success: false, |
| 89 | + message: "Du må svare på alle spørsmålene", |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + const { registration, isWaitlisted } = await db.transaction( |
| 94 | + async (tx) => { |
| 95 | + await tx.execute(sql`LOCK TABLE ${registrations} IN EXCLUSIVE MODE`); |
| 96 | + |
| 97 | + const regs = await tx |
| 98 | + .select() |
| 99 | + .from(registrations) |
| 100 | + .where( |
| 101 | + and( |
| 102 | + eq(registrations.happeningId, id), |
| 103 | + lte(users.year, userSpotRange.maxYear), |
| 104 | + gte(users.year, userSpotRange.minYear), |
| 105 | + or(eq(registrations.status, "registered"), eq(registrations.status, "waiting")), |
| 106 | + ), |
| 107 | + ) |
| 108 | + .leftJoin(users, eq(registrations.userId, users.id)); |
| 109 | + |
| 110 | + const isInfiniteSpots = userSpotRange.spots === 0; |
| 111 | + const isWaitlisted = !isInfiniteSpots && regs.length >= userSpotRange.spots; |
| 112 | + |
| 113 | + const registration = await tx |
| 114 | + .insert(registrations) |
| 115 | + .values({ |
| 116 | + status: isWaitlisted ? "waiting" : "registered", |
| 117 | + happeningId: id, |
| 118 | + userId: user.id, |
| 119 | + changedBy: null, |
| 120 | + }) |
| 121 | + .returning() |
| 122 | + .onConflictDoUpdate({ |
| 123 | + target: [registrations.happeningId, registrations.userId], |
| 124 | + set: { |
| 125 | + status: isWaitlisted ? "waiting" : "registered", |
| 126 | + }, |
| 127 | + }) |
| 128 | + .then((res) => res[0] ?? null); |
| 129 | + |
| 130 | + return { |
| 131 | + registration, |
| 132 | + isWaitlisted, |
| 133 | + }; |
| 134 | + }, |
| 135 | + { |
| 136 | + isolationLevel: "read committed", |
| 137 | + }, |
| 138 | + ); |
| 139 | + |
| 140 | + revalidateRegistrations(id, user.id); |
| 141 | + |
| 142 | + if (!registration) { |
| 143 | + throw new Error("Failed to update registration"); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Insert answers |
| 148 | + */ |
| 149 | + const answersToInsert = data.questions.map( |
| 150 | + (question) => |
| 151 | + ({ |
| 152 | + happeningId: happening.id, |
| 153 | + userId: user.id, |
| 154 | + questionId: question.questionId, |
| 155 | + answer: question.answer |
| 156 | + ? { |
| 157 | + answer: question.answer, |
| 158 | + } |
| 159 | + : null, |
| 160 | + }) satisfies AnswerInsert, |
| 161 | + ); |
| 162 | + |
| 163 | + if (answersToInsert.length > 0) { |
| 164 | + await db.insert(answers).values(answersToInsert).onConflictDoNothing(); |
| 165 | + } |
| 166 | + |
| 167 | + console.info("Successful registration", { |
| 168 | + userId: user.id, |
| 169 | + happeningId: happening.id, |
| 170 | + isWaitlisted, |
| 171 | + }); |
| 172 | + |
| 173 | + return { |
| 174 | + success: true, |
| 175 | + message: isWaitlisted ? "Du er nå på venteliste" : "Du er nå påmeldt arrangementet", |
| 176 | + }; |
38 | 177 | } catch (error) { |
39 | 178 | console.error("Failed to register"); |
40 | 179 |
|
|
0 commit comments