Skip to content

Commit e0330b7

Browse files
committed
Prevent rejected projects shipped for clubs to be shipped individually after deadline
1 parent 1c643ef commit e0330b7

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/routes/dashboard/projects/[id]/ship/+page.server.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { db } from '$lib/server/db/index.js';
2-
import { devlog, project, clubMembership, club } from '$lib/server/db/schema.js';
2+
import { devlog, project, clubMembership, club, ship } from '$lib/server/db/schema.js';
33
import { error, fail, redirect } from '@sveltejs/kit';
4-
import { eq, and, or, sql } from 'drizzle-orm';
4+
import { eq, and, or, sql, desc } from 'drizzle-orm';
55
import type { Actions } from './$types';
66
import { sendSlackDM } from '$lib/server/slack.js';
77
import { isValidUrl } from '$lib/utils';
@@ -10,7 +10,6 @@ import { extname } from 'path';
1010
import { env } from '$env/dynamic/private';
1111
import { PutObjectCommand } from '@aws-sdk/client-s3';
1212
import { S3 } from '$lib/server/s3';
13-
import { ship } from '$lib/server/db/schema.js';
1413
import { sanitizeUrl } from '@braintree/sanitize-url';
1514
import { END_DATE } from '$lib/defs';
1615

@@ -75,7 +74,8 @@ export async function load({ params, locals }) {
7574

7675
return {
7776
project: queriedProject,
78-
clubMembership: membership.length > 0 ? membership[0] : null
77+
clubMembership: membership.length > 0 ? membership[0] : null,
78+
lastIsClubsShip: await lastIsClubsShip(id)
7979
};
8080
}
8181

@@ -282,7 +282,7 @@ export const actions = {
282282
}
283283
}
284284

285-
if (END_DATE <= new Date() && queriedProject.status === 'building' && clubIdForShip === null) {
285+
if (END_DATE <= new Date() && clubIdForShip === null && (queriedProject.status === 'building' || await lastIsClubsShip(id))) {
286286
return error(400, { message: "can't submit individual project past end date" });
287287
}
288288

@@ -349,3 +349,18 @@ export const actions = {
349349
return redirect(303, '/dashboard/projects');
350350
}
351351
} satisfies Actions;
352+
353+
async function lastIsClubsShip(id: number) {
354+
const [latestShip] = await db
355+
.select({
356+
clubId: ship.clubId
357+
})
358+
.from(ship)
359+
.where(eq(ship.projectId, id))
360+
.orderBy(desc(ship.timestamp))
361+
.limit(1);
362+
363+
if (latestShip && latestShip.clubId) return true;
364+
365+
return false;
366+
}

src/routes/dashboard/projects/[id]/ship/+page.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
let editorUploadFile = $state(null);
1919
let modelFile = $state(null);
2020
let submitAsClub = $state(
21-
END_DATE <= new Date() && data.project.status === 'building' && data.clubMembership
21+
END_DATE <= new Date() && (data.project.status === 'building' || data.lastIsClubsShip) && data.clubMembership
2222
? true
2323
: false
2424
);
@@ -190,17 +190,17 @@
190190
<div class="themed-box flex flex-col gap-2 p-3">
191191
<label
192192
class="flex items-center gap-2"
193-
class:opacity-50={END_DATE <= new Date() && data.project.status === 'building'}
194-
class:cursor-pointer={!(END_DATE <= new Date() && data.project.status === 'building')}
195-
class:cursor-not-allowed={END_DATE <= new Date() && data.project.status === 'building'}
193+
class:opacity-50={END_DATE <= new Date() && (data.project.status === 'building' || data.lastIsClubsShip)}
194+
class:cursor-pointer={!(END_DATE <= new Date() && (data.project.status === 'building' || data.lastIsClubsShip))}
195+
class:cursor-not-allowed={END_DATE <= new Date() && (data.project.status === 'building' || data.lastIsClubsShip)}
196196
>
197197
<input
198198
type="radio"
199199
name="submission_type"
200200
value="individual"
201201
checked={!submitAsClub}
202202
onchange={() => (submitAsClub = false)}
203-
disabled={END_DATE <= new Date() && data.project.status === 'building'}
203+
disabled={END_DATE <= new Date() && (data.project.status === 'building' || data.lastIsClubsShip)}
204204
class="radio"
205205
/>
206206
<span>Individual submission</span>

0 commit comments

Comments
 (0)