From aa9ba61a9a42e31fa3c58dfc45f18e1947c2cc26 Mon Sep 17 00:00:00 2001 From: Dominic Ferrando Date: Fri, 26 Jun 2026 16:36:01 -0400 Subject: [PATCH] More cleanup --- apps/api/src/scripts/register-webhooks.ts | 2 +- apps/api/src/server.ts | 11 +++-- packages/commerce/src/sync.ts | 51 ++++++++++------------- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/apps/api/src/scripts/register-webhooks.ts b/apps/api/src/scripts/register-webhooks.ts index bacbba1..bd51dad 100644 --- a/apps/api/src/scripts/register-webhooks.ts +++ b/apps/api/src/scripts/register-webhooks.ts @@ -1,7 +1,7 @@ import { Printful, PrintfulClient, PrintfulError, Webflow, WebflowClient, WebflowError } from "@blade-and-brawn/commerce"; import { env } from "../env"; -const BASE_URL = "https://dev.api.bladeandbrawn.com"; +const BASE_URL = "http://dev.api.bladeandbrawn.com"; const PRINTFUL_WEBHOOK_URL = `${BASE_URL}/webhook/printful`; const WEBFLOW_WEBHOOK_URL = `${BASE_URL}/webhook/webflow`; diff --git a/apps/api/src/server.ts b/apps/api/src/server.ts index 410e3bb..9f69deb 100644 --- a/apps/api/src/server.ts +++ b/apps/api/src/server.ts @@ -100,11 +100,16 @@ export const app = new Elysia() } }) - .onAfterHandle(({ request, set }) => { + .onAfterResponse(({ request, status, path }) => { + const skip: Record = { + "/products/sync/": ["GET"] + }; + if (env.NODE_ENV === "development" && skip[path]?.includes(request.method)) return; + log.info({ method: request.method, - path: new URL(request.url).pathname, - status: set.status ?? 200 + path, + status }, "request"); }) diff --git a/packages/commerce/src/sync.ts b/packages/commerce/src/sync.ts index 6bad9e2..0334ce7 100644 --- a/packages/commerce/src/sync.ts +++ b/packages/commerce/src/sync.ts @@ -33,44 +33,37 @@ export class ProductSyncer { } async sync(printfulProductId?: number): Promise { + this.log.info("attempting sync run"); const canRun = await this.run(); if (!canRun) return false; try { const syncStart = Date.now(); - if (printfulProductId) this.log.info({ printfulProductId }, "sync started"); - else this.log.info("sync started"); + if (printfulProductId) this.log.info({ printfulProductId }, "sync run started"); + else this.log.info("sync run started"); this.log.debug("populating webflow products"); const webflowProducts: Webflow.Products.ProductAndSkus[] = await this.webflow.Products.list({ forceAll: true }); this.log.debug("populating printful products"); let printfulProducts = await this.printful.Products.list({ forceAll: true }); - if (printfulProductId) { - const printfulProduct = printfulProducts.find((p) => p.id === printfulProductId); - if (printfulProduct) { - const metaProductName = this.getMainProductName( - printfulProduct.name, - ); - printfulProducts = printfulProducts.filter((p) => - p.name.includes(metaProductName), - ); - } + // optionally filter to sync only the given printful product + const printfulProductFilter = printfulProducts.find((p) => p.id === printfulProductId); + if (printfulProductFilter) { + const metaPrintfulProductName = this.getMetaPrintfulProductName(printfulProductFilter.name); + printfulProducts = printfulProducts.filter((p) => p.name.includes(metaPrintfulProductName)); } this.log.info({ count: printfulProducts.length }, "generating meta printful products"); const metaPrintfulProducts: MetaPrintfulProduct[] = []; for (const printfulProduct of printfulProducts) { - const mainProductName = this.getMainProductName( - printfulProduct.name, - ); + const metaPrintfulProductName = this.getMetaPrintfulProductName(printfulProduct.name); - let metaPrintfulProduct = metaPrintfulProducts.find((mp) => - mp.name.includes(mainProductName), - ); + let metaPrintfulProduct = metaPrintfulProducts.find((mp) => mp.name.includes(metaPrintfulProductName)); if (!metaPrintfulProduct) { + this.log.debug({ metaPrintfulProductName }, "appending meta printful product"); metaPrintfulProduct = { - name: mainProductName, + name: metaPrintfulProductName, variants: [], externalId: printfulProduct.external_id, colors: new Set(), @@ -78,9 +71,8 @@ export class ProductSyncer { metaPrintfulProducts.push(metaPrintfulProduct); } - const productColor = this.findColorInProductName( - printfulProduct.name, - ); + const productColor = this.findColorInProductName(printfulProduct.name); + this.log.debug({ productColor }, "appending meta printful product variant"); metaPrintfulProduct.variants.push({ color: productColor, product: (await this.printful.Products.get(printfulProduct.id))!, @@ -103,10 +95,10 @@ export class ProductSyncer { const webflowSkus: DeepPartial[] = []; - for (const specialVariant of metaPrintfulProduct.variants) { - for (const printfulVariant of specialVariant.product.sync_variants) { + for (const metaVariant of metaPrintfulProduct.variants) { + for (const printfulVariant of metaVariant.product.sync_variants) { this.log.debug( - { size: printfulVariant.size, color: printfulVariant.color }, + { size: printfulVariant.size, color: metaVariant.color }, "generating webflow SKU from printful variant" ); @@ -127,7 +119,7 @@ export class ProductSyncer { name: printfulVariant.name, slug: formatSlug(printfulVariant.name), "sku-values": { - color: specialVariant.color, + color: metaVariant.color, size: printfulVariant.size, }, price: { @@ -304,13 +296,12 @@ export class ProductSyncer { return m?.[1] ? m[1] : "N/A"; } - getMainProductName(productName: string): string { + getMetaPrintfulProductName(productName: string): string { const colorInName = this.findColorInProductName(productName); - if (colorInName) { + if (colorInName) return productName.replace(`[${colorInName}]`, "").trimEnd(); - } else { + else return productName; - } } async isRunning(): Promise {