From c62c12642e8fc7017a7620f424749c2b47b825a8 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Mon, 21 Oct 2024 11:09:43 +0700 Subject: [PATCH] feat: upload multiple payment file --- .../05-quotation-payment-controller.ts | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/controllers/05-quotation-payment-controller.ts b/src/controllers/05-quotation-payment-controller.ts index 7b5d637..57f03a3 100644 --- a/src/controllers/05-quotation-payment-controller.ts +++ b/src/controllers/05-quotation-payment-controller.ts @@ -1,11 +1,12 @@ -import { Body, Controller, Get, Path, Post, Put, Query, Request, Route, Tags } from "tsoa"; +import { Body, Controller, Delete, 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"; import HttpStatus from "../interfaces/http-status"; -import { fileLocation, getFile, setFile } from "../utils/minio"; +import { deleteFile, fileLocation, getFile, listFile, setFile } from "../utils/minio"; +import { RequestWithUser } from "../interfaces/user"; @Tags("Quotation") @Route("api/v1/quotation/{quotationId}/payment") @@ -78,19 +79,28 @@ export class QuotationPayment extends Controller { } @Get("{paymentId}/file") + async listPaymentFile(@Path() quotationId: string, @Path() paymentId: string) { + return await listFile(fileLocation.quotation.payment(quotationId, paymentId)); + } + + @Get("{paymentId}/file/{name}") async getPaymentFile( @Request() req: express.Request, @Path() quotationId: string, @Path() paymentId: string, + @Path() name: string, ) { - return req.res?.redirect(await getFile(fileLocation.quotation.payment(quotationId, paymentId))); + return req.res?.redirect( + await getFile(fileLocation.quotation.payment(quotationId, paymentId, name)), + ); } - @Put("{paymentId}/file") + @Put("{paymentId}/file/{name}") async uploadPayment( @Request() req: express.Request, @Path() quotationId: string, @Path() paymentId: string, + @Path() name: string, ) { const record = await prisma.quotation.findUnique({ where: { id: quotationId }, @@ -107,7 +117,24 @@ export class QuotationPayment extends Controller { ); } - return req.res?.redirect(await setFile(fileLocation.quotation.payment(quotationId, paymentId))); + return req.res?.redirect( + await setFile(fileLocation.quotation.payment(quotationId, paymentId, name)), + ); + } + + @Delete("{paymentId}/file/{name}") + async deletePayment( + @Path() quotationId: string, + @Path() paymentId: string, + @Path() name: string, + ) { + const record = await prisma.quotation.findUnique({ + where: { id: quotationId }, + }); + + if (!record) throw notFoundError("Quotation"); + + return await deleteFile(fileLocation.quotation.payment(quotationId, paymentId, name)); } @Post("confirm")