Improve products & orders list endpoints
This commit is contained in:
+26
-24
@@ -203,21 +203,21 @@ export const app = new Elysia()
|
||||
.group("/commerce", { auth: true }, (app) => app
|
||||
.group("/products", (app) => app
|
||||
.get("/", async ({ query }) => {
|
||||
const pProducts = await s.Commerce.Printful.Products.list({
|
||||
limit: query.limit,
|
||||
offset: query.offset,
|
||||
const [pProducts, wProducts] = await Promise.all([
|
||||
s.Commerce.Printful.Products.list({
|
||||
limit: query.limit,
|
||||
offset: query.offset,
|
||||
}),
|
||||
s.Commerce.Webflow.Products.list({ forceAll: true }),
|
||||
]);
|
||||
const wProductIds = new Set(wProducts.map((wProduct) => wProduct.product.id));
|
||||
return pProducts.map((pProduct) => {
|
||||
const wProductId = pProduct.external_id.split("-")[0];
|
||||
return { pProduct, isSynced: wProductId ? wProductIds.has(wProductId) : false };
|
||||
});
|
||||
const isSyncedFlags = await Promise.all(
|
||||
pProducts.map(async (pProduct) => {
|
||||
const wProductId = pProduct.external_id.split("-")[0];
|
||||
if (!wProductId) return false;
|
||||
return (await s.Commerce.Webflow.Products.get(wProductId)) !== undefined;
|
||||
})
|
||||
);
|
||||
return pProducts.map((pProduct, i) => ({ pProduct, isSynced: isSyncedFlags[i] }));
|
||||
}, {
|
||||
query: t.Object({
|
||||
limit: t.Optional(t.Numeric()),
|
||||
limit: t.Optional(t.Numeric({ maximum: 100 })),
|
||||
offset: t.Optional(t.Numeric()),
|
||||
})
|
||||
})
|
||||
@@ -258,21 +258,23 @@ export const app = new Elysia()
|
||||
)
|
||||
.group("/orders", (app) => app
|
||||
.get("/", async ({ query }) => {
|
||||
const wOrders = await s.Commerce.Webflow.Orders.list({
|
||||
status: query.status,
|
||||
limit: query.limit,
|
||||
offset: query.offset,
|
||||
});
|
||||
const isSyncedFlags = await Promise.all(
|
||||
wOrders.map(async (wOrder) =>
|
||||
(await s.Commerce.Printful.Orders.get(`@${wOrder.orderId}`)) !== undefined
|
||||
)
|
||||
);
|
||||
return wOrders.map((wOrder, i) => ({ wOrder, isSynced: isSyncedFlags[i] }));
|
||||
const [wOrders, pOrders] = await Promise.all([
|
||||
s.Commerce.Webflow.Orders.list({
|
||||
status: query.status,
|
||||
limit: query.limit,
|
||||
offset: query.offset,
|
||||
}),
|
||||
s.Commerce.Printful.Orders.list({ forceAll: true }),
|
||||
]);
|
||||
const pOrderExternalIds = new Set(pOrders.map((pOrder) => pOrder.external_id));
|
||||
return wOrders.map((wOrder) => ({
|
||||
wOrder,
|
||||
isSynced: pOrderExternalIds.has(wOrder.orderId),
|
||||
}));
|
||||
}, {
|
||||
query: t.Object({
|
||||
status: t.Optional(WOrderStatusSchema),
|
||||
limit: t.Optional(t.Numeric()),
|
||||
limit: t.Optional(t.Numeric({ maximum: 100 })),
|
||||
offset: t.Optional(t.Numeric()),
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user