dispatch testing

This commit is contained in:
2026-07-20 18:54:09 +05:30
parent 6b154997fb
commit 407574d62a
11 changed files with 663 additions and 518 deletions

View File

@@ -77,17 +77,52 @@ export async function getImportedCatalogueRefs(tenantid: number, brand?: string)
}
export async function importCatalogueProducts(items: ImportCatalogueProductRequest[]) {
const res = await fetch(`${API_BASE}/products/importcatalogueproduct`, {
try {
const res = await fetch(`${API_BASE}/products/importcatalogueproduct`, {
method: "POST",
headers: { "Content-Type": "application/json", "Accept": "application/json" },
body: JSON.stringify(items),
});
if (res.ok) {
const json = await res.json();
if (json && typeof json === 'object' && 'status' in json && json.status === false) {
console.warn("importcatalogueproduct backend notice, falling back to createproductlocation:", json.message);
return await fallbackCreateProductLocation(items);
}
return json;
}
} catch (err) {
console.warn("importcatalogueproduct error, falling back to createproductlocation:", err);
}
return await fallbackCreateProductLocation(items);
}
async function fallbackCreateProductLocation(items: ImportCatalogueProductRequest[]) {
const payloads = items.map(item => ({
tenantid: item.tenantid,
locationid: item.locationid,
productid: item.catalogueid,
quantity: item.quantity || 1,
stocktype: item.stocktype || "in",
status: item.status || "Active",
retailprice: item.retailprice || 0,
productcost: item.productcost || 0,
taxpercent: item.taxpercent || 0
}));
const res = await fetch(`${API_BASE}/products/createproductlocation`, {
method: "POST",
headers: { "Content-Type": "application/json", "Accept": "application/json" },
body: JSON.stringify(items),
body: JSON.stringify(payloads),
});
if (!res.ok) throw new Error("Failed to import products");
const json = await res.json();
if (json && typeof json === 'object' && 'status' in json && !json.status) {
throw new Error(json.message || "Failed to import products");
if (!res.ok) {
let errText = "";
try { errText = await res.text(); } catch (_) {}
throw new Error(errText || `Failed to add product to catalogue (${res.status})`);
}
return json;
return await res.json();
}
export async function removeFromStoreCatalogue(tenantid: number, locationid: number, productid: number) {

View File

@@ -941,14 +941,9 @@ export function useFiestaCreateProductLocation() {
return useMutation({
mutationFn: (input: CreateProductLocationInput) => createProductLocation(input),
onSuccess: (_, variables) => {
// Invalidate the store catalogue so it fetches the new item (prefix match)
qc.invalidateQueries({
queryKey: ['fiesta', 'productLocations']
});
// Also invalidate productStocks so the Admin Catalogue UI updates
qc.invalidateQueries({
queryKey: ['fiesta', 'productStocks']
});
qc.invalidateQueries({ queryKey: ['catalogue', 'imported'] });
qc.invalidateQueries({ queryKey: ['fiesta', 'productLocations'] });
qc.invalidateQueries({ queryKey: ['fiesta', 'productStocks'] });
},
onError: (error: any) => {
alert(`Error adding product: ${error.message}`);
@@ -961,9 +956,9 @@ export function useFiestaDeleteProductLocation() {
return useMutation({
mutationFn: (input: DeleteProductLocationInput) => deleteProductLocation(input),
onSuccess: (_, variables) => {
qc.invalidateQueries({
queryKey: ['fiesta', 'productLocations']
});
qc.invalidateQueries({ queryKey: ['catalogue', 'imported'] });
qc.invalidateQueries({ queryKey: ['fiesta', 'productLocations'] });
qc.invalidateQueries({ queryKey: ['fiesta', 'productStocks'] });
},
});
}