refactor: use prisma nested create instead
This commit is contained in:
parent
17e00ae60f
commit
ab484f4026
2 changed files with 138 additions and 192 deletions
|
|
@ -406,83 +406,7 @@ export class QuotationController extends Controller {
|
|||
update: { value: { increment: 1 } },
|
||||
});
|
||||
|
||||
const quotation = await tx.quotation.create({
|
||||
data: {
|
||||
...rest,
|
||||
statusOrder: +(rest.status === "INACTIVE"),
|
||||
code: `${currentYear.toString().padStart(2, "0")}${currentMonth.toString().padStart(2, "0")}${currentDate.toString().padStart(2, "0")}${lastQuotation.value.toString().padStart(4, "0")}`,
|
||||
worker: {
|
||||
createMany: {
|
||||
data: sortedEmployeeId.map((v, i) => ({
|
||||
no: i,
|
||||
code: "",
|
||||
employeeId: v,
|
||||
})),
|
||||
},
|
||||
},
|
||||
totalPrice: price.totalPrice,
|
||||
totalDiscount: price.totalDiscount,
|
||||
|
||||
vat: price.totalVat,
|
||||
vatExcluded: 0,
|
||||
|
||||
finalPrice: price.totalPrice - price.totalDiscount,
|
||||
|
||||
paySplit: {
|
||||
createMany: {
|
||||
data: (rest.paySplit || []).map((v, i) => ({
|
||||
no: i + 1,
|
||||
date: v,
|
||||
})),
|
||||
},
|
||||
},
|
||||
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
restructureService.map(async (a) => {
|
||||
const { id: _currentServiceId } = await tx.quotationService.create({
|
||||
data: {
|
||||
code: a.code,
|
||||
name: a.name,
|
||||
detail: a.detail,
|
||||
attributes: a.attributes,
|
||||
quotationId: quotation.id,
|
||||
refServiceId: a.id,
|
||||
},
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
a.work.map(async (b) => {
|
||||
await tx.quotationServiceWork.create({
|
||||
data: {
|
||||
order: b.order,
|
||||
name: b.name,
|
||||
attributes: b.attributes,
|
||||
serviceId: _currentServiceId,
|
||||
productOnWork: {
|
||||
createMany: {
|
||||
data: b.product.map((v, i) => ({
|
||||
productId: v.id,
|
||||
order: i + 1,
|
||||
vat: v.vat,
|
||||
amount: v.amount,
|
||||
discount: v.discount,
|
||||
pricePerUnit: v.pricePerUnit,
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
return await tx.quotation.findUnique({
|
||||
return await tx.quotation.create({
|
||||
include: {
|
||||
service: {
|
||||
include: {
|
||||
|
|
@ -504,7 +428,64 @@ export class QuotationController extends Controller {
|
|||
select: { service: true },
|
||||
},
|
||||
},
|
||||
where: { id: quotation.id },
|
||||
|
||||
data: {
|
||||
...rest,
|
||||
statusOrder: +(rest.status === "INACTIVE"),
|
||||
code: `${currentYear.toString().padStart(2, "0")}${currentMonth.toString().padStart(2, "0")}${currentDate.toString().padStart(2, "0")}${lastQuotation.value.toString().padStart(4, "0")}`,
|
||||
worker: {
|
||||
createMany: {
|
||||
data: sortedEmployeeId.map((v, i) => ({
|
||||
no: i,
|
||||
code: "",
|
||||
employeeId: v,
|
||||
})),
|
||||
},
|
||||
},
|
||||
totalPrice: price.totalPrice,
|
||||
totalDiscount: price.totalDiscount,
|
||||
vat: price.totalVat,
|
||||
vatExcluded: 0,
|
||||
finalPrice: price.totalPrice - price.totalDiscount,
|
||||
paySplit: {
|
||||
createMany: {
|
||||
data: (rest.paySplit || []).map((v, i) => ({
|
||||
no: i + 1,
|
||||
date: v,
|
||||
})),
|
||||
},
|
||||
},
|
||||
service: {
|
||||
create: restructureService.map((a) => ({
|
||||
code: a.code,
|
||||
name: a.name,
|
||||
detail: a.detail,
|
||||
attributes: a.attributes,
|
||||
refServiceId: a.id,
|
||||
work: {
|
||||
create: a.work.map((b) => ({
|
||||
order: b.order,
|
||||
name: b.name,
|
||||
attributes: b.attributes,
|
||||
productOnWork: {
|
||||
createMany: {
|
||||
data: b.product.map((v, i) => ({
|
||||
productId: v.id,
|
||||
order: i + 1,
|
||||
vat: v.vat,
|
||||
amount: v.amount,
|
||||
discount: v.discount,
|
||||
pricePerUnit: v.pricePerUnit,
|
||||
})),
|
||||
},
|
||||
},
|
||||
})),
|
||||
},
|
||||
})),
|
||||
},
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -691,7 +672,28 @@ export class QuotationController extends Controller {
|
|||
};
|
||||
});
|
||||
|
||||
const quotation = await tx.quotation.update({
|
||||
return await tx.quotation.update({
|
||||
include: {
|
||||
service: {
|
||||
include: {
|
||||
work: {
|
||||
include: {
|
||||
productOnWork: {
|
||||
include: { product: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
paySplit: true,
|
||||
worker: true,
|
||||
customerBranch: {
|
||||
include: { customer: true },
|
||||
},
|
||||
_count: {
|
||||
select: { service: true },
|
||||
},
|
||||
},
|
||||
where: { id: quotationId },
|
||||
data: {
|
||||
...rest,
|
||||
|
|
@ -731,78 +733,42 @@ export class QuotationController extends Controller {
|
|||
}
|
||||
: undefined,
|
||||
|
||||
service: body.service ? { deleteMany: {} } : undefined,
|
||||
service:
|
||||
body.service && restructureService
|
||||
? {
|
||||
deleteMany: {},
|
||||
create: restructureService.map((a) => ({
|
||||
code: a.code,
|
||||
name: a.name,
|
||||
detail: a.detail,
|
||||
attributes: a.attributes,
|
||||
refServiceId: a.id,
|
||||
work: {
|
||||
create: a.work.map((b) => ({
|
||||
order: b.order,
|
||||
name: b.name,
|
||||
attributes: b.attributes,
|
||||
productOnWork: {
|
||||
createMany: {
|
||||
data: b.product.map((v, i) => ({
|
||||
productId: v.id,
|
||||
order: i + 1,
|
||||
vat: v.vat,
|
||||
amount: v.amount,
|
||||
discount: v.discount,
|
||||
pricePerUnit: v.pricePerUnit,
|
||||
})),
|
||||
},
|
||||
},
|
||||
})),
|
||||
},
|
||||
})),
|
||||
}
|
||||
: undefined,
|
||||
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
});
|
||||
|
||||
if (restructureService) {
|
||||
await Promise.all(
|
||||
restructureService.map(async (a) => {
|
||||
const { id: _currentServiceId } = await tx.quotationService.create({
|
||||
data: {
|
||||
code: a.code,
|
||||
name: a.name,
|
||||
detail: a.detail,
|
||||
attributes: a.attributes,
|
||||
quotationId: quotation.id,
|
||||
refServiceId: a.id,
|
||||
},
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
a.work.map(async (b) => {
|
||||
await tx.quotationServiceWork.create({
|
||||
data: {
|
||||
order: b.order,
|
||||
name: b.name,
|
||||
attributes: b.attributes,
|
||||
serviceId: _currentServiceId,
|
||||
productOnWork: {
|
||||
createMany: {
|
||||
data: b.product.map((v, i) => ({
|
||||
productId: v.id,
|
||||
order: i + 1,
|
||||
vat: v.vat,
|
||||
amount: v.amount,
|
||||
discount: v.discount,
|
||||
pricePerUnit: v.pricePerUnit,
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return await tx.quotation.findUnique({
|
||||
include: {
|
||||
service: {
|
||||
include: {
|
||||
work: {
|
||||
include: {
|
||||
productOnWork: {
|
||||
include: { product: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
paySplit: true,
|
||||
worker: true,
|
||||
customerBranch: {
|
||||
include: { customer: true },
|
||||
},
|
||||
_count: {
|
||||
select: { service: true },
|
||||
},
|
||||
},
|
||||
where: { id: quotation.id },
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue