diff --git a/src/controllers/09-line-controller.ts b/src/controllers/09-line-controller.ts index cf8d7f3..93996c7 100644 --- a/src/controllers/09-line-controller.ts +++ b/src/controllers/09-line-controller.ts @@ -1368,3 +1368,70 @@ export class LineQuotationFileController extends Controller { return await deleteFile(fileLocation.quotation.attachment(quotationId, name)); } } + +@Route("api/v1/line/payment/{paymentId}/attachment") +@Tags("Line") +export class PaymentFileLineController extends Controller { + private async checkPermission(_user: RequestWithUser["user"], id: string) { + const data = await prisma.payment.findUnique({ + include: { + invoice: { + include: { + quotation: true, + }, + }, + }, + where: { id }, + }); + console.log("data", data); + + if (!data) throw notFoundError("Payment"); + return { paymentId: id, quotationId: data.invoice.quotationId }; + } + + @Get() + @Security("line") + async listAttachment(@Request() req: RequestWithUser, @Path() paymentId: string) { + console.log("req", req); + + const { quotationId } = await this.checkPermission(req.user, paymentId); + console.log("quotationId", quotationId); + + return await listFile(fileLocation.quotation.payment(quotationId, paymentId)); + } + + @Head("{name}") + async headAttachment( + @Request() req: RequestWithUser, + @Path() paymentId: string, + @Path() name: string, + ) { + const data = await prisma.payment.findUnique({ + where: { id: paymentId }, + include: { invoice: true }, + }); + if (!data) throw notFoundError("Payment"); + return req.res?.redirect( + await getPresigned( + "head", + fileLocation.quotation.payment(data.invoice.quotationId, paymentId, name), + ), + ); + } + + @Get("{name}") + async getAttachment( + @Request() req: RequestWithUser, + @Path() paymentId: string, + @Path() name: string, + ) { + const data = await prisma.payment.findUnique({ + where: { id: paymentId }, + include: { invoice: true }, + }); + if (!data) throw notFoundError("Payment"); + return req.res?.redirect( + await getFile(fileLocation.quotation.payment(data.invoice.quotationId, paymentId, name)), + ); + } +}