Some cleanup
This commit is contained in:
@@ -5,7 +5,7 @@ interface ProductRecord {
|
|||||||
printfulProductId: number;
|
printfulProductId: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ProductRecords {
|
class ProductRecords {
|
||||||
private db: Database;
|
private db: Database;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -56,3 +56,4 @@ export class ProductRecords {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const productRecords = new ProductRecords();
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
|
import { productRecords } from "./data/product-records.ts";
|
||||||
import { Printful } from "./printful.ts"
|
import { Printful } from "./printful.ts"
|
||||||
import { ProductRecords } from "./data/product-records.ts"
|
|
||||||
import { Webflow } from "./webflow.ts";
|
import { Webflow } from "./webflow.ts";
|
||||||
|
|
||||||
const productRecords = new ProductRecords();
|
|
||||||
|
|
||||||
const server = Bun.serve({
|
const server = Bun.serve({
|
||||||
routes: {
|
routes: {
|
||||||
"/test": {
|
"/test": {
|
||||||
@@ -24,16 +22,16 @@ const server = Bun.serve({
|
|||||||
const productRecord = productRecords.findFromPrintful(payload.data.sync_product.id);
|
const productRecord = productRecords.findFromPrintful(payload.data.sync_product.id);
|
||||||
|
|
||||||
if (productRecord) {
|
if (productRecord) {
|
||||||
await Webflow.updateProduct(printfulProduct, productRecords);
|
await Webflow.updateProductFromPrintful(printfulProduct);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
await Webflow.createProduct(printfulProduct, productRecords);
|
await Webflow.createProductFromPrintful(printfulProduct);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// DELETE
|
// DELETE
|
||||||
case Printful.Webhook.Event.ProductDeleted: {
|
case Printful.Webhook.Event.ProductDeleted: {
|
||||||
await Webflow.deleteProduct(payload.data.sync_product.id, productRecords);
|
await Webflow.deleteProductFromPrintful(payload.data.sync_product.id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,6 +48,4 @@ const server = Bun.serve({
|
|||||||
development: true
|
development: true
|
||||||
})
|
})
|
||||||
|
|
||||||
productRecords.add({ webflowProductId: 123, printfulProductId: 321 });
|
|
||||||
|
|
||||||
console.log(`Listening on ${server.url}`);
|
console.log(`Listening on ${server.url}`);
|
||||||
|
|||||||
+7
-7
@@ -1,4 +1,4 @@
|
|||||||
import { ProductRecords } from "./data/product-records";
|
import { productRecords } from "./data/product-records";
|
||||||
import { Printful } from "./printful"
|
import { Printful } from "./printful"
|
||||||
|
|
||||||
export namespace Webflow {
|
export namespace Webflow {
|
||||||
@@ -52,7 +52,7 @@ export namespace Webflow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteProduct(printfulProductId: number, productRecords: ProductRecords) {
|
export async function deleteProductFromPrintful(printfulProductId: number) {
|
||||||
const webflowProductId = productRecords.findFromPrintful(printfulProductId)?.webflowProductId;
|
const webflowProductId = productRecords.findFromPrintful(printfulProductId)?.webflowProductId;
|
||||||
if (webflowProductId) {
|
if (webflowProductId) {
|
||||||
const res = await fetch(`${Webflow.API_COLLECTIONS_URL}/items/${webflowProductId}`, {
|
const res = await fetch(`${Webflow.API_COLLECTIONS_URL}/items/${webflowProductId}`, {
|
||||||
@@ -67,7 +67,7 @@ export namespace Webflow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateProduct(printfulProduct: Printful.Products.SyncProduct, productRecords: ProductRecords) {
|
export async function updateProductFromPrintful(printfulProduct: Printful.Products.SyncProduct) {
|
||||||
const webflowProductId = productRecords.findFromPrintful(printfulProduct.result.sync_product.id)?.webflowProductId;
|
const webflowProductId = productRecords.findFromPrintful(printfulProduct.result.sync_product.id)?.webflowProductId;
|
||||||
if (!webflowProductId) {
|
if (!webflowProductId) {
|
||||||
throw new Error("Product is not recognized");
|
throw new Error("Product is not recognized");
|
||||||
@@ -161,7 +161,7 @@ export namespace Webflow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createProduct(printfulProduct: Printful.Products.SyncProduct, productRecords: ProductRecords) {
|
export async function createProductFromPrintful(printfulProduct: Printful.Products.SyncProduct) {
|
||||||
const printfulVariants = printfulProduct.result.sync_variants;
|
const printfulVariants = printfulProduct.result.sync_variants;
|
||||||
|
|
||||||
const webflowVariants: Products.Sku[] = [];
|
const webflowVariants: Products.Sku[] = [];
|
||||||
@@ -189,7 +189,7 @@ export namespace Webflow {
|
|||||||
const foundSizes = Array.from(new Set(printfulVariants.map(v => v.size)));
|
const foundSizes = Array.from(new Set(printfulVariants.map(v => v.size)));
|
||||||
|
|
||||||
// CREATE WEBFLOW PRODUCT
|
// CREATE WEBFLOW PRODUCT
|
||||||
let addProductResponse = await fetch(`${Webflow.API_SITES_URL}/products`, {
|
let addProductResponse = await (await fetch(`${Webflow.API_SITES_URL}/products`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -224,8 +224,8 @@ export namespace Webflow {
|
|||||||
},
|
},
|
||||||
"sku": webflowVariants[0],
|
"sku": webflowVariants[0],
|
||||||
})
|
})
|
||||||
});
|
}))?.json();
|
||||||
addProductResponse = await addProductResponse.json();
|
|
||||||
if (!addProductResponse?.product?.id) {
|
if (!addProductResponse?.product?.id) {
|
||||||
console.error("Webflow product creation failed:", addProductResponse);
|
console.error("Webflow product creation failed:", addProductResponse);
|
||||||
throw new Error("Failed to create Webflow product");
|
throw new Error("Failed to create Webflow product");
|
||||||
|
|||||||
Reference in New Issue
Block a user