Files
ProjectE/apps/api/src/middleware/webhook-queue.ts
T
bot-hermes a60b75f075 feat: full plan execution - CI/CD, critical fixes, UX polish, secondary/advanced features, E2E + docs
Phase 0 (CI/CD): fix root typecheck to cover api+worker+web; reconcile migration
story into idempotent db:migrate (db:sync + db:triggers); add Gitea Actions
quality/deploy/smoke workflow; rewrite README/AGENTS/DEPLOY docs; add
requireWorkspaceAccess + recordActivityForEntity conventions.

Phase 1 (critical fixes): calendar delete + drag/resize DnD; canvas card CRUD +
bulk save + debounced autosave; logout route; graph edge workspaceId derivation;
real analytics endpoints (drop Math.random); task board droppable columns +
reorder persistence; Tiptap notes editor with sanitized HTML rendering; remove
insecure passkey auth; domain/owner scoping (IDOR) on all by-ID routes + search/
export/realtime scoping; command palette routing + agent mention fetch; agent
activity SSE handler; graph fly-to with tracked positions.

Phase 2 (UX polish): login on design system; Sonner toasts app-wide; shared
Loading/Empty/Error state components; working density/sidebarPos/reduce-motion
settings; Inter typography; consolidated status-colors lib; unified detail
routes; dashboard sort/realtime/responsive fixes; mobile responsive; a11y
(radiogroups, sanitized snippets, badge labels).

Phase 3 (features): daily notes timezone fix + delete + autosave + mood/energy
create; active-domain store + topbar picker; graph domain picker + navigable
entity links; tag assign/remove UI + server-side tag filter; real CSV export +
import validation; custom fields on tasks.

Phase 4 (advanced): migrate job worker into apps/worker (webhook delivery with
HMAC, recurring spawn, ai_dispatch disabled); webhook queue helper + entity
event enqueuing + test endpoint fix; recurring scheduledJobs pipeline; agents
CRUD + permission editing + activity filters; real notifications feed; MCP
polish (validation, error codes, domain scoping, dead sql leftover).

Phase 5 (E2E + docs): rewrite Playwright suite for the Vite SPA (15 specs, new
auth helpers, chromium-only in CI); add ephemeral-Postgres e2e CI job; rewrite
docs/API.md for the real Hono API.
2026-08-10 08:53:18 +00:00

85 lines
2.1 KiB
TypeScript

import { db, jobs, webhooks } from "@project-e/db";
import { and, eq } from "drizzle-orm";
/**
* Enqueue a single webhook delivery job for a specific webhook. Used directly by
* the test endpoint and by `enqueueWebhooks` for every matching webhook. The
* worker reads this job, signs the payload with the webhook secret, delivers it
* via fetch, and records the delivery row.
*/
export async function enqueueWebhookDelivery({
webhookId,
event,
entityType,
entityId,
data,
workspaceId,
}: {
webhookId: string;
event: string;
entityType: string;
entityId: string;
data?: Record<string, unknown>;
workspaceId: string;
}): Promise<void> {
await db.insert(jobs).values({
type: "webhook_delivery",
payload: {
webhook_id: webhookId,
event,
entity_type: entityType,
entity_id: entityId,
data: data ?? {},
timestamp: new Date().toISOString(),
workspace_id: workspaceId,
},
status: "pending",
});
}
/**
* Enqueue webhook deliveries for every active webhook in a workspace that
* subscribes to the given event. Never throws — failures are logged and the
* caller's request proceeds, matching the activity-feed behavior.
*/
export async function enqueueWebhooks({
workspaceId,
event,
entityType,
entityId,
data,
}: {
workspaceId: string;
event: string;
entityType: string;
entityId: string;
data?: Record<string, unknown>;
}): Promise<void> {
try {
const activeWebhooks = await db.select()
.from(webhooks)
.where(and(eq(webhooks.workspaceId, workspaceId), eq(webhooks.active, true)));
const matches = activeWebhooks.filter((webhook) =>
(webhook.events ?? []).includes(event)
);
for (const webhook of matches) {
await enqueueWebhookDelivery({
webhookId: webhook.id,
event,
entityType,
entityId,
data,
workspaceId,
});
}
if (matches.length > 0) {
console.log(`[webhooks] Enqueued ${matches.length} delivery job(s) for ${entityType}.${event}`);
}
} catch (error) {
console.error(`[webhooks] Failed to enqueue webhook deliveries for ${entityType}.${event}:`, error);
}
}