Skip to content

Commit

Permalink
feat: EMS/FD/LEO incident webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Oct 23, 2023
1 parent 02e93ac commit a372974
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { QueryParams, BodyParams, Context, PathParams } from "@tsed/platform-par
import { prisma } from "lib/data/prisma";
import { IsAuth } from "middlewares/auth/is-auth";
import { EMS_FD_INCIDENT_SCHEMA } from "@snailycad/schemas";
import { type Officer, type MiscCadSettings, type CombinedLeoUnit } from "@prisma/client";
import {
type Officer,
type MiscCadSettings,
type CombinedLeoUnit,
DiscordWebhookType,
} from "@prisma/client";
import { validateSchema } from "lib/data/validate-schema";
import { Socket } from "services/socket-service";
import { UsePermissions, Permissions } from "middlewares/use-permissions";
Expand All @@ -18,6 +23,11 @@ import { assignUnitsInvolvedToIncident } from "lib/incidents/handle-involved-uni
import { ActiveDeputy } from "middlewares/active-deputy";
import { AuditLogActionType, createAuditLogEntry } from "@snailycad/audit-logger/server";
import { _leoProperties, assignedUnitsInclude, unitProperties } from "utils/leo/includes";
import { sendDiscordWebhook } from "~/lib/discord/webhooks";
import { User } from "@snailycad/types";
import type { APIEmbed } from "discord-api-types/v10";
import { getTranslator } from "~/utils/get-translator";
import { slateDataToString } from "@snailycad/utils/editor";

export const incidentInclude = {
creator: { include: unitProperties },
Expand Down Expand Up @@ -116,6 +126,7 @@ export class IncidentController {
@Context("cad") cad: { miscCadSettings: MiscCadSettings },
@Context("activeOfficer") activeOfficer: (CombinedLeoUnit & { officers: Officer[] }) | Officer,
@Context("sessionUserId") sessionUserId: string,
@Context("user") user: User,
): Promise<APITypes.PostIncidentsData<"ems-fd">> {
const data = validateSchema(EMS_FD_INCIDENT_SCHEMA, body);
const officer = getUserOfficerFromActiveOfficer({
Expand Down Expand Up @@ -170,6 +181,13 @@ export class IncidentController {
await this.socket.emitUpdateOfficerStatus();
}

const webhookData = await createIncidentWebhookData(corrected, user.locale ?? "en");
await sendDiscordWebhook({
data: webhookData,
type: DiscordWebhookType.EMS_FD_INCIDENT_CREATED,
extraMessageData: { userDiscordId: user.discordId },
});

return corrected;
}

Expand Down Expand Up @@ -381,3 +399,53 @@ export class IncidentController {
return true;
}
}

export async function createIncidentWebhookData(
incident: APITypes.PostIncidentsData<"ems-fd" | "leo">,
locale: string,
) {
const t = await getTranslator({
type: "webhooks",
namespace: "Incidents",
locale,
});

const isEmsFd = "fireType" in incident;
const title = isEmsFd ? t("createdEms") : t("createdLeo");

return {
embeds: [
{
title,
description: slateDataToString(incident.descriptionData) || incident.description || "---",
fields: [
{
inline: true,
name: t("fireArmsInvolved"),
value: incident.firearmsInvolved ? t("yes") : t("no"),
},
{
inline: true,
name: t("injuriesOrFatalities"),
value: incident.injuriesOrFatalities ? t("yes") : t("no"),
},
{
inline: true,
name: t("arrestsMade"),
value: incident.arrestsMade ? t("yes") : t("no"),
},
{
name: t("situationCode"),
value: incident.situationCode?.value.value ?? t("none"),
inline: true,
},
{
name: t("postal"),
value: incident.postal ?? t("none"),
inline: true,
},
],
},
],
} as { embeds: APIEmbed[] };
}
19 changes: 17 additions & 2 deletions apps/api/src/controllers/leo/incidents/IncidentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { IsAuth } from "middlewares/auth/is-auth";
import { leoProperties, _leoProperties, assignedUnitsInclude } from "utils/leo/includes";
import { LEO_INCIDENT_SCHEMA } from "@snailycad/schemas";
import { ActiveOfficer } from "middlewares/active-officer";
import type { Officer, MiscCadSettings, CombinedLeoUnit } from "@prisma/client";
import {
type Officer,
type MiscCadSettings,
type CombinedLeoUnit,
DiscordWebhookType,
} from "@prisma/client";
import { validateSchema } from "lib/data/validate-schema";
import { Socket } from "services/socket-service";
import { UsePermissions, Permissions } from "middlewares/use-permissions";
Expand All @@ -17,8 +22,10 @@ import { getUserOfficerFromActiveOfficer, getInactivityFilter } from "lib/leo/ut
import type * as APITypes from "@snailycad/types/api";
import { getNextIncidentId } from "lib/incidents/get-next-incident-id";
import { assignUnitsInvolvedToIncident } from "lib/incidents/handle-involved-units";
import { type cad } from "@snailycad/types";
import { User, type cad } from "@snailycad/types";
import { AuditLogActionType, createAuditLogEntry } from "@snailycad/audit-logger/server";
import { createIncidentWebhookData } from "~/controllers/ems-fd/incidents/ems-fd-incidents-controller";
import { sendDiscordWebhook } from "~/lib/discord/webhooks";

export const incidentInclude = {
creator: { include: leoProperties },
Expand Down Expand Up @@ -112,6 +119,7 @@ export class IncidentController {
@Context("cad") cad: { miscCadSettings: MiscCadSettings },
@Context("activeOfficer") activeOfficer: (CombinedLeoUnit & { officers: Officer[] }) | Officer,
@Context("sessionUserId") sessionUserId: string,
@Context("user") user: User,
): Promise<APITypes.PostIncidentsData<"leo">> {
const data = validateSchema(LEO_INCIDENT_SCHEMA, body);
const officer = getUserOfficerFromActiveOfficer({
Expand Down Expand Up @@ -164,6 +172,13 @@ export class IncidentController {
await this.socket.emitUpdateOfficerStatus();
}

const webhookData = await createIncidentWebhookData(corrected, user.locale ?? "en");
await sendDiscordWebhook({
data: webhookData,
type: DiscordWebhookType.LEO_INCIDENT_CREATED,
extraMessageData: { userDiscordId: user.discordId },
});

return corrected;
}

Expand Down
11 changes: 11 additions & 0 deletions apps/client/locales/en/webhooks.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,16 @@
"userChangeTitle": "User Whitelist Status Change",
"departmentChangeDescription": "Unit: {unit}'s whitelist status has been changed to {status}.",
"departmentChangeTitle": "Unit Department Whitelist Status Change"
},
"Incidents": {
"createdEms": "EMS/FD Incident Created",
"createdLeo": "LEO Incident Created",
"fireArmsIncluded": "Firearm(s) included",
"injuriesOrFatalities": "Injuries or fatalities",
"arrestsMade": "Arrest(s) made",
"situationCode": "Situation Code",
"postal": "Postal",
"yes": "Yes",
"no": "No"
}
}

0 comments on commit a372974

Please sign in to comment.