Hook up syncing with postgres
This commit is contained in:
+16
-9
@@ -13,7 +13,6 @@ import { Elysia, NotFoundError, status, t } from "elysia";
|
||||
import {
|
||||
PrintfulClient,
|
||||
PrintfulError,
|
||||
ProductSyncer,
|
||||
WebflowClient,
|
||||
WebflowError,
|
||||
Printful,
|
||||
@@ -23,6 +22,7 @@ import zipcodesUs from "zipcodes-us";
|
||||
import { env, log } from "./util";
|
||||
import serverTiming from "@elysia/server-timing";
|
||||
import jwt from "@elysia/jwt";
|
||||
import { SyncService } from "./services/sync";
|
||||
|
||||
// SERVICES
|
||||
// -----------
|
||||
@@ -42,7 +42,7 @@ const webflow = new WebflowClient({
|
||||
webhookSecret: env.WEBFLOW_WEBHOOK_SECRET,
|
||||
});
|
||||
|
||||
const productSyncer = new ProductSyncer(printful, webflow, log);
|
||||
const syncService = new SyncService(printful, webflow);
|
||||
|
||||
// ELYSIA
|
||||
// -----------
|
||||
@@ -143,15 +143,22 @@ export const app = new Elysia()
|
||||
.group("/sync", (app) =>
|
||||
app
|
||||
// Sync status
|
||||
.get("/", async ({ }) => ({
|
||||
state: await productSyncer.getState(),
|
||||
isRunning: await productSyncer.isRunning()
|
||||
}))
|
||||
.get("/", async ({ }) => {
|
||||
const activeRun = await syncService.getActiveRun();
|
||||
if (!activeRun) throw new NotFoundError("No sync in progress");
|
||||
return {
|
||||
startDate: activeRun.started_at,
|
||||
syncingPrintfulProductIds: activeRun.syncing_printful_product_ids
|
||||
}
|
||||
})
|
||||
// Run sync
|
||||
.post("/:printfulProductId?", async ({ params: { printfulProductId } }) => {
|
||||
log.debug({ printfulProductId });
|
||||
const canSync = await productSyncer.sync({ filter: { printfulProductIds: printfulProductId } });
|
||||
if (!canSync) throw status(409, "Sync already in progress");
|
||||
await syncService.start({
|
||||
printfulProductIds: printfulProductId ?
|
||||
[printfulProductId] :
|
||||
undefined
|
||||
});
|
||||
}, { params: t.Object({ printfulProductId: t.Optional(t.Numeric()) }) }),
|
||||
)
|
||||
.get("/:printfulProductId", async ({ params }) => {
|
||||
@@ -178,7 +185,7 @@ export const app = new Elysia()
|
||||
case Printful.Webhook.Event.ProductUpdated: {
|
||||
const printfulProduct = payload.data.sync_product;
|
||||
log.info({ productId: printfulProduct.id }, "printful webhook: product updated");
|
||||
await productSyncer.sync({ filter: { printfulProductIds: printfulProduct.id } });
|
||||
await syncService.start({ printfulProductIds: [printfulProduct.id] });
|
||||
break;
|
||||
}
|
||||
case Printful.Webhook.Event.ProductDeleted: {
|
||||
|
||||
Reference in New Issue
Block a user