From a9201f715adecac609f09aec195b0f4506b450d4 Mon Sep 17 00:00:00 2001 From: HAM Date: Fri, 19 Sep 2025 15:35:14 +0700 Subject: [PATCH] =?UTF-8?q?fix:=20missing=20product=20code,=20buyPrice=20w?= =?UTF-8?q?hen=20create=20and=20edit=20and=20add=20(=E0=B8=A3=E0=B8=B2?= =?UTF-8?q?=E0=B8=84=E0=B8=B2=E0=B8=95=E0=B8=B1=E0=B8=A7=E0=B9=81=E0=B8=97?= =?UTF-8?q?=E0=B8=99)=20for=20agentPrice=20product=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/04-product-controller.ts | 19 ++++++++- src/services/flowaccount.ts | 54 +++++++++++++----------- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/controllers/04-product-controller.ts b/src/controllers/04-product-controller.ts index d58aca3..512fff8 100644 --- a/src/controllers/04-product-controller.ts +++ b/src/controllers/04-product-controller.ts @@ -78,6 +78,7 @@ type ProductCreate = { type ProductUpdate = { status?: "ACTIVE" | "INACTIVE"; + code?: string; name?: string; detail?: string; process?: number; @@ -302,7 +303,10 @@ export class ProductController extends Controller { update: { value: { increment: 1 } }, }); - const listId = await flowAccount.createProducts(body); + const listId = await flowAccount.createProducts( + `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`, + body, + ); return await tx.product.create({ include: { @@ -390,10 +394,21 @@ export class ProductController extends Controller { product.flowAccountProductIdSellPrice !== null && product.flowAccountProductIdAgentPrice !== null ) { + const mergedBody = { + ...body, + code: body.code ?? product.code, + price: body.price ?? product.price, + agentPrice: body.agentPrice ?? product.agentPrice, + serviceCharge: body.serviceCharge ?? product.serviceCharge, + vatIncluded: body.vatIncluded ?? product.vatIncluded, + agentPriceVatIncluded: body.agentPriceVatIncluded ?? product.agentPriceVatIncluded, + serviceChargeVatIncluded: body.serviceChargeVatIncluded ?? product.serviceChargeVatIncluded, + }; + await flowAccount.editProducts( product.flowAccountProductIdSellPrice, product.flowAccountProductIdAgentPrice, - body, + mergedBody, ); } else { throw notFoundError("FlowAccountProductId"); diff --git a/src/services/flowaccount.ts b/src/services/flowaccount.ts index 2b4b706..6f131fe 100644 --- a/src/services/flowaccount.ts +++ b/src/services/flowaccount.ts @@ -455,19 +455,22 @@ const flowAccount = { }, // flowAccount POST create Product - async createProducts(body: JsonObject) { + async createProducts(code: String, body: JsonObject) { const { token } = await flowAccountAPI.auth(); const commonBody = { productStructureType: null, - type: "3", + type: 3, + code: `${code}`, name: body.name, sellDescription: body.detail, sellVatType: 3, - unitName: "Unit", + buyPrice: body.serviceCharge, + buyVatType: body.serviceChargeVatIncluded ? 1 : 3, + buyDescription: body.detail, }; - const createProduct = async (price: any, vatIncluded: boolean) => { + const createProduct = async (name: string, price: any, vatIncluded: boolean) => { try { const res = await fetch(api + "/products", { method: "POST", @@ -477,13 +480,14 @@ const flowAccount = { }, body: JSON.stringify({ ...commonBody, + name: name, sellPrice: price, sellVatType: vatIncluded ? 1 : 3, }), }); if (!res.ok) { - throw new Error(`Request failed with status ${res.status}`); + throw new Error(`Request failed with status ${res.status} ${res}`); } let json: any = null; @@ -501,8 +505,12 @@ const flowAccount = { }; const [sellId, agentId] = await Promise.all([ - createProduct(body.price, /true/.test(`${body.vatIncluded}`)), - createProduct(body.agentPrice, /true/.test(`${body.agentPriceVatIncluded}`)), + createProduct(`${body.name}`, body.price, /true/.test(`${body.vatIncluded}`)), + createProduct( + `${body.name} (ราคาตัวแทน)`, + body.agentPrice, + /true/.test(`${body.agentPriceVatIncluded}`), + ), ]); return { @@ -521,15 +529,16 @@ const flowAccount = { const commonBody = { productStructureType: null, - type: "3", + type: 3, name: body.name, sellDescription: body.detail, sellVatType: 3, - unitName: "Unit", - categoryName: "Car", + buyPrice: body.serviceCharge, + buyVatType: body.serviceChargeVatIncluded ? 1 : 3, + buyDescription: body.detail, }; - const editProduct = async (id: String, price: any, vatIncluded: boolean) => { + const editProduct = async (id: String, name: String, price: any, vatIncluded: boolean) => { try { const res = await fetch(api + `/products/${id}`, { method: "PUT", @@ -539,13 +548,14 @@ const flowAccount = { }, body: JSON.stringify({ ...commonBody, + name: name, sellPrice: price, sellVatType: vatIncluded ? 1 : 3, }), }); if (!res.ok) { - throw new Error(`Request failed with status ${res.status}`); + throw new Error(`Request failed with status ${res.status} ${res}`); } let json: any = null; @@ -562,19 +572,15 @@ const flowAccount = { } }; - const [sellId, agentId] = await Promise.all([ - editProduct(sellPriceId, body.price, /true/.test(`${body.vatIncluded}`)), - editProduct(agentPriceId, body.agentPrice, /true/.test(`${body.agentPriceVatIncluded}`)), + await Promise.all([ + editProduct(sellPriceId, `${body.name}`, body.price, /true/.test(`${body.vatIncluded}`)), + editProduct( + agentPriceId, + `${body.name} (ราคาตัวแทน)`, + body.agentPrice, + /true/.test(`${body.agentPriceVatIncluded}`), + ), ]); - - return { - ok: !!(agentId && sellId), - status: agentId && sellId ? 200 : 500, - data: { - productIdSellPrice: sellId, - productIdAgentPrice: agentId, - }, - }; }, // flowAccount DELETE Product