diff --git a/src/controllers/04-product-controller.ts b/src/controllers/04-product-controller.ts index b50462c..c1c40d8 100644 --- a/src/controllers/04-product-controller.ts +++ b/src/controllers/04-product-controller.ts @@ -30,6 +30,7 @@ import { deleteFile, deleteFolder, fileLocation, getFile, listFile, setFile } fr import { isUsedError, notFoundError, relationError } from "../utils/error"; import { queryOrNot, whereDateQuery } from "../utils/relation"; import spreadsheet from "../utils/spreadsheet"; +import flowAccount from "../services/flowaccount"; const MANAGE_ROLES = [ "system", @@ -299,6 +300,9 @@ export class ProductController extends Controller { }, update: { value: { increment: 1 } }, }); + + const listId = await flowAccount.createProducts(body); + return await prisma.product.create({ include: { createdBy: true, @@ -306,6 +310,8 @@ export class ProductController extends Controller { }, data: { ...body, + flowAccountProductIdAgentPrice: `${listId.data.productIdAgentPrice}`, + flowAccountProductIdSellPrice: `${listId.data.productIdSellPrice}`, document: body.document ? { createMany: { data: body.document.map((v) => ({ name: v })) }, @@ -379,6 +385,19 @@ export class ProductController extends Controller { 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({ include: { productGroup: true, @@ -441,6 +460,18 @@ export class ProductController extends Controller { 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)); return await prisma.product.delete({