Skip to content

Commit

Permalink
🎉 add truck-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Oct 24, 2021
1 parent d1d57e8 commit ff9ab4d
Show file tree
Hide file tree
Showing 13 changed files with 583 additions and 13 deletions.
20 changes: 20 additions & 0 deletions packages/api/prisma/migrations/20211024074740_/migration.sql
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;
4 changes: 4 additions & 0 deletions packages/api/prisma/migrations/20211024083227_/migration.sql
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);
36 changes: 26 additions & 10 deletions packages/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ model User {
OfficerLog OfficerLog[]
emsFdDeputies EmsFdDeputy[]
TaxiCall TaxiCall[]
TruckLog TruckLog[]
}

model Citizen {
Expand Down Expand Up @@ -122,6 +123,7 @@ model Citizen {
emsFdDeputies EmsFdDeputy[]
TaxiCall TaxiCall[]
createdTaxiCalls TaxiCall[] @relation("taxiCallCreator")
truckLogs TruckLog[]
}

enum Rank {
Expand All @@ -138,20 +140,21 @@ enum WhitelistStatus {
}

model RegisteredVehicle {
id String @id @default(cuid())
user User @relation(fields: [userId], references: [id])
id String @id @default(cuid())
user User @relation(fields: [userId], references: [id])
userId String
citizen Citizen @relation(fields: [citizenId], references: [id])
citizen Citizen @relation(fields: [citizenId], references: [id])
citizenId String
vinNumber String @unique @db.VarChar(255)
plate String @unique @db.VarChar(255)
model Value @relation("modelToValue", fields: [modelId], references: [id])
vinNumber String @unique @db.VarChar(255)
plate String @unique @db.VarChar(255)
model Value @relation("modelToValue", fields: [modelId], references: [id])
modelId String
color String @db.VarChar(255)
createdAt DateTime @default(now())
registrationStatus Value @relation("registrationStatusToValue", fields: [registrationStatusId], references: [id])
color String @db.VarChar(255)
createdAt DateTime @default(now())
registrationStatus Value @relation("registrationStatusToValue", fields: [registrationStatusId], references: [id])
registrationStatusId String
insuranceStatus String @db.VarChar(255)
insuranceStatus String @db.VarChar(255)
TruckLog TruckLog[]
}

model Weapon {
Expand Down Expand Up @@ -523,6 +526,19 @@ model EmsFdDeputy {
// call911Id String?
}

// truck logs
model TruckLog {
id String @id @default(uuid())
citizen Citizen? @relation(fields: [citizenId], references: [id], onDelete: Cascade)
citizenId String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
vehicle RegisteredVehicle? @relation(fields: [vehicleId], references: [id], onDelete: SetNull)
vehicleId String?
startedAt String @db.VarChar(255)
endedAt String @db.VarChar(255)
}

// other
enum Feature {
BLEETER
Expand Down
187 changes: 187 additions & 0 deletions packages/api/src/controllers/citizen/TruckLogsController.ts
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;
}
}
13 changes: 13 additions & 0 deletions packages/client/locales/en/truck-logs.json
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."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export const CitizenDropdown = () => {
</Menu.Item>

{items.map((item) => {
const upperCase = item.toUpperCase() as Feature;
const lower = item.toLowerCase();
const upperCase = item.replace(/ +/g, "_").toUpperCase() as Feature;
const lower = item.replace(/ +/g, "-").toLowerCase();

if (!enabled[upperCase]) {
return null;
Expand Down
Loading

0 comments on commit ff9ab4d

Please sign in to comment.