Skip to content

Commit

Permalink
🔨 chore: improve findUnit code
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Apr 9, 2022
2 parents 9912337 + c414760 commit 41d2c2b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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: {
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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 };
}
4 changes: 2 additions & 2 deletions packages/api/src/controllers/dispatch/DispatchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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" });
Expand Down
9 changes: 3 additions & 6 deletions packages/api/src/controllers/dispatch/StatusController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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)
Expand Down Expand Up @@ -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");
Expand Down
46 changes: 46 additions & 0 deletions packages/api/src/lib/leo/findUnit.ts
Original file line number Diff line number Diff line change
@@ -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<OfficerReturn | EmsFdReturn | CombinedUnitReturn> {
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;
}

0 comments on commit 41d2c2b

Please sign in to comment.