diff --git a/packages/api/src/controllers/dispatch/911-calls/Calls911Controller.ts b/packages/api/src/controllers/dispatch/911-calls/Calls911Controller.ts index 2631e2fa5..90d8929f3 100644 --- a/packages/api/src/controllers/dispatch/911-calls/Calls911Controller.ts +++ b/packages/api/src/controllers/dispatch/911-calls/Calls911Controller.ts @@ -7,17 +7,13 @@ import { prisma } from "lib/prisma"; import { Socket } from "services/SocketService"; import { UseBeforeEach } from "@tsed/platform-middlewares"; import { IsAuth } from "middlewares/IsAuth"; -import { unitProperties, combinedUnitProperties, _leoProperties } from "lib/leo/activeOfficer"; +import { unitProperties, _leoProperties } from "lib/leo/activeOfficer"; import { validateSchema } from "lib/validateSchema"; import { ShouldDoType, - CombinedLeoUnit, - Officer, - EmsFdDeputy, DepartmentValue, DivisionValue, User, - StatusValue, MiscCadSettings, Call911, } from "@prisma/client"; @@ -27,6 +23,7 @@ import type { APIEmbed } from "discord-api-types/v10"; import { manyToManyHelper } from "utils/manyToMany"; import { Permissions, UsePermissions } from "middlewares/UsePermissions"; import { officerOrDeputyToUnit } from "lib/leo/officerOrDeputyToUnit"; +import { findUnit } from "lib/leo/findUnit"; export const assignedUnitsInclude = { include: { @@ -346,7 +343,7 @@ export class Calls911Controller { throw new BadRequest("unitIsRequired"); } - const { unit, type } = await findUnit(rawUnit, undefined, true); + const { unit, type } = await findUnit(rawUnit); if (!unit) { throw new NotFound("unitNotFound"); } @@ -438,13 +435,9 @@ export class Calls911Controller { ) { await Promise.all( units.map(async (id) => { - const { unit, type } = await findUnit( - id, - { - NOT: { status: { shouldDo: ShouldDoType.SET_OFF_DUTY } }, - }, - true, - ); + const { unit, type } = await findUnit(id, { + NOT: { status: { shouldDo: ShouldDoType.SET_OFF_DUTY } }, + }); if (!unit) { throw new BadRequest("unitOffDuty"); @@ -540,41 +533,3 @@ export class Calls911Controller { }; } } - -export async function findUnit( - id: string, - extraFind?: any, - searchCombined?: false, -): Promise<{ unit: Officer | EmsFdDeputy | null; type: "leo" | "ems-fd" }>; -export async function findUnit( - id: string, - extraFind?: any, - searchCombined?: true, -): Promise<{ - unit: Officer | EmsFdDeputy | (CombinedLeoUnit & { status: StatusValue }) | null; - type: "leo" | "ems-fd" | "combined"; -}>; -export async function findUnit(id: string, extraFind?: any, searchCombined?: boolean) { - let type: "leo" | "ems-fd" = "leo"; - let unit: any = await prisma.officer.findFirst({ - where: { id, ...extraFind }, - }); - - if (!unit) { - type = "ems-fd"; - unit = await prisma.emsFdDeputy.findFirst({ where: { id, ...extraFind } }); - } - - if (searchCombined && !unit) { - unit = await prisma.combinedLeoUnit.findFirst({ - where: { - id, - }, - include: combinedUnitProperties, - }); - - return { type: "combined", unit: unit ?? null }; - } - - return { type, unit: unit ?? null }; -} diff --git a/packages/api/src/controllers/dispatch/DispatchController.ts b/packages/api/src/controllers/dispatch/DispatchController.ts index 4df5d28ea..f52019650 100644 --- a/packages/api/src/controllers/dispatch/DispatchController.ts +++ b/packages/api/src/controllers/dispatch/DispatchController.ts @@ -10,12 +10,12 @@ import type { cad, User } from "@prisma/client"; import { validateSchema } from "lib/validateSchema"; import { UPDATE_AOP_SCHEMA, UPDATE_RADIO_CHANNEL_SCHEMA } from "@snailycad/schemas"; import { leoProperties, unitProperties, combinedUnitProperties } from "lib/leo/activeOfficer"; -import { findUnit } from "./911-calls/Calls911Controller"; import { ExtendedNotFound } from "src/exceptions/ExtendedNotFound"; import { incidentInclude } from "controllers/leo/incidents/IncidentController"; import { UsePermissions, Permissions } from "middlewares/UsePermissions"; import { userProperties } from "lib/auth/user"; import { officerOrDeputyToUnit } from "lib/leo/officerOrDeputyToUnit"; +import { findUnit } from "lib/leo/findUnit"; @Controller("/dispatch") @UseBeforeEach(IsAuth) @@ -166,7 +166,7 @@ export class DispatchController { }) async updateRadioChannel(@PathParams("unitId") unitId: string, @BodyParams() body: unknown) { const data = validateSchema(UPDATE_RADIO_CHANNEL_SCHEMA, body); - const { unit, type } = await findUnit(unitId, undefined, true); + const { unit, type } = await findUnit(unitId); if (!unit) { throw new ExtendedNotFound({ radioChannel: "Unit not found" }); diff --git a/packages/api/src/controllers/dispatch/StatusController.ts b/packages/api/src/controllers/dispatch/StatusController.ts index 75d343491..a62945409 100644 --- a/packages/api/src/controllers/dispatch/StatusController.ts +++ b/packages/api/src/controllers/dispatch/StatusController.ts @@ -18,7 +18,7 @@ import { BadRequest, NotFound } from "@tsed/exceptions"; import { BodyParams, Context, PathParams } from "@tsed/platform-params"; import { Description, Put } from "@tsed/schema"; import { prisma } from "lib/prisma"; -import { callInclude, findUnit } from "./911-calls/Calls911Controller"; +import { callInclude } from "./911-calls/Calls911Controller"; import { combinedUnitProperties, leoProperties, unitProperties } from "lib/leo/activeOfficer"; import { sendDiscordWebhook } from "lib/discord/webhooks"; import { Socket } from "services/SocketService"; @@ -27,6 +27,7 @@ import { generateCallsign } from "@snailycad/utils/callsign"; import { validateSchema } from "lib/validateSchema"; import { handleStartEndOfficerLog } from "lib/leo/handleStartEndOfficerLog"; import { UsePermissions, Permissions } from "middlewares/UsePermissions"; +import { findUnit } from "lib/leo/findUnit"; @Controller("/dispatch/status") @UseBeforeEach(IsAuth) @@ -60,11 +61,7 @@ export class StatusController { const isFromDispatch = req.headers["is-from-dispatch"]?.toString() === "true"; const isDispatch = isFromDispatch && user.isDispatch; - const { type, unit } = await findUnit( - unitId, - { userId: isDispatch ? undefined : user.id }, - true, - ); + const { type, unit } = await findUnit(unitId, { userId: isDispatch ? undefined : user.id }); if (!unit) { throw new NotFound("unitNotFound"); diff --git a/packages/api/src/lib/leo/findUnit.ts b/packages/api/src/lib/leo/findUnit.ts new file mode 100644 index 000000000..c2e20a8df --- /dev/null +++ b/packages/api/src/lib/leo/findUnit.ts @@ -0,0 +1,46 @@ +import { combinedUnitProperties } from "lib/leo/activeOfficer"; +import type { CombinedLeoUnit, Officer, EmsFdDeputy, StatusValue } from "@prisma/client"; +import { prisma } from "lib/prisma"; + +export async function findUnit( + id: string, + extraFind?: any, +): Promise { + let type: "leo" | "ems-fd" = "leo"; + let unit: any = await prisma.officer.findFirst({ + where: { id, ...extraFind }, + }); + + if (!unit) { + type = "ems-fd"; + unit = await prisma.emsFdDeputy.findFirst({ where: { id, ...extraFind } }); + } + + if (!unit) { + unit = await prisma.combinedLeoUnit.findFirst({ + where: { + id, + }, + include: combinedUnitProperties, + }); + + return { type: "combined", unit: unit ?? null }; + } + + return { type, unit: unit ?? null }; +} + +interface OfficerReturn { + type: "leo"; + unit: Officer | null; +} + +interface EmsFdReturn { + type: "ems-fd"; + unit: EmsFdDeputy | null; +} + +interface CombinedUnitReturn { + type: "combined"; + unit: (CombinedLeoUnit & { status: StatusValue }) | null; +}