Product sync
This commit is contained in:
@@ -61,32 +61,60 @@ const server = Bun.serve({
|
||||
})
|
||||
}
|
||||
},
|
||||
"/products/sync": {
|
||||
OPTIONS() {
|
||||
return new ClientResponse(undefined, { status: 204 });
|
||||
},
|
||||
async POST(req) {
|
||||
const printfulProducts = await Printful.Products.getSyncProducts();
|
||||
|
||||
// handle update/create
|
||||
for (const printfulProduct of printfulProducts) {
|
||||
const productRecord = productRecords.findUsingPrintful(printfulProduct.sync_product.id);
|
||||
if (productRecord) {
|
||||
await Webflow.updateProductUsingPrintful(printfulProduct);
|
||||
}
|
||||
else {
|
||||
await Webflow.createProductUsingPrintful(printfulProduct);
|
||||
}
|
||||
}
|
||||
|
||||
// handle delete
|
||||
// for (const productRecord of productRecords.all()) {
|
||||
// const existsInPrintful = printfulProducts
|
||||
// .some(printfulProduct => printfulProduct.sync_product.id === productRecord.printfulProductId);
|
||||
// if (!existsInPrintful) {
|
||||
// await Webflow.deleteProductUsingPrintful(productRecord.printfulProductId);
|
||||
// }
|
||||
// }
|
||||
|
||||
return ClientResponse.json({});
|
||||
}
|
||||
},
|
||||
"/webhook/printful": {
|
||||
OPTIONS() {
|
||||
return new ClientResponse(undefined, { status: 204 });
|
||||
},
|
||||
async POST(req) {
|
||||
const payload: Printful.Webhook.EventPayload = await req.json()
|
||||
console.log(JSON.stringify(payload));
|
||||
console.log("PRINTFUL WEBHOOK: " + payload.type);
|
||||
try {
|
||||
switch (payload.type) {
|
||||
// ADD/UPDATE
|
||||
case Printful.Webhook.Event.ProductUpdated: {
|
||||
const printfulProduct = await Printful.getSyncProduct(payload.data.sync_product.id);
|
||||
const productRecord = productRecords.findFromPrintful(payload.data.sync_product.id);
|
||||
const printfulProduct = await Printful.Products.getSyncProduct(payload.data.sync_product.id);
|
||||
const productRecord = productRecords.findUsingPrintful(payload.data.sync_product.id);
|
||||
|
||||
if (productRecord) {
|
||||
await Webflow.updateProductFromPrintful(printfulProduct);
|
||||
await Webflow.updateProductUsingPrintful(printfulProduct);
|
||||
}
|
||||
else {
|
||||
await Webflow.createProductFromPrintful(printfulProduct);
|
||||
await Webflow.createProductUsingPrintful(printfulProduct);
|
||||
}
|
||||
break;
|
||||
}
|
||||
// DELETE
|
||||
case Printful.Webhook.Event.ProductDeleted: {
|
||||
await Webflow.deleteProductFromPrintful(payload.data.sync_product.id);
|
||||
await Webflow.deleteProductUsingPrintful(payload.data.sync_product.id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user