diff --git a/.env b/.env new file mode 100644 index 0000000..019ff2a --- /dev/null +++ b/.env @@ -0,0 +1,6 @@ +PRINTFUL_AUTH=RGI4WiWeQkrEYek3obwxZL6c424LAJsUimDX1bHF +PRINTFUL_STORE_ID=15619272 + +WEBFLOW_AUTH=115ded1e97162f5d4835bcb87e236fe4bce1ad602f519bca8179394092a8c6f6 +WEBFLOW_SITE_ID=6737d2c9511d414861f1726e +WEBFLOW_COLLECTION_ID=675a3fbe966e58ea9aa7110f \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 188447d..81eb82e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,5 +31,8 @@ FROM base # Copy built application COPY --from=build /app /app +RUN apt-get update && apt-get install -y sqlite3 && rm -rf /var/lib/apt/lists/* + # Start the server by default, this can be overwritten at runtime +CMD [ "bun", "run", "db:seed:prod" ] CMD [ "bun", "run", "start" ] diff --git a/database/data.db b/data/data.db similarity index 100% rename from database/data.db rename to data/data.db diff --git a/database/product-records.ts b/data/product-records.ts similarity index 87% rename from database/product-records.ts rename to data/product-records.ts index 0600780..8b17256 100644 --- a/database/product-records.ts +++ b/data/product-records.ts @@ -9,7 +9,11 @@ export class ProductRecords { private db: Database; constructor() { - this.db = new Database("./database/data.db"); + const dbPath = Bun.env.NODE_ENV === "production" ? + "/data/data.db" : + "./data/data.db"; + + this.db = new Database(dbPath); } findFromWebflow(webflowProductId: number): ProductRecord | undefined { diff --git a/database/seed.sql b/data/seed.sql similarity index 100% rename from database/seed.sql rename to data/seed.sql diff --git a/fly.toml b/fly.toml index c3d1029..695367f 100644 --- a/fly.toml +++ b/fly.toml @@ -23,3 +23,7 @@ primary_region = 'ord' memory = '1gb' cpu_kind = 'shared' cpus = 1 + +[[mounts]] + source = "data" + destination = "/data" \ No newline at end of file diff --git a/main.ts b/main.ts index baa6c24..d29de22 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,5 @@ import { Printful } from "./printful.ts" -import { ProductRecords } from "./database/product-records.ts" +import { ProductRecords } from "./data/product-records.ts" import { Webflow } from "./webflow.ts"; const productRecords = new ProductRecords(); @@ -7,13 +7,14 @@ const productRecords = new ProductRecords(); const server = Bun.serve({ routes: { "/test": { - async GET(req){ + async GET(req) { return Response.json("Hello world"); } }, "/webhook/printful": { async POST(req) { const payload: Printful.Webhook.EventPayload = await req.json() + console.log(JSON.stringify(payload)); switch (payload.type) { case Printful.Webhook.Event.ProductUpdated: const printfulProduct = await Printful.getSyncProduct(payload.data.sync_product.id) diff --git a/package.json b/package.json index 9c39e5b..4ed378a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "@types/bun": "^1.2.11" }, "scripts": { - "db:seed": "sqlite3 ./database/data.db < ./database/seed.sql", + "db:seed:dev": "sqlite3 ./data/data.db < ./data/seed.sql", + "db:seed:prod": "sqlite3 /data/data.db < ./data/seed.sql", "start": "bun run main.ts" } } diff --git a/webflow.ts b/webflow.ts index 4e2d0d2..45c471b 100644 --- a/webflow.ts +++ b/webflow.ts @@ -1,4 +1,4 @@ -import { ProductRecords } from "./database/product-records"; +import { ProductRecords } from "./data/product-records"; import { Printful } from "./printful" export namespace Webflow { @@ -71,6 +71,7 @@ export namespace Webflow { const foundSizes = Array.from(new Set(printfulVariants.map(v => v.size))); // CREATE WEBFLOW PRODUCT + console.log("CREATE WEBFLOW PRODUCT"); let addProductResponse = await fetch(`${API_URL}/products`, { method: "POST", headers: { @@ -108,11 +109,13 @@ export namespace Webflow { }) }); addProductResponse = await addProductResponse.json(); + console.log(JSON.stringify(addProductResponse)); const webflowProductId = addProductResponse["product"]["id"]; const printfulProductId = printfulProduct.result.sync_product.id; // CREATE WEBFLOW PRODUCT SKUs + console.log("CREATE SKUS"); let createSkuResponse = await fetch(`${API_URL}/products/${webflowProductId}/skus`, { method: "POST", headers: { @@ -124,10 +127,12 @@ export namespace Webflow { }) }); createSkuResponse = await createSkuResponse.json(); + console.log(JSON.stringify(createSkuResponse)); const responseSkus = [addProductResponse["sku"], ...createSkuResponse["skus"]]; // SYNC WEBFLOW/PRINTFUL PRODUCT AND VARIANT IDs + console.log("SYNC PRODUCT IDs"); let modifyPrintfulProductResponse = await fetch(`${Printful.API_URL}/store/products/${printfulProductId}`, { method: "PUT", headers: { @@ -146,8 +151,10 @@ export namespace Webflow { }) }); modifyPrintfulProductResponse = await modifyPrintfulProductResponse.json(); + console.log(JSON.stringify(modifyPrintfulProductResponse)); // CACHE WEBFLOW/PRINTFUL PRODUCT ASSOCIATION + console.log("CACHING PRODUCT ASSOCIATION"); productRecords.add({ printfulProductId, webflowProductId }); } }