u
This commit is contained in:
@@ -1,26 +1,69 @@
|
||||
import { getStore } from './store.js';
|
||||
|
||||
async function enrichSingleOrder(store, googleAuthId, order) {
|
||||
if (!order) return order;
|
||||
const [shippings, payments] = await Promise.all([
|
||||
store.shipping.listByManifestId(googleAuthId, order.id).catch(() => []),
|
||||
store.payment.listByManifestId(googleAuthId, order.id).catch(() => []),
|
||||
]);
|
||||
|
||||
const isPaid = Array.isArray(payments) && payments.length > 0;
|
||||
const isDelivered = Array.isArray(shippings) && shippings.some((s) => s.state === 'shipped' || s.state === 'delivered');
|
||||
|
||||
return {
|
||||
...order,
|
||||
shipping: shippings[0] ?? null,
|
||||
shippings: shippings ?? [],
|
||||
payment: payments[0] ?? null,
|
||||
payments: payments ?? [],
|
||||
paid: isPaid,
|
||||
delivered: isDelivered,
|
||||
};
|
||||
}
|
||||
|
||||
async function enrichOrderResult(store, googleAuthId, result) {
|
||||
if (!result) return result;
|
||||
if (Array.isArray(result)) {
|
||||
return Promise.all(result.map((o) => enrichSingleOrder(store, googleAuthId, o)));
|
||||
}
|
||||
if (Array.isArray(result.orders)) {
|
||||
const enrichedOrders = await Promise.all(
|
||||
result.orders.map((o) => enrichSingleOrder(store, googleAuthId, o))
|
||||
);
|
||||
return {
|
||||
...result,
|
||||
orders: enrichedOrders,
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function list(googleAuthId, opts) {
|
||||
const store = await getStore();
|
||||
return store.order.list(googleAuthId, opts);
|
||||
const result = await store.order.list(googleAuthId, opts);
|
||||
return enrichOrderResult(store, googleAuthId, result);
|
||||
}
|
||||
|
||||
export async function getById(googleAuthId, id) {
|
||||
const store = await getStore();
|
||||
return store.order.getById(googleAuthId, id);
|
||||
const order = await store.order.getById(googleAuthId, id);
|
||||
return enrichSingleOrder(store, googleAuthId, order);
|
||||
}
|
||||
|
||||
export async function create(googleAuthId, data) {
|
||||
const store = await getStore();
|
||||
return store.order.create(googleAuthId, data);
|
||||
const order = await store.order.create(googleAuthId, data);
|
||||
return enrichSingleOrder(store, googleAuthId, order);
|
||||
}
|
||||
|
||||
export async function update(googleAuthId, id, data) {
|
||||
const store = await getStore();
|
||||
return store.order.update(googleAuthId, id, data);
|
||||
const order = await store.order.update(googleAuthId, id, data);
|
||||
return enrichSingleOrder(store, googleAuthId, order);
|
||||
}
|
||||
|
||||
export async function remove(googleAuthId, id) {
|
||||
const store = await getStore();
|
||||
return store.order.delete(googleAuthId, id);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user