fix: missing product code, buyPrice when create and edit and add (ราคาตัวแทน) for agentPrice product name
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 4s

This commit is contained in:
HAM 2025-09-19 15:35:14 +07:00
parent 95ee32fc57
commit a9201f715a
2 changed files with 47 additions and 26 deletions

View file

@ -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");

View file

@ -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