Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 40 additions & 19 deletions src/routes/dashboard/projects/[id]/ship/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { db } from '$lib/server/db/index.js';
import { devlog, project, clubMembership, club } from '$lib/server/db/schema.js';
import { devlog, project, clubMembership, club, ship } from '$lib/server/db/schema.js';
import { error, fail, redirect } from '@sveltejs/kit';
import { eq, and, or, sql } from 'drizzle-orm';
import { eq, and, or, sql, desc } from 'drizzle-orm';
import type { Actions } from './$types';
import { sendSlackDM } from '$lib/server/slack.js';
import { isValidUrl } from '$lib/utils';
Expand All @@ -10,8 +10,8 @@ import { extname } from 'path';
import { env } from '$env/dynamic/private';
import { PutObjectCommand } from '@aws-sdk/client-s3';
import { S3 } from '$lib/server/s3';
import { ship } from '$lib/server/db/schema.js';
import { sanitizeUrl } from '@braintree/sanitize-url';
import { END_DATE } from '$lib/defs';

export async function load({ params, locals }) {
const id: number = parseInt(params.id);
Expand Down Expand Up @@ -74,7 +74,8 @@ export async function load({ params, locals }) {

return {
project: queriedProject,
clubMembership: membership.length > 0 ? membership[0] : null
clubMembership: membership.length > 0 ? membership[0] : null,
lastIsClubsShip: await lastIsClubsShip(id)
};
}

Expand Down Expand Up @@ -109,7 +110,7 @@ export const actions = {
});
}

// Double dipping
// Double dipping
if (doubleDippingWith !== 'none' && doubleDippingWith !== 'enclosure') {
return error(400);
}
Expand Down Expand Up @@ -233,6 +234,7 @@ export const actions = {
name: project.name,
description: project.description,
url: project.url,
status: project.status,
timeSpent: sql<number>`COALESCE(SUM(${devlog.timeSpent}), 0)`,
devlogCount: sql<number>`COALESCE(COUNT(${devlog.id}), 0)`
})
Expand Down Expand Up @@ -267,6 +269,23 @@ export const actions = {
return error(400, { message: 'project must have a description' });
}

// Get club ID if submitting as club
let clubIdForShip: number | null = null;
if (submitAsClub) {
const [membership] = await db
.select({ clubId: clubMembership.clubId })
.from(clubMembership)
.where(eq(clubMembership.userId, locals.user.id))
.limit(1);
if (membership) {
clubIdForShip = membership.clubId;
}
}

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

// Editor file
const editorFilePath = `ships/editor-files/${crypto.randomUUID()}${extname(editorFile.name)}`;

Expand Down Expand Up @@ -299,7 +318,7 @@ export const actions = {
uploadedFileUrl: editorFileExists ? editorFilePath : undefined,

modelFile: modelPath,
doubleDippingWith
doubleDippingWith
})
.where(
and(
Expand All @@ -309,19 +328,6 @@ export const actions = {
)
);

// Get club ID if submitting as club
let clubIdForShip: number | null = null;
if (submitAsClub) {
const [membership] = await db
.select({ clubId: clubMembership.clubId })
.from(clubMembership)
.where(eq(clubMembership.userId, locals.user.id))
.limit(1);
if (membership) {
clubIdForShip = membership.clubId;
}
}

await db.insert(ship).values({
userId: locals.user.id,
projectId: queriedProject.id,
Expand All @@ -343,3 +349,18 @@ export const actions = {
return redirect(303, '/dashboard/projects');
}
} satisfies Actions;

async function lastIsClubsShip(id: number) {
const [latestShip] = await db
.select({
clubId: ship.clubId
})
.from(ship)
.where(eq(ship.projectId, id))
.orderBy(desc(ship.timestamp))
.limit(1);

if (latestShip && latestShip.clubId) return true;

return false;
}
45 changes: 38 additions & 7 deletions src/routes/dashboard/projects/[id]/ship/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import Head from '$lib/components/Head.svelte';
import Project from '$lib/components/Project.svelte';
import { calculateCurrencyPayout, calculateMinutes } from '$lib/currency';
import { END_DATE } from '$lib/defs';
import { MAX_UPLOAD_SIZE } from '../config';
import type { PageProps } from './$types';
import { Ship, SquarePen } from '@lucide/svelte';
Expand All @@ -16,7 +17,11 @@
let editorUrl = $state(data.project.editorUrl);
let editorUploadFile = $state(null);
let modelFile = $state(null);
let submitAsClub = $state(false);
let submitAsClub = $state(
END_DATE <= new Date() && (data.project.status === 'building' || data.lastIsClubsShip) && data.clubMembership
? true
: false
);

let hasEditorFile = $derived((editorUrl || editorUploadFile) && !(editorUrl && editorUploadFile));

Expand All @@ -33,6 +38,20 @@
<Head title="Ship project" />

<h1 class="mt-5 mb-3 font-hero text-2xl font-medium">Ship project</h1>

{#if END_DATE <= new Date() && data.project.status == 'building'}
<div
class="mt-3 mb-3 flex flex-col gap-0.5 rounded-lg border-3 border-primary-700 bg-primary-900 p-3"
>
<h2 class="text-xl font-bold text-primary-400">New non-clubs submissions are now closed!</h2>
<p>
Construct has ended for non-clubs projects. The market is still open and you can re-ship
rejected projects, however you can't ship new projects.
</p>
<p class="mt-1">You can still ship new projects as normal for clubs.</p>
</div>
{/if}

<Project
id={data.project.id}
name={data.project.name}
Expand Down Expand Up @@ -169,13 +188,19 @@
<div class="mt-1">
<p class="mb-1 font-bold">Submit as</p>
<div class="themed-box flex flex-col gap-2 p-3">
<label class="flex cursor-pointer items-center gap-2">
<label
class="flex items-center gap-2"
class:opacity-50={END_DATE <= new Date() && (data.project.status === 'building' || data.lastIsClubsShip)}
class:cursor-pointer={!(END_DATE <= new Date() && (data.project.status === 'building' || data.lastIsClubsShip))}
class:cursor-not-allowed={END_DATE <= new Date() && (data.project.status === 'building' || data.lastIsClubsShip)}
>
<input
type="radio"
name="submission_type"
value="individual"
checked={!submitAsClub}
onchange={() => (submitAsClub = false)}
disabled={END_DATE <= new Date() && (data.project.status === 'building' || data.lastIsClubsShip)}
class="radio"
/>
<span>Individual submission</span>
Expand Down Expand Up @@ -239,10 +264,15 @@
</div>
<div class="mb-1">
{#if data.project.timeSpent >= 60 && data.project.description != '' && data.project.url != ''}
<p class="text-primary-300">
Are you sure you want to ship "{data.project.name}"?
<span class="font-bold">You won't be able to edit it or journal again</span> unless it gets rejected.
</p>
{#if !submitAsClub && END_DATE <= new Date() && data.project.status === 'building'}
<p class="text-red-400">Non-clubs submissions are now closed!</p>
{:else}
<p class="text-primary-300">
Are you sure you want to ship "{data.project.name}"?
<span class="font-bold">You won't be able to edit it or journal again</span> unless it gets
rejected.
</p>
{/if}
{/if}
</div>
<div class="flex flex-row gap-2">
Expand All @@ -258,7 +288,8 @@
data.project.description == '' ||
!printablesUrl ||
!hasEditorFile ||
!modelFile}
!modelFile ||
(!submitAsClub && END_DATE <= new Date())}
>
<Ship />
Ship
Expand Down
Loading