More refactoring of sync logic

This commit is contained in:
Dominic Ferrando
2026-06-26 22:18:41 -04:00
parent c772944a44
commit ae623b9c09
2 changed files with 196 additions and 240 deletions
+1 -1
View File
@@ -54,7 +54,7 @@ for (const wProduct of wProducts) {
// validateEquality(sku.fieldData.name, pVariant.name), // validateEquality(sku.fieldData.name, pVariant.name),
validateEquality( validateEquality(
sku.fieldData["sku-values"]?.["color"], sku.fieldData["sku-values"]?.["color"],
syncer.findColorInProductName(pVariant.name), syncer.parseColorGroup(pVariant.name),
), ),
validateEquality( validateEquality(
sku.fieldData["sku-values"]?.["size"], sku.fieldData["sku-values"]?.["size"],
+178 -222
View File
@@ -7,21 +7,25 @@ import pino from "pino";
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
type MetaPrintfulProduct = { type ProductEntry = {
name: string;
externalId: string;
isColorGrouped: boolean;
colorGroups: Set<string>;
variants: {
colorGroup: string | null; colorGroup: string | null;
product: Printful.Products.Product; product: Printful.Products.Product;
}[]; };
type MetaPrintfulProduct = {
name: string;
webflowProductId: string;
entries: ProductEntry[];
}; };
type SyncState = { type SyncState = {
printfulProductIds: number[]; printfulProductIds: number[];
}; };
type SyncOptions = {
printfulProductIdsFilter?: number[];
};
export class ProductSyncer { export class ProductSyncer {
private log: pino.Logger; private log: pino.Logger;
@@ -33,261 +37,164 @@ export class ProductSyncer {
this.log = (log ?? pino({ level: "silent" })).child({ component: "ProductSyncer" }); this.log = (log ?? pino({ level: "silent" })).child({ component: "ProductSyncer" });
} }
async sync(printfulProductId?: number): Promise<boolean> { async sync(opt: SyncOptions = {}): Promise<boolean> {
this.log.info("attempting sync run"); this.log.info("attempting sync run");
const canRun = await this.run(); const canRun = await this.run();
if (!canRun) return false; if (!canRun) return false;
try { try {
const syncStart = Date.now(); const syncStart = Date.now();
if (printfulProductId) this.log.info({ printfulProductId }, "sync run started"); if (opt.printfulProductIdsFilter)
else this.log.info("sync run started"); this.log.info({ printfulProductIds: opt.printfulProductIdsFilter }, "sync started (filtered)");
else
this.log.info("sync started (all)");
this.log.debug("populating webflow products"); this.log.debug("populating webflow products");
const webflowProducts: Webflow.Products.ProductAndSkus[] = await this.webflow.Products.list({ forceAll: true }); const webflowProducts: Webflow.Products.ProductAndSkus[] = await this.webflow.Products.list({ forceAll: true });
this.log.debug("populating printful products"); this.log.debug("populating printful products");
let printfulProducts = await this.printful.Products.list({ forceAll: true }); const printfulProducts = await this.printful.Products.list({ forceAll: true });
// optionally filter to sync only the given printful product
if (printfulProductId) {
const printfulProductFilter = printfulProducts.find((p) => p.id === printfulProductId);
if (!printfulProductFilter) {
this.log.warn({ printfulProductId }, "printful product not found, skipping sync");
return true;
}
const metaPrintfulProductName = this.getMetaPrintfulProductName(printfulProductFilter.name);
printfulProducts = printfulProducts.filter((p) =>
this.getMetaPrintfulProductName(p.name) === metaPrintfulProductName
);
}
this.log.info({ count: printfulProducts.length }, "generating meta printful products"); this.log.info({ count: printfulProducts.length }, "generating meta printful products");
const metaPrintfulProducts = await this.generateMetaPrintfulProducts(printfulProducts); const metaPrintfulProducts = await this.generateMetaPrintfulProducts(printfulProducts, opt.printfulProductIdsFilter);
for (const metaPrintfulProduct of metaPrintfulProducts) { for (const metaPrintfulProduct of metaPrintfulProducts) {
this.log.info({ name: metaPrintfulProduct.name, externalId: metaPrintfulProduct.externalId }, "syncing meta printful product"); this.log.info({ name: metaPrintfulProduct.name, externalId: metaPrintfulProduct.webflowProductId }, "syncing meta printful product");
await this.setState({ await this.setState({
printfulProductIds: metaPrintfulProduct.variants.map( printfulProductIds: metaPrintfulProduct.entries.map((e) => e.product.sync_product.id),
(v) => v.product.sync_product.id,
),
}); });
// For color-grouped products, colors are the bracket-extracted values. const newWebflowSkus: DeepPartial<Webflow.Products.Skus.Sku>[] = this.generateWebflowSkus(metaPrintfulProduct);
// For normal products, colors are collected from the variant data below. const foundColors = new Set<string>(
const foundColors: Set<string> = metaPrintfulProduct.isColorGrouped newWebflowSkus.map((sku) => sku.fieldData?.["sku-values"]?.["color"]).filter((v) => v !== undefined)
? metaPrintfulProduct.colorGroups
: new Set();
const foundSizes: Set<string> = new Set();
const webflowSkus: DeepPartial<Webflow.Products.Skus.Sku>[] = [];
for (const metaVariant of metaPrintfulProduct.variants) {
for (const printfulVariant of metaVariant.product.sync_variants) {
let skuColor: string;
if (metaPrintfulProduct.isColorGrouped) {
// Only include sizes present across every color variant.
const sizeInAll = metaPrintfulProduct.variants.every((v) =>
v.product.sync_variants.some((sv) => sv.size === printfulVariant.size)
); );
if (!sizeInAll) continue; const foundSizes = new Set<string>(
skuColor = metaVariant.colorGroup!; newWebflowSkus.map((sku) => sku.fieldData?.["sku-values"]?.["size"]).filter((v) => v !== undefined)
}
else {
skuColor = printfulVariant.color;
foundColors.add(skuColor);
}
this.log.debug(
{ size: printfulVariant.size, color: skuColor, retailPrice: printfulVariant.retail_price },
"generating webflow SKU from printful variant"
); );
foundSizes.add(printfulVariant.size); if (!newWebflowSkus[0]) throw new Error("no webflow SKUs generated — all variants were filtered out");
webflowSkus.push({ const webflowProductFieldData = {
id: printfulVariant.external_id, name: metaPrintfulProduct.name,
fieldData: { slug: formatSlug(metaPrintfulProduct.name),
name: printfulVariant.name, shippable: true,
slug: formatSlug(printfulVariant.name), "tax-category": "standard-taxable",
"sku-values": { "sku-properties": [
color: skuColor, {
size: printfulVariant.size, id: "color",
name: "Color",
enum: Array.from(foundColors).map((color) => ({
id: color,
slug: formatSlug(color),
name: color,
})),
}, },
price: { {
value: Math.floor( id: "size",
+printfulVariant.retail_price * 100, name: "Size",
), enum: Array.from(foundSizes).map((size) => ({
unit: printfulVariant.currency, id: size,
currency: printfulVariant.currency, slug: formatSlug(size),
name: size,
})),
}, },
"main-image": PrintfulClient.Util.getVariantMainImage( ],
printfulVariant, };
),
},
});
}
}
// SYNC PRODUCT DATA const webflowProduct = webflowProducts.find(
let existingWebflowProduct = webflowProducts.find( (wp) => wp.product.id === metaPrintfulProduct.webflowProductId,
(webflowProduct) => webflowProduct.product.id === metaPrintfulProduct.externalId.split("-")[0],
); );
if (existingWebflowProduct) {
// SYNC PRODUCT UPDATE if (webflowProduct) {
this.log.info("existing webflow product found, updating it"); this.log.info("existing webflow product found, updating it");
// do not update images // do not update images
for (const webflowSku of webflowSkus) for (const sku of newWebflowSkus) delete sku.fieldData?.["main-image"];
delete webflowSku.fieldData?.["main-image"];
const webflowProductId = metaPrintfulProduct.externalId.split("-")[0];
if (!webflowProductId) throw new Error("Malformed printful product ID");
if (!webflowSkus[0]) throw new Error("no webflow SKUs generated — all variants were filtered out");
const firstExistingWebflowSku = this.resolveWebflowSku( const firstExistingWebflowSku = this.resolveWebflowSku(
existingWebflowProduct.skus, webflowProduct.skus,
webflowSkus[0].fieldData?.["sku-values"]?.["color"], newWebflowSkus[0].fieldData?.["sku-values"]?.["color"],
webflowSkus[0].fieldData?.["sku-values"]?.["size"], newWebflowSkus[0].fieldData?.["sku-values"]?.["size"],
webflowSkus[0].id, newWebflowSkus[0].id,
); );
await this.webflow.Products.update(webflowProductId, {
product: { if (firstExistingWebflowSku) {
fieldData: { newWebflowSkus[0].id = firstExistingWebflowSku.id;
name: metaPrintfulProduct.name, }
slug: formatSlug(metaPrintfulProduct.name),
shippable: true, await this.webflow.Products.update(webflowProduct.product.id, {
"tax-category": "standard-taxable", product: { fieldData: webflowProductFieldData },
"sku-properties": [ sku: newWebflowSkus[0],
{
id: "color",
name: "Color",
enum: Array.from(foundColors).map((color) => ({
id: color,
slug: formatSlug(color),
name: color,
})),
},
{
id: "size",
name: "Size",
enum: Array.from(foundSizes).map((size) => ({
id: size,
slug: formatSlug(size),
name: size,
})),
},
],
},
},
sku: firstExistingWebflowSku
? { ...webflowSkus[0], id: firstExistingWebflowSku.id }
: webflowSkus[0],
}); });
for (const webflowSku of webflowSkus) { for (const newWebflowSku of newWebflowSkus) {
const existingWebflowSku = this.resolveWebflowSku( const existingWebflowSku = this.resolveWebflowSku(
existingWebflowProduct.skus, webflowProduct.skus,
webflowSku.fieldData?.["sku-values"]?.["color"], newWebflowSku.fieldData?.["sku-values"]?.["color"],
webflowSku.fieldData?.["sku-values"]?.["size"], newWebflowSku.fieldData?.["sku-values"]?.["size"],
webflowSku.id, newWebflowSku.id,
); );
if (existingWebflowSku) { if (existingWebflowSku) {
await this.webflow.Products.Skus.update( await this.webflow.Products.Skus.update(
webflowProductId, metaPrintfulProduct.webflowProductId,
existingWebflowSku.id, existingWebflowSku.id,
webflowSku, newWebflowSku,
); );
} else { } else {
await this.webflow.Products.Skus.create( await this.webflow.Products.Skus.create(
webflowProductId, metaPrintfulProduct.webflowProductId,
[webflowSku], [newWebflowSku],
); );
} }
} }
} else { } else {
// SYNC PRODUCT CREATE
this.log.info("existing webflow product not found, creating it"); this.log.info("existing webflow product not found, creating it");
if (!webflowSkus[0]) throw new Error("no webflow SKUs generated — all variants were filtered out");
const webflowProductId = await this.webflow.Products.create({ const webflowProductId = await this.webflow.Products.create({
product: { product: { fieldData: webflowProductFieldData },
fieldData: { sku: newWebflowSkus[0],
name: metaPrintfulProduct.name,
slug: formatSlug(metaPrintfulProduct.name),
shippable: true,
"tax-category": "standard-taxable",
"sku-properties": [
{
id: "color",
name: "Color",
enum: Array.from(foundColors).map((color) => ({
id: color,
slug: formatSlug(color),
name: color,
})),
},
{
id: "size",
name: "Size",
enum: Array.from(foundSizes).map((size) => ({
id: size,
slug: formatSlug(size),
name: size,
})),
},
],
},
},
sku: webflowSkus[0],
}); });
metaPrintfulProduct.webflowProductId = webflowProductId;
// create webflow product SKUs if (newWebflowSkus.length > 1)
if (webflowSkus.length > 1) await this.webflow.Products.Skus.create(webflowProductId, newWebflowSkus.slice(1));
await this.webflow.Products.Skus.create(webflowProductId, webflowSkus.slice(1)); }
existingWebflowProduct = await this.webflow.Products.get(webflowProductId); // Re-fetch after SKU mutations so newly created SKUs have their real IDs.
if (!existingWebflowProduct) const freshWebflowProduct = await this.webflow.Products.get(metaPrintfulProduct.webflowProductId);
throw new Error("webflow product missing after create"); if (!freshWebflowProduct) throw new Error("webflow product missing");
for (const { colorGroup, product: printfulProduct } of metaPrintfulProduct.entries) {
const expectedExternalId = colorGroup
? `${metaPrintfulProduct.webflowProductId}-${colorGroup}`
: metaPrintfulProduct.webflowProductId;
if (printfulProduct.sync_product.external_id === expectedExternalId) continue;
for (const metaVariant of metaPrintfulProduct.variants) {
const newPrintfulVariants: DeepPartial<Printful.Products.SyncVariant>[] = []; const newPrintfulVariants: DeepPartial<Printful.Products.SyncVariant>[] = [];
for (const printfulVariant of metaVariant.product.sync_variants) { for (const printfulVariant of printfulProduct.sync_variants) {
// For color-grouped products, the webflow SKU color is the bracket color. const webflowSku = this.resolveWebflowSku(
// For normal products, the webflow SKU color is the variant's own color. freshWebflowProduct.skus,
const skuColor = metaPrintfulProduct.isColorGrouped colorGroup ?? printfulVariant.color,
? metaVariant.colorGroup!
: printfulVariant.color;
const associatedWebflowSku = this.resolveWebflowSku(
existingWebflowProduct.skus,
skuColor,
printfulVariant.size, printfulVariant.size,
); );
if (associatedWebflowSku) { if (webflowSku) {
newPrintfulVariants.push({ newPrintfulVariants.push({
id: printfulVariant.id, id: printfulVariant.id,
external_id: String(associatedWebflowSku.id), external_id: String(webflowSku.id),
}); });
} }
} }
await sleep(10000); await sleep(10000);
this.log.info({ printfulProduct: metaVariant.product.sync_product.id }, "updating printful product"); this.log.info({ printfulProduct: printfulProduct.sync_product.id }, "updating printful product");
await this.printful.Products.update( await this.printful.Products.update(printfulProduct.sync_product.id, {
metaVariant.product.sync_product.id,
{
sync_product: { sync_product: {
id: metaVariant.product.sync_product.id, id: printfulProduct.sync_product.id,
external_id: metaPrintfulProduct.isColorGrouped external_id: expectedExternalId,
? `${webflowProductId}-${metaVariant.colorGroup}`
: webflowProductId,
}, },
sync_variants: newPrintfulVariants, sync_variants: newPrintfulVariants,
}, });
);
}
} }
} }
@@ -303,48 +210,97 @@ export class ProductSyncer {
return true; return true;
} }
findColorInProductName(productName: string): string | null { parseColorGroup(name: string): string | null {
const m = productName.match(/\[([^\]]+)\]/); const m = name.match(/\[([^\]]+)\]/);
return m?.[1] ?? null; return m?.[1] ?? null;
} }
async generateMetaPrintfulProducts(printfulProducts: Printful.Products.SyncProduct[]) { private generateWebflowSkus(metaPrintfulProduct: MetaPrintfulProduct): DeepPartial<Webflow.Products.Skus.Sku>[] {
const webflowSkus: DeepPartial<Webflow.Products.Skus.Sku>[] = [];
for (const { colorGroup, product: printfulProduct } of metaPrintfulProduct.entries) {
for (const printfulVariant of printfulProduct.sync_variants) {
if (colorGroup !== null) {
// Only include sizes present across every color variant.
const sizeInAll = metaPrintfulProduct.entries.every((e) =>
e.product.sync_variants.some((sv) => sv.size === printfulVariant.size)
);
if (!sizeInAll) continue;
}
const skuColor = colorGroup ?? printfulVariant.color;
this.log.debug(
{ size: printfulVariant.size, color: skuColor, retailPrice: printfulVariant.retail_price },
"generating webflow SKU from printful variant"
);
webflowSkus.push({
id: printfulVariant.external_id,
fieldData: {
name: printfulVariant.name,
slug: formatSlug(printfulVariant.name),
"sku-values": {
color: skuColor,
size: printfulVariant.size,
},
price: {
value: Math.floor(+printfulVariant.retail_price * 100),
unit: printfulVariant.currency,
currency: printfulVariant.currency,
},
"main-image": PrintfulClient.Util.getVariantMainImage(printfulVariant),
},
});
}
}
return webflowSkus;
}
async generateMetaPrintfulProducts(
printfulProducts: Printful.Products.SyncProduct[],
printfulProductIdsFilter?: number[],
) {
const nameFilter = printfulProductIdsFilter
?.map((id) => printfulProducts.find((p) => p.id === id))
.filter((p) => p !== undefined)
.map((p) => this.getMetaPrintfulProductName(p));
const metaPrintfulProducts: MetaPrintfulProduct[] = []; const metaPrintfulProducts: MetaPrintfulProduct[] = [];
for (const printfulProduct of printfulProducts) { for (const printfulProduct of printfulProducts) {
const metaPrintfulProductName = this.getMetaPrintfulProductName(printfulProduct.name); const metaName = this.getMetaPrintfulProductName(printfulProduct);
const colorGroup = this.findColorInProductName(printfulProduct.name); const colorGroup = this.parseColorGroup(printfulProduct.name);
let metaPrintfulProduct = metaPrintfulProducts.find((mp) => mp.name === metaPrintfulProductName); if (nameFilter && !nameFilter.includes(metaName)) continue;
let metaPrintfulProduct = metaPrintfulProducts.find((mp) => mp.name === metaName);
if (!metaPrintfulProduct) { if (!metaPrintfulProduct) {
this.log.debug({ metaPrintfulProductName, externalId: printfulProduct.external_id }, "appending meta printful product"); this.log.debug({ metaName, externalId: printfulProduct.external_id }, "initializing meta printful product");
metaPrintfulProduct = { metaPrintfulProduct = {
name: metaPrintfulProductName, name: metaName,
variants: [], webflowProductId: printfulProduct.external_id.split("-")[0] ?? "",
externalId: printfulProduct.external_id, entries: [],
isColorGrouped: colorGroup !== null,
colorGroups: new Set(),
}; };
metaPrintfulProducts.push(metaPrintfulProduct); metaPrintfulProducts.push(metaPrintfulProduct);
} else if (!metaPrintfulProduct.webflowProductId && printfulProduct.external_id) {
// Prefer a non-empty external_id so a newly added color doesn't shadow an already-synced sibling's ID.
metaPrintfulProduct.webflowProductId = printfulProduct.external_id.split("-")[0] ?? "";
} }
this.log.debug({ colorGroup }, "appending meta printful product variant"); const fullPrintfulProduct = await this.printful.Products.get(printfulProduct.id);
const product = await this.printful.Products.get(printfulProduct.id); if (!fullPrintfulProduct) throw new Error(`Printful product ${printfulProduct.id} not found`);
if (!product) throw new Error(`Printful product ${printfulProduct.id} not found`);
metaPrintfulProduct.variants.push({ colorGroup, product }); this.log.debug({ metaName, colorGroup }, "adding entry to meta printful product");
if (colorGroup) metaPrintfulProduct.colorGroups.add(colorGroup); metaPrintfulProduct.entries.push({ colorGroup, product: fullPrintfulProduct });
// Prefer a non-empty external_id so a newly added color (which has no
// external_id yet) doesn't shadow an already-synced sibling's ID.
if (!metaPrintfulProduct.externalId && printfulProduct.external_id)
metaPrintfulProduct.externalId = printfulProduct.external_id;
} }
return metaPrintfulProducts; return metaPrintfulProducts;
} }
getMetaPrintfulProductName(productName: string): string { getMetaPrintfulProductName(printfulProduct: Printful.Products.SyncProduct): string {
const color = this.findColorInProductName(productName); const color = this.parseColorGroup(printfulProduct.name);
if (color) if (color) return printfulProduct.name.replace(`[${color}]`, "").trimEnd();
return productName.replace(`[${color}]`, "").trimEnd(); return printfulProduct.name;
return productName;
} }
private resolveWebflowSku( private resolveWebflowSku(