feat: sync data from data to flowAccount
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 5s

This commit is contained in:
HAM 2025-09-12 15:20:20 +07:00
parent 250bbca226
commit 61825309d1

View file

@ -30,6 +30,7 @@ import { deleteFile, deleteFolder, fileLocation, getFile, listFile, setFile } fr
import { isUsedError, notFoundError, relationError } from "../utils/error"; import { isUsedError, notFoundError, relationError } from "../utils/error";
import { queryOrNot, whereDateQuery } from "../utils/relation"; import { queryOrNot, whereDateQuery } from "../utils/relation";
import spreadsheet from "../utils/spreadsheet"; import spreadsheet from "../utils/spreadsheet";
import flowAccount from "../services/flowaccount";
const MANAGE_ROLES = [ const MANAGE_ROLES = [
"system", "system",
@ -299,6 +300,9 @@ export class ProductController extends Controller {
}, },
update: { value: { increment: 1 } }, update: { value: { increment: 1 } },
}); });
const listId = await flowAccount.createProducts(body);
return await prisma.product.create({ return await prisma.product.create({
include: { include: {
createdBy: true, createdBy: true,
@ -306,6 +310,8 @@ export class ProductController extends Controller {
}, },
data: { data: {
...body, ...body,
flowAccountProductIdAgentPrice: `${listId.data.productIdAgentPrice}`,
flowAccountProductIdSellPrice: `${listId.data.productIdSellPrice}`,
document: body.document document: body.document
? { ? {
createMany: { data: body.document.map((v) => ({ name: v })) }, createMany: { data: body.document.map((v) => ({ name: v })) },
@ -379,6 +385,19 @@ export class ProductController extends Controller {
await permissionCheck(req.user, productGroup.registeredBranch); await permissionCheck(req.user, productGroup.registeredBranch);
} }
if (
product.flowAccountProductIdSellPrice !== null &&
product.flowAccountProductIdAgentPrice !== null
) {
await flowAccount.editProducts(
product.flowAccountProductIdSellPrice,
product.flowAccountProductIdAgentPrice,
body,
);
} else {
throw notFoundError("FlowAccountProductId");
}
const record = await prisma.product.update({ const record = await prisma.product.update({
include: { include: {
productGroup: true, productGroup: true,
@ -441,6 +460,18 @@ export class ProductController extends Controller {
if (record.status !== Status.CREATED) throw isUsedError("Product"); if (record.status !== Status.CREATED) throw isUsedError("Product");
if (
record.flowAccountProductIdSellPrice !== null &&
record.flowAccountProductIdAgentPrice !== null
) {
await Promise.all([
flowAccount.deleteProduct(record.flowAccountProductIdSellPrice),
flowAccount.deleteProduct(record.flowAccountProductIdAgentPrice),
]);
} else {
throw notFoundError("FlowAccountProductId");
}
await deleteFolder(fileLocation.product.img(productId)); await deleteFolder(fileLocation.product.img(productId));
return await prisma.product.delete({ return await prisma.product.delete({