-
-
Notifications
You must be signed in to change notification settings - Fork 88
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
13 changed files
with
583 additions
and
13 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
packages/api/prisma/migrations/20211024074740_/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,20 @@ | ||
-- CreateTable | ||
CREATE TABLE "TruckLog" ( | ||
"id" TEXT NOT NULL, | ||
"citizenId" TEXT, | ||
"userId" TEXT NOT NULL, | ||
"vehicleId" TEXT, | ||
"startedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"endedAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "TruckLog_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "TruckLog" ADD CONSTRAINT "TruckLog_citizenId_fkey" FOREIGN KEY ("citizenId") REFERENCES "Citizen"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "TruckLog" ADD CONSTRAINT "TruckLog_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "TruckLog" ADD CONSTRAINT "TruckLog_vehicleId_fkey" FOREIGN KEY ("vehicleId") REFERENCES "RegisteredVehicle"("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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-- AlterTable | ||
ALTER TABLE "TruckLog" ALTER COLUMN "startedAt" DROP DEFAULT, | ||
ALTER COLUMN "startedAt" SET DATA TYPE VARCHAR(255), | ||
ALTER COLUMN "endedAt" SET DATA TYPE VARCHAR(255); |
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
187 changes: 187 additions & 0 deletions
187
packages/api/src/controllers/citizen/TruckLogsController.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,187 @@ | ||
import { User } from ".prisma/client"; | ||
import { validate } from "@snailycad/schemas"; | ||
import { Controller } from "@tsed/di"; | ||
import { BadRequest, NotFound } from "@tsed/exceptions"; | ||
import { BodyParams, Context, PathParams } from "@tsed/platform-params"; | ||
import { Delete, Get, JsonRequestBody, Post, Put } from "@tsed/schema"; | ||
import { prisma } from "../../lib/prisma"; | ||
import { CREATE_TRUCK_LOG_SCHEMA } from "@snailycad/schemas"; | ||
import { IsAuth } from "../../middlewares"; | ||
import { UseBeforeEach } from "@tsed/platform-middlewares"; | ||
|
||
@Controller("/truck-logs") | ||
@UseBeforeEach(IsAuth) | ||
export class TruckLogsController { | ||
@Get("/") | ||
async getTruckLogs(@Context("user") user: User) { | ||
const logs = await prisma.truckLog.findMany({ | ||
where: { | ||
userId: user.id, | ||
}, | ||
include: { | ||
citizen: true, | ||
vehicle: { | ||
include: { | ||
model: true, | ||
registrationStatus: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const registeredVehicles = await prisma.registeredVehicle.findMany({ | ||
where: { | ||
userId: user.id, | ||
}, | ||
include: { | ||
model: true, | ||
}, | ||
}); | ||
|
||
console.log({ registeredVehicles }); | ||
|
||
return { logs, registeredVehicles }; | ||
} | ||
|
||
@Post("/") | ||
async createTruckLog(@Context("user") user: User, @BodyParams() body: JsonRequestBody) { | ||
const error = validate(CREATE_TRUCK_LOG_SCHEMA, body.toJSON(), true); | ||
if (error) { | ||
throw new BadRequest(error); | ||
} | ||
|
||
const citizen = await prisma.citizen.findFirst({ | ||
where: { | ||
id: body.get("citizenId"), | ||
userId: user.id, | ||
}, | ||
}); | ||
|
||
if (!citizen) { | ||
throw new NotFound("citizenNotFound"); | ||
} | ||
|
||
const vehicle = await prisma.registeredVehicle.findFirst({ | ||
where: { | ||
id: body.get("vehicleId"), | ||
userId: user.id, | ||
citizenId: citizen.id, | ||
}, | ||
}); | ||
|
||
if (!vehicle) { | ||
throw new NotFound("vehicleNotFound"); | ||
} | ||
|
||
const log = await prisma.truckLog.create({ | ||
data: { | ||
userId: user.id, | ||
citizenId: body.get("citizenId"), | ||
endedAt: body.get("endedAt"), | ||
startedAt: body.get("startedAt"), | ||
vehicleId: body.get("vehicleId"), | ||
}, | ||
include: { | ||
citizen: true, | ||
vehicle: { | ||
include: { | ||
model: true, | ||
registrationStatus: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return log; | ||
} | ||
|
||
@Put("/:id") | ||
async updateTruckLog( | ||
@Context("user") user: User, | ||
@BodyParams() body: JsonRequestBody, | ||
@PathParams("id") id: string, | ||
) { | ||
const error = validate(CREATE_TRUCK_LOG_SCHEMA, body.toJSON(), true); | ||
if (error) { | ||
throw new BadRequest(error); | ||
} | ||
|
||
const log = await prisma.truckLog.findFirst({ | ||
where: { | ||
id, | ||
userId: user.id, | ||
}, | ||
}); | ||
|
||
if (!log) { | ||
throw new NotFound("notFound"); | ||
} | ||
|
||
const citizen = await prisma.citizen.findFirst({ | ||
where: { | ||
id: body.get("citizenId"), | ||
userId: user.id, | ||
}, | ||
}); | ||
|
||
if (!citizen) { | ||
throw new NotFound("citizenNotFound"); | ||
} | ||
|
||
const vehicle = await prisma.registeredVehicle.findFirst({ | ||
where: { | ||
id: body.get("vehicleId"), | ||
userId: user.id, | ||
citizenId: citizen.id, | ||
}, | ||
}); | ||
|
||
if (!vehicle) { | ||
throw new NotFound("vehicleNotFound"); | ||
} | ||
|
||
const updated = await prisma.truckLog.update({ | ||
where: { | ||
id, | ||
}, | ||
data: { | ||
citizenId: body.get("citizenId"), | ||
endedAt: body.get("endedAt"), | ||
vehicleId: body.get("vehicleId"), | ||
}, | ||
include: { | ||
citizen: true, | ||
vehicle: { | ||
include: { | ||
model: true, | ||
registrationStatus: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return updated; | ||
} | ||
|
||
@Delete("/:id") | ||
async deleteTruckLog(@Context("user") user: User, @PathParams("id") id: string) { | ||
const log = await prisma.truckLog.findFirst({ | ||
where: { | ||
id, | ||
userId: user.id, | ||
}, | ||
}); | ||
|
||
if (!log) { | ||
throw new NotFound("notFound"); | ||
} | ||
|
||
await prisma.truckLog.delete({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
|
||
return true; | ||
} | ||
} |
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,13 @@ | ||
{ | ||
"TruckLogs": { | ||
"truckLogs": "Truck Logs", | ||
"createTruckLog": "Create Truck Log", | ||
"editTruckLog": "Edit Truck Log", | ||
"deleteTruckLog": "Delete Truck Log", | ||
"driver": "Driver", | ||
"vehicle": "Vehicle", | ||
"startedAt": "Started At", | ||
"endedAt": "Ended At", | ||
"alert_deleteTruckLog": "Are you sure you want to delete this truck log? This action cannot be undone." | ||
} | ||
} |
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.