From b660b8bc8d4534f505f4a77f7a0475211c19ccf8 Mon Sep 17 00:00:00 2001 From: Casper <53900565+Dev-CasperTheGhost@users.noreply.github.com> Date: Sat, 1 Jan 2022 15:30:06 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20able=20to=20unset=20panic?= =?UTF-8?q?-button=20(#226)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/src/controllers/leo/LeoController.ts | 50 +++++++++++++++---- packages/api/src/services/SocketService.ts | 8 ++- packages/client/src/hooks/usePanicButton.tsx | 6 ++- packages/config/src/socket-events.ts | 4 +- 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/packages/api/src/controllers/leo/LeoController.ts b/packages/api/src/controllers/leo/LeoController.ts index 871397114..092ba42e0 100644 --- a/packages/api/src/controllers/leo/LeoController.ts +++ b/packages/api/src/controllers/leo/LeoController.ts @@ -287,20 +287,50 @@ export class LeoController { }, }); + let type: "ON" | "OFF" = "ON"; if (code) { - fullOfficer = await prisma.officer.update({ - where: { - id: officer.id, - }, - data: { - statusId: code.id, - }, - include: unitProperties, - }); + /** + * officer is already in panic-mode -> set status back to `ON_DUTY` + */ + if (fullOfficer?.statusId === code?.id) { + const onDutyCode = await prisma.statusValue.findFirst({ + where: { + shouldDo: ShouldDoType.SET_ON_DUTY, + }, + }); + + if (!onDutyCode) { + throw new BadRequest("mustHaveOnDutyCode"); + } + + type = "OFF"; + fullOfficer = await prisma.officer.update({ + where: { + id: officer.id, + }, + data: { + statusId: onDutyCode?.id, + }, + include: unitProperties, + }); + } else { + /** + * officer is not yet in panic-mode -> set status to panic button status + */ + fullOfficer = await prisma.officer.update({ + where: { + id: officer.id, + }, + data: { + statusId: code.id, + }, + include: unitProperties, + }); + } } this.socket.emitUpdateOfficerStatus(); - this.socket.emitPanicButtonLeo(fullOfficer); + this.socket.emitPanicButtonLeo(fullOfficer, type); } @Get("/impounded-vehicles") diff --git a/packages/api/src/services/SocketService.ts b/packages/api/src/services/SocketService.ts index 184e3265b..373aa6466 100644 --- a/packages/api/src/services/SocketService.ts +++ b/packages/api/src/services/SocketService.ts @@ -100,7 +100,11 @@ export class Socket { this.io.sockets.emit(SocketEvents.Signal100, value); } - emitPanicButtonLeo(officer: any) { - this.io.sockets.emit(SocketEvents.PANIC_BUTTON, officer); + emitPanicButtonLeo(officer: any, type?: "ON" | "OFF") { + if (type === "OFF") { + this.io.sockets.emit(SocketEvents.PANIC_BUTTON_OFF, officer); + } else { + this.io.sockets.emit(SocketEvents.PANIC_BUTTON_ON, officer); + } } } diff --git a/packages/client/src/hooks/usePanicButton.tsx b/packages/client/src/hooks/usePanicButton.tsx index 24c4fb969..c6f449fe3 100644 --- a/packages/client/src/hooks/usePanicButton.tsx +++ b/packages/client/src/hooks/usePanicButton.tsx @@ -9,10 +9,14 @@ import { makeUnitName } from "lib/utils"; export function usePanicButton() { const [unit, setUnit] = React.useState(null); - useListener(SocketEvents.PANIC_BUTTON, (officer: FullOfficer) => { + useListener(SocketEvents.PANIC_BUTTON_ON, (officer: FullOfficer) => { setUnit(officer); }); + useListener(SocketEvents.PANIC_BUTTON_OFF, () => { + setUnit(null); + }); + return { unit, PanicButton: Component }; } diff --git a/packages/config/src/socket-events.ts b/packages/config/src/socket-events.ts index e6dc78d28..f0d298f5b 100644 --- a/packages/config/src/socket-events.ts +++ b/packages/config/src/socket-events.ts @@ -28,7 +28,9 @@ export enum SocketEvents { DeleteCallEvent = "DELETE_CALL_EVENT", Signal100 = "SIGNAL_100", - PANIC_BUTTON = "PANIC_BUTTON", + + PANIC_BUTTON_ON = "PANIC_BUTTON_ON", + PANIC_BUTTON_OFF = "PANIC_BUTTON_OFF", RoleplayStopped = "ROLEPLAY_STOPPED", }