diff --git a/src/controllers/05-payment-controller.ts b/src/controllers/05-payment-controller.ts index 36d433e..7856f1c 100644 --- a/src/controllers/05-payment-controller.ts +++ b/src/controllers/05-payment-controller.ts @@ -22,6 +22,9 @@ import { createPermCheck, createPermCondition, } from "../services/permission"; +import flowAccount from "../services/flowaccount"; +import HttpError from "../interfaces/http-error"; +import HttpStatus from "../interfaces/http-status"; const MANAGE_ROLES = ["system", "head_of_admin", "admin", "head_of_accountant", "accountant"]; @@ -304,3 +307,46 @@ export class PaymentFileController extends Controller { return await deleteFile(fileLocation.quotation.payment(quotationId, paymentId, name)); } } + +@Route("api/v1/payment/{paymentId}/flow-account") +@Tags("Payment") +export class FlowAccountController extends Controller { + @Get() + async getDocument(@Path() paymentId: string) { + const payment = await prisma.payment.findFirst({ + where: { + id: paymentId, + }, + include: { invoice: true }, + }); + + if (!payment) throw notFoundError("Payment"); + if (payment.paymentStatus !== PaymentStatus.PaymentSuccess) + throw new HttpError( + HttpStatus.PRECONDITION_FAILED, + "Payment not success", + "paymentNotSuccess", + ); + if (!payment.invoice.flowAccountRecordId) { + const result = await flowAccount.issueInvoice(payment.invoice.id); + const documentId = result?.body?.data?.documentId; + + await prisma.invoice.update({ + where: { id: payment.invoice.id }, + data: { flowAccountRecordId: documentId }, + }); + + if (documentId) { + return flowAccount.getInvoiceDocument(documentId); + } else { + throw new HttpError( + HttpStatus.INTERNAL_SERVER_ERROR, + "Failed to issue invoice/receipt document.", + "InvoiceReceiptIssueFailed", + ); + } + } else { + return flowAccount.getInvoiceDocument(payment.invoice.flowAccountRecordId); + } + } +}