Skip to content

Commit

Permalink
🎉 feat: refactor 911 call modals (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 authored Jan 30, 2022
1 parent 72d9968 commit 4337a07
Show file tree
Hide file tree
Showing 15 changed files with 703 additions and 388 deletions.
35 changes: 35 additions & 0 deletions packages/api/prisma/migrations/20220130071539_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- CreateTable
CREATE TABLE "_Call911ToDivisionValue" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "_Call911ToDepartmentValue" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "_Call911ToDivisionValue_AB_unique" ON "_Call911ToDivisionValue"("A", "B");

-- CreateIndex
CREATE INDEX "_Call911ToDivisionValue_B_index" ON "_Call911ToDivisionValue"("B");

-- CreateIndex
CREATE UNIQUE INDEX "_Call911ToDepartmentValue_AB_unique" ON "_Call911ToDepartmentValue"("A", "B");

-- CreateIndex
CREATE INDEX "_Call911ToDepartmentValue_B_index" ON "_Call911ToDepartmentValue"("B");

-- AddForeignKey
ALTER TABLE "_Call911ToDivisionValue" ADD FOREIGN KEY ("A") REFERENCES "Call911"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_Call911ToDivisionValue" ADD FOREIGN KEY ("B") REFERENCES "DivisionValue"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_Call911ToDepartmentValue" ADD FOREIGN KEY ("A") REFERENCES "Call911"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_Call911ToDepartmentValue" ADD FOREIGN KEY ("B") REFERENCES "DepartmentValue"("id") ON DELETE CASCADE ON UPDATE CASCADE;
22 changes: 13 additions & 9 deletions packages/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ model DivisionValue {
officers Officer[] @relation("officerDivisionToDivision")
officerDivisionsToDivision Officer[] @relation("officerDivisionsToDivision")
deputies EmsFdDeputy[] @relation("emsFdDivisionToDivision")
Call911 Call911[]
}

model DepartmentValue {
Expand All @@ -372,6 +373,7 @@ model DepartmentValue {
Officer Officer[] @relation("officerDepartmentToDepartment")
division DivisionValue[] @relation("divisionDepartmentToValue")
LeoWhitelistStatus LeoWhitelistStatus[]
Call911 Call911[]
}

model DriversLicenseCategoryValue {
Expand Down Expand Up @@ -698,19 +700,21 @@ model ActiveDispatchers {

// 911 calls & bolos
model Call911 {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
position Position? @relation(fields: [positionId], references: [id])
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
position Position? @relation(fields: [positionId], references: [id])
positionId String?
userId String?
assignedUnits AssignedUnit[]
location String @db.Text
postal String? @db.Text
description String? @db.Text
location String @db.Text
postal String? @db.Text
description String? @db.Text
descriptionData Json?
name String @db.VarChar(255)
ended Boolean? @default(false)
name String @db.VarChar(255)
ended Boolean? @default(false)
divisions DivisionValue[]
departments DepartmentValue[]
events Call911Event[]
incidents LeoIncident[]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { Controller } from "@tsed/di";
import { Delete, Post, Put } from "@tsed/schema";
import { CREATE_911_CALL_EVENT } from "@snailycad/schemas";
import { BodyParams, PathParams } from "@tsed/platform-params";
import { NotFound } from "@tsed/exceptions";
import { prisma } from "lib/prisma";
import { Socket } from "services/SocketService";
import { UseBeforeEach } from "@tsed/platform-middlewares";
import { IsAuth } from "middlewares/index";
import { validateSchema } from "lib/validateSchema";

@Controller("/911-calls/events")
@UseBeforeEach(IsAuth)
export class Calls911Controller {
private socket: Socket;
constructor(socket: Socket) {
this.socket = socket;
}

@Post("/:callId")
async createCallEvent(@PathParams("callId") callId: string, @BodyParams() body: unknown) {
const data = validateSchema(CREATE_911_CALL_EVENT, body);

const call = await prisma.call911.findUnique({
where: { id: callId },
});

if (!call) {
throw new NotFound("callNotFound");
}

const event = await prisma.call911Event.create({
data: {
call911Id: call.id,
description: data.description,
},
});

this.socket.emitAddCallEvent(event);

return event;
}

@Put("/:callId/:eventId")
async updateCallEvent(
@PathParams("callId") callId: string,
@PathParams("eventId") eventId: string,
@BodyParams() body: unknown,
) {
const data = validateSchema(CREATE_911_CALL_EVENT, body);

const call = await prisma.call911.findUnique({
where: { id: callId },
});

if (!call) {
throw new NotFound("callNotFound");
}

const event = await prisma.call911Event.findFirst({
where: {
id: eventId,
call911Id: callId,
},
});

if (!event) {
throw new NotFound("eventNotFound");
}

const updated = await prisma.call911Event.update({
where: {
id: event.id,
},
data: {
description: data.description,
},
});

this.socket.emitUpdateCallEvent(updated);

return updated;
}

@Delete("/:callId/:eventId")
async deleteCallEvent(
@PathParams("callId") callId: string,
@PathParams("eventId") eventId: string,
) {
const call = await prisma.call911.findUnique({
where: { id: callId },
});

if (!call) {
throw new NotFound("callNotFound");
}

const event = await prisma.call911Event.findFirst({
where: {
id: eventId,
call911Id: callId,
},
});

if (!event) {
throw new NotFound("eventNotFound");
}

await prisma.call911Event.delete({
where: {
id: event.id,
},
});

this.socket.emitDeleteCallEvent(event);

return true;
}
}
Loading

0 comments on commit 4337a07

Please sign in to comment.