some organization and implement apparel_order_created event

This commit is contained in:
Dominic Ferrando
2026-07-12 19:12:09 -04:00
parent 27c85e6676
commit 7149d95eeb
3 changed files with 80 additions and 41 deletions
+13 -29
View File
@@ -10,7 +10,6 @@ import {
Printful,
Webflow,
} from "@blade-and-brawn/commerce";
import zipcodesUs from "zipcodes-us";
import { DEFAULT_NAME, env, log } from "./util";
import serverTiming from "@elysia/server-timing";
import jwt from "@elysia/jwt";
@@ -20,7 +19,6 @@ import { randomUUIDv7, sleep } from "bun";
import { CalculatorService, CalculatorUnavailableError } from "./services/calculator";
import { StandardsParamsSchema } from "@blade-and-brawn/calculator";
import { StandardsService } from "./services/standards";
import type { EventQueue, EventTypeOptions } from "./services/event-queue";
// CONSTANTS
// -----------------------
@@ -196,14 +194,14 @@ export const app = new Elysia()
.group("/sync", (app) => app
// Sync status
.get("/", async ({ sessionId }) => {
const latestSyncState = await s.Commerce.ProductSync.getLatestSyncState(sessionId);
const latestSyncState = await s.Commerce.Apparel.Syncs.getLatestSyncState(sessionId);
if (!latestSyncState) throw new NotFoundError("No product sync found for the provided session");
return latestSyncState;
})
// Run sync
.post("/:pProductId?", async ({ params: { pProductId }, sessionId }) => {
await s.Commerce.ProductSync.Queue.enqueue({
type: "product_sync_update",
await s.Commerce.Apparel.Syncs.Queue.enqueue({
type: "apparel_sync_update",
source: "portal",
payload: {
session: { id: sessionId, name: "Portal" },
@@ -230,6 +228,7 @@ export const app = new Elysia()
))
// WEBHOOKS
// TODO: account for automatic webhook retries from the webhook sender end (especially timeouts)
.post("/webhooks/printful", async ({ body }) => {
// https://webflow.com/integrations/printful
const payload = body as Printful.Webhook.EventPayload;
@@ -239,8 +238,8 @@ export const app = new Elysia()
const pProduct = payload.data.sync_product;
log.info({ productId: pProduct.id }, "printful webhook: product updated");
await s.Commerce.ProductSync.Queue.enqueue({
type: "product_sync_update",
await s.Commerce.Apparel.Syncs.Queue.enqueue({
type: "apparel_sync_update",
source: "printful",
payload: {
session: { id: randomUUIDv7(), name: "Printful" },
@@ -255,8 +254,8 @@ export const app = new Elysia()
log.info({ externalId: payload.data.sync_product.external_id, wProductId }, "printful webhook: product deleted");
if (!wProductId) throw new NotFoundError("Missing webflow product ID");
await s.Commerce.ProductSync.Queue.enqueue({
type: "product_sync_delete",
await s.Commerce.Apparel.Syncs.Queue.enqueue({
type: "apparel_sync_delete",
source: "printful",
payload: { wProductId }
});
@@ -293,25 +292,10 @@ export const app = new Elysia()
const wOrder = payload.payload;
log.info({ orderId: wOrder.orderId }, "webflow webhook: order created");
await s.Commerce.Printful.Orders.create({
external_id: wOrder.orderId,
// TODO: derive from webflow
shipping: "STANDARD",
recipient: {
name: wOrder.shippingAddress.addressee,
address1: wOrder.shippingAddress.line1,
address2: wOrder.shippingAddress.line2,
city: wOrder.shippingAddress.city,
state_code: zipcodesUs.find(
wOrder.shippingAddress.postalCode.split("-")[0] ?? "",
).stateCode,
country_code: wOrder.shippingAddress.country,
zip: wOrder.shippingAddress.postalCode,
},
items: wOrder.purchasedItems.map((wOrderSku) => ({
external_variant_id: wOrderSku.variantId,
quantity: wOrderSku.count,
})),
await s.Commerce.Apparel.Orders.Queue.enqueue({
type: "apparel_order_create",
source: "webflow",
payload: { wOrder }
});
break;
@@ -326,7 +310,7 @@ app.listen(3000, async () => {
log.info({ port: 3000 }, "server started")
// MANAGE QUEUES
const queues: EventQueue<string, Record<string, EventTypeOptions<any, any>>>[] = [s.Commerce.ProductSync.Queue];
const queues = [s.Commerce.Apparel.Syncs.Queue];
for (const queue of queues) {
(async () => {
while (true) {