jws-backend/src/controllers/04-receipt-controller.ts
Methapon Metanipat 824727582d
refactor: invoice relation (#2)
* refactor: update database table

* refactor: update invoice to have relation with installments

* chore: add migration
2024-10-30 17:32:46 +07:00

76 lines
1.9 KiB
TypeScript

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() quotationId?: string,
) {
const where: Prisma.PaymentWhereInput = {
paymentStatus: "PaymentSuccess",
invoice: {
quotationId,
quotation: {
registeredBranch: {
OR: permissionCondCompany(req.user),
},
},
},
};
const [result, total] = await prisma.$transaction([
prisma.payment.findMany({
where,
include: {
invoice: {
include: {
installments: true,
quotation: true,
createdBy: true,
},
},
},
orderBy: { createdAt: "asc" },
}),
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;
}
}