From d661c52c564aa03bba40b2a1c53db350652b4e4e Mon Sep 17 00:00:00 2001 From: Anthony Powell Date: Thu, 19 Oct 2023 11:45:38 -0400 Subject: [PATCH] Automatically donate key to new projects --- apps/web/src/routes/(app)/+page.server.ts | 15 ++++++++++++++- packages/database/queries/projects.ts | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/apps/web/src/routes/(app)/+page.server.ts b/apps/web/src/routes/(app)/+page.server.ts index 3bec421..8620ca5 100644 --- a/apps/web/src/routes/(app)/+page.server.ts +++ b/apps/web/src/routes/(app)/+page.server.ts @@ -28,7 +28,20 @@ export const actions = { }); } - await queries.Project.createProject(userId, name); + const project = await queries.Project.createProject(userId, name); + + if (!project) { + return fail(400, { + createProject: { + success: false, + errors: { + general: 'Could not create project' + } + } + }); + } + + await queries.Project.donateKey(project.id, userId); return { createProject: { success: true } }; }, diff --git a/packages/database/queries/projects.ts b/packages/database/queries/projects.ts index 99fc82f..59e0b3b 100644 --- a/packages/database/queries/projects.ts +++ b/packages/database/queries/projects.ts @@ -113,12 +113,20 @@ export class ProjectQueries { return null; } - await tx.insert(usersInProjects).values({ - userId, - projectId: newProject[0].id, - admin: true, - updatedAt: getCurrentDateInUTC(), - }); + const uip = await tx + .insert(usersInProjects) + .values({ + userId, + projectId: newProject[0].id, + admin: true, + updatedAt: getCurrentDateInUTC(), + }) + .returning(); + + if (uip.length === 0) { + tx.rollback(); + return null; + } return newProject[0]; });