import { Controller, Get, OperationId, Path, Query, Request, Route, Security, Tags } from "tsoa"; import prisma from "../db"; import { Prisma } from "@prisma/client"; import { notFoundError } from "../utils/error"; import { RequestWithUser } from "../interfaces/user"; import { createPermCondition } from "../services/permission"; const permissionCondCompany = createPermCondition((_) => true); @Route("/api/v1/receipt") @Tags("Receipt") export class ReceiptController extends Controller { @Get() @OperationId("getReceiptList") @Security("keycloak") async getReceiptList( @Request() req: RequestWithUser, @Query() page: number = 1, @Query() pageSize: number = 30, @Query() quotationOnly: boolean = true, @Query() quotationId?: string, @Query() debitNoteId?: string, @Query() debitNoteOnly?: boolean, ) { const where: Prisma.PaymentWhereInput = { paymentStatus: "PaymentSuccess", invoice: { quotation: { id: quotationId || debitNoteId, isDebitNote: debitNoteId || debitNoteOnly ? true : quotationOnly ? false : undefined, registeredBranch: { OR: permissionCondCompany(req.user), }, }, }, }; const [result, total] = await prisma.$transaction([ prisma.payment.findMany({ where, include: { invoice: { include: { installments: true, quotation: { include: { customerBranch: { include: { customer: true }, }, }, }, createdBy: true, }, }, }, orderBy: { createdAt: "asc" }, take: pageSize, skip: (page - 1) * pageSize, }), prisma.payment.count({ where }), ]); return { result, page, pageSize, total }; } @Get("{receiptId}") @OperationId("getReceipt") @Security("keycloak") async getReceipt(@Path() receiptId: string) { const record = await prisma.payment.findFirst({ where: { id: receiptId }, include: { invoice: { include: { installments: true, quotation: true, createdBy: true, }, }, }, orderBy: { createdAt: "asc" }, }); if (!record) throw notFoundError("Receipt"); return record; } }