From 37b4bcfbbe76212788df23aa3674fd33198b4e1d Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Mon, 7 Oct 2024 10:16:07 +0700 Subject: [PATCH] feat: add payment endpoints (incomplete) --- .../05-quotation-payment-controller.ts | 66 +++++++++++++++++++ src/utils/minio.ts | 3 + 2 files changed, 69 insertions(+) create mode 100644 src/controllers/05-quotation-payment-controller.ts diff --git a/src/controllers/05-quotation-payment-controller.ts b/src/controllers/05-quotation-payment-controller.ts new file mode 100644 index 0000000..7918d77 --- /dev/null +++ b/src/controllers/05-quotation-payment-controller.ts @@ -0,0 +1,66 @@ +import { Controller, Path, Post, Put, Route, Tags } from "tsoa"; +import prisma from "../db"; +import { notFoundError } from "../utils/error"; +import HttpError from "../interfaces/http-error"; +import HttpStatus from "../interfaces/http-status"; + +@Tags("Quotation") +@Route("api/v1/quotation/{quotationId}/payment") +export class QuotationPayment extends Controller { + @Post("submit") + async submitPayment(@Path("quotationId") id: string) { + const record = await prisma.quotation.findUnique({ + where: { id }, + }); + + if (!record) throw notFoundError("Quotation"); + + if (record.quotationStatus !== "PaymentPending" && record.quotationStatus !== "PaymentWait") { + // NOTE: The quotation must be in waiting for payment or waiting for payment confirmation (re-submit payment) + throw new HttpError( + HttpStatus.PRECONDITION_FAILED, + "Cannot submit payment info of this quotation", + "quotationStatusWrong", + ); + } + + await prisma.quotation.update({ + where: { id }, + data: { quotationStatus: "PaymentWait" }, + }); + } + + @Put("file") + async uploadPayment(@Path("quotationId") id: string) { + const record = await prisma.quotation.findUnique({ + where: { id }, + }); + + if (!record) throw notFoundError("Quotation"); + + if (record.quotationStatus !== "PaymentPending" && record.quotationStatus !== "PaymentWait") { + // NOTE: The quotation must be in waiting for payment or waiting for payment confirmation (re-submit payment) + throw new HttpError( + HttpStatus.PRECONDITION_FAILED, + "Cannot submit payment info of this quotation", + "quotationStatusWrong", + ); + } + } + + @Post("confirm") + async confirmPayment(@Path("quotationId") id: string) { + const record = await prisma.quotation.findUnique({ + where: { id }, + }); + + if (!record) throw notFoundError("Quotation"); + + await prisma.quotation.update({ + where: { id }, + data: { quotationStatus: "PaymentSuccess" }, + }); + + // TODO: Generate request list (Work) by match product with worker (Employee) + } +} diff --git a/src/utils/minio.ts b/src/utils/minio.ts index bb2bd8a..6eba4db 100644 --- a/src/utils/minio.ts +++ b/src/utils/minio.ts @@ -90,4 +90,7 @@ export const fileLocation = { service: { img: (serviceId: string, name?: string) => `service/img-${serviceId}/${name || ""}`, }, + quotation: { + payment: (quotationId: string) => `quotation/payment-${quotationId}`, + }, };