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("/commerce", { auth: true }, (app) => app
|
||||||
.group("/products", (app) => app
|
.group("/products", (app) => app
|
||||||
.get("/", async ({ query }) => {
|
.get("/", async ({ query }) => {
|
||||||
const pProducts = await s.Commerce.Printful.Products.list({
|
const [pProducts, wProducts] = await Promise.all([
|
||||||
limit: query.limit,
|
s.Commerce.Printful.Products.list({
|
||||||
offset: query.offset,
|
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({
|
query: t.Object({
|
||||||
limit: t.Optional(t.Numeric()),
|
limit: t.Optional(t.Numeric({ maximum: 100 })),
|
||||||
offset: t.Optional(t.Numeric()),
|
offset: t.Optional(t.Numeric()),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -258,21 +258,23 @@ export const app = new Elysia()
|
|||||||
)
|
)
|
||||||
.group("/orders", (app) => app
|
.group("/orders", (app) => app
|
||||||
.get("/", async ({ query }) => {
|
.get("/", async ({ query }) => {
|
||||||
const wOrders = await s.Commerce.Webflow.Orders.list({
|
const [wOrders, pOrders] = await Promise.all([
|
||||||
status: query.status,
|
s.Commerce.Webflow.Orders.list({
|
||||||
limit: query.limit,
|
status: query.status,
|
||||||
offset: query.offset,
|
limit: query.limit,
|
||||||
});
|
offset: query.offset,
|
||||||
const isSyncedFlags = await Promise.all(
|
}),
|
||||||
wOrders.map(async (wOrder) =>
|
s.Commerce.Printful.Orders.list({ forceAll: true }),
|
||||||
(await s.Commerce.Printful.Orders.get(`@${wOrder.orderId}`)) !== undefined
|
]);
|
||||||
)
|
const pOrderExternalIds = new Set(pOrders.map((pOrder) => pOrder.external_id));
|
||||||
);
|
return wOrders.map((wOrder) => ({
|
||||||
return wOrders.map((wOrder, i) => ({ wOrder, isSynced: isSyncedFlags[i] }));
|
wOrder,
|
||||||
|
isSynced: pOrderExternalIds.has(wOrder.orderId),
|
||||||
|
}));
|
||||||
}, {
|
}, {
|
||||||
query: t.Object({
|
query: t.Object({
|
||||||
status: t.Optional(WOrderStatusSchema),
|
status: t.Optional(WOrderStatusSchema),
|
||||||
limit: t.Optional(t.Numeric()),
|
limit: t.Optional(t.Numeric({ maximum: 100 })),
|
||||||
offset: t.Optional(t.Numeric()),
|
offset: t.Optional(t.Numeric()),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user