From 9238b20eb86856292438757fe426ff6c6769fb0b Mon Sep 17 00:00:00 2001 From: Lewis Carhart Date: Wed, 19 Feb 2025 22:20:08 +0000 Subject: [PATCH 1/3] fix: style of unauthorized page --- apps/portal/src/app/[locale]/(app)/unauthorized/page.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/portal/src/app/[locale]/(app)/unauthorized/page.tsx b/apps/portal/src/app/[locale]/(app)/unauthorized/page.tsx index 03b8df5f78..1f6497e559 100644 --- a/apps/portal/src/app/[locale]/(app)/unauthorized/page.tsx +++ b/apps/portal/src/app/[locale]/(app)/unauthorized/page.tsx @@ -7,11 +7,11 @@ export default async function Unauthorized() { }); return ( -
-
+
+

We couldn't find an organization for you. Please contact your administrator. -

+
); } From 2f29525495aae1cff1e75fee27feb4a0552eb243 Mon Sep 17 00:00:00 2001 From: Lewis Carhart Date: Wed, 19 Feb 2025 22:20:24 +0000 Subject: [PATCH 2/3] fix: style of unauthorized page --- apps/portal/src/app/[locale]/(app)/unauthorized/page.tsx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/apps/portal/src/app/[locale]/(app)/unauthorized/page.tsx b/apps/portal/src/app/[locale]/(app)/unauthorized/page.tsx index 1f6497e559..53a33e5ff1 100644 --- a/apps/portal/src/app/[locale]/(app)/unauthorized/page.tsx +++ b/apps/portal/src/app/[locale]/(app)/unauthorized/page.tsx @@ -1,11 +1,4 @@ -import { auth } from "@/app/lib/auth"; -import { headers } from "next/headers"; - export default async function Unauthorized() { - const session = await auth.api.getSession({ - headers: await headers(), - }); - return (

From 45884076e98638b76ee20901617740af37786c8e Mon Sep 17 00:00:00 2001 From: Lewis Carhart Date: Wed, 19 Feb 2025 22:26:38 +0000 Subject: [PATCH 3/3] fix: edge case of portal user existing before employee record --- .../actions/people/create-employee-action.ts | 75 +++++++++++++++---- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/apps/app/src/actions/people/create-employee-action.ts b/apps/app/src/actions/people/create-employee-action.ts index 5ad7eba710..ab15b1074e 100644 --- a/apps/app/src/actions/people/create-employee-action.ts +++ b/apps/app/src/actions/people/create-employee-action.ts @@ -1,6 +1,6 @@ "use server"; -import { db } from "@bubba/db"; +import { type Employee, db } from "@bubba/db"; import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library"; import { authActionClient } from "../safe-action"; import { createEmployeeSchema } from "../schema"; @@ -46,20 +46,56 @@ export const createEmployeeAction = authActionClient } try { - // Create the employee - const employee = await db.employee.create({ - data: { - name, - email, - department, - organizationId: user.organizationId, - isActive: true, - externalEmployeeId, + // First check if an employee exists (active or inactive) + const existingEmployee = await db.employee.findUnique({ + where: { + email_organizationId: { + email, + organizationId: user.organizationId, + }, }, }); - const portalUser = await db.portalUser.create({ - data: { + let employee: Employee; + + if (existingEmployee) { + if (existingEmployee.isActive) { + return { + success: false, + error: + "An employee with this email already exists in your organization", + }; + } + + // Reactivate the existing employee + employee = await db.employee.update({ + where: { id: existingEmployee.id }, + data: { + name, + department, + isActive: true, + externalEmployeeId, + organizationId: user.organizationId, + updatedAt: new Date(), + }, + }); + } else { + employee = await db.employee.create({ + data: { + name, + email, + department, + organizationId: user.organizationId, + isActive: true, + externalEmployeeId, + }, + }); + } + + // Update or create portalUser + const portalUser = await db.portalUser.upsert({ + where: { email }, + create: { id: employee.id, name, email, @@ -73,6 +109,17 @@ export const createEmployeeAction = authActionClient }, }, }, + update: { + updatedAt: new Date(), + name, + email, + organizationId: user.organizationId, + employees: { + connect: { + id: employee.id, + }, + }, + }, }); // Create or get the required task definitions first and store their IDs @@ -87,7 +134,7 @@ export const createEmployeeAction = authActionClient }, update: {}, }); - }) + }), ); // Now create the employee tasks using the actual task IDs @@ -100,7 +147,7 @@ export const createEmployeeAction = authActionClient status: "assigned", }, }); - }) + }), ); return {