Refactor API client and stock management logic for improved clarity, error handling, and support for additional stock types.
This commit is contained in:
+17
-14
@@ -1,5 +1,9 @@
|
||||
import { apiRequest, getPath } from './client.js';
|
||||
|
||||
function unwrapEntryPayload(payload) {
|
||||
return payload?.data || payload?.entry || payload?.item || payload;
|
||||
}
|
||||
|
||||
export async function searchItemDefinitions(store, query) {
|
||||
if (query.trim().length <= 2) {
|
||||
return [];
|
||||
@@ -30,8 +34,10 @@ export async function listStockEntries(store, filters = {}) {
|
||||
}
|
||||
|
||||
export async function getStockEntry(store, stockId) {
|
||||
const payload = await apiRequest(store, `${getPath('stockEntries')}/${stockId}`);
|
||||
return payload?.data || payload?.entry || payload;
|
||||
const payload = await apiRequest(store, `${getPath('items')}/${stockId}`, {
|
||||
includeKitchen: false,
|
||||
});
|
||||
return unwrapEntryPayload(payload);
|
||||
}
|
||||
|
||||
export async function createStockEntry(store, body) {
|
||||
@@ -41,7 +47,7 @@ export async function createStockEntry(store, body) {
|
||||
includeKitchen: false,
|
||||
query: { label: 1 },
|
||||
});
|
||||
return payload?.data || payload?.entry || payload;
|
||||
return unwrapEntryPayload(payload);
|
||||
}
|
||||
|
||||
export async function updateStockItem(store, uuidB64, body) {
|
||||
@@ -50,7 +56,7 @@ export async function updateStockItem(store, uuidB64, body) {
|
||||
body,
|
||||
includeKitchen: false,
|
||||
});
|
||||
return payload?.data || payload?.entry || payload;
|
||||
return unwrapEntryPayload(payload);
|
||||
}
|
||||
|
||||
export async function deleteStockItem(store, uuidB64) {
|
||||
@@ -58,17 +64,14 @@ export async function deleteStockItem(store, uuidB64) {
|
||||
method: 'DELETE',
|
||||
includeKitchen: false,
|
||||
});
|
||||
return payload?.data || payload?.entry || payload;
|
||||
return unwrapEntryPayload(payload);
|
||||
}
|
||||
|
||||
export async function adjustStockEntry(store, stockId, body) {
|
||||
const payload = await apiRequest(
|
||||
store,
|
||||
`${getPath('stockEntries')}/${stockId}/adjust`,
|
||||
{
|
||||
method: 'POST',
|
||||
body,
|
||||
},
|
||||
);
|
||||
return payload?.data || payload?.entry || payload;
|
||||
const payload = await apiRequest(store, `${getPath('items')}/${stockId}/stock`, {
|
||||
method: 'POST',
|
||||
body,
|
||||
includeKitchen: false,
|
||||
});
|
||||
return unwrapEntryPayload(payload);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user