From c8483df0e473ffaf39fa19b4319f34702f68ddbd Mon Sep 17 00:00:00 2001 From: casperiv0 <53900565+casperiv0@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:46:07 +0200 Subject: [PATCH] fix: unable to create more than 1 temp unit --- .../manage/units/manage-units-controller.ts | 2 +- .../leo/my-officers/upsert-officer.ts | 21 +++++++++++-------- .../src/lib/ems-fd/upsert-ems-fd-deputy.ts | 21 +++++++++++-------- .../create-temporary-unit-modal.tsx | 6 ++++-- apps/client/src/pages/_app.tsx | 2 +- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/apps/api/src/controllers/admin/manage/units/manage-units-controller.ts b/apps/api/src/controllers/admin/manage/units/manage-units-controller.ts index 1acc9b30f..bea49fad7 100644 --- a/apps/api/src/controllers/admin/manage/units/manage-units-controller.ts +++ b/apps/api/src/controllers/admin/manage/units/manage-units-controller.ts @@ -531,7 +531,7 @@ export class AdminManageUnitsController { badgeNumberString: data.badgeNumberString, imageId: validatedImageURL, imageBlurData: await generateBlurPlaceholder(validatedImageURL), - userId: data.userId, + userId: data.userId || undefined, isTemporary: Boolean(data.userId), }, include: type === "officer" ? leoProperties : unitProperties, diff --git a/apps/api/src/controllers/leo/my-officers/upsert-officer.ts b/apps/api/src/controllers/leo/my-officers/upsert-officer.ts index 2769cf915..15e5f2c7a 100644 --- a/apps/api/src/controllers/leo/my-officers/upsert-officer.ts +++ b/apps/api/src/controllers/leo/my-officers/upsert-officer.ts @@ -236,6 +236,10 @@ function toIdString(array: (string | { value: string })[]) { async function upsertOfficerCitizen( options: Omit & { data: any }, ) { + if (options.citizen) { + return options.citizen; + } + // means the officer that is being created is a temporary unit let citizen: { id: string; userId: string | null } | null = options.existingOfficer?.citizenId ? { id: options.existingOfficer.citizenId, userId: options.existingOfficer.userId } @@ -243,6 +247,7 @@ async function upsertOfficerCitizen( if (!citizen) { if (!options.user) { + // temporary unit's citizen citizen = await prisma.citizen.create({ data: { address: "", @@ -261,15 +266,13 @@ async function upsertOfficerCitizen( cad: options.cad, user: options.user, }); - citizen = - options.citizen ?? - (await prisma.citizen.findFirst({ - where: { - id: options.data.citizenId, - userId: checkCitizenUserId ? options.user.id : undefined, - }, - select: { userId: true, id: true }, - })); + citizen = await prisma.citizen.findFirst({ + where: { + id: options.data.citizenId, + userId: checkCitizenUserId ? options.user.id : undefined, + }, + select: { userId: true, id: true }, + }); } } diff --git a/apps/api/src/lib/ems-fd/upsert-ems-fd-deputy.ts b/apps/api/src/lib/ems-fd/upsert-ems-fd-deputy.ts index 589198e8e..2c8cd69b5 100644 --- a/apps/api/src/lib/ems-fd/upsert-ems-fd-deputy.ts +++ b/apps/api/src/lib/ems-fd/upsert-ems-fd-deputy.ts @@ -152,6 +152,10 @@ export async function upsertEmsFdDeputy(options: UpsertEmsFdDeputyOptions) { async function upsertEmsFdCitizen( options: Omit & { data: any }, ) { + if (options.citizen) { + return options.citizen; + } + // means the ems-fd deputy that is being created is a temporary unit let citizen: { id: string; userId: string | null } | null = options.existingDeputy?.citizenId ? { id: options.existingDeputy.citizenId, userId: options.existingDeputy.userId } @@ -159,6 +163,7 @@ async function upsertEmsFdCitizen( if (!citizen) { if (!options.user) { + // temporary unit's citizen citizen = await prisma.citizen.create({ data: { address: "", @@ -177,15 +182,13 @@ async function upsertEmsFdCitizen( cad: options.cad, user: options.user, }); - citizen = - options.citizen ?? - (await prisma.citizen.findFirst({ - where: { - id: options.data.citizenId, - userId: checkCitizenUserId ? options.user.id : undefined, - }, - select: { userId: true, id: true }, - })); + citizen = await prisma.citizen.findFirst({ + where: { + id: options.data.citizenId, + userId: checkCitizenUserId ? options.user.id : undefined, + }, + select: { userId: true, id: true }, + }); } } diff --git a/apps/client/src/components/dispatch/modals/temporary-units/create-temporary-unit-modal.tsx b/apps/client/src/components/dispatch/modals/temporary-units/create-temporary-unit-modal.tsx index 8f3f4f4b9..a3c73f49a 100644 --- a/apps/client/src/components/dispatch/modals/temporary-units/create-temporary-unit-modal.tsx +++ b/apps/client/src/components/dispatch/modals/temporary-units/create-temporary-unit-modal.tsx @@ -42,7 +42,10 @@ export function CreateTemporaryUnitModal({ onClose }: Props) { const { json } = await execute({ path: `/temporary-units/${type}`, method: "POST", - data: { ...values, identifiers: values.identifier.split(",") }, + data: { + ...values, + identifiers: values.identifier.length <= 0 ? [] : values.identifier.split(","), + }, }); if (json.id) { @@ -64,7 +67,6 @@ export function CreateTemporaryUnitModal({ onClose }: Props) { callsign2: "", badgeNumberString: "", identifier: "", - citizenId: "test", }; return ( diff --git a/apps/client/src/pages/_app.tsx b/apps/client/src/pages/_app.tsx index 07b80e7a2..dba3c98e9 100644 --- a/apps/client/src/pages/_app.tsx +++ b/apps/client/src/pages/_app.tsx @@ -38,7 +38,7 @@ export default function App({ Component, router, pageProps, ...rest }: AppProps) const user = pageProps.session as User | null; const locale = user?.locale ?? router.locale ?? "en"; const cad = pageProps.cad ?? pageProps.session?.cad ?? null; - const timeZone = cad?.timeZone ?? undefined; + const timeZone = cad?.timeZone || undefined; React.useEffect(() => { const handleRouteStart = async () => {