More cleanup
This commit is contained in:
@@ -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`;
|
||||
|
||||
@@ -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({
|
||||
method: request.method,
|
||||
path: new URL(request.url).pathname,
|
||||
status: set.status ?? 200
|
||||
path,
|
||||
status
|
||||
}, "request");
|
||||
})
|
||||
|
||||
|
||||
@@ -33,44 +33,37 @@ export class ProductSyncer {
|
||||
}
|
||||
|
||||
async sync(printfulProductId?: number): Promise<boolean> {
|
||||
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<Webflow.Products.Skus.Sku>[] = [];
|
||||
|
||||
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<boolean> {
|
||||
|
||||
Reference in New Issue
Block a user