Skip to content

Commit

Permalink
fix: ensure correct upgrade of draft rounds
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiDood committed Jul 11, 2024
1 parent 1e985b8 commit bb224b0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
19 changes: 10 additions & 9 deletions src/lib/server/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ export class Database implements Loggable {
sql =>
[
sql`SELECT quota FROM drap.labs WHERE lab_id = ${lab}`,
sql`SELECT count(student_email) FROM drap.faculty_choices_emails JOIN drap.faculty_choices USING (draft_id, round, faculty_email) WHERE draft_id = ${draft} AND lab_id = ${lab}`,
sql`SELECT count(student_email) FROM drap.faculty_choices_emails fce WHERE draft_id = ${draft} AND fce.lab_id = ${lab}`,
] as const,
);
strictEqual(quotaRest.length, 0);
strictEqual(selectedRest.length, 0);
return {
quota: typeof quota === 'undefined' ? null : parse(LabQuota, lab).quota,
quota: typeof quota === 'undefined' ? null : parse(LabQuota, quota).quota,
selected: parse(CountResult, selected).count,
};
}
Expand All @@ -271,18 +271,18 @@ export class Database implements Loggable {
return parse(CreatedDraft, first);
}

@timed async incrementDraftRound(draft_id: Draft['draft_id']) {
@timed async incrementDraftRound(draft: Draft['draft_id']) {
const sql = this.#sql;
const [first, ...rest] =
await sql`UPDATE drap.drafts SET curr_round = curr_round + 1 WHERE draft_id = ${draft_id} RETURNING curr_round, max_rounds`;
await sql`UPDATE drap.drafts SET curr_round = curr_round + 1 WHERE draft_id = ${draft} RETURNING curr_round, max_rounds`;
strictEqual(rest.length, 0);
return typeof first === 'undefined' ? null : parse(IncrementedDraftRound, first);
}

@timed async insertStudentRanking(id: Draft['draft_id'], email: User['email'], labs: StudentRank['labs']) {
@timed async insertStudentRanking(draft: Draft['draft_id'], email: User['email'], labs: StudentRank['labs']) {
const sql = this.#sql;
const { count } =
await sql`INSERT INTO drap.student_ranks (draft_id, email, labs) VALUES (${id}, ${email}, ${labs}) ON CONFLICT ON CONSTRAINT student_ranks_pkey DO NOTHING`;
await sql`INSERT INTO drap.student_ranks (draft_id, email, labs) VALUES (${draft}, ${email}, ${labs}) ON CONFLICT ON CONSTRAINT student_ranks_pkey DO NOTHING`;
switch (count) {
case 0:
return false;
Expand All @@ -293,10 +293,10 @@ export class Database implements Loggable {
}
}

@timed async getStudentRankings(id: StudentRank['draft_id'], email: StudentRank['email']) {
@timed async getStudentRankings(draft: StudentRank['draft_id'], email: StudentRank['email']) {
const sql = this.#sql;
const [first, ...rest] =
await sql`SELECT created_at, array_agg(lab_name) labs FROM drap.labs JOIN (SELECT created_at, unnest(labs) lab_id FROM drap.student_ranks WHERE draft_id = ${id} AND email = ${email}) ranks USING (lab_id) GROUP BY created_at`;
await sql`SELECT created_at, array_agg(lab_name) labs FROM drap.labs JOIN (SELECT created_at, unnest(labs) lab_id FROM drap.student_ranks WHERE draft_id = ${draft} AND email = ${email}) ranks USING (lab_id) GROUP BY created_at`;
strictEqual(rest.length, 0);
return typeof first === 'undefined' ? null : parse(QueriedStudentRank, first);
}
Expand All @@ -308,8 +308,9 @@ export class Database implements Loggable {
studentEmails: StudentRank['email'][],
) {
const sql = this.#sql;
const emails = sql(studentEmails.map(email => [email] as const));
const { count } =
await sql`INSERT INTO drap.faculty_choices_emails (draft_id, round, faculty_email, student_email) SELECT draft_id, round, faculty_email, _.email FROM (INSERT INTO drap.faculty_choices (draft_id, round, faculty_email, lab_id) SELECT draft_id, curr_round, ${facultyEmail}, ${labId} FROM drap.drafts WHERE draft_id = ${draftId} RETURNING draft_id, round, faculty_email) fc, (VALUES ${studentEmails}) _ (email)`;
await sql`WITH fc AS (INSERT INTO drap.faculty_choices (draft_id, round, lab_id, faculty_email) SELECT draft_id, curr_round, ${labId}, ${facultyEmail} FROM drap.drafts WHERE draft_id = ${draftId} RETURNING draft_id, round, lab_id) INSERT INTO drap.faculty_choices_emails (draft_id, round, lab_id, student_email) SELECT draft_id, round, lab_id, email FROM fc, (VALUES ${emails}) _ (email)`;
strictEqual(studentEmails.length, count);
}

Expand Down
12 changes: 9 additions & 3 deletions src/routes/dashboard/(draft)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@

<div class="card prose max-w-none p-4 dark:prose-invert">
<p>
<strong>Draft &num;{draft_id}</strong> is currently on Round <strong>{curr_round}</strong>
of <strong>{max_rounds}</strong>. It opened last <strong>{startDate}</strong> at
<strong>{startTime}</strong>.
{#if curr_round > max_rounds}
<strong>Draft &num;{draft_id}</strong> (which opened last <strong>{startDate}</strong> at
<strong>{startTime}</strong>) has recently finished the main drafting process. It is currently in the
lottery rounds.
{:else}
<strong>Draft &num;{draft_id}</strong> is currently on Round <strong>{curr_round}</strong>
of <strong>{max_rounds}</strong>. It opened last <strong>{startDate}</strong> at
<strong>{startTime}</strong>.
{/if}
</p>
</div>
<slot />
2 changes: 1 addition & 1 deletion src/routes/dashboard/(draft)/students/+page.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const actions = {
assert(quota !== null);

const total = selected + BigInt(students.length);
if (quota < total) error(403);
if (total > quota) error(403);

const lab = user.lab_id;
const faculty = user.email;
Expand Down
3 changes: 2 additions & 1 deletion src/routes/dashboard/(draft)/students/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
$: suffix = getOrdinalSuffix(curr_round);
// TODO: Prevent the user from selecting too many.
let draftees: string[] = [];
$: remainingQuota = quota - researchers.length;
$: remainingDraftees = remainingQuota - draftees.length;
Expand All @@ -27,7 +28,7 @@
<h2>Draft Picks</h2>
<p>
Welcome to the draft! The <strong>{lab_name}</strong> has been allocated <strong>{quota}</strong> slots
in total. Note that <strong>{researchers.length}</strong> students (show below if any) have already been
in total. Note that <strong>{researchers.length}</strong> students (shown below if any) have already been
reserved in previous rounds.
</p>
<nav class="not-prose list-nav">
Expand Down
12 changes: 9 additions & 3 deletions src/routes/dashboard/drafts/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@

<div class="card prose max-w-none p-4 dark:prose-invert">
<p>
<strong>Draft &num;{draft_id}</strong> is currently on Round <strong>{curr_round}</strong>
of <strong>{max_rounds}</strong>. It opened last <strong>{startDate}</strong> at
<strong>{startTime}</strong>.
{#if curr_round > max_rounds}
<strong>Draft &num;{draft_id}</strong> (which opened last <strong>{startDate}</strong> at
<strong>{startTime}</strong>) has recently finished the main drafting process. It is currently in the
lottery rounds.
{:else}
<strong>Draft &num;{draft_id}</strong> is currently on Round <strong>{curr_round}</strong>
of <strong>{max_rounds}</strong>. It opened last <strong>{startDate}</strong> at
<strong>{startTime}</strong>.
{/if}
</p>
</div>
{#if curr_round > 0}
Expand Down

0 comments on commit bb224b0

Please sign in to comment.