feat: add payment for split
This commit is contained in:
parent
ca6b1e74d0
commit
20b7e56a0d
4 changed files with 151 additions and 30 deletions
|
|
@ -1,21 +1,37 @@
|
|||
import { Controller, Path, Post, Put, Route, Tags } from "tsoa";
|
||||
import { Body, Controller, Get, Path, Post, Put, Query, Request, Route, Tags } from "tsoa";
|
||||
import express from "express";
|
||||
import prisma from "../db";
|
||||
import { notFoundError } from "../utils/error";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import { fileLocation, getFile, setFile } from "../utils/minio";
|
||||
|
||||
@Tags("Quotation")
|
||||
@Route("api/v1/quotation/{quotationId}/payment")
|
||||
export class QuotationPayment extends Controller {
|
||||
@Post("submit")
|
||||
async submitPayment(@Path("quotationId") id: string) {
|
||||
@Get("quotationId")
|
||||
async getPayment(@Path() quotationId: string) {
|
||||
const record = await prisma.quotation.findFirst({
|
||||
where: { id: quotationId },
|
||||
include: {
|
||||
quotationPaymentData: true,
|
||||
createdBy: true,
|
||||
updatedBy: true,
|
||||
},
|
||||
});
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
@Post()
|
||||
async addPayment(@Path() quotationId: string, @Body() body: { amount: number; date: Date }) {
|
||||
const record = await prisma.quotation.findUnique({
|
||||
where: { id },
|
||||
where: { id: quotationId },
|
||||
});
|
||||
|
||||
if (!record) throw notFoundError("Quotation");
|
||||
|
||||
if (record.quotationStatus !== "PaymentPending" && record.quotationStatus !== "PaymentWait") {
|
||||
if (record.quotationStatus !== "PaymentPending") {
|
||||
// NOTE: The quotation must be in waiting for payment or waiting for payment confirmation (re-submit payment)
|
||||
throw new HttpError(
|
||||
HttpStatus.PRECONDITION_FAILED,
|
||||
|
|
@ -24,21 +40,43 @@ export class QuotationPayment extends Controller {
|
|||
);
|
||||
}
|
||||
|
||||
await prisma.quotation.update({
|
||||
where: { id },
|
||||
data: { quotationStatus: "PaymentWait" },
|
||||
return await prisma.quotation.update({
|
||||
where: { id: quotationId },
|
||||
include: { quotationPaymentData: true },
|
||||
data: {
|
||||
quotationStatus: "PaymentInProcess",
|
||||
quotationPaymentData: {
|
||||
create: {
|
||||
paymentStatus: "PaymentWait",
|
||||
...body,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@Put("file")
|
||||
async uploadPayment(@Path("quotationId") id: string) {
|
||||
@Get("{paymentId}/file")
|
||||
async getPaymentFile(
|
||||
@Request() req: express.Request,
|
||||
@Path() quotationId: string,
|
||||
@Path() paymentId: string,
|
||||
) {
|
||||
return req.res?.redirect(await getFile(fileLocation.quotation.payment(quotationId, paymentId)));
|
||||
}
|
||||
|
||||
@Put("{paymentId}/file")
|
||||
async uploadPayment(
|
||||
@Request() req: express.Request,
|
||||
@Path() quotationId: string,
|
||||
@Path() paymentId: string,
|
||||
) {
|
||||
const record = await prisma.quotation.findUnique({
|
||||
where: { id },
|
||||
where: { id: quotationId },
|
||||
});
|
||||
|
||||
if (!record) throw notFoundError("Quotation");
|
||||
|
||||
if (record.quotationStatus !== "PaymentPending" && record.quotationStatus !== "PaymentWait") {
|
||||
if (record.quotationStatus !== "PaymentPending") {
|
||||
// NOTE: The quotation must be in waiting for payment or waiting for payment confirmation (re-submit payment)
|
||||
throw new HttpError(
|
||||
HttpStatus.PRECONDITION_FAILED,
|
||||
|
|
@ -46,13 +84,22 @@ export class QuotationPayment extends Controller {
|
|||
"quotationStatusWrong",
|
||||
);
|
||||
}
|
||||
|
||||
return req.res?.redirect(await setFile(fileLocation.quotation.payment(quotationId, paymentId)));
|
||||
}
|
||||
|
||||
@Post("confirm")
|
||||
async confirmPayment(@Path("quotationId") id: string) {
|
||||
async confirmPayment(@Path() quotationId: string, @Query() paymentId?: string) {
|
||||
const record = await prisma.quotation.findUnique({
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
quotationPaymentData: true,
|
||||
paySplit: true,
|
||||
},
|
||||
},
|
||||
worker: true,
|
||||
quotationPaymentData: true,
|
||||
productServiceList: {
|
||||
include: {
|
||||
worker: true,
|
||||
|
|
@ -62,28 +109,44 @@ export class QuotationPayment extends Controller {
|
|||
},
|
||||
},
|
||||
},
|
||||
where: { id },
|
||||
where: { id: quotationId },
|
||||
});
|
||||
|
||||
if (!record) throw notFoundError("Quotation");
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.quotation.update({
|
||||
where: { id },
|
||||
where: { id: quotationId },
|
||||
data: {
|
||||
quotationStatus: "PaymentSuccess",
|
||||
requestData: {
|
||||
create: record.worker.map((v) => ({
|
||||
employeeId: v.employeeId,
|
||||
requestWork: {
|
||||
create: record.productServiceList.flatMap((item) =>
|
||||
item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1
|
||||
? { productServiceId: item.id }
|
||||
: [],
|
||||
),
|
||||
},
|
||||
})),
|
||||
quotationStatus:
|
||||
record.payCondition === "Full" ||
|
||||
record.payCondition === "BillFull" ||
|
||||
record._count.paySplit === record._count.quotationPaymentData
|
||||
? "PaymentSuccess"
|
||||
: undefined,
|
||||
quotationPaymentData: {
|
||||
update: paymentId
|
||||
? {
|
||||
where: { id: paymentId },
|
||||
data: { paymentStatus: "PaymentSuccess" },
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
requestData:
|
||||
record.quotationStatus === "PaymentPending"
|
||||
? {
|
||||
create: record.worker.map((v) => ({
|
||||
employeeId: v.employeeId,
|
||||
requestWork: {
|
||||
create: record.productServiceList.flatMap((item) =>
|
||||
item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1
|
||||
? { productServiceId: item.id }
|
||||
: [],
|
||||
),
|
||||
},
|
||||
})),
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue