-
-
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.
- Loading branch information
Showing
12 changed files
with
432 additions
and
14 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
packages/api/prisma/migrations/20211024071230_/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,21 @@ | ||
-- CreateTable | ||
CREATE TABLE "TaxiCall" ( | ||
"id" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"userId" TEXT NOT NULL, | ||
"assignedUnitId" TEXT, | ||
"location" VARCHAR(255) NOT NULL, | ||
"description" TEXT NOT NULL, | ||
"creatorId" TEXT, | ||
|
||
CONSTRAINT "TaxiCall_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "TaxiCall" ADD CONSTRAINT "TaxiCall_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "TaxiCall" ADD CONSTRAINT "TaxiCall_assignedUnitId_fkey" FOREIGN KEY ("assignedUnitId") REFERENCES "Citizen"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "TaxiCall" ADD CONSTRAINT "TaxiCall_creatorId_fkey" FOREIGN KEY ("creatorId") REFERENCES "Citizen"("id") ON DELETE SET NULL 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
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,155 @@ | ||
import { Controller, BodyParams, Context, UseBefore, PathParams } from "@tsed/common"; | ||
import { Delete, Get, JsonRequestBody, Post, Put } from "@tsed/schema"; | ||
import { prisma } from "../../lib/prisma"; | ||
import { validate, TOW_SCHEMA, UPDATE_TOW_SCHEMA } from "@snailycad/schemas"; | ||
import { BadRequest, NotFound } from "@tsed/exceptions"; | ||
import { IsAuth } from "../../middlewares"; | ||
import { Socket } from "../../services/SocketService"; | ||
|
||
const CITIZEN_SELECTS = { | ||
name: true, | ||
surname: true, | ||
id: true, | ||
}; | ||
|
||
@Controller("/taxi") | ||
export class TowController { | ||
private socket: Socket; | ||
constructor(socket: Socket) { | ||
this.socket = socket; | ||
} | ||
|
||
@Get("/") | ||
async getTaxiCalls() { | ||
const calls = await prisma.taxiCall.findMany({ | ||
include: { | ||
assignedUnit: { | ||
select: CITIZEN_SELECTS, | ||
}, | ||
creator: { | ||
select: CITIZEN_SELECTS, | ||
}, | ||
}, | ||
}); | ||
|
||
return calls; | ||
} | ||
|
||
@UseBefore(IsAuth) | ||
@Post("/") | ||
async createTaxiCall(@BodyParams() body: JsonRequestBody, @Context() ctx: Context) { | ||
const error = validate(TOW_SCHEMA, body.toJSON(), true); | ||
if (error) { | ||
throw new BadRequest(error); | ||
} | ||
|
||
const citizen = await prisma.citizen.findUnique({ | ||
where: { | ||
id: body.get("creatorId"), | ||
}, | ||
}); | ||
|
||
if (!citizen || citizen.userId !== ctx.get("user").id) { | ||
throw new NotFound("notFound"); | ||
} | ||
|
||
const call = await prisma.taxiCall.create({ | ||
data: { | ||
creatorId: body.get("creatorId"), | ||
userId: ctx.get("user").id, | ||
description: body.get("description"), | ||
location: body.get("location"), | ||
}, | ||
include: { | ||
assignedUnit: { | ||
select: CITIZEN_SELECTS, | ||
}, | ||
creator: { | ||
select: CITIZEN_SELECTS, | ||
}, | ||
}, | ||
}); | ||
|
||
this.socket.emitCreateTaxiCall(call); | ||
|
||
return call; | ||
} | ||
|
||
@UseBefore(IsAuth) | ||
@Put("/:id") | ||
async updateTaxiCall(@PathParams("id") callId: string, @BodyParams() body: JsonRequestBody) { | ||
const error = validate(UPDATE_TOW_SCHEMA, body.toJSON(), true); | ||
if (error) { | ||
throw new BadRequest(error); | ||
} | ||
|
||
console.log({ callId }); | ||
|
||
const call = await prisma.taxiCall.findUnique({ | ||
where: { | ||
id: callId, | ||
}, | ||
}); | ||
|
||
if (!call) { | ||
throw new NotFound("notFound"); | ||
} | ||
|
||
const rawAssignedUnitId = body.get("assignedUnitId"); | ||
const assignedUnitId = | ||
rawAssignedUnitId === null | ||
? { | ||
disconnect: true, | ||
} | ||
: body.get("assignedUnitId") | ||
? { connect: { id: body.get("assignedUnitId") } } | ||
: undefined; | ||
|
||
const updated = await prisma.taxiCall.update({ | ||
where: { | ||
id: callId, | ||
}, | ||
data: { | ||
description: body.get("description"), | ||
location: body.get("location"), | ||
assignedUnit: assignedUnitId, | ||
}, | ||
include: { | ||
assignedUnit: { | ||
select: CITIZEN_SELECTS, | ||
}, | ||
creator: { | ||
select: CITIZEN_SELECTS, | ||
}, | ||
}, | ||
}); | ||
|
||
this.socket.emitUpdateTaxiCall(updated); | ||
|
||
return updated; | ||
} | ||
|
||
@UseBefore(IsAuth) | ||
@Delete("/:id") | ||
async deleteTowCall(@PathParams("id") callId: string) { | ||
const call = await prisma.taxiCall.findUnique({ | ||
where: { | ||
id: callId, | ||
}, | ||
}); | ||
|
||
if (!call) { | ||
throw new NotFound("notFound"); | ||
} | ||
|
||
await prisma.taxiCall.delete({ | ||
where: { | ||
id: call.id, | ||
}, | ||
}); | ||
|
||
this.socket.emitDeleteTaxiCall(call); | ||
|
||
return true; | ||
} | ||
} |
File renamed without changes.
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
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
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
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
Oops, something went wrong.