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 },
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -286,34 +286,12 @@ export class ServiceController extends Controller {
|
|||
update: { value: { increment: 1 } },
|
||||
});
|
||||
|
||||
const workList = await Promise.all(
|
||||
(work || []).map(async (w, wIdx) =>
|
||||
tx.work.create({
|
||||
data: {
|
||||
name: w.name,
|
||||
order: wIdx + 1,
|
||||
attributes: w.attributes,
|
||||
productOnWork: {
|
||||
createMany: {
|
||||
data: w.productId.map((p, pIdx) => ({
|
||||
productId: p,
|
||||
order: pIdx + 1,
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
return tx.service.create({
|
||||
include: {
|
||||
work: {
|
||||
include: {
|
||||
productOnWork: {
|
||||
include: {
|
||||
product: true,
|
||||
},
|
||||
include: { product: true },
|
||||
orderBy: { order: "asc" },
|
||||
},
|
||||
},
|
||||
|
|
@ -326,7 +304,19 @@ export class ServiceController extends Controller {
|
|||
productTypeId,
|
||||
statusOrder: +(body.status === "INACTIVE"),
|
||||
code: `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`,
|
||||
work: { connect: workList.map((v) => ({ id: v.id })) },
|
||||
work: {
|
||||
create: (work || []).map((w, wIdx) => ({
|
||||
name: w.name,
|
||||
order: wIdx + 1,
|
||||
attributes: w.attributes,
|
||||
productOnWork: {
|
||||
create: w.productId.map((p, pIdx) => ({
|
||||
productId: p,
|
||||
order: pIdx + 1,
|
||||
})),
|
||||
},
|
||||
})),
|
||||
},
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
|
|
@ -412,26 +402,6 @@ export class ServiceController extends Controller {
|
|||
}
|
||||
|
||||
const record = await prisma.$transaction(async (tx) => {
|
||||
const workList = await Promise.all(
|
||||
(work || []).map(async (w, wIdx) =>
|
||||
tx.work.create({
|
||||
data: {
|
||||
name: w.name,
|
||||
order: wIdx + 1,
|
||||
attributes: w.attributes,
|
||||
productOnWork: {
|
||||
createMany: {
|
||||
data: w.productId.map((p, pIdx) => ({
|
||||
productId: p,
|
||||
order: pIdx + 1,
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
return await tx.service.update({
|
||||
include: {
|
||||
createdBy: true,
|
||||
|
|
@ -442,7 +412,17 @@ export class ServiceController extends Controller {
|
|||
statusOrder: +(payload.status === "INACTIVE"),
|
||||
work: {
|
||||
deleteMany: {},
|
||||
connect: workList.map((v) => ({ id: v.id })),
|
||||
create: (work || []).map((w, wIdx) => ({
|
||||
name: w.name,
|
||||
order: wIdx + 1,
|
||||
attributes: w.attributes,
|
||||
productOnWork: {
|
||||
create: w.productId.map((p, pIdx) => ({
|
||||
productId: p,
|
||||
order: pIdx + 1,
|
||||
})),
|
||||
},
|
||||
})),
|
||||
},
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue