More sync cleanup and fixing
This commit is contained in:
@@ -147,7 +147,7 @@ export const app = new Elysia()
|
|||||||
})
|
})
|
||||||
// Per-product sync
|
// Per-product sync
|
||||||
.post("/:printfulProductId", async ({ params }) => {
|
.post("/:printfulProductId", async ({ params }) => {
|
||||||
if (!await productSyncer.sync(params.printfulProductId))
|
if (!await productSyncer.sync({ printfulProductIdsFilter: [params.printfulProductId] }))
|
||||||
throw status(409, "Sync already in progress");
|
throw status(409, "Sync already in progress");
|
||||||
}, { params: z.object({ printfulProductId: z.coerce.number() }) }),
|
}, { params: z.object({ printfulProductId: z.coerce.number() }) }),
|
||||||
)
|
)
|
||||||
@@ -175,7 +175,7 @@ export const app = new Elysia()
|
|||||||
case Printful.Webhook.Event.ProductUpdated: {
|
case Printful.Webhook.Event.ProductUpdated: {
|
||||||
const printfulProduct = payload.data.sync_product;
|
const printfulProduct = payload.data.sync_product;
|
||||||
log.info({ productId: printfulProduct.id }, "printful webhook: product updated");
|
log.info({ productId: printfulProduct.id }, "printful webhook: product updated");
|
||||||
await productSyncer.sync(printfulProduct.id);
|
await productSyncer.sync({ printfulProductIdsFilter: [printfulProduct.id] });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Printful.Webhook.Event.ProductDeleted: {
|
case Printful.Webhook.Event.ProductDeleted: {
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# Printful
|
||||||
|
|
||||||
|
## Definitions
|
||||||
|
|
||||||
|
- "Color-grouped" products
|
||||||
|
- Products in this format: "Product Name [color]"
|
||||||
|
|
||||||
|
## Constraints
|
||||||
|
|
||||||
|
1. Color-grouped products should no more than *one* printful color variant
|
||||||
|
2. The single color-grouped product variant should match [color]
|
||||||
|
3. Printful product names should be unique
|
||||||
@@ -49,14 +49,14 @@ export class ProductSyncer {
|
|||||||
else
|
else
|
||||||
this.log.info("sync started (all)");
|
this.log.info("sync started (all)");
|
||||||
|
|
||||||
this.log.debug("populating webflow products");
|
this.log.debug("retrieving webflow products");
|
||||||
const webflowProducts: Webflow.Products.ProductAndSkus[] = await this.webflow.Products.list({ forceAll: true });
|
const allWebflowProducts = await this.webflow.Products.list({ forceAll: true });
|
||||||
|
|
||||||
this.log.debug("populating printful products");
|
this.log.debug("retrieving printful products");
|
||||||
const printfulProducts = await this.printful.Products.list({ forceAll: true });
|
const allPrintfulProducts = await this.printful.Products.list({ forceAll: true });
|
||||||
|
|
||||||
this.log.info({ count: printfulProducts.length }, "generating meta printful products");
|
this.log.info({ printfulProductCount: allPrintfulProducts.length, filter: opt.printfulProductIdsFilter ?? "N/A" }, "generating meta printful products");
|
||||||
const metaPrintfulProducts = await this.generateMetaPrintfulProducts(printfulProducts, opt.printfulProductIdsFilter);
|
const metaPrintfulProducts = await this.generateMetaPrintfulProducts(allWebflowProducts, allPrintfulProducts, opt.printfulProductIdsFilter);
|
||||||
|
|
||||||
for (const metaPrintfulProduct of metaPrintfulProducts) {
|
for (const metaPrintfulProduct of metaPrintfulProducts) {
|
||||||
this.log.info({ name: metaPrintfulProduct.name, externalId: metaPrintfulProduct.webflowProductId }, "syncing meta printful product");
|
this.log.info({ name: metaPrintfulProduct.name, externalId: metaPrintfulProduct.webflowProductId }, "syncing meta printful product");
|
||||||
@@ -102,25 +102,27 @@ export class ProductSyncer {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const webflowProduct = webflowProducts.find(
|
const webflowProduct = allWebflowProducts.find((wp) => wp.product.id === metaPrintfulProduct.webflowProductId);
|
||||||
(wp) => wp.product.id === metaPrintfulProduct.webflowProductId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (webflowProduct) {
|
if (webflowProduct) {
|
||||||
this.log.info("existing webflow product found, updating it");
|
this.log.info({ id: webflowProduct.product.id }, "existing webflow product found, updating it");
|
||||||
|
|
||||||
// do not update images
|
// do not update images
|
||||||
for (const sku of newWebflowSkus) delete sku.fieldData?.["main-image"];
|
for (const sku of newWebflowSkus) delete sku.fieldData?.["main-image"];
|
||||||
|
|
||||||
const firstExistingWebflowSku = this.resolveWebflowSku(
|
// handle case where webflow SKU created by associated printful variant external_id update failed
|
||||||
|
if (!newWebflowSkus[0].id) {
|
||||||
|
this.log.warn({ webflowSku: newWebflowSkus[0] }), "missing webflow SKU id";
|
||||||
|
const webflowSkuMatch = this.resolveWebflowSku(
|
||||||
webflowProduct.skus,
|
webflowProduct.skus,
|
||||||
newWebflowSkus[0].fieldData?.["sku-values"]?.["color"],
|
newWebflowSkus[0].fieldData?.["sku-values"]?.["color"],
|
||||||
newWebflowSkus[0].fieldData?.["sku-values"]?.["size"],
|
newWebflowSkus[0].fieldData?.["sku-values"]?.["size"],
|
||||||
newWebflowSkus[0].id,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (firstExistingWebflowSku) {
|
if (webflowSkuMatch)
|
||||||
newWebflowSkus[0].id = firstExistingWebflowSku.id;
|
newWebflowSkus[0].id = webflowSkuMatch.id;
|
||||||
|
else
|
||||||
|
this.log.warn({ webflowSku: newWebflowSkus[0] }), "could not find existing webflow SKU that matches";
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.webflow.Products.update(webflowProduct.product.id, {
|
await this.webflow.Products.update(webflowProduct.product.id, {
|
||||||
@@ -165,13 +167,14 @@ export class ProductSyncer {
|
|||||||
const freshWebflowProduct = await this.webflow.Products.get(metaPrintfulProduct.webflowProductId);
|
const freshWebflowProduct = await this.webflow.Products.get(metaPrintfulProduct.webflowProductId);
|
||||||
if (!freshWebflowProduct) throw new Error("webflow product missing");
|
if (!freshWebflowProduct) throw new Error("webflow product missing");
|
||||||
|
|
||||||
|
// Update printful external IDs
|
||||||
for (const { colorGroup, product: printfulProduct } of metaPrintfulProduct.entries) {
|
for (const { colorGroup, product: printfulProduct } of metaPrintfulProduct.entries) {
|
||||||
const expectedExternalId = colorGroup
|
const expectedExternalId = colorGroup ?
|
||||||
? `${metaPrintfulProduct.webflowProductId}-${colorGroup}`
|
`${metaPrintfulProduct.webflowProductId}-${colorGroup}` :
|
||||||
: metaPrintfulProduct.webflowProductId;
|
metaPrintfulProduct.webflowProductId;
|
||||||
if (printfulProduct.sync_product.external_id === expectedExternalId) continue;
|
if (printfulProduct.sync_product.external_id === expectedExternalId) continue;
|
||||||
|
|
||||||
const newPrintfulVariants: DeepPartial<Printful.Products.SyncVariant>[] = [];
|
const printfulVariantPatches: DeepPartial<Printful.Products.SyncVariant>[] = [];
|
||||||
for (const printfulVariant of printfulProduct.sync_variants) {
|
for (const printfulVariant of printfulProduct.sync_variants) {
|
||||||
const webflowSku = this.resolveWebflowSku(
|
const webflowSku = this.resolveWebflowSku(
|
||||||
freshWebflowProduct.skus,
|
freshWebflowProduct.skus,
|
||||||
@@ -179,7 +182,7 @@ export class ProductSyncer {
|
|||||||
printfulVariant.size,
|
printfulVariant.size,
|
||||||
);
|
);
|
||||||
if (webflowSku) {
|
if (webflowSku) {
|
||||||
newPrintfulVariants.push({
|
printfulVariantPatches.push({
|
||||||
id: printfulVariant.id,
|
id: printfulVariant.id,
|
||||||
external_id: String(webflowSku.id),
|
external_id: String(webflowSku.id),
|
||||||
});
|
});
|
||||||
@@ -187,13 +190,13 @@ export class ProductSyncer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await sleep(10000);
|
await sleep(10000);
|
||||||
this.log.info({ printfulProduct: printfulProduct.sync_product.id }, "updating printful product");
|
this.log.info({ printfulProduct: printfulProduct.sync_product.id, externalId: expectedExternalId }, "updating printful product's external ID");
|
||||||
await this.printful.Products.update(printfulProduct.sync_product.id, {
|
await this.printful.Products.update(printfulProduct.sync_product.id, {
|
||||||
sync_product: {
|
sync_product: {
|
||||||
id: printfulProduct.sync_product.id,
|
id: printfulProduct.sync_product.id,
|
||||||
external_id: expectedExternalId,
|
external_id: expectedExternalId,
|
||||||
},
|
},
|
||||||
sync_variants: newPrintfulVariants,
|
sync_variants: printfulVariantPatches,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -218,8 +221,9 @@ export class ProductSyncer {
|
|||||||
private generateWebflowSkus(metaPrintfulProduct: MetaPrintfulProduct): DeepPartial<Webflow.Products.Skus.Sku>[] {
|
private generateWebflowSkus(metaPrintfulProduct: MetaPrintfulProduct): DeepPartial<Webflow.Products.Skus.Sku>[] {
|
||||||
const webflowSkus: DeepPartial<Webflow.Products.Skus.Sku>[] = [];
|
const webflowSkus: DeepPartial<Webflow.Products.Skus.Sku>[] = [];
|
||||||
for (const { colorGroup, product: printfulProduct } of metaPrintfulProduct.entries) {
|
for (const { colorGroup, product: printfulProduct } of metaPrintfulProduct.entries) {
|
||||||
|
const isColorGrouped = colorGroup !== null;
|
||||||
for (const printfulVariant of printfulProduct.sync_variants) {
|
for (const printfulVariant of printfulProduct.sync_variants) {
|
||||||
if (colorGroup !== null) {
|
if (isColorGrouped) {
|
||||||
// Only include sizes present across every color variant.
|
// Only include sizes present across every color variant.
|
||||||
const sizeInAll = metaPrintfulProduct.entries.every((e) =>
|
const sizeInAll = metaPrintfulProduct.entries.every((e) =>
|
||||||
e.product.sync_variants.some((sv) => sv.size === printfulVariant.size)
|
e.product.sync_variants.some((sv) => sv.size === printfulVariant.size)
|
||||||
@@ -227,10 +231,12 @@ export class ProductSyncer {
|
|||||||
if (!sizeInAll) continue;
|
if (!sizeInAll) continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const skuColor = colorGroup ?? printfulVariant.color;
|
const color = isColorGrouped ?
|
||||||
|
colorGroup :
|
||||||
|
printfulVariant.color;
|
||||||
|
|
||||||
this.log.debug(
|
this.log.debug(
|
||||||
{ size: printfulVariant.size, color: skuColor, retailPrice: printfulVariant.retail_price },
|
{ color, size: printfulVariant.size, retailPrice: printfulVariant.retail_price },
|
||||||
"generating webflow SKU from printful variant"
|
"generating webflow SKU from printful variant"
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -240,7 +246,7 @@ export class ProductSyncer {
|
|||||||
name: printfulVariant.name,
|
name: printfulVariant.name,
|
||||||
slug: formatSlug(printfulVariant.name),
|
slug: formatSlug(printfulVariant.name),
|
||||||
"sku-values": {
|
"sku-values": {
|
||||||
color: skuColor,
|
color,
|
||||||
size: printfulVariant.size,
|
size: printfulVariant.size,
|
||||||
},
|
},
|
||||||
price: {
|
price: {
|
||||||
@@ -258,39 +264,46 @@ export class ProductSyncer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async generateMetaPrintfulProducts(
|
async generateMetaPrintfulProducts(
|
||||||
printfulProducts: Printful.Products.SyncProduct[],
|
allWebflowProducts: Webflow.Products.ProductAndSkus[],
|
||||||
|
allPrintfulProducts: Printful.Products.SyncProduct[],
|
||||||
printfulProductIdsFilter?: number[],
|
printfulProductIdsFilter?: number[],
|
||||||
) {
|
) {
|
||||||
const nameFilter = printfulProductIdsFilter
|
const metaNameFilter = printfulProductIdsFilter
|
||||||
?.map((id) => printfulProducts.find((p) => p.id === id))
|
?.map((id) => allPrintfulProducts.find((p) => p.id === id))
|
||||||
.filter((p) => p !== undefined)
|
?.filter((p) => p !== undefined)
|
||||||
.map((p) => this.getMetaPrintfulProductName(p));
|
?.map((p) => this.getMetaPrintfulProductName(p));
|
||||||
|
|
||||||
const metaPrintfulProducts: MetaPrintfulProduct[] = [];
|
const metaPrintfulProducts: MetaPrintfulProduct[] = [];
|
||||||
for (const printfulProduct of printfulProducts) {
|
for (const printfulProduct of allPrintfulProducts) {
|
||||||
const metaName = this.getMetaPrintfulProductName(printfulProduct);
|
const metaName = this.getMetaPrintfulProductName(printfulProduct);
|
||||||
const colorGroup = this.parseColorGroup(printfulProduct.name);
|
const colorGroup = this.parseColorGroup(printfulProduct.name);
|
||||||
|
const webflowProductId = this.isPrintfulProductSynced(printfulProduct, allWebflowProducts) ?
|
||||||
|
printfulProduct.external_id.split("-")[0] ?? "" :
|
||||||
|
"";
|
||||||
|
|
||||||
if (nameFilter && !nameFilter.includes(metaName)) continue;
|
if (metaNameFilter && !metaNameFilter.includes(metaName)) continue;
|
||||||
|
|
||||||
let metaPrintfulProduct = metaPrintfulProducts.find((mp) => mp.name === metaName);
|
let metaPrintfulProduct = metaPrintfulProducts.find((mp) => mp.name === metaName);
|
||||||
if (!metaPrintfulProduct) {
|
if (!metaPrintfulProduct) {
|
||||||
this.log.debug({ metaName, externalId: printfulProduct.external_id }, "initializing meta printful product");
|
this.log.debug({ metaName, colorGroup, webflowProductId }, "generating meta printful product");
|
||||||
metaPrintfulProduct = {
|
metaPrintfulProduct = {
|
||||||
name: metaName,
|
name: metaName,
|
||||||
webflowProductId: printfulProduct.external_id.split("-")[0] ?? "",
|
webflowProductId,
|
||||||
entries: [],
|
entries: [],
|
||||||
};
|
};
|
||||||
metaPrintfulProducts.push(metaPrintfulProduct);
|
metaPrintfulProducts.push(metaPrintfulProduct);
|
||||||
} else if (!metaPrintfulProduct.webflowProductId && printfulProduct.external_id) {
|
} else if (!metaPrintfulProduct.webflowProductId && webflowProductId) {
|
||||||
// Prefer a non-empty external_id so a newly added color doesn't shadow an already-synced sibling's ID.
|
// Prefer a non-empty webflowProductId so a newly added color doesn't shadow an already-synced color's ID.
|
||||||
metaPrintfulProduct.webflowProductId = printfulProduct.external_id.split("-")[0] ?? "";
|
metaPrintfulProduct.webflowProductId = webflowProductId;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fullPrintfulProduct = await this.printful.Products.get(printfulProduct.id);
|
const fullPrintfulProduct = await this.printful.Products.get(printfulProduct.id);
|
||||||
if (!fullPrintfulProduct) throw new Error(`Printful product ${printfulProduct.id} not found`);
|
if (!fullPrintfulProduct) throw new Error(`Printful product ${printfulProduct.id} not found`);
|
||||||
|
|
||||||
this.log.debug({ metaName, colorGroup }, "adding entry to meta printful product");
|
const isDuplicate = allPrintfulProducts.some((p) => p.id !== printfulProduct.id && p.name === printfulProduct.name);
|
||||||
|
if (isDuplicate) this.log.warn({ printfulProductName: printfulProduct.name }, "found duplicate printful product");
|
||||||
|
|
||||||
|
this.log.debug({ name: metaName, colorGroup }, "adding entry to meta printful product");
|
||||||
metaPrintfulProduct.entries.push({ colorGroup, product: fullPrintfulProduct });
|
metaPrintfulProduct.entries.push({ colorGroup, product: fullPrintfulProduct });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,6 +330,10 @@ export class ProductSyncer {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private isPrintfulProductSynced(printfulProduct: Printful.Products.SyncProduct, allWebflowProducts: Webflow.Products.ProductAndSkus[]) {
|
||||||
|
return allWebflowProducts.some((wp) => wp.product.id === printfulProduct.external_id.split("-")[0]);
|
||||||
|
}
|
||||||
|
|
||||||
async isRunning(): Promise<boolean> {
|
async isRunning(): Promise<boolean> {
|
||||||
return !!await redis.get("commerce:sync:lock");
|
return !!await redis.get("commerce:sync:lock");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user