-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 feat: refactor 911 call modals (#352)
- Loading branch information
Showing
15 changed files
with
703 additions
and
388 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
packages/api/prisma/migrations/20220130071539_/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
packages/api/src/controllers/dispatch/911-calls/CallEventsController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.