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