From fe50a31e080d1adeac88c99aa227a2b845db59b9 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Fri, 19 Jul 2024 10:41:37 +0700 Subject: [PATCH] feat: add get quotation by id endpoint --- src/controllers/quotation-controller.ts | 103 +++++++++++++++++++++--- 1 file changed, 92 insertions(+), 11 deletions(-) diff --git a/src/controllers/quotation-controller.ts b/src/controllers/quotation-controller.ts index 77dd9c7..f3622df 100644 --- a/src/controllers/quotation-controller.ts +++ b/src/controllers/quotation-controller.ts @@ -14,6 +14,9 @@ import { Tags, } from "tsoa"; import { RequestWithUser } from "../interfaces/user"; +import prisma from "../db"; +import HttpError from "../interfaces/http-error"; +import HttpStatus from "../interfaces/http-status"; type QuotationCreate = { status?: Status; @@ -26,14 +29,42 @@ type QuotationCreate = { payBillDate?: number; workerCount: number; - workerId: string[]; // EmployeeId + // EmployeeId or Create new employee + worker: ( + | string + | { + dateOfBirth: Date; + gender: string; + nationality: string; + + firstName: string; + firstNameEN: string; + lastName: string; + lastNameEN: string; + + addressEN: string; + address: string; + zipCode: string; + + passportType: string; + passportNumber: string; + passportIssueDate: Date; + passportExpiryDate: Date; + passportIssuingCountry: string; + passportIssuingPlace: string; + previousPassportReference?: string; + } + )[]; + + customerBranchId: string; + customerId: string; urgent?: boolean; service: { id: string; // Other fields will come from original data - work?: { + work: { // Name field will come from original data product: { id: string; @@ -42,7 +73,7 @@ type QuotationCreate = { pricePerUnit: number; }[]; }[]; - }; + }[]; }; type QuotationUpdate = { @@ -56,14 +87,42 @@ type QuotationUpdate = { payBillDate?: number; workerCount: number; - workerId: string[]; // EmployeeId + // EmployeeId or Create new employee + worker?: ( + | string + | { + dateOfBirth: Date; + gender: string; + nationality: string; + + firstName: string; + firstNameEN: string; + lastName: string; + lastNameEN: string; + + addressEN: string; + address: string; + zipCode: string; + + passportType: string; + passportNumber: string; + passportIssueDate: Date; + passportExpiryDate: Date; + passportIssuingCountry: string; + passportIssuingPlace: string; + previousPassportReference?: string; + } + )[]; + + customerBranchId: string; + customerId: string; urgent?: boolean; service: { id: string; // Other fields will come from original data - work?: { + work: { // Name field will come from original data product: { id: string; @@ -72,7 +131,7 @@ type QuotationUpdate = { pricePerUnit: number; }[]; }[]; - }; + }[]; }; @Route("/api/v1/quotation") @@ -80,11 +139,33 @@ type QuotationUpdate = { export class QuotationController extends Controller { @Get("{quotationId}") @Security("keycloak") - async getQuotationById( - @Path() quotationId: string, - @Query() page: number = 1, - @Query() pageSize: number = 30, - ) {} + async getQuotationById(@Path() quotationId: string) { + const record = await prisma.quotation.findUnique({ + include: { + worker: true, + service: { + include: { + _count: { select: { work: true } }, + work: { + include: { + _count: { select: { productOnWork: true } }, + productOnWork: { + include: { product: true }, + }, + }, + }, + }, + }, + }, + where: { id: quotationId }, + }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "Quotation not found.", "quotationNotFound"); + } + + return record; + } @Get() @Security("keycloak")