refactor: invoice relation (#2)

* refactor: update database table

* refactor: update invoice to have relation with installments

* chore: add migration
This commit is contained in:
Methapon Metanipat 2024-10-30 17:32:46 +07:00 committed by GitHub
parent ba41bf45cb
commit 824727582d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 106 additions and 163 deletions

View file

@ -2,12 +2,10 @@ import { Prisma } from "@prisma/client";
import {
Body,
Controller,
Delete,
Get,
OperationId,
Path,
Post,
Put,
Query,
Request,
Route,
@ -26,10 +24,7 @@ import {
type InvoicePayload = {
quotationId: string;
amount: number;
// NOTE: For individual list that will be include in the quotation
productServiceListId?: string[];
// NOTE: Will be pulled from quotation
installmentNo?: number[];
installmentNo: number[];
};
const MANAGE_ROLES = ["system", "head_of_admin", "admin", "head_of_account", "account"];
@ -67,14 +62,7 @@ export class InvoiceController extends Controller {
prisma.invoice.findMany({
where,
include: {
productServiceList: {
include: {
worker: true,
service: true,
work: true,
product: true,
},
},
installments: true,
quotation: true,
createdBy: true,
},
@ -93,14 +81,7 @@ export class InvoiceController extends Controller {
const record = await prisma.invoice.findFirst({
where: { id: invoiceId },
include: {
productServiceList: {
include: {
worker: true,
service: true,
work: true,
product: true,
},
},
installments: true,
quotation: true,
createdBy: true,
},
@ -116,44 +97,35 @@ export class InvoiceController extends Controller {
@OperationId("createInvoice")
@Security("keycloak", MANAGE_ROLES)
async createInvoice(@Request() req: RequestWithUser, @Body() body: InvoicePayload) {
const [quotation, productServiceList] = await prisma.$transaction([
const [quotation] = await prisma.$transaction([
prisma.quotation.findUnique({
where: { id: body.quotationId },
include: { registeredBranch: { include: branchRelationPermInclude(req.user) } },
}),
prisma.quotationProductServiceList.findMany({
where: {
OR: [
{ id: { in: body.productServiceListId }, invoiceId: null },
{ installmentNo: { in: body.installmentNo }, invoiceId: null },
],
},
}),
]);
if (!quotation) throw notFoundError("Quotation");
await permissionCheck(req.user, quotation.registeredBranch);
return await prisma.$transaction(async (tx) => {
await tx.quotation.update({
const record = await tx.quotation.update({
include: {
paySplit: {
where: { no: { in: body.installmentNo } },
},
},
where: { id: body.quotationId },
data: {
quotationStatus: "PaymentInProcess",
paySplit: body.installmentNo
? {
updateMany: {
where: { no: { in: body.installmentNo } },
data: { invoice: true },
},
}
: undefined,
},
});
return await tx.invoice.create({
data: {
productServiceList: { connect: productServiceList.map((v) => ({ id: v.id })) },
quotationId: body.quotationId,
amount: body.amount,
installments: {
connect: record.paySplit.map((v) => ({ id: v.id })),
},
payment: {
create: {
paymentStatus: "PaymentWait",
@ -165,85 +137,4 @@ export class InvoiceController extends Controller {
});
});
}
@Put("{invoiceId}")
@OperationId("updateInvoice")
@Security("keycloak", MANAGE_ROLES)
async updateInvoice(
@Request() req: RequestWithUser,
@Body() body: InvoicePayload,
@Path() invoiceId: string,
) {
const [record, quotation, productServiceList] = await prisma.$transaction([
prisma.invoice.findUnique({
where: { id: invoiceId },
include: {
productServiceList: {
where: {
id: { notIn: body.productServiceListId },
installmentNo: { notIn: body.installmentNo },
},
},
},
}),
prisma.quotation.findUnique({
where: { id: body.quotationId },
include: { registeredBranch: { include: branchRelationPermInclude(req.user) } },
}),
prisma.quotationProductServiceList.findMany({
where: {
OR: [
{ id: { in: body.productServiceListId }, invoiceId: null },
{ installmentNo: { in: body.installmentNo }, invoiceId: null },
],
},
}),
]);
if (!record) throw notFoundError("Invoice");
if (!quotation) throw notFoundError("Quotation");
await permissionCheck(req.user, quotation.registeredBranch);
return await prisma.$transaction(async (tx) => {
return await tx.invoice.update({
where: { id: invoiceId },
data: {
productServiceList: {
disconnect: record.productServiceList.map((v) => ({ id: v.id })),
connect: productServiceList.map((v) => ({ id: v.id })),
},
},
});
});
}
@Delete("{invoiceId}")
@OperationId("deleteInvoice")
@Security("keycloak", MANAGE_ROLES)
async deleteInvoice(@Request() req: RequestWithUser, @Path() invoiceId: string) {
return await prisma.$transaction(async (tx) => {
const record = await tx.invoice.delete({
where: { id: invoiceId },
include: {
productServiceList: {
include: {
worker: true,
service: true,
work: true,
product: true,
},
},
quotation: {
include: { registeredBranch: { include: branchRelationPermInclude(req.user) } },
},
createdBy: true,
},
});
if (!record) throw notFoundError("Invoice");
await permissionCheck(req.user, record.quotation.registeredBranch);
return record;
});
}
}

View file

@ -37,14 +37,7 @@ export class ReceiptController extends Controller {
include: {
invoice: {
include: {
productServiceList: {
include: {
worker: true,
service: true,
work: true,
product: true,
},
},
installments: true,
quotation: true,
createdBy: true,
},
@ -67,14 +60,7 @@ export class ReceiptController extends Controller {
include: {
invoice: {
include: {
productServiceList: {
include: {
worker: true,
service: true,
work: true,
product: true,
},
},
installments: true,
quotation: true,
createdBy: true,
},

View file

@ -61,14 +61,7 @@ export class QuotationPayment extends Controller {
include: {
invoice: {
include: {
productServiceList: {
include: {
worker: true,
service: true,
work: true,
product: true,
},
},
installments: true,
quotation: true,
createdBy: true,
},
@ -90,14 +83,7 @@ export class QuotationPayment extends Controller {
include: {
invoice: {
include: {
productServiceList: {
include: {
worker: true,
service: true,
work: true,
product: true,
},
},
installments: true,
quotation: true,
createdBy: true,
},

View file

@ -289,7 +289,9 @@ export class QuotationController extends Controller {
worker: true,
},
},
paySplit: true,
paySplit: {
include: { invoice: true },
},
createdBy: true,
updatedBy: true,
},
@ -482,7 +484,9 @@ export class QuotationController extends Controller {
worker: true,
},
},
paySplit: true,
paySplit: {
include: { invoice: true },
},
worker: true,
customerBranch: {
include: { customer: true },