feat: allow company change

This commit is contained in:
Methapon Metanipat 2024-09-23 17:47:02 +07:00
parent 4b963cf69c
commit 6ba5234587

View file

@ -230,22 +230,35 @@ export class ProductGroup extends Controller {
let companyAfter = let companyAfter =
!!body.registeredBranchId && branch ? (branch.headOffice || branch).code : false; !!body.registeredBranchId && branch ? (branch.headOffice || branch).code : false;
const result = await prisma.$transaction(async (tx) => {
let code = "";
if (companyBefore && companyAfter && companyBefore !== companyAfter) { if (companyBefore && companyAfter && companyBefore !== companyAfter) {
throw new HttpError( const last = await tx.runningNo.upsert({
HttpStatus.BAD_REQUEST, where: {
"Cannot move between different headoffice", key: `PRODGRP_${companyAfter}`,
"crossCompanyNotPermit", },
); create: {
key: `PRODGRP_${companyAfter}`,
value: 1,
},
update: { value: { increment: 1 } },
});
code = `G${last.value.toString().padStart(2, "0")}`;
} }
return await prisma.productGroup.update({
const result = await prisma.productGroup.update({
include: { include: {
createdBy: true, createdBy: true,
updatedBy: true, updatedBy: true,
}, },
data: { ...body, statusOrder: +(body.status === "INACTIVE"), updatedByUserId: req.user.sub }, data: {
...body,
code: !!code ? code : undefined,
statusOrder: +(body.status === "INACTIVE"),
updatedByUserId: req.user.sub,
},
where: { id: groupId }, where: { id: groupId },
}); });
});
return result; return result;
} }