Some refactoring
This commit is contained in:
+20
-66
@@ -3,20 +3,11 @@ import { Elysia } from "elysia";
|
||||
import { cors } from "@elysiajs/cors";
|
||||
import { LevelCalculator, Standards, type ActivityStandards } from "$lib/services/calculator/main";
|
||||
import type { ActivityPerformance, Player } from "$lib/services/calculator/util";
|
||||
import { Webflow } from "$lib/services/commerce/webflow";
|
||||
import { Printful } from "$lib/services/commerce/printful";
|
||||
import rawStandards from "$lib/data/standards.json" assert { type: "json" }
|
||||
|
||||
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
|
||||
const syncProduct = async (printfulProduct: Printful.Products.Product) => {
|
||||
let webflowProduct = await Webflow.Products.get(printfulProduct.sync_product.external_id);
|
||||
if (webflowProduct)
|
||||
await Webflow.Products.updateUsingPrintful(printfulProduct);
|
||||
else
|
||||
webflowProduct = await Webflow.Products.createUsingPrintful(printfulProduct);
|
||||
await Webflow.Products.syncImages(webflowProduct);
|
||||
};
|
||||
import { Printful } from "./services/commerce/util/types";
|
||||
import WebflowService from "./services/commerce/webflow";
|
||||
import PrintfulService from "./services/commerce/printful";
|
||||
import SyncService from "./services/commerce/sync";
|
||||
|
||||
export const app = new Elysia({ prefix: "/api" })
|
||||
.use(cors({
|
||||
@@ -27,7 +18,7 @@ export const app = new Elysia({ prefix: "/api" })
|
||||
|
||||
// CALCULATOR
|
||||
|
||||
.post("/calculate", async ({ body, store }) => {
|
||||
.post("/calculate", async ({ body }) => {
|
||||
const MAIN_STANDARDS = new Standards(rawStandards as ActivityStandards);
|
||||
interface CalcRequest {
|
||||
player: Player;
|
||||
@@ -49,55 +40,19 @@ export const app = new Elysia({ prefix: "/api" })
|
||||
// COMMERCE
|
||||
.group("/products", app => app
|
||||
.group("/sync", app => app
|
||||
.state("job", {
|
||||
isSyncing: false,
|
||||
printfulProductId: undefined as undefined | number
|
||||
})
|
||||
.get("/", async ({ store }) => {
|
||||
return store.job;
|
||||
})
|
||||
.post("/:printfulProductId?", async ({ params, store }) => {
|
||||
if (store.job.isSyncing) return "";
|
||||
store.job.isSyncing = true;
|
||||
try {
|
||||
if (params.printfulProductId) {
|
||||
const printfulProductId = +params.printfulProductId;
|
||||
.get("/", async ({ }) => SyncService.state)
|
||||
.post("/:printfulProductId?", async ({ params }) => {
|
||||
SyncService.state.isSyncing = true;
|
||||
SyncService.state.printfulProductId = params.printfulProductId ?
|
||||
+params.printfulProductId :
|
||||
undefined;
|
||||
|
||||
store.job.printfulProductId = printfulProductId;
|
||||
const printfulProducts = params.printfulProductId ?
|
||||
[(await PrintfulService.Products.get(+params.printfulProductId)).sync_product] :
|
||||
await PrintfulService.Products.getAll();
|
||||
|
||||
const printfulProduct = await Printful.Products.get(printfulProductId);
|
||||
await syncProduct(printfulProduct);
|
||||
}
|
||||
else {
|
||||
console.log("Syncing all products...");
|
||||
|
||||
const printfulProducts = await Printful.Products.getAll();
|
||||
const webflowProducts = await Webflow.Products.getAll();
|
||||
for (const printfulProduct of printfulProducts) {
|
||||
store.job.printfulProductId = printfulProduct.id;
|
||||
|
||||
await sleep(2000);
|
||||
const fullPrintfulProduct = await Printful.Products.get(printfulProduct.id);
|
||||
|
||||
let webflowProduct = webflowProducts
|
||||
.find(webflowProduct => webflowProduct.product.id === printfulProduct.external_id);
|
||||
if (webflowProduct)
|
||||
await Webflow.Products.updateUsingPrintful(fullPrintfulProduct);
|
||||
else
|
||||
webflowProduct = await Webflow.Products.createUsingPrintful(fullPrintfulProduct);
|
||||
await Webflow.Products.syncImages(webflowProduct);
|
||||
}
|
||||
}
|
||||
console.log("Done");
|
||||
return "";
|
||||
}
|
||||
catch (error) {
|
||||
throw error;
|
||||
}
|
||||
finally {
|
||||
store.job.isSyncing = false;
|
||||
store.job.printfulProductId = undefined;
|
||||
}
|
||||
await SyncService.sync(printfulProducts);
|
||||
return "";
|
||||
}, {
|
||||
error({ error }) {
|
||||
console.error(error);
|
||||
@@ -111,17 +66,16 @@ export const app = new Elysia({ prefix: "/api" })
|
||||
)
|
||||
|
||||
// WEBHOOKS
|
||||
|
||||
.post("/webhook/printful", async ({ body, set }) => {
|
||||
.post("/webhook/printful", async ({ body }) => {
|
||||
const payload = body as Printful.Webhook.EventPayload;
|
||||
switch (payload.type) {
|
||||
case Printful.Webhook.Event.ProductUpdated: {
|
||||
const printfulProduct = await Printful.Products.get(payload.data.sync_product.id);
|
||||
await syncProduct(printfulProduct);
|
||||
const printfulProduct = payload.data.sync_product;
|
||||
await SyncService.sync([printfulProduct]);
|
||||
break;
|
||||
}
|
||||
case Printful.Webhook.Event.ProductDeleted: {
|
||||
await Webflow.Products.remove(payload.data.sync_product.external_id);
|
||||
await WebflowService.Products.remove(payload.data.sync_product.external_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user