From 8462a8f2c2021c398877f5f987296fed3f833623 Mon Sep 17 00:00:00 2001 From: Dev-CasperTheGhost <53900565+Dev-CasperTheGhost@users.noreply.github.com> Date: Fri, 6 Oct 2023 09:56:09 +0200 Subject: [PATCH] feat: able to transfer vehicle to business --- .../citizen/vehicles-controller.ts | 57 ++++++++++++++----- packages/schemas/src/citizen.ts | 3 +- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/apps/api/src/controllers/citizen/vehicles-controller.ts b/apps/api/src/controllers/citizen/vehicles-controller.ts index 59b270e9b..3b78a27ef 100644 --- a/apps/api/src/controllers/citizen/vehicles-controller.ts +++ b/apps/api/src/controllers/citizen/vehicles-controller.ts @@ -39,6 +39,7 @@ import { getLastOfArray, manyToManyHelper } from "lib/data/many-to-many"; import { AllowedFileExtension, allowedFileExtensions } from "@snailycad/config"; import { getImageWebPPath } from "~/lib/images/get-image-webp-path"; import fs from "node:fs/promises"; +import { ExtendedNotFound } from "~/exceptions/extended-not-found"; @Controller("/vehicles") @UseBeforeEach(IsAuth) @@ -484,25 +485,51 @@ export class VehiclesController { throw new NotFound("vehicleNotFound"); } - const newOwner = await prisma.citizen.findFirst({ - where: { - AND: [{ id: data.ownerId }, { NOT: { id: String(vehicle.citizenId) } }], - }, - }); + if (data.businessId) { + const business = await prisma.business.findUnique({ + where: { id: data.businessId }, + }); - if (!newOwner) { - throw new NotFound("newOwnerNotFound"); + if (!business) { + throw new ExtendedNotFound({ business: "businessNotFound" }); + } + + const updatedVehicle = await prisma.registeredVehicle.update({ + where: { id: vehicle.id }, + data: { + Business: { connect: { id: data.businessId } }, + }, + }); + + return updatedVehicle; } - const updatedVehicle = await prisma.registeredVehicle.update({ - where: { id: vehicle.id }, - data: { - citizenId: newOwner.id, - userId: newOwner.userId, - }, - }); + if (data.ownerId) { + const newOwner = await prisma.citizen.findFirst({ + where: { + AND: [{ id: data.ownerId }, { NOT: { id: String(vehicle.citizenId) } }], + }, + }); - return updatedVehicle; + if (!newOwner) { + throw new NotFound("newOwnerNotFound"); + } + + const updatedVehicle = await prisma.registeredVehicle.update({ + where: { id: vehicle.id }, + data: { + citizenId: newOwner.id, + userId: newOwner.userId, + }, + }); + + return updatedVehicle; + } + + throw new ExtendedBadRequest({ + ownerId: "ownerIdOrBusinessIdRequired", + businessId: "ownerIdOrBusinessIdRequired", + }); } @Delete("/:id") diff --git a/packages/schemas/src/citizen.ts b/packages/schemas/src/citizen.ts index bc42f6bdc..aef06131e 100644 --- a/packages/schemas/src/citizen.ts +++ b/packages/schemas/src/citizen.ts @@ -63,7 +63,8 @@ export const VEHICLE_SCHEMA = z.object({ }); export const TRANSFER_VEHICLE_SCHEMA = z.object({ - ownerId: z.string().min(2).max(255), + ownerId: z.string().max(255).nullish(), + businessId: z.string().max(255).nullish(), }); export const DELETE_VEHICLE_SCHEMA = z.object({