feat: add new employee while add worker after accepted
This commit is contained in:
parent
24e379fbf4
commit
01d517cc27
1 changed files with 102 additions and 12 deletions
|
|
@ -923,14 +923,63 @@ export class QuotationActionController extends Controller {
|
||||||
@Request() req: RequestWithUser,
|
@Request() req: RequestWithUser,
|
||||||
@Path() quotationId: string,
|
@Path() quotationId: string,
|
||||||
@Body()
|
@Body()
|
||||||
body: {
|
body: (
|
||||||
workerId: string;
|
| {
|
||||||
productServiceId: string[];
|
workerId: string;
|
||||||
}[],
|
productServiceId: string[];
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
workerData: {
|
||||||
|
dateOfBirth: Date;
|
||||||
|
gender: string;
|
||||||
|
nationality: string;
|
||||||
|
namePrefix?: string;
|
||||||
|
firstName: string;
|
||||||
|
firstNameEN: string;
|
||||||
|
middleName?: string;
|
||||||
|
middleNameEN?: string;
|
||||||
|
lastName: string;
|
||||||
|
lastNameEN: string;
|
||||||
|
};
|
||||||
|
productServiceId: string[];
|
||||||
|
}
|
||||||
|
)[],
|
||||||
) {
|
) {
|
||||||
|
const { existsEmployee, newEmployee } = body.reduce<{
|
||||||
|
existsEmployee: {
|
||||||
|
workerId: string;
|
||||||
|
productServiceId: string[];
|
||||||
|
}[];
|
||||||
|
newEmployee: {
|
||||||
|
workerData: {
|
||||||
|
dateOfBirth: Date;
|
||||||
|
gender: string;
|
||||||
|
nationality: string;
|
||||||
|
namePrefix?: string;
|
||||||
|
firstName: string;
|
||||||
|
firstNameEN: string;
|
||||||
|
middleName?: string;
|
||||||
|
middleNameEN?: string;
|
||||||
|
lastName: string;
|
||||||
|
lastNameEN: string;
|
||||||
|
};
|
||||||
|
productServiceId: string[];
|
||||||
|
}[];
|
||||||
|
}>(
|
||||||
|
(acc, current) => {
|
||||||
|
if ("workerId" in current) {
|
||||||
|
acc.existsEmployee.push(current);
|
||||||
|
} else {
|
||||||
|
acc.newEmployee.push(current);
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
{ existsEmployee: [], newEmployee: [] },
|
||||||
|
);
|
||||||
|
|
||||||
const ids = {
|
const ids = {
|
||||||
employee: body.map((v) => v.workerId),
|
employee: existsEmployee.map((v) => v.workerId),
|
||||||
productService: body
|
productService: existsEmployee
|
||||||
.flatMap((v) => v.productServiceId)
|
.flatMap((v) => v.productServiceId)
|
||||||
.filter((lhs, i, a) => a.findIndex((rhs) => lhs === rhs) === i),
|
.filter((lhs, i, a) => a.findIndex((rhs) => lhs === rhs) === i),
|
||||||
};
|
};
|
||||||
|
|
@ -940,6 +989,7 @@ export class QuotationActionController extends Controller {
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
tx.quotation.findFirst({
|
tx.quotation.findFirst({
|
||||||
include: {
|
include: {
|
||||||
|
customerBranch: true,
|
||||||
worker: true,
|
worker: true,
|
||||||
_count: {
|
_count: {
|
||||||
select: {
|
select: {
|
||||||
|
|
@ -965,11 +1015,13 @@ export class QuotationActionController extends Controller {
|
||||||
if (ids.productService.length !== productService.length) throw relationError("Product");
|
if (ids.productService.length !== productService.length) throw relationError("Product");
|
||||||
if (
|
if (
|
||||||
quotation._count.worker +
|
quotation._count.worker +
|
||||||
body.filter((lhs) => !quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId))
|
existsEmployee.filter(
|
||||||
.length >
|
(lhs) => !quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId),
|
||||||
|
).length +
|
||||||
|
newEmployee.length >
|
||||||
(quotation.workerMax || 0)
|
(quotation.workerMax || 0)
|
||||||
) {
|
) {
|
||||||
if (body.length === 0) return;
|
if (existsEmployee.length === 0) return;
|
||||||
throw new HttpError(
|
throw new HttpError(
|
||||||
HttpStatus.PRECONDITION_FAILED,
|
HttpStatus.PRECONDITION_FAILED,
|
||||||
"Worker exceed current quotation max worker.",
|
"Worker exceed current quotation max worker.",
|
||||||
|
|
@ -978,8 +1030,46 @@ export class QuotationActionController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
await prisma.$transaction(async (tx) => {
|
await prisma.$transaction(async (tx) => {
|
||||||
|
const customerBranch = quotation.customerBranch;
|
||||||
|
const lastEmployee = await tx.runningNo.upsert({
|
||||||
|
where: {
|
||||||
|
key: `EMPLOYEE_${customerBranch.id}-${`${new Date().getFullYear()}`.slice(-2).padStart(2, "0")}`,
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
key: `EMPLOYEE_${customerBranch.id}-${`${new Date().getFullYear()}`.slice(-2).padStart(2, "0")}`,
|
||||||
|
value: newEmployee.length,
|
||||||
|
},
|
||||||
|
update: { value: { increment: newEmployee.length } },
|
||||||
|
});
|
||||||
|
|
||||||
|
const newEmployeeWithId = await Promise.all(
|
||||||
|
newEmployee.map(async (v, i) => {
|
||||||
|
const data = await tx.employee.create({
|
||||||
|
data: {
|
||||||
|
...v.workerData,
|
||||||
|
code: `${customerBranch.code}-${`${new Date().getFullYear()}`.slice(-2).padStart(2, "0")}${`${lastEmployee.value - nonExistEmployee.length + i + 1}`.padStart(7, "0")}`,
|
||||||
|
customerBranchId: customerBranch.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return { workerId: data.id, productServiceId: v.productServiceId };
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const rearrange: typeof existsEmployee = [];
|
||||||
|
|
||||||
|
while (body.length > 0) {
|
||||||
|
const item = body.shift();
|
||||||
|
if (item && "workerId" in item) {
|
||||||
|
rearrange.push(item);
|
||||||
|
} else {
|
||||||
|
const popNew = newEmployeeWithId.shift();
|
||||||
|
popNew && rearrange.push(popNew);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await tx.quotationProductServiceWorker.createMany({
|
await tx.quotationProductServiceWorker.createMany({
|
||||||
data: body
|
data: existsEmployee
|
||||||
.filter((lhs) => !quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId))
|
.filter((lhs) => !quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId))
|
||||||
.flatMap((lhs) =>
|
.flatMap((lhs) =>
|
||||||
lhs.productServiceId.map((rhs) => ({
|
lhs.productServiceId.map((rhs) => ({
|
||||||
|
|
@ -1010,7 +1100,7 @@ export class QuotationActionController extends Controller {
|
||||||
quotationStatus: QuotationStatus.PaymentSuccess, // NOTE: change back if already complete or canceled
|
quotationStatus: QuotationStatus.PaymentSuccess, // NOTE: change back if already complete or canceled
|
||||||
worker: {
|
worker: {
|
||||||
createMany: {
|
createMany: {
|
||||||
data: body
|
data: rearrange
|
||||||
.filter((lhs) => !quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId))
|
.filter((lhs) => !quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId))
|
||||||
.map((v, i) => ({
|
.map((v, i) => ({
|
||||||
no: quotation._count.worker + i + 1,
|
no: quotation._count.worker + i + 1,
|
||||||
|
|
@ -1022,7 +1112,7 @@ export class QuotationActionController extends Controller {
|
||||||
quotation.quotationStatus === "PaymentInProcess" ||
|
quotation.quotationStatus === "PaymentInProcess" ||
|
||||||
quotation.quotationStatus === "PaymentSuccess"
|
quotation.quotationStatus === "PaymentSuccess"
|
||||||
? {
|
? {
|
||||||
create: body
|
create: rearrange
|
||||||
.filter(
|
.filter(
|
||||||
(lhs) =>
|
(lhs) =>
|
||||||
!quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId) &&
|
!quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId) &&
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue