feat: update payload and update endpoint

This commit is contained in:
Methapon Metanipat 2024-10-16 17:39:13 +07:00
parent 4835b22294
commit db2323a24b

View file

@ -1,5 +1,6 @@
import { Body, Controller, Get, Path, Post, Put, Query, Request, Route, Tags } from "tsoa";
import express from "express";
import { PaymentStatus } from "@prisma/client";
import prisma from "../db";
import { notFoundError } from "../utils/error";
import HttpError from "../interfaces/http-error";
@ -26,7 +27,7 @@ export class QuotationPayment extends Controller {
@Post()
async addPayment(
@Path() quotationId: string,
@Body() body: { amount: number; date: Date; remark: string },
@Body() body: { amount: number; date: Date; remark: string; paymentStatus?: PaymentStatus },
) {
const record = await prisma.quotation.findUnique({
where: { id: quotationId },
@ -34,7 +35,7 @@ export class QuotationPayment extends Controller {
if (!record) throw notFoundError("Quotation");
if (record.quotationStatus !== "PaymentPending") {
if (!body.paymentStatus && record.quotationStatus !== "PaymentPending") {
// NOTE: The quotation must be in waiting for payment or waiting for payment confirmation (re-submit payment)
throw new HttpError(
HttpStatus.PRECONDITION_FAILED,
@ -58,6 +59,24 @@ export class QuotationPayment extends Controller {
});
}
@Put("{paymentId}")
async updatePayment(
@Path() quotationId: string,
@Path() paymentId: string,
@Body() body: { amount?: number; date?: Date; remark?: string; paymentStatus?: PaymentStatus },
) {
const record = await prisma.quotationPayment.findUnique({
where: { id: paymentId, quotationId },
});
if (!record) throw notFoundError("Quotation Payment");
return await prisma.quotationPayment.update({
where: { id: paymentId, quotationId },
data: body,
});
}
@Get("{paymentId}/file")
async getPaymentFile(
@Request() req: express.Request,