From 734e6be028c2b17abd14dbb6df07dd418d380699 Mon Sep 17 00:00:00 2001 From: Caitlin Lee Date: Mon, 6 Apr 2026 20:52:35 -0400 Subject: [PATCH 1/2] AUTH-9 UserProject server actions --- src/lib/userProject.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/lib/userProject.ts diff --git a/src/lib/userProject.ts b/src/lib/userProject.ts new file mode 100644 index 0000000..dc958f0 --- /dev/null +++ b/src/lib/userProject.ts @@ -0,0 +1,30 @@ +"use server"; +import { prisma } from "@/lib/prisma"; + +async function createUserProject(userId: string, projectId: string) { + const existing = await prisma.userProject.findUnique({ + where: { userId_projectId: { userId, projectId } } + }); + if (existing) { + throw new Error("User project already exists"); + } + return await prisma.userProject.create({ data: { userId, projectId } }); +} + +async function getUserProject(userId: string, projectId: string) { + return await prisma.userProject.findUnique({ + where: { userId_projectId: { userId, projectId } } + }); +} + +async function getUserProjects(filters?: { userId?: string, projectId?: string }) { + return await prisma.userProject.findMany({ + where: filters , + }); +} + +async function deleteUserProject(userId: string, projectId: string) { + return await prisma.userProject.delete({ + where: { userId_projectId: { userId, projectId } } + }); +} \ No newline at end of file From 4d41c9205427effba121879cded1e491b5fe2b50 Mon Sep 17 00:00:00 2001 From: Caitlin Lee Date: Tue, 7 Apr 2026 18:46:11 -0400 Subject: [PATCH 2/2] AUTH-9 Implemented soft delete and renamed deleteUserProject() --- src/lib/userProject.ts | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/lib/userProject.ts b/src/lib/userProject.ts index dc958f0..8658329 100644 --- a/src/lib/userProject.ts +++ b/src/lib/userProject.ts @@ -2,29 +2,32 @@ import { prisma } from "@/lib/prisma"; async function createUserProject(userId: string, projectId: string) { - const existing = await prisma.userProject.findUnique({ - where: { userId_projectId: { userId, projectId } } - }); + const existing = await prisma.userProject.findUnique({ where: { userId_projectId: { userId, projectId, deletedAt: null } }}); if (existing) { throw new Error("User project already exists"); } - return await prisma.userProject.create({ data: { userId, projectId } }); + return await prisma.userProject.upsert({ + where: { userId_projectId: { userId, projectId } }, + update: {deletedAt: null}, + create: { userId, projectId } }); } -async function getUserProject(userId: string, projectId: string) { +async function getUserProject(userId: string, projectId: string) { return await prisma.userProject.findUnique({ - where: { userId_projectId: { userId, projectId } } - }); + where: { userId_projectId: { userId, projectId, deletedAt: null } } } + ); } + -async function getUserProjects(filters?: { userId?: string, projectId?: string }) { +async function getUserProjects({userId, projectId}: { userId?: string, projectId?: string }) { return await prisma.userProject.findMany({ - where: filters , + where: { userId, projectId, deletedAt: null }, }); } -async function deleteUserProject(userId: string, projectId: string) { - return await prisma.userProject.delete({ - where: { userId_projectId: { userId, projectId } } +async function removeUserFromProject(userId: string, projectId: string) { + return await prisma.userProject.update({ + where: { userId_projectId: { userId, projectId, deletedAt: null } }, + data: { deletedAt: new Date() } }); -} \ No newline at end of file +}