Per item sync bugfix and cleanup
This commit is contained in:
@@ -23,7 +23,9 @@ type SyncState = {
|
||||
};
|
||||
|
||||
type SyncOptions = {
|
||||
printfulProductIdsFilter?: number[];
|
||||
filter?: {
|
||||
printfulProductIds?: number[] | number;
|
||||
}
|
||||
};
|
||||
|
||||
export class ProductSyncer {
|
||||
@@ -38,14 +40,17 @@ export class ProductSyncer {
|
||||
}
|
||||
|
||||
async sync(opt: SyncOptions = {}): Promise<boolean> {
|
||||
if (typeof opt.filter?.printfulProductIds === "number")
|
||||
opt.filter.printfulProductIds = [opt.filter.printfulProductIds];
|
||||
|
||||
this.log.info("attempting sync run");
|
||||
const canRun = await this.run();
|
||||
if (!canRun) return false;
|
||||
|
||||
try {
|
||||
const syncStart = Date.now();
|
||||
if (opt.printfulProductIdsFilter)
|
||||
this.log.info({ printfulProductIds: opt.printfulProductIdsFilter }, "sync started (filtered)");
|
||||
if (opt.filter?.printfulProductIds)
|
||||
this.log.info({ printfulProductIds: opt.filter.printfulProductIds }, "sync started (filtered)");
|
||||
else
|
||||
this.log.info("sync started (all)");
|
||||
|
||||
@@ -55,8 +60,8 @@ export class ProductSyncer {
|
||||
this.log.debug("retrieving printful products");
|
||||
const allPrintfulProducts = await this.printful.Products.list({ forceAll: true });
|
||||
|
||||
this.log.info({ printfulProductCount: allPrintfulProducts.length, filter: opt.printfulProductIdsFilter ?? "N/A" }, "generating meta printful products");
|
||||
const metaPrintfulProducts = await this.generateMetaPrintfulProducts(allWebflowProducts, allPrintfulProducts, opt.printfulProductIdsFilter);
|
||||
this.log.info({ printfulProductCount: allPrintfulProducts.length, filter: opt.filter ?? "N/A" }, "generating meta printful products");
|
||||
const metaPrintfulProducts = await this.generateMetaPrintfulProducts(allWebflowProducts, allPrintfulProducts, opt);
|
||||
|
||||
for (const metaPrintfulProduct of metaPrintfulProducts) {
|
||||
this.log.info({ name: metaPrintfulProduct.name, externalId: metaPrintfulProduct.webflowProductId }, "syncing meta printful product");
|
||||
@@ -266,9 +271,12 @@ export class ProductSyncer {
|
||||
async generateMetaPrintfulProducts(
|
||||
allWebflowProducts: Webflow.Products.ProductAndSkus[],
|
||||
allPrintfulProducts: Printful.Products.SyncProduct[],
|
||||
printfulProductIdsFilter?: number[],
|
||||
opt: SyncOptions,
|
||||
) {
|
||||
const metaNameFilter = printfulProductIdsFilter
|
||||
if (typeof opt.filter?.printfulProductIds === "number")
|
||||
opt.filter.printfulProductIds = [opt.filter.printfulProductIds];
|
||||
|
||||
const metaNameFilter = opt.filter?.printfulProductIds
|
||||
?.map((id) => allPrintfulProducts.find((p) => p.id === id))
|
||||
?.filter((p) => p !== undefined)
|
||||
?.map((p) => this.getMetaPrintfulProductName(p));
|
||||
|
||||
Reference in New Issue
Block a user