diff --git a/packages/api/src/controllers/admin/manage/Citizens.ts b/packages/api/src/controllers/admin/manage/Citizens.ts index 8f41ff198..c8e6ebe05 100644 --- a/packages/api/src/controllers/admin/manage/Citizens.ts +++ b/packages/api/src/controllers/admin/manage/Citizens.ts @@ -4,7 +4,7 @@ import { UseBeforeEach } from "@tsed/platform-middlewares"; import { BodyParams, Context, PathParams } from "@tsed/platform-params"; import { Delete, Description, Get, Put } from "@tsed/schema"; import { userProperties } from "lib/auth/user"; -import { leoProperties } from "lib/officer"; +import { leoProperties } from "lib/leo/activeOfficer"; import { prisma } from "lib/prisma"; import { IsAuth } from "middlewares/index"; import { CREATE_CITIZEN_SCHEMA } from "@snailycad/schemas"; diff --git a/packages/api/src/controllers/admin/manage/Units.ts b/packages/api/src/controllers/admin/manage/Units.ts index 5171ccf3c..51fefc766 100644 --- a/packages/api/src/controllers/admin/manage/Units.ts +++ b/packages/api/src/controllers/admin/manage/Units.ts @@ -10,7 +10,7 @@ import { unlinkDivisionsFromOfficer, validateMaxDivisionsPerOfficer, } from "controllers/leo/LeoController"; -import { leoProperties, unitProperties } from "lib/officer"; +import { leoProperties, unitProperties } from "lib/leo/activeOfficer"; import { prisma } from "lib/prisma"; import { validateSchema } from "lib/validateSchema"; import { IsAuth } from "middlewares/index"; diff --git a/packages/api/src/controllers/admin/manage/cad-settings/discord/DiscordSettingsController.ts b/packages/api/src/controllers/admin/manage/cad-settings/discord/DiscordSettingsController.ts index 75a182748..ff5a43465 100644 --- a/packages/api/src/controllers/admin/manage/cad-settings/discord/DiscordSettingsController.ts +++ b/packages/api/src/controllers/admin/manage/cad-settings/discord/DiscordSettingsController.ts @@ -8,7 +8,7 @@ import type { cad } from "@prisma/client"; import { BadRequest } from "@tsed/exceptions"; import { DISCORD_SETTINGS_SCHEMA } from "@snailycad/schemas"; import { validateSchema } from "lib/validateSchema"; -import { getRest } from "lib/discord"; +import { getRest } from "lib/discord/config"; const guildId = process.env.DISCORD_SERVER_ID; diff --git a/packages/api/src/controllers/admin/manage/cad-settings/discord/DiscordWebhooksController.ts b/packages/api/src/controllers/admin/manage/cad-settings/discord/DiscordWebhooksController.ts index 66fdce312..e43162b29 100644 --- a/packages/api/src/controllers/admin/manage/cad-settings/discord/DiscordWebhooksController.ts +++ b/packages/api/src/controllers/admin/manage/cad-settings/discord/DiscordWebhooksController.ts @@ -14,7 +14,7 @@ import type { cad, MiscCadSettings } from "@prisma/client"; import { BadRequest } from "@tsed/exceptions"; import { DISCORD_WEBHOOKS_SCHEMA } from "@snailycad/schemas"; import { validateSchema } from "lib/validateSchema"; -import { getRest } from "lib/discord"; +import { getRest } from "lib/discord/config"; const guildId = process.env.DISCORD_SERVER_ID; diff --git a/packages/api/src/controllers/auth/Discord.ts b/packages/api/src/controllers/auth/Discord.ts index 690b93cc3..c0f891d11 100644 --- a/packages/api/src/controllers/auth/Discord.ts +++ b/packages/api/src/controllers/auth/Discord.ts @@ -14,7 +14,7 @@ import { signJWT } from "utils/jwt"; import { setCookie } from "utils/setCookie"; import { Cookie } from "@snailycad/config"; import { IsAuth } from "middlewares/index"; -import { DISCORD_API_URL } from "lib/discord"; +import { DISCORD_API_URL } from "lib/discord/config"; import { updateMemberRolesLogin } from "lib/discord/auth"; import { Description } from "@tsed/schema"; diff --git a/packages/api/src/controllers/citizen/CitizenController.ts b/packages/api/src/controllers/citizen/CitizenController.ts index 0fca215ec..ea711564e 100644 --- a/packages/api/src/controllers/citizen/CitizenController.ts +++ b/packages/api/src/controllers/citizen/CitizenController.ts @@ -10,7 +10,7 @@ import { CREATE_CITIZEN_SCHEMA, validate } from "@snailycad/schemas"; import fs from "node:fs"; import { AllowedFileExtension, allowedFileExtensions } from "@snailycad/config"; import { Feature, cad, MiscCadSettings } from ".prisma/client"; -import { leoProperties } from "lib/officer"; +import { leoProperties } from "lib/leo/activeOfficer"; import { validateImgurURL } from "utils/image"; import { generateString } from "utils/generateString"; import type { Citizen, DriversLicenseCategoryValue, User } from "@prisma/client"; diff --git a/packages/api/src/controllers/dispatch/911-calls/Calls911Controller.ts b/packages/api/src/controllers/dispatch/911-calls/Calls911Controller.ts index 5493292a5..412084824 100644 --- a/packages/api/src/controllers/dispatch/911-calls/Calls911Controller.ts +++ b/packages/api/src/controllers/dispatch/911-calls/Calls911Controller.ts @@ -8,7 +8,7 @@ import { Socket } from "services/SocketService"; import { UseBeforeEach } from "@tsed/platform-middlewares"; import { IsAuth } from "middlewares/index"; import { ShouldDoType, CombinedLeoUnit, Officer, EmsFdDeputy } from ".prisma/client"; -import { unitProperties, leoProperties } from "lib/officer"; +import { unitProperties, leoProperties } from "lib/leo/activeOfficer"; import { validateSchema } from "lib/validateSchema"; import type { DepartmentValue, DivisionValue, User, StatusValue } from "@prisma/client"; import { sendDiscordWebhook } from "lib/discord/webhooks"; diff --git a/packages/api/src/controllers/dispatch/BoloController.ts b/packages/api/src/controllers/dispatch/BoloController.ts index b4f50a8e4..2662d64c0 100644 --- a/packages/api/src/controllers/dispatch/BoloController.ts +++ b/packages/api/src/controllers/dispatch/BoloController.ts @@ -8,7 +8,7 @@ import { Use, UseBeforeEach } from "@tsed/platform-middlewares"; import { IsAuth } from "middlewares/index"; import { ActiveOfficer } from "middlewares/ActiveOfficer"; import { Socket } from "services/SocketService"; -import { leoProperties } from "lib/officer"; +import { leoProperties } from "lib/leo/activeOfficer"; import { validateSchema } from "lib/validateSchema"; import type { BoloType } from "@prisma/client"; diff --git a/packages/api/src/controllers/dispatch/DispatchController.ts b/packages/api/src/controllers/dispatch/DispatchController.ts index 00feea771..6d25da266 100644 --- a/packages/api/src/controllers/dispatch/DispatchController.ts +++ b/packages/api/src/controllers/dispatch/DispatchController.ts @@ -10,7 +10,7 @@ import type { cad } from ".prisma/client"; import { Feature, User } from "@prisma/client"; import { validateSchema } from "lib/validateSchema"; import { UPDATE_AOP_SCHEMA, UPDATE_RADIO_CHANNEL_SCHEMA } from "@snailycad/schemas"; -import { leoProperties, unitProperties } from "lib/officer"; +import { leoProperties, unitProperties } from "lib/leo/activeOfficer"; import { findUnit } from "./911-calls/Calls911Controller"; import { ExtendedNotFound } from "src/exceptions/ExtendedNotFound"; diff --git a/packages/api/src/controllers/dispatch/StatusController.ts b/packages/api/src/controllers/dispatch/StatusController.ts index 4d8f4592f..f0c2ecf64 100644 --- a/packages/api/src/controllers/dispatch/StatusController.ts +++ b/packages/api/src/controllers/dispatch/StatusController.ts @@ -15,7 +15,7 @@ import { BodyParams, Context, PathParams } from "@tsed/platform-params"; import { Description, Post, Put } from "@tsed/schema"; import { prisma } from "lib/prisma"; import { callInclude, findUnit } from "./911-calls/Calls911Controller"; -import { leoProperties, unitProperties } from "lib/officer"; +import { leoProperties, unitProperties } from "lib/leo/activeOfficer"; import { sendDiscordWebhook } from "lib/discord/webhooks"; import { Socket } from "services/SocketService"; import { IsAuth } from "middlewares/index"; diff --git a/packages/api/src/controllers/ems-fd/EmsFdController.ts b/packages/api/src/controllers/ems-fd/EmsFdController.ts index c785946fa..6d7d10b17 100644 --- a/packages/api/src/controllers/ems-fd/EmsFdController.ts +++ b/packages/api/src/controllers/ems-fd/EmsFdController.ts @@ -10,7 +10,7 @@ import { AllowedFileExtension, allowedFileExtensions } from "@snailycad/config"; import { IsAuth } from "middlewares/index"; import { ActiveDeputy } from "middlewares/ActiveDeputy"; import fs from "node:fs"; -import { unitProperties } from "lib/officer"; +import { unitProperties } from "lib/leo/activeOfficer"; import { validateImgurURL } from "utils/image"; import { validateSchema } from "lib/validateSchema"; import { ExtendedBadRequest } from "src/exceptions/ExtendedBadRequest"; diff --git a/packages/api/src/controllers/leo/JailController.ts b/packages/api/src/controllers/leo/JailController.ts index 0aabdcdcd..69977f993 100644 --- a/packages/api/src/controllers/leo/JailController.ts +++ b/packages/api/src/controllers/leo/JailController.ts @@ -4,7 +4,7 @@ import { PathParams } from "@tsed/platform-params"; import { NotFound } from "@tsed/exceptions"; import { prisma } from "lib/prisma"; import { IsAuth } from "middlewares/index"; -import { leoProperties } from "lib/officer"; +import { leoProperties } from "lib/leo/activeOfficer"; import { ReleaseType } from "@prisma/client"; import { validateSchema } from "lib/validateSchema"; import { RELEASE_CITIZEN_SCHEMA } from "@snailycad/schemas"; diff --git a/packages/api/src/controllers/leo/LeoController.ts b/packages/api/src/controllers/leo/LeoController.ts index 2e8870951..12c1d2dfc 100644 --- a/packages/api/src/controllers/leo/LeoController.ts +++ b/packages/api/src/controllers/leo/LeoController.ts @@ -17,7 +17,7 @@ import { IsAuth } from "middlewares/index"; import { ActiveOfficer } from "middlewares/ActiveOfficer"; import { Socket } from "services/SocketService"; import fs from "node:fs"; -import { leoProperties } from "lib/officer"; +import { leoProperties } from "lib/leo/activeOfficer"; import { citizenInclude } from "controllers/citizen/CitizenController"; import { validateImgurURL } from "utils/image"; import type { DivisionValue, MiscCadSettings } from "@prisma/client"; diff --git a/packages/api/src/controllers/leo/SearchController.ts b/packages/api/src/controllers/leo/SearchController.ts index 11d34938a..163cfaab2 100644 --- a/packages/api/src/controllers/leo/SearchController.ts +++ b/packages/api/src/controllers/leo/SearchController.ts @@ -4,7 +4,7 @@ import { NotFound } from "@tsed/exceptions"; import { BodyParams, QueryParams } from "@tsed/platform-params"; import { prisma } from "lib/prisma"; import { IsAuth } from "middlewares/index"; -import { leoProperties } from "lib/officer"; +import { leoProperties } from "lib/leo/activeOfficer"; import { citizenInclude } from "controllers/citizen/CitizenController"; export const citizenSearchInclude = { diff --git a/packages/api/src/controllers/leo/incidents/IncidentController.ts b/packages/api/src/controllers/leo/incidents/IncidentController.ts index 6ee94a513..84970e182 100644 --- a/packages/api/src/controllers/leo/incidents/IncidentController.ts +++ b/packages/api/src/controllers/leo/incidents/IncidentController.ts @@ -4,7 +4,7 @@ import { NotFound, InternalServerError } from "@tsed/exceptions"; import { BodyParams, Context, PathParams } from "@tsed/platform-params"; import { prisma } from "lib/prisma"; import { IsAuth } from "middlewares/index"; -import { leoProperties } from "lib/officer"; +import { leoProperties } from "lib/leo/activeOfficer"; import { LEO_INCIDENT_SCHEMA } from "@snailycad/schemas"; import { ActiveOfficer } from "middlewares/ActiveOfficer"; import type { Officer } from ".prisma/client"; diff --git a/packages/api/src/controllers/leo/incidents/IncidentEventsController.ts b/packages/api/src/controllers/leo/incidents/IncidentEventsController.ts index 908134978..7a48db56d 100644 --- a/packages/api/src/controllers/leo/incidents/IncidentEventsController.ts +++ b/packages/api/src/controllers/leo/incidents/IncidentEventsController.ts @@ -4,7 +4,7 @@ import { NotFound } from "@tsed/exceptions"; import { BodyParams, PathParams } from "@tsed/platform-params"; import { prisma } from "lib/prisma"; import { IsAuth } from "middlewares/index"; -import { leoProperties } from "lib/officer"; +import { leoProperties } from "lib/leo/activeOfficer"; import { CREATE_911_CALL_EVENT } from "@snailycad/schemas"; import { validateSchema } from "lib/validateSchema"; import { Socket } from "services/SocketService"; diff --git a/packages/api/src/controllers/record/RecordsController.ts b/packages/api/src/controllers/record/RecordsController.ts index 46f991ea6..3c84c38de 100644 --- a/packages/api/src/controllers/record/RecordsController.ts +++ b/packages/api/src/controllers/record/RecordsController.ts @@ -14,7 +14,7 @@ import { IsAuth } from "middlewares/index"; import type { RecordType, SeizedItem, Violation, WarrantStatus } from "@prisma/client"; import { validateSchema } from "lib/validateSchema"; import { validateRecordData } from "lib/records/validateRecordData"; -import { leoProperties } from "lib/officer"; +import { leoProperties } from "lib/leo/activeOfficer"; @UseBeforeEach(IsAuth, ActiveOfficer) @Controller("/records") diff --git a/packages/api/src/lib/discord/admin.ts b/packages/api/src/lib/discord/admin.ts index 2d3ec0e5a..a44fb7e49 100644 --- a/packages/api/src/lib/discord/admin.ts +++ b/packages/api/src/lib/discord/admin.ts @@ -1,6 +1,6 @@ import { Rank, User, WhitelistStatus } from "@prisma/client"; import { RESTGetAPIGuildMemberResult, Routes } from "discord-api-types/v10"; -import { BOT_TOKEN, getRest, GUILD_ID } from "lib/discord"; +import { BOT_TOKEN, getRest, GUILD_ID } from "lib/discord/config"; import { prisma } from "lib/prisma"; type UserProperties = diff --git a/packages/api/src/lib/discord/auth.ts b/packages/api/src/lib/discord/auth.ts index 01d0dd233..c3610d9c7 100644 --- a/packages/api/src/lib/discord/auth.ts +++ b/packages/api/src/lib/discord/auth.ts @@ -1,6 +1,6 @@ import { Rank, User, WhitelistStatus } from "@prisma/client"; import { Routes, type RESTGetAPIGuildMemberResult } from "discord-api-types/v10"; -import { getRest, GUILD_ID } from "lib/discord"; +import { getRest, GUILD_ID } from "lib/discord/config"; import { prisma } from "lib/prisma"; /** diff --git a/packages/api/src/lib/discord.ts b/packages/api/src/lib/discord/config.ts similarity index 67% rename from packages/api/src/lib/discord.ts rename to packages/api/src/lib/discord/config.ts index d4f76ed45..a07d823e4 100644 --- a/packages/api/src/lib/discord.ts +++ b/packages/api/src/lib/discord/config.ts @@ -1,8 +1,8 @@ import process from "process"; import { REST } from "@discordjs/rest"; -export const DISCORD_API_VERSION = "v10"; -export const DISCORD_API_URL = `https://discord.com/api/${DISCORD_API_VERSION}`; +export const DISCORD_API_VERSION = "10" as const; +export const DISCORD_API_URL = `https://discord.com/api/v${DISCORD_API_VERSION}`; export const GUILD_ID = process.env.DISCORD_SERVER_ID; export const BOT_TOKEN = process.env.DISCORD_BOT_TOKEN; @@ -12,7 +12,7 @@ export function getRest(): REST { throw new Error("mustSetBotTokenGuildId"); } - cacheREST ??= new REST({ version: "10" }).setToken(BOT_TOKEN); + cacheREST ??= new REST({ version: DISCORD_API_VERSION }).setToken(BOT_TOKEN); if (process.env.NODE_ENV === "development") { cacheREST.on("restDebug", console.log); diff --git a/packages/api/src/lib/discord/webhooks.ts b/packages/api/src/lib/discord/webhooks.ts index 2eed295dc..f1eeb936c 100644 --- a/packages/api/src/lib/discord/webhooks.ts +++ b/packages/api/src/lib/discord/webhooks.ts @@ -4,7 +4,7 @@ import { type RESTPostAPIWebhookWithTokenJSONBody, Routes, } from "discord-api-types/v10"; -import { getRest } from "lib/discord"; +import { getRest } from "lib/discord/config"; export async function sendDiscordWebhook( miscCadSettings: MiscCadSettings | null, diff --git a/packages/api/src/lib/ems-fd.ts b/packages/api/src/lib/ems-fd.ts index f24c3b66a..b9644507d 100644 --- a/packages/api/src/lib/ems-fd.ts +++ b/packages/api/src/lib/ems-fd.ts @@ -1,7 +1,7 @@ import type { User } from "@prisma/client"; import type { Req, Context } from "@tsed/common"; import { BadRequest, Forbidden, Unauthorized } from "@tsed/exceptions"; -import { unitProperties } from "./officer"; +import { unitProperties } from "lib/leo/activeOfficer"; import { prisma } from "./prisma"; export async function getActiveDeputy(req: Req, user: User, ctx: Context) { @@ -19,6 +19,10 @@ export async function getActiveDeputy(req: Req, user: User, ctx: Context) { } } + if (isDispatch) { + return null; + } + const deputy = await prisma.emsFdDeputy.findFirst({ where: { userId: user.id, @@ -33,11 +37,8 @@ export async function getActiveDeputy(req: Req, user: User, ctx: Context) { if (!deputy) { ctx.delete("activeDeputy"); - } - - if (!isDispatch && !deputy) { throw new BadRequest("noActiveDeputy"); } - return isDispatch ? null : deputy; + return deputy; } diff --git a/packages/api/src/lib/officer.ts b/packages/api/src/lib/leo/activeOfficer.ts similarity index 89% rename from packages/api/src/lib/officer.ts rename to packages/api/src/lib/leo/activeOfficer.ts index 4783a4e5e..40dda085e 100644 --- a/packages/api/src/lib/officer.ts +++ b/packages/api/src/lib/leo/activeOfficer.ts @@ -1,8 +1,8 @@ import type { User } from ".prisma/client"; import type { Req, Context } from "@tsed/common"; import { BadRequest, Forbidden, Unauthorized } from "@tsed/exceptions"; -import { userProperties } from "./auth/user"; -import { prisma } from "./prisma"; +import { userProperties } from "lib/auth/user"; +import { prisma } from "lib/prisma"; export const unitProperties = { department: { include: { value: true } }, @@ -44,6 +44,10 @@ export async function getActiveOfficer(req: Req, user: User, ctx: Context) { } } + if (isDispatch) { + return null; + } + const combinedUnit = await prisma.combinedLeoUnit.findFirst({ where: { NOT: { status: { shouldDo: "SET_OFF_DUTY" } }, @@ -73,15 +77,12 @@ export async function getActiveOfficer(req: Req, user: User, ctx: Context) { include: unitProperties, }); - const off = combinedUnit ?? officer; + const activeOfficerOrCombinedUnit = combinedUnit ?? officer; - if (!off) { + if (!activeOfficerOrCombinedUnit) { ctx.delete("activeOfficer"); - } - - if (!off && !isDispatch) { throw new BadRequest("noActiveOfficer"); } - return isDispatch ? null : off; + return activeOfficerOrCombinedUnit; } diff --git a/packages/api/src/middlewares/ActiveOfficer.ts b/packages/api/src/middlewares/ActiveOfficer.ts index d75d3325d..fb6c20796 100644 --- a/packages/api/src/middlewares/ActiveOfficer.ts +++ b/packages/api/src/middlewares/ActiveOfficer.ts @@ -1,6 +1,6 @@ import { Context, Middleware, Req, MiddlewareMethods } from "@tsed/common"; import { getSessionUser } from "lib/auth/user"; -import { getActiveOfficer } from "lib/officer"; +import { getActiveOfficer } from "lib/leo/activeOfficer"; @Middleware() export class ActiveOfficer implements MiddlewareMethods {