add import file product
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 7s
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 7s
This commit is contained in:
parent
fd7833a592
commit
05d16f22de
3 changed files with 236 additions and 0 deletions
|
|
@ -11,6 +11,7 @@ import {
|
|||
Security,
|
||||
Tags,
|
||||
Query,
|
||||
UploadedFile,
|
||||
} from "tsoa";
|
||||
import { Prisma, Product, Status } from "@prisma/client";
|
||||
|
||||
|
|
@ -28,6 +29,7 @@ import { filterStatus } from "../services/prisma";
|
|||
import { deleteFile, deleteFolder, fileLocation, getFile, listFile, setFile } from "../utils/minio";
|
||||
import { isUsedError, notFoundError, relationError } from "../utils/error";
|
||||
import { queryOrNot, whereDateQuery } from "../utils/relation";
|
||||
import spreadsheet from "../utils/spreadsheet";
|
||||
|
||||
const MANAGE_ROLES = [
|
||||
"system",
|
||||
|
|
@ -447,6 +449,132 @@ export class ProductController extends Controller {
|
|||
where: { id: productId },
|
||||
});
|
||||
}
|
||||
|
||||
@Post("uploadedFile")
|
||||
@Security("keycloak", MANAGE_ROLES)
|
||||
async importProduct(
|
||||
@Request() req: RequestWithUser,
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
@Query() productGroupId: string,
|
||||
) {
|
||||
if (!file?.buffer) throw notFoundError("File");
|
||||
|
||||
const buffer = new Uint8Array(file.buffer).buffer;
|
||||
const dataFile = await spreadsheet.readExcel(buffer, {
|
||||
header: true,
|
||||
worksheet: "Sheet1",
|
||||
});
|
||||
|
||||
let dataName: string[] = [];
|
||||
const data = await dataFile.map((item: any) => {
|
||||
dataName.push(item.name);
|
||||
return {
|
||||
...item,
|
||||
expenseType:
|
||||
item.expenseType === "ค่าธรรมเนียม"
|
||||
? "fee"
|
||||
: item.expenseType === "ค่าบริการ"
|
||||
? "serviceFee"
|
||||
: "processingFee",
|
||||
shared: item.shared === "ใช่" ? true : false,
|
||||
calcVat: item.calcVat === "ใช่" ? true : false,
|
||||
vatIncluded: item.vatIncluded === "รวม" ? true : false,
|
||||
agentPriceCalcVat: item.agentPriceCalcVat === "ใช่" ? true : false,
|
||||
agentPriceVatIncluded: item.agentPriceVatIncluded === "รวม" ? true : false,
|
||||
serviceChargeCalcVat: item.serviceChargeCalcVat === "ใช่" ? true : false,
|
||||
serviceChargeVatIncluded: item.serviceChargeVatIncluded === "รวม" ? true : false,
|
||||
};
|
||||
});
|
||||
|
||||
const [productGroup, productSameName] = await prisma.$transaction([
|
||||
prisma.productGroup.findFirst({
|
||||
include: {
|
||||
registeredBranch: {
|
||||
include: branchRelationPermInclude(req.user),
|
||||
},
|
||||
createdBy: true,
|
||||
updatedBy: true,
|
||||
},
|
||||
where: { id: productGroupId },
|
||||
}),
|
||||
prisma.product.findMany({
|
||||
where: {
|
||||
productGroup: {
|
||||
registeredBranch: {
|
||||
OR: permissionCondCompany(req.user),
|
||||
},
|
||||
},
|
||||
name: { in: dataName },
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
if (!productGroup) throw relationError("Product Group");
|
||||
|
||||
await permissionCheck(req.user, productGroup.registeredBranch);
|
||||
let dataProduct: ProductCreate[] = [];
|
||||
|
||||
const record = await prisma.$transaction(
|
||||
async (tx) => {
|
||||
const branch = productGroup.registeredBranch;
|
||||
const company = (branch.headOffice || branch).code;
|
||||
console.log(branch, company);
|
||||
for (const item of data) {
|
||||
const dataDuplicate = productSameName.some(
|
||||
(v) => v.code.slice(0, -3) === item.code.toUpperCase() && v.name === item.name,
|
||||
);
|
||||
|
||||
if (!dataDuplicate) {
|
||||
const last = await tx.runningNo.upsert({
|
||||
where: {
|
||||
key: `PRODUCT_${company}_${item.code.toLocaleUpperCase()}`,
|
||||
},
|
||||
create: {
|
||||
key: `PRODUCT_${company}_${item.code.toLocaleUpperCase()}`,
|
||||
value: 1,
|
||||
},
|
||||
update: { value: { increment: 1 } },
|
||||
});
|
||||
|
||||
dataProduct.push({
|
||||
...item,
|
||||
code: `${item.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
productGroupId: productGroupId,
|
||||
});
|
||||
}
|
||||
}
|
||||
console.log("dataProduct", dataProduct);
|
||||
|
||||
return await prisma.product.createManyAndReturn({
|
||||
data: dataProduct,
|
||||
include: {
|
||||
createdBy: true,
|
||||
updatedBy: true,
|
||||
},
|
||||
});
|
||||
},
|
||||
{
|
||||
isolationLevel: Prisma.TransactionIsolationLevel.Serializable,
|
||||
},
|
||||
);
|
||||
|
||||
if (productGroup.status === "CREATED") {
|
||||
await prisma.productGroup.update({
|
||||
include: {
|
||||
createdBy: true,
|
||||
updatedBy: true,
|
||||
},
|
||||
where: { id: productGroupId },
|
||||
data: { status: Status.ACTIVE },
|
||||
});
|
||||
}
|
||||
|
||||
this.setStatus(HttpStatus.CREATED);
|
||||
|
||||
return record;
|
||||
}
|
||||
}
|
||||
|
||||
@Route("api/v1/product/{productId}")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue