Implement session tracking product syncs
This commit is contained in:
+37
-23
@@ -24,6 +24,7 @@ import serverTiming from "@elysia/server-timing";
|
||||
import jwt from "@elysia/jwt";
|
||||
import { SyncService } from "./services/sync";
|
||||
import cluster from "node:cluster";
|
||||
import { randomUUIDv7 } from "bun";
|
||||
|
||||
// SERVICES
|
||||
// -----------
|
||||
@@ -104,7 +105,7 @@ export const app = new Elysia()
|
||||
if (body.password !== env.ADMIN_PASSWORD) throw status(401, "Invalid credentials");
|
||||
|
||||
auth.set({
|
||||
value: await jwt.sign({ id: "admin", exp: "7d" }),
|
||||
value: await jwt.sign({ sessionId: randomUUIDv7(), exp: "7d" }),
|
||||
path: "/",
|
||||
maxAge: 60 * 60 * 24 * 7,
|
||||
sameSite: "lax",
|
||||
@@ -115,17 +116,13 @@ export const app = new Elysia()
|
||||
undefined
|
||||
});
|
||||
}, {
|
||||
body: t.Object({
|
||||
password: t.String()
|
||||
}),
|
||||
body: t.Object({ password: t.String() }),
|
||||
cookie: t.Cookie({ auth: t.Optional(t.String()) })
|
||||
})
|
||||
|
||||
// CALCULATOR
|
||||
.post("/calculate", async ({ body }) => {
|
||||
return {
|
||||
levels: levelCalculator.calculate(body.player, body.activityPerformances),
|
||||
};
|
||||
return { levels: levelCalculator.calculate(body.player, body.activityPerformances) };
|
||||
}, {
|
||||
body: t.Object({
|
||||
player: PlayerSchema,
|
||||
@@ -134,29 +131,34 @@ export const app = new Elysia()
|
||||
})
|
||||
|
||||
// COMMERCE
|
||||
.group("/products", (app) =>
|
||||
app
|
||||
.guard({ cookie: t.Cookie({ auth: t.Optional(t.String()) }) })
|
||||
.onBeforeHandle(async ({ jwt, cookie: { auth } }) => {
|
||||
.group("/products",
|
||||
{ cookie: t.Cookie({ auth: t.Optional(t.String()) }) },
|
||||
(app) => app
|
||||
.resolve(async ({ jwt, cookie: { auth } }) => {
|
||||
const token = auth.value && await jwt.verify(auth.value);
|
||||
if (!token) throw status(401, "Unauthorized");
|
||||
if (!token || !token.sessionId) throw status(401, "Unauthorized");
|
||||
return { sessionId: token.sessionId.toString() };
|
||||
})
|
||||
.group("/sync", (app) =>
|
||||
app
|
||||
// Sync status
|
||||
.get("/", async ({ }) => {
|
||||
const activeSync = await syncService.getActiveSync();
|
||||
if (!activeSync) throw new NotFoundError("No sync in progress");
|
||||
.get("/", async ({ sessionId }) => {
|
||||
const latestSync = await syncService.getLatestSync(sessionId);
|
||||
if (!latestSync) throw new NotFoundError("No sync found");
|
||||
return {
|
||||
startDate: activeSync.started_at,
|
||||
syncingPrintfulProductIds: activeSync.syncing_printful_product_ids
|
||||
startDate: latestSync.started_at,
|
||||
status: syncService.deriveSyncStatus(latestSync),
|
||||
syncingPrintfulProductIds: latestSync.syncing_printful_product_ids
|
||||
}
|
||||
})
|
||||
// Run sync
|
||||
.post("/:printfulProductId?", async ({ params: { printfulProductId } }) => {
|
||||
const printfulProductIds = printfulProductId ? [printfulProductId] : null;
|
||||
log.debug({ printfulProductIds });
|
||||
await syncService.sync({ filter: { printfulProductIds } });
|
||||
.post("/:printfulProductId?", async ({ params: { printfulProductId }, sessionId }) => {
|
||||
await syncService.sync({
|
||||
session: { id: sessionId, name: "Portal" },
|
||||
filter: {
|
||||
printfulProductIds: printfulProductId ? [printfulProductId] : null
|
||||
}
|
||||
});
|
||||
}, { params: t.Object({ printfulProductId: t.Optional(t.Numeric()) }) }),
|
||||
)
|
||||
.get("/:printfulProductId", async ({ params }) => {
|
||||
@@ -184,7 +186,12 @@ export const app = new Elysia()
|
||||
const printfulProduct = payload.data.sync_product;
|
||||
log.info({ productId: printfulProduct.id }, "printful webhook: product updated");
|
||||
|
||||
await syncService.sync({ filter: { printfulProductIds: [printfulProduct.id] } });
|
||||
await syncService.sync({
|
||||
session: { id: randomUUIDv7(), name: "Printful" },
|
||||
filter: {
|
||||
printfulProductIds: [printfulProduct.id]
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
case Printful.Webhook.Event.ProductDeleted: {
|
||||
@@ -257,7 +264,14 @@ export const app = new Elysia()
|
||||
app.listen(3000, async () => {
|
||||
if (cluster.worker?.id === 1) {
|
||||
log.info({ port: 3000 }, "server started")
|
||||
await syncService.syncNext().catch((err) => log.error({ err }, "failed to resume product sync queue"));
|
||||
|
||||
try {
|
||||
const frontQueuedSync = await syncService.queue.front();
|
||||
if (frontQueuedSync) await syncService.sync(frontQueuedSync);
|
||||
}
|
||||
catch (err) {
|
||||
log.error({ err }, "failed to resume product sync queue")
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user