Skip to content

Commit

Permalink
fix: unable to create more than 1 temp unit
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Oct 2, 2023
1 parent 1f3c5da commit c8483df
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 12 additions & 9 deletions apps/api/src/controllers/leo/my-officers/upsert-officer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,18 @@ function toIdString(array: (string | { value: string })[]) {
async function upsertOfficerCitizen(
options: Omit<CreateOfficerOptions, "body" | "schema"> & { 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 }
: null;

if (!citizen) {
if (!options.user) {
// temporary unit's citizen
citizen = await prisma.citizen.create({
data: {
address: "",
Expand All @@ -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 },
});
}
}

Expand Down
21 changes: 12 additions & 9 deletions apps/api/src/lib/ems-fd/upsert-ems-fd-deputy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,18 @@ export async function upsertEmsFdDeputy(options: UpsertEmsFdDeputyOptions) {
async function upsertEmsFdCitizen(
options: Omit<UpsertEmsFdDeputyOptions, "body" | "schema"> & { 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 }
: null;

if (!citizen) {
if (!options.user) {
// temporary unit's citizen
citizen = await prisma.citizen.create({
data: {
address: "",
Expand All @@ -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 },
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ export function CreateTemporaryUnitModal({ onClose }: Props) {
const { json } = await execute<Put911CallByIdData>({
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) {
Expand All @@ -64,7 +67,6 @@ export function CreateTemporaryUnitModal({ onClose }: Props) {
callsign2: "",
badgeNumberString: "",
identifier: "",
citizenId: "test",
};

return (
Expand Down
2 changes: 1 addition & 1 deletion apps/client/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit c8483df

Please sign in to comment.