Skip to content

Commit

Permalink
🔨 chore: refactor some API folders (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 authored Feb 26, 2022
1 parent 06a1a64 commit 66964ba
Show file tree
Hide file tree
Showing 24 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/controllers/admin/manage/Citizens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/admin/manage/Units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/auth/Discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/citizen/CitizenController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/dispatch/BoloController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/dispatch/StatusController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/ems-fd/EmsFdController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/leo/JailController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/leo/LeoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/leo/SearchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/record/RecordsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/lib/discord/admin.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/lib/discord/auth.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/lib/discord/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions packages/api/src/lib/ems-fd.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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,
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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 } },
Expand Down Expand Up @@ -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" } },
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion packages/api/src/middlewares/ActiveOfficer.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 66964ba

Please sign in to comment.