More cleanup
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Printful, PrintfulClient, PrintfulError, Webflow, WebflowClient, WebflowError } from "@blade-and-brawn/commerce";
|
import { Printful, PrintfulClient, PrintfulError, Webflow, WebflowClient, WebflowError } from "@blade-and-brawn/commerce";
|
||||||
import { env } from "../env";
|
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 PRINTFUL_WEBHOOK_URL = `${BASE_URL}/webhook/printful`;
|
||||||
const WEBFLOW_WEBHOOK_URL = `${BASE_URL}/webhook/webflow`;
|
const WEBFLOW_WEBHOOK_URL = `${BASE_URL}/webhook/webflow`;
|
||||||
|
|||||||
@@ -100,11 +100,16 @@ export const app = new Elysia()
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
.onAfterHandle(({ request, set }) => {
|
.onAfterResponse(({ request, status, path }) => {
|
||||||
|
const skip: Record<string, string[]> = {
|
||||||
|
"/products/sync/": ["GET"]
|
||||||
|
};
|
||||||
|
if (env.NODE_ENV === "development" && skip[path]?.includes(request.method)) return;
|
||||||
|
|
||||||
log.info({
|
log.info({
|
||||||
method: request.method,
|
method: request.method,
|
||||||
path: new URL(request.url).pathname,
|
path,
|
||||||
status: set.status ?? 200
|
status
|
||||||
}, "request");
|
}, "request");
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -33,44 +33,37 @@ export class ProductSyncer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async sync(printfulProductId?: number): Promise<boolean> {
|
async sync(printfulProductId?: number): Promise<boolean> {
|
||||||
|
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 started");
|
if (printfulProductId) this.log.info({ printfulProductId }, "sync run started");
|
||||||
else this.log.info("sync started");
|
else this.log.info("sync run started");
|
||||||
|
|
||||||
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 });
|
let printfulProducts = await this.printful.Products.list({ forceAll: true });
|
||||||
if (printfulProductId) {
|
// optionally filter to sync only the given printful product
|
||||||
const printfulProduct = printfulProducts.find((p) => p.id === printfulProductId);
|
const printfulProductFilter = printfulProducts.find((p) => p.id === printfulProductId);
|
||||||
if (printfulProduct) {
|
if (printfulProductFilter) {
|
||||||
const metaProductName = this.getMainProductName(
|
const metaPrintfulProductName = this.getMetaPrintfulProductName(printfulProductFilter.name);
|
||||||
printfulProduct.name,
|
printfulProducts = printfulProducts.filter((p) => p.name.includes(metaPrintfulProductName));
|
||||||
);
|
|
||||||
printfulProducts = printfulProducts.filter((p) =>
|
|
||||||
p.name.includes(metaProductName),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.log.info({ count: printfulProducts.length }, "generating meta printful products");
|
this.log.info({ count: printfulProducts.length }, "generating meta printful products");
|
||||||
const metaPrintfulProducts: MetaPrintfulProduct[] = [];
|
const metaPrintfulProducts: MetaPrintfulProduct[] = [];
|
||||||
for (const printfulProduct of printfulProducts) {
|
for (const printfulProduct of printfulProducts) {
|
||||||
const mainProductName = this.getMainProductName(
|
const metaPrintfulProductName = this.getMetaPrintfulProductName(printfulProduct.name);
|
||||||
printfulProduct.name,
|
|
||||||
);
|
|
||||||
|
|
||||||
let metaPrintfulProduct = metaPrintfulProducts.find((mp) =>
|
let metaPrintfulProduct = metaPrintfulProducts.find((mp) => mp.name.includes(metaPrintfulProductName));
|
||||||
mp.name.includes(mainProductName),
|
|
||||||
);
|
|
||||||
if (!metaPrintfulProduct) {
|
if (!metaPrintfulProduct) {
|
||||||
|
this.log.debug({ metaPrintfulProductName }, "appending meta printful product");
|
||||||
metaPrintfulProduct = {
|
metaPrintfulProduct = {
|
||||||
name: mainProductName,
|
name: metaPrintfulProductName,
|
||||||
variants: [],
|
variants: [],
|
||||||
externalId: printfulProduct.external_id,
|
externalId: printfulProduct.external_id,
|
||||||
colors: new Set(),
|
colors: new Set(),
|
||||||
@@ -78,9 +71,8 @@ export class ProductSyncer {
|
|||||||
metaPrintfulProducts.push(metaPrintfulProduct);
|
metaPrintfulProducts.push(metaPrintfulProduct);
|
||||||
}
|
}
|
||||||
|
|
||||||
const productColor = this.findColorInProductName(
|
const productColor = this.findColorInProductName(printfulProduct.name);
|
||||||
printfulProduct.name,
|
this.log.debug({ productColor }, "appending meta printful product variant");
|
||||||
);
|
|
||||||
metaPrintfulProduct.variants.push({
|
metaPrintfulProduct.variants.push({
|
||||||
color: productColor,
|
color: productColor,
|
||||||
product: (await this.printful.Products.get(printfulProduct.id))!,
|
product: (await this.printful.Products.get(printfulProduct.id))!,
|
||||||
@@ -103,10 +95,10 @@ export class ProductSyncer {
|
|||||||
|
|
||||||
const webflowSkus: DeepPartial<Webflow.Products.Skus.Sku>[] = [];
|
const webflowSkus: DeepPartial<Webflow.Products.Skus.Sku>[] = [];
|
||||||
|
|
||||||
for (const specialVariant of metaPrintfulProduct.variants) {
|
for (const metaVariant of metaPrintfulProduct.variants) {
|
||||||
for (const printfulVariant of specialVariant.product.sync_variants) {
|
for (const printfulVariant of metaVariant.product.sync_variants) {
|
||||||
this.log.debug(
|
this.log.debug(
|
||||||
{ size: printfulVariant.size, color: printfulVariant.color },
|
{ size: printfulVariant.size, color: metaVariant.color },
|
||||||
"generating webflow SKU from printful variant"
|
"generating webflow SKU from printful variant"
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -127,7 +119,7 @@ export class ProductSyncer {
|
|||||||
name: printfulVariant.name,
|
name: printfulVariant.name,
|
||||||
slug: formatSlug(printfulVariant.name),
|
slug: formatSlug(printfulVariant.name),
|
||||||
"sku-values": {
|
"sku-values": {
|
||||||
color: specialVariant.color,
|
color: metaVariant.color,
|
||||||
size: printfulVariant.size,
|
size: printfulVariant.size,
|
||||||
},
|
},
|
||||||
price: {
|
price: {
|
||||||
@@ -304,13 +296,12 @@ export class ProductSyncer {
|
|||||||
return m?.[1] ? m[1] : "N/A";
|
return m?.[1] ? m[1] : "N/A";
|
||||||
}
|
}
|
||||||
|
|
||||||
getMainProductName(productName: string): string {
|
getMetaPrintfulProductName(productName: string): string {
|
||||||
const colorInName = this.findColorInProductName(productName);
|
const colorInName = this.findColorInProductName(productName);
|
||||||
if (colorInName) {
|
if (colorInName)
|
||||||
return productName.replace(`[${colorInName}]`, "").trimEnd();
|
return productName.replace(`[${colorInName}]`, "").trimEnd();
|
||||||
} else {
|
else
|
||||||
return productName;
|
return productName;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async isRunning(): Promise<boolean> {
|
async isRunning(): Promise<boolean> {
|
||||||
|
|||||||
Reference in New Issue
Block a user