From 3ecaec6f4597e1cb2d41b29f00c105db0172c9ad Mon Sep 17 00:00:00 2001 From: Jan Gloger Date: Tue, 18 Jun 2024 22:35:36 +0200 Subject: [PATCH] Add sergeant role (second LEO supervisor) --- .../migration.sql | 20 +++++++++++++ apps/api/prisma/schema.prisma | 3 ++ .../discord/DiscordSettingsController.ts | 11 +++++++ apps/api/src/lib/discord/auth.ts | 3 ++ apps/api/src/middlewares/auth/is-auth.ts | 1 + apps/client/locales/en-gb/cad-settings.json | 2 ++ apps/client/locales/en/cad-settings.json | 2 ++ .../manage/cad-settings/discord-roles-tab.tsx | 29 +++++++++++++++++++ packages/permissions/src/defaults/leo.ts | 5 ++++ packages/schemas/src/admin/index.ts | 2 ++ packages/types/src/api/cad-settings.ts | 1 + packages/types/src/index.ts | 2 ++ 12 files changed, 81 insertions(+) create mode 100644 apps/api/prisma/migrations/20240616111022_add_sergreant_role/migration.sql diff --git a/apps/api/prisma/migrations/20240616111022_add_sergreant_role/migration.sql b/apps/api/prisma/migrations/20240616111022_add_sergreant_role/migration.sql new file mode 100644 index 000000000..020b9a7dd --- /dev/null +++ b/apps/api/prisma/migrations/20240616111022_add_sergreant_role/migration.sql @@ -0,0 +1,20 @@ +-- AlterTable +ALTER TABLE "DiscordRoles" ADD COLUMN "sergeantRolePermissions" TEXT[]; + +-- CreateTable +CREATE TABLE "_sergeantRoles" ( + "A" TEXT NOT NULL, + "B" TEXT NOT NULL +); + +-- CreateIndex +CREATE UNIQUE INDEX "_sergeantRoles_AB_unique" ON "_sergeantRoles"("A", "B"); + +-- CreateIndex +CREATE INDEX "_sergeantRoles_B_index" ON "_sergeantRoles"("B"); + +-- AddForeignKey +ALTER TABLE "_sergeantRoles" ADD CONSTRAINT "_sergeantRoles_A_fkey" FOREIGN KEY ("A") REFERENCES "DiscordRole"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_sergeantRoles" ADD CONSTRAINT "_sergeantRoles_B_fkey" FOREIGN KEY ("B") REFERENCES "DiscordRoles"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/apps/api/prisma/schema.prisma b/apps/api/prisma/schema.prisma index e68ed08a6..6edb05b15 100644 --- a/apps/api/prisma/schema.prisma +++ b/apps/api/prisma/schema.prisma @@ -192,6 +192,7 @@ model DiscordRoles { leoRoles DiscordRole[] @relation("leoRoles") leoSupervisorRoles DiscordRole[] @relation("leoSupervisorRoles") + sergeantRoles DiscordRole[] @relation("sergeantRoles") emsFdRoles DiscordRole[] @relation("emsFdRoles") dispatchRoles DiscordRole[] @relation("dispatchRoles") towRoles DiscordRole[] @relation("towRoles") @@ -204,6 +205,7 @@ model DiscordRoles { adminRolePermissions String[] leoRolePermissions String[] leoSupervisorRolePermissions String[] + sergeantRolePermissions String[] emsFdRolePermissions String[] dispatchRolePermissions String[] towRolePermissions String[] @@ -227,6 +229,7 @@ model DiscordRole { whitelistedRoles DiscordRoles[] @relation("whitelistedRole") dispatchRolesArr DiscordRoles[] @relation("dispatchRoles") leoSupervisorRolesArr DiscordRoles[] @relation("leoSupervisorRoles") + sergeantRolesArr DiscordRoles[] @relation("sergeantRoles") taxiRolesArr DiscordRoles[] @relation("taxiRoles") towRolesArr DiscordRoles[] @relation("towRoles") courthouseRoles DiscordRoles[] @relation("courthouseRoles") diff --git a/apps/api/src/controllers/admin/manage/cad-settings/discord/DiscordSettingsController.ts b/apps/api/src/controllers/admin/manage/cad-settings/discord/DiscordSettingsController.ts index 5728ba607..da9298d96 100644 --- a/apps/api/src/controllers/admin/manage/cad-settings/discord/DiscordSettingsController.ts +++ b/apps/api/src/controllers/admin/manage/cad-settings/discord/DiscordSettingsController.ts @@ -111,6 +111,7 @@ export class DiscordSettingsController { leoRoles: data.leoRoles, emsFdRoles: data.emsFdRoles, leoSupervisorRoles: data.leoSupervisorRoles, + sergeantRoles: data.sergeantRoles, dispatchRoles: data.dispatchRoles, courthouseRoles: data.courthouseRoles, towRoles: data.towRoles, @@ -132,6 +133,7 @@ export class DiscordSettingsController { adminRolePermissions: data.adminRolePermissions ?? [], leoRolePermissions: data.leoRolePermissions ?? [], leoSupervisorRolePermissions: data.leoSupervisorRolePermissions ?? [], + sergeantRolePermissions: data.sergeantRolePermissions ?? [], emsFdRolePermissions: data.emsFdRolePermissions ?? [], dispatchRolePermissions: data.dispatchRolePermissions ?? [], towRolePermissions: data.towRolePermissions ?? [], @@ -148,6 +150,7 @@ export class DiscordSettingsController { leoRoles: true, emsFdRoles: true, leoSupervisorRoles: true, + sergeantRoles: true, towRoles: true, taxiRoles: true, dispatchRoles: true, @@ -180,6 +183,12 @@ export class DiscordSettingsController { newRoles: (data.leoSupervisorRoles as string[] | null) ?? [], type: "leoSupervisorRoles", }), + ...this.updateRoles({ + discordRoleId: discordRoles.id, + discordRoles: discordRoles.sergeantRoles, + newRoles: (data.sergeantRoles as string[] | null) ?? [], + type: "sergeantRoles", + }), ...this.updateRoles({ discordRoleId: discordRoles.id, discordRoles: discordRoles.towRoles, @@ -217,6 +226,7 @@ export class DiscordSettingsController { leoRoles: true, emsFdRoles: true, leoSupervisorRoles: true, + sergeantRoles: true, towRoles: true, taxiRoles: true, dispatchRoles: true, @@ -269,6 +279,7 @@ interface UpdateRolesOptions { | "leoRoles" | "emsFdRoles" | "leoSupervisorRoles" + | "sergeantRoles" | "dispatchRoles" | "towRoles" | "taxiRoles" diff --git a/apps/api/src/lib/discord/auth.ts b/apps/api/src/lib/discord/auth.ts index b8522a9cb..8f1a5cc0b 100644 --- a/apps/api/src/lib/discord/auth.ts +++ b/apps/api/src/lib/discord/auth.ts @@ -61,6 +61,7 @@ export async function updateMemberRolesLogin< towRoles: true, taxiRoles: true, leoSupervisorRoles: true, + sergeantRoles: true, courthouseRoles: true, }, }); @@ -83,6 +84,7 @@ export async function updateMemberRolesLogin< const isTow = doesDiscordMemberHaveCADRole(discordRoles.towRoles, memberObj); const isTaxi = doesDiscordMemberHaveCADRole(discordRoles.taxiRoles, memberObj); const isSupervisor = doesDiscordMemberHaveCADRole(discordRoles.leoSupervisorRoles, memberObj); + const isSergeant = doesDiscordMemberHaveCADRole(discordRoles.sergeantRoles, memberObj); const isCourthouse = doesDiscordMemberHaveCADRole(discordRoles.courthouseRoles, memberObj); const isAdmin = doesDiscordMemberHaveCADRole(discordRoles.adminRoles, memberObj); const hasWhitelistAccess = doesDiscordMemberHaveCADRole( @@ -93,6 +95,7 @@ export async function updateMemberRolesLogin< const grantablePermissions = { leo: { permissions: discordRoles.leoRolePermissions, value: isLeo }, supervisor: { permissions: discordRoles.leoSupervisorRolePermissions, value: isSupervisor }, + sergeant: { permissions: discordRoles.sergeantRolePermissions, value: isSergeant }, emsFd: { permissions: discordRoles.emsFdRolePermissions, value: isEmsFd }, dispatch: { permissions: discordRoles.dispatchRolePermissions, value: isDispatch }, tow: { permissions: discordRoles.towRolePermissions, value: isTow }, diff --git a/apps/api/src/middlewares/auth/is-auth.ts b/apps/api/src/middlewares/auth/is-auth.ts index b10be268a..3aac52ffd 100644 --- a/apps/api/src/middlewares/auth/is-auth.ts +++ b/apps/api/src/middlewares/auth/is-auth.ts @@ -158,6 +158,7 @@ export function CAD_SELECT(options: CadSelectOptions) { leoRoles: true, emsFdRoles: true, leoSupervisorRoles: true, + sergeantRoles: true, towRoles: true, taxiRoles: true, dispatchRoles: true, diff --git a/apps/client/locales/en-gb/cad-settings.json b/apps/client/locales/en-gb/cad-settings.json index 934f18e7e..05f5abc88 100644 --- a/apps/client/locales/en-gb/cad-settings.json +++ b/apps/client/locales/en-gb/cad-settings.json @@ -38,6 +38,8 @@ "leoRoleInfo": "The Discord Role that will represent set police permissions in the CAD.", "leoSupervisorRole": "Senior Policing Role", "leoSupervisorRoleInfo": "The Discord Role that will represent set senior policing permissions in the CAD.", + "sergeantRole": "Sergeant Role", + "sergeantRoleInfo": "The Discord Role that will represent set Sergeant permissions in the CAD.", "emsFdRole": "Emergency Services Role", "emsFdRoleInfo": "The Discord Role that will represent set emergency services permissions in the CAD.", "dispatchRole": "Control Role", diff --git a/apps/client/locales/en/cad-settings.json b/apps/client/locales/en/cad-settings.json index 5057a8d9a..f944cb0b8 100644 --- a/apps/client/locales/en/cad-settings.json +++ b/apps/client/locales/en/cad-settings.json @@ -38,6 +38,8 @@ "leoRoleInfo": "The Discord Role that will represent set LEO permissions in the CAD.", "leoSupervisorRole": "LEO Supervisor Role", "leoSupervisorRoleInfo": "The Discord Role that will represent set LEO Supervisor permissions in the CAD.", + "sergeantRole": "Sergeant Role", + "sergeantRoleInfo": "The Discord Role that will represent set Sergeant permissions in the CAD.", "emsFdRole": "EMS/FD Role", "emsFdRoleInfo": "The Discord Role that will represent set EMS/FD permissions in the CAD.", "dispatchRole": "Dispatch Role", diff --git a/apps/client/src/components/admin/manage/cad-settings/discord-roles-tab.tsx b/apps/client/src/components/admin/manage/cad-settings/discord-roles-tab.tsx index e986df6c5..4a0c4b033 100644 --- a/apps/client/src/components/admin/manage/cad-settings/discord-roles-tab.tsx +++ b/apps/client/src/components/admin/manage/cad-settings/discord-roles-tab.tsx @@ -49,6 +49,7 @@ export function DiscordRolesTab() { emsFdRoles: makeRoleValues(discordRoles.emsFdRoles), dispatchRoles: makeRoleValues(discordRoles.dispatchRoles), leoSupervisorRoles: makeRoleValues(discordRoles.leoSupervisorRoles), + sergeantRoles: makeRoleValues(discordRoles.sergeantRoles), towRoles: makeRoleValues(discordRoles.towRoles), taxiRoles: makeRoleValues(discordRoles.taxiRoles), courthouseRoles: makeRoleValues(discordRoles.courthouseRoles), @@ -56,6 +57,7 @@ export function DiscordRolesTab() { adminRolePermissions: makeValue(discordRoles.adminRolePermissions, tPermission), leoRolePermissions: makeValue(discordRoles.leoRolePermissions, tPermission), leoSupervisorRolePermissions: makeValue(discordRoles.leoSupervisorRolePermissions, tPermission), + sergeantRolePermissions: makeValue(discordRoles.sergeantRolePermissions, tPermission), emsFdRolePermissions: makeValue(discordRoles.emsFdRolePermissions, tPermission), dispatchRolePermissions: makeValue(discordRoles.dispatchRolePermissions, tPermission), towRolePermissions: makeValue(discordRoles.towRolePermissions, tPermission), @@ -97,9 +99,11 @@ export function DiscordRolesTab() { taxiRoles: toValue(values.taxiRoles), courthouseRoles: toValue(values.courthouseRoles), leoSupervisorRoles: toValue(values.leoSupervisorRoles), + sergeantRoles: toValue(values.sergeantRoles), adminRolePermissions: toValue(values.adminRolePermissions), leoRolePermissions: toValue(values.leoRolePermissions), leoSupervisorRolePermissions: toValue(values.leoSupervisorRolePermissions), + sergeantRolePermissions: toValue(values.sergeantRolePermissions), emsFdRolePermissions: toValue(values.emsFdRolePermissions), dispatchRolePermissions: toValue(values.dispatchRolePermissions), towRolePermissions: toValue(values.towRolePermissions), @@ -231,6 +235,31 @@ export function DiscordRolesTab() { /> + +