Fix updating sync variants

This commit is contained in:
Dominic Ferrando
2025-06-08 15:51:45 -04:00
parent 231f726d08
commit 4d09e843fe
3 changed files with 35 additions and 25 deletions
+7 -1
View File
@@ -16,6 +16,7 @@ const server = Bun.serve({
const payload: Printful.Webhook.EventPayload = await req.json() const payload: Printful.Webhook.EventPayload = await req.json()
console.log(JSON.stringify(payload)); console.log(JSON.stringify(payload));
console.log("PRINTFUL WEBHOOK: " + payload.type); console.log("PRINTFUL WEBHOOK: " + payload.type);
try {
switch (payload.type) { switch (payload.type) {
// ADD/UPDATE // ADD/UPDATE
case Printful.Webhook.Event.ProductUpdated: case Printful.Webhook.Event.ProductUpdated:
@@ -33,8 +34,13 @@ const server = Bun.serve({
case Printful.Webhook.Event.ProductDeleted: case Printful.Webhook.Event.ProductDeleted:
break; break;
} }
}
catch (error){
console.error(error);
Response.error();
}
return Response.json("Hello world") return Response.json("Hello world");
} }
} }
}, },
+2 -2
View File
@@ -1,5 +1,5 @@
export namespace Printful { export namespace Printful {
export const API_URL = "https://api.printful.com/store"; export const API_URL = "https://api.printful.com";
export namespace Webhook { export namespace Webhook {
// meta // meta
@@ -120,7 +120,7 @@ export namespace Printful {
export async function getSyncProduct(syncProductId: number): Promise<Products.SyncProduct> { export async function getSyncProduct(syncProductId: number): Promise<Products.SyncProduct> {
const payload: Products.SyncProduct = await ( const payload: Products.SyncProduct = await (
await fetch(`${API_URL}/products/${syncProductId}`, { await fetch(`${Printful.API_URL}/store/products/${syncProductId}`, {
method: "GET", method: "GET",
headers: { headers: {
"Authorization": `Bearer ${Bun.env.PRINTFUL_AUTH}`, "Authorization": `Bearer ${Bun.env.PRINTFUL_AUTH}`,
+11 -7
View File
@@ -79,7 +79,7 @@ export namespace Webflow {
const foundColors = Array.from(new Set(variantColors)); const foundColors = Array.from(new Set(variantColors));
const foundSizes = Array.from(new Set(variantSizes)); const foundSizes = Array.from(new Set(variantSizes));
let updateProductResponse = await fetch(`${API_URL}/products/${webflowProductId}`, { let updateProductResponse = await fetch(`${Webflow.API_URL}/products/${webflowProductId}`, {
method: "PATCH", method: "PATCH",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@@ -117,7 +117,7 @@ export namespace Webflow {
}); });
updateProductResponse = await updateProductResponse.json(); updateProductResponse = await updateProductResponse.json();
const getProductResponse = await fetch(`${API_URL}/products/${webflowProductId}`, { const getProductResponse = await fetch(`${Webflow.API_URL}/products/${webflowProductId}`, {
method: "GET", method: "GET",
headers: { headers: {
"Authorization": `bearer ${Bun.env.WEBFLOW_AUTH}` "Authorization": `bearer ${Bun.env.WEBFLOW_AUTH}`
@@ -128,7 +128,7 @@ export namespace Webflow {
for (let i = 0; i < webflowProduct["skus"].length; ++i) { for (let i = 0; i < webflowProduct["skus"].length; ++i) {
const webflowSkuId = variantExternalIds[i]; const webflowSkuId = variantExternalIds[i];
const res = await fetch(`${API_URL}/products/${webflowProductId}/skus/${webflowSkuId}`, { const res = await fetch(`${Webflow.API_URL}/products/${webflowProductId}/skus/${webflowSkuId}`, {
method: "PATCH", method: "PATCH",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@@ -170,7 +170,7 @@ export namespace Webflow {
const foundSizes = Array.from(new Set(printfulVariants.map(v => v.size))); const foundSizes = Array.from(new Set(printfulVariants.map(v => v.size)));
// CREATE WEBFLOW PRODUCT // CREATE WEBFLOW PRODUCT
let addProductResponse = await fetch(`${API_URL}/products`, { let addProductResponse = await fetch(`${Webflow.API_URL}/products`, {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@@ -207,12 +207,16 @@ export namespace Webflow {
}) })
}); });
addProductResponse = await addProductResponse.json(); addProductResponse = await addProductResponse.json();
if (!addProductResponse?.product?.id) {
console.error("Webflow product creation failed:", addProductResponse);
throw new Error("Failed to create Webflow product");
}
const webflowProductId = addProductResponse["product"]["id"]; const webflowProductId = addProductResponse["product"]["id"];
const printfulProductId = printfulProduct.result.sync_product.id; const printfulProductId = printfulProduct.result.sync_product.id;
// CREATE WEBFLOW PRODUCT SKUs // CREATE WEBFLOW PRODUCT SKUs
let createSkuResponse = await fetch(`${API_URL}/products/${webflowProductId}/skus`, { let createSkuResponse = await fetch(`${Webflow.API_URL}/products/${webflowProductId}/skus`, {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@@ -227,6 +231,7 @@ export namespace Webflow {
const responseSkus = [addProductResponse["sku"], ...createSkuResponse["skus"]]; const responseSkus = [addProductResponse["sku"], ...createSkuResponse["skus"]];
// SYNC WEBFLOW/PRINTFUL PRODUCT AND VARIANT IDs // SYNC WEBFLOW/PRINTFUL PRODUCT AND VARIANT IDs
console.log(`Hitting: ${Printful.API_URL}/store/products/${printfulProductId}`);
let modifyPrintfulProductResponse = await fetch(`${Printful.API_URL}/store/products/${printfulProductId}`, { let modifyPrintfulProductResponse = await fetch(`${Printful.API_URL}/store/products/${printfulProductId}`, {
method: "PUT", method: "PUT",
headers: { headers: {
@@ -240,12 +245,11 @@ export namespace Webflow {
}, },
"sync_variants": responseSkus.map((sku, i) => ({ "sync_variants": responseSkus.map((sku, i) => ({
"id": Number(printfulVariants[i].id), // printful variant id "id": Number(printfulVariants[i].id), // printful variant id
"external_id": String(sku["id"]), // webflow variant id "external_id": String(sku.id), // webflow variant id
})) }))
}) })
}); });
modifyPrintfulProductResponse = await modifyPrintfulProductResponse.json(); modifyPrintfulProductResponse = await modifyPrintfulProductResponse.json();
console.log(JSON.stringify(modifyPrintfulProductResponse));
// CACHE WEBFLOW/PRINTFUL PRODUCT ASSOCIATION // CACHE WEBFLOW/PRINTFUL PRODUCT ASSOCIATION
productRecords.add({ printfulProductId, webflowProductId }); productRecords.add({ printfulProductId, webflowProductId });