refactor(api): extract webhook procedures
This commit is contained in:
@@ -25,6 +25,7 @@ Done
|
|||||||
- `system-role-config`
|
- `system-role-config`
|
||||||
- `audit-log`
|
- `audit-log`
|
||||||
- `calculation-rules`
|
- `calculation-rules`
|
||||||
|
- `webhook`
|
||||||
|
|
||||||
Ready next
|
Ready next
|
||||||
- none in the conflict-safe backlog
|
- none in the conflict-safe backlog
|
||||||
|
|||||||
@@ -0,0 +1,206 @@
|
|||||||
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { createAuditEntry } from "../lib/audit.js";
|
||||||
|
import {
|
||||||
|
createWebhook,
|
||||||
|
deleteWebhook,
|
||||||
|
getWebhookById,
|
||||||
|
listWebhooks,
|
||||||
|
testWebhook,
|
||||||
|
updateWebhook,
|
||||||
|
} from "../router/webhook-procedure-support.js";
|
||||||
|
import * as webhookSupport from "../router/webhook-support.js";
|
||||||
|
|
||||||
|
vi.mock("../lib/audit.js", () => ({
|
||||||
|
createAuditEntry: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
function createContext(db: Record<string, unknown>) {
|
||||||
|
return {
|
||||||
|
db: db as never,
|
||||||
|
dbUser: { id: "user_admin" },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("webhook-procedure-support", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lists webhooks ordered by creation date descending", async () => {
|
||||||
|
const findMany = vi.fn().mockResolvedValue([{ id: "wh_1" }]);
|
||||||
|
const ctx = createContext({
|
||||||
|
webhook: { findMany },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await listWebhooks(ctx);
|
||||||
|
|
||||||
|
expect(result).toEqual([{ id: "wh_1" }]);
|
||||||
|
expect(findMany).toHaveBeenCalledWith({
|
||||||
|
orderBy: { createdAt: "desc" },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("loads a webhook by id", async () => {
|
||||||
|
const findUnique = vi.fn().mockResolvedValue({ id: "wh_1", name: "Primary" });
|
||||||
|
const ctx = createContext({
|
||||||
|
webhook: { findUnique },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await getWebhookById(ctx, { id: "wh_1" });
|
||||||
|
|
||||||
|
expect(result).toEqual({ id: "wh_1", name: "Primary" });
|
||||||
|
expect(findUnique).toHaveBeenCalledWith({ where: { id: "wh_1" } });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates a webhook and records an audit entry", async () => {
|
||||||
|
const created = {
|
||||||
|
id: "wh_2",
|
||||||
|
name: "Primary",
|
||||||
|
url: "https://example.com/webhook",
|
||||||
|
events: ["project.created"],
|
||||||
|
isActive: true,
|
||||||
|
};
|
||||||
|
const create = vi.fn().mockResolvedValue(created);
|
||||||
|
const ctx = createContext({
|
||||||
|
webhook: { create },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await createWebhook(ctx, {
|
||||||
|
name: "Primary",
|
||||||
|
url: "https://example.com/webhook",
|
||||||
|
events: ["project.created"],
|
||||||
|
isActive: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toBe(created);
|
||||||
|
expect(create).toHaveBeenCalledWith({
|
||||||
|
data: {
|
||||||
|
name: "Primary",
|
||||||
|
url: "https://example.com/webhook",
|
||||||
|
events: ["project.created"],
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(createAuditEntry).toHaveBeenCalledWith(expect.objectContaining({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "Webhook",
|
||||||
|
entityId: "wh_2",
|
||||||
|
entityName: "Primary",
|
||||||
|
action: "CREATE",
|
||||||
|
userId: "user_admin",
|
||||||
|
after: created,
|
||||||
|
source: "ui",
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("updates a webhook and records before/after audit snapshots", async () => {
|
||||||
|
const before = {
|
||||||
|
id: "wh_3",
|
||||||
|
name: "Primary",
|
||||||
|
url: "https://old.example.com/webhook",
|
||||||
|
};
|
||||||
|
const after = {
|
||||||
|
id: "wh_3",
|
||||||
|
name: "Primary",
|
||||||
|
url: "https://new.example.com/webhook",
|
||||||
|
};
|
||||||
|
const findUnique = vi.fn().mockResolvedValue(before);
|
||||||
|
const update = vi.fn().mockResolvedValue(after);
|
||||||
|
const ctx = createContext({
|
||||||
|
webhook: { findUnique, update },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await updateWebhook(ctx, {
|
||||||
|
id: "wh_3",
|
||||||
|
data: { url: "https://new.example.com/webhook" },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toBe(after);
|
||||||
|
expect(findUnique).toHaveBeenCalledWith({ where: { id: "wh_3" } });
|
||||||
|
expect(update).toHaveBeenCalledWith({
|
||||||
|
where: { id: "wh_3" },
|
||||||
|
data: {
|
||||||
|
url: "https://new.example.com/webhook",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(createAuditEntry).toHaveBeenCalledWith(expect.objectContaining({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "Webhook",
|
||||||
|
entityId: "wh_3",
|
||||||
|
entityName: "Primary",
|
||||||
|
action: "UPDATE",
|
||||||
|
userId: "user_admin",
|
||||||
|
before,
|
||||||
|
after,
|
||||||
|
source: "ui",
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("deletes a webhook and records a delete audit entry", async () => {
|
||||||
|
const existing = { id: "wh_4", name: "Legacy Webhook" };
|
||||||
|
const findUnique = vi.fn().mockResolvedValue(existing);
|
||||||
|
const deleteFn = vi.fn().mockResolvedValue(existing);
|
||||||
|
const ctx = createContext({
|
||||||
|
webhook: { findUnique, delete: deleteFn },
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(deleteWebhook(ctx, { id: "wh_4" })).resolves.toBeUndefined();
|
||||||
|
|
||||||
|
expect(findUnique).toHaveBeenCalledWith({ where: { id: "wh_4" } });
|
||||||
|
expect(deleteFn).toHaveBeenCalledWith({ where: { id: "wh_4" } });
|
||||||
|
expect(createAuditEntry).toHaveBeenCalledWith(expect.objectContaining({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "Webhook",
|
||||||
|
entityId: "wh_4",
|
||||||
|
entityName: "Legacy Webhook",
|
||||||
|
action: "DELETE",
|
||||||
|
userId: "user_admin",
|
||||||
|
before: existing,
|
||||||
|
source: "ui",
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("tests a webhook delivery and records the result", async () => {
|
||||||
|
const existing = {
|
||||||
|
id: "wh_5",
|
||||||
|
name: "Primary",
|
||||||
|
url: "https://example.com/webhook",
|
||||||
|
secret: null,
|
||||||
|
};
|
||||||
|
const findUnique = vi.fn().mockResolvedValue(existing);
|
||||||
|
const sendWebhookTestRequestSpy = vi
|
||||||
|
.spyOn(webhookSupport, "sendWebhookTestRequest")
|
||||||
|
.mockResolvedValue({
|
||||||
|
success: true,
|
||||||
|
statusCode: 202,
|
||||||
|
statusText: "Accepted",
|
||||||
|
});
|
||||||
|
const ctx = createContext({
|
||||||
|
webhook: { findUnique },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await testWebhook(ctx, { id: "wh_5" });
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
success: true,
|
||||||
|
statusCode: 202,
|
||||||
|
statusText: "Accepted",
|
||||||
|
});
|
||||||
|
expect(sendWebhookTestRequestSpy).toHaveBeenCalledWith(existing);
|
||||||
|
expect(createAuditEntry).toHaveBeenCalledWith(expect.objectContaining({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "Webhook",
|
||||||
|
entityId: "wh_5",
|
||||||
|
entityName: "Primary",
|
||||||
|
action: "UPDATE",
|
||||||
|
userId: "user_admin",
|
||||||
|
summary: "Tested webhook (result: success)",
|
||||||
|
metadata: {
|
||||||
|
success: true,
|
||||||
|
statusCode: 202,
|
||||||
|
statusText: "Accepted",
|
||||||
|
},
|
||||||
|
source: "ui",
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
import { createAuditEntry } from "../lib/audit.js";
|
||||||
|
import type { TRPCContext } from "../trpc.js";
|
||||||
|
import {
|
||||||
|
buildWebhookCreateData,
|
||||||
|
buildWebhookUpdateData,
|
||||||
|
createWebhookInputSchema,
|
||||||
|
loadWebhookOrThrow,
|
||||||
|
sendWebhookTestRequest,
|
||||||
|
updateWebhookInputSchema,
|
||||||
|
} from "./webhook-support.js";
|
||||||
|
|
||||||
|
export const WebhookIdInputSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const CreateWebhookInputSchema = createWebhookInputSchema;
|
||||||
|
|
||||||
|
export const UpdateWebhookProcedureInputSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
|
data: updateWebhookInputSchema,
|
||||||
|
});
|
||||||
|
|
||||||
|
type WebhookProcedureContext = Pick<TRPCContext, "db" | "dbUser">;
|
||||||
|
|
||||||
|
function withAuditUser(userId: string | undefined) {
|
||||||
|
return userId ? { userId } : {};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function listWebhooks(ctx: WebhookProcedureContext) {
|
||||||
|
return ctx.db.webhook.findMany({
|
||||||
|
orderBy: { createdAt: "desc" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getWebhookById(
|
||||||
|
ctx: WebhookProcedureContext,
|
||||||
|
input: z.infer<typeof WebhookIdInputSchema>,
|
||||||
|
) {
|
||||||
|
return loadWebhookOrThrow(ctx.db, input.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createWebhook(
|
||||||
|
ctx: WebhookProcedureContext,
|
||||||
|
input: z.infer<typeof CreateWebhookInputSchema>,
|
||||||
|
) {
|
||||||
|
const webhook = await ctx.db.webhook.create({
|
||||||
|
data: buildWebhookCreateData(input),
|
||||||
|
});
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "Webhook",
|
||||||
|
entityId: webhook.id,
|
||||||
|
entityName: webhook.name,
|
||||||
|
action: "CREATE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
after: webhook as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return webhook;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateWebhook(
|
||||||
|
ctx: WebhookProcedureContext,
|
||||||
|
input: z.infer<typeof UpdateWebhookProcedureInputSchema>,
|
||||||
|
) {
|
||||||
|
const existing = await loadWebhookOrThrow(ctx.db, input.id);
|
||||||
|
|
||||||
|
const updated = await ctx.db.webhook.update({
|
||||||
|
where: { id: input.id },
|
||||||
|
data: buildWebhookUpdateData(input.data),
|
||||||
|
});
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "Webhook",
|
||||||
|
entityId: input.id,
|
||||||
|
entityName: updated.name,
|
||||||
|
action: "UPDATE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
before: existing as unknown as Record<string, unknown>,
|
||||||
|
after: updated as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteWebhook(
|
||||||
|
ctx: WebhookProcedureContext,
|
||||||
|
input: z.infer<typeof WebhookIdInputSchema>,
|
||||||
|
) {
|
||||||
|
const existing = await loadWebhookOrThrow(ctx.db, input.id);
|
||||||
|
await ctx.db.webhook.delete({ where: { id: input.id } });
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "Webhook",
|
||||||
|
entityId: input.id,
|
||||||
|
entityName: existing.name,
|
||||||
|
action: "DELETE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
before: existing as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function testWebhook(
|
||||||
|
ctx: WebhookProcedureContext,
|
||||||
|
input: z.infer<typeof WebhookIdInputSchema>,
|
||||||
|
) {
|
||||||
|
const webhook = await loadWebhookOrThrow(ctx.db, input.id);
|
||||||
|
const result = await sendWebhookTestRequest(webhook);
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "Webhook",
|
||||||
|
entityId: webhook.id,
|
||||||
|
entityName: webhook.name,
|
||||||
|
action: "UPDATE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
summary: `Tested webhook (result: ${result.success ? "success" : "failed"})`,
|
||||||
|
metadata: result as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
@@ -1,119 +1,42 @@
|
|||||||
import { z } from "zod";
|
|
||||||
import { createTRPCRouter, adminProcedure } from "../trpc.js";
|
import { createTRPCRouter, adminProcedure } from "../trpc.js";
|
||||||
import { createAuditEntry } from "../lib/audit.js";
|
|
||||||
import {
|
import {
|
||||||
buildWebhookCreateData,
|
CreateWebhookInputSchema,
|
||||||
buildWebhookUpdateData,
|
createWebhook,
|
||||||
createWebhookInputSchema,
|
deleteWebhook,
|
||||||
loadWebhookOrThrow,
|
getWebhookById,
|
||||||
sendWebhookTestRequest,
|
listWebhooks,
|
||||||
updateWebhookInputSchema,
|
testWebhook,
|
||||||
} from "./webhook-support.js";
|
UpdateWebhookProcedureInputSchema,
|
||||||
|
updateWebhook,
|
||||||
|
WebhookIdInputSchema,
|
||||||
|
} from "./webhook-procedure-support.js";
|
||||||
|
|
||||||
export const webhookRouter = createTRPCRouter({
|
export const webhookRouter = createTRPCRouter({
|
||||||
/** List all webhooks. */
|
/** List all webhooks. */
|
||||||
list: adminProcedure.query(async ({ ctx }) => {
|
list: adminProcedure.query(({ ctx }) => listWebhooks(ctx)),
|
||||||
return ctx.db.webhook.findMany({
|
|
||||||
orderBy: { createdAt: "desc" },
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
|
|
||||||
/** Get a single webhook by ID. */
|
/** Get a single webhook by ID. */
|
||||||
getById: adminProcedure
|
getById: adminProcedure
|
||||||
.input(z.object({ id: z.string() }))
|
.input(WebhookIdInputSchema)
|
||||||
.query(async ({ ctx, input }) => loadWebhookOrThrow(ctx.db, input.id)),
|
.query(({ ctx, input }) => getWebhookById(ctx, input)),
|
||||||
|
|
||||||
/** Create a new webhook. */
|
/** Create a new webhook. */
|
||||||
create: adminProcedure
|
create: adminProcedure
|
||||||
.input(createWebhookInputSchema)
|
.input(CreateWebhookInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => createWebhook(ctx, input)),
|
||||||
const webhook = await ctx.db.webhook.create({
|
|
||||||
data: buildWebhookCreateData(input),
|
|
||||||
});
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "Webhook",
|
|
||||||
entityId: webhook.id,
|
|
||||||
entityName: webhook.name,
|
|
||||||
action: "CREATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
after: webhook as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return webhook;
|
|
||||||
}),
|
|
||||||
|
|
||||||
/** Update an existing webhook. */
|
/** Update an existing webhook. */
|
||||||
update: adminProcedure
|
update: adminProcedure
|
||||||
.input(
|
.input(UpdateWebhookProcedureInputSchema)
|
||||||
z.object({
|
.mutation(({ ctx, input }) => updateWebhook(ctx, input)),
|
||||||
id: z.string(),
|
|
||||||
data: updateWebhookInputSchema,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.mutation(async ({ ctx, input }) => {
|
|
||||||
const existing = await loadWebhookOrThrow(ctx.db, input.id);
|
|
||||||
|
|
||||||
const updated = await ctx.db.webhook.update({
|
|
||||||
where: { id: input.id },
|
|
||||||
data: buildWebhookUpdateData(input.data),
|
|
||||||
});
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "Webhook",
|
|
||||||
entityId: input.id,
|
|
||||||
entityName: updated.name,
|
|
||||||
action: "UPDATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
before: existing as unknown as Record<string, unknown>,
|
|
||||||
after: updated as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return updated;
|
|
||||||
}),
|
|
||||||
|
|
||||||
/** Delete a webhook. */
|
/** Delete a webhook. */
|
||||||
delete: adminProcedure
|
delete: adminProcedure
|
||||||
.input(z.object({ id: z.string() }))
|
.input(WebhookIdInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => deleteWebhook(ctx, input)),
|
||||||
const existing = await loadWebhookOrThrow(ctx.db, input.id);
|
|
||||||
await ctx.db.webhook.delete({ where: { id: input.id } });
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "Webhook",
|
|
||||||
entityId: input.id,
|
|
||||||
entityName: existing.name,
|
|
||||||
action: "DELETE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
before: existing as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
|
|
||||||
/** Send a test payload to a webhook URL. */
|
/** Send a test payload to a webhook URL. */
|
||||||
test: adminProcedure
|
test: adminProcedure
|
||||||
.input(z.object({ id: z.string() }))
|
.input(WebhookIdInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => testWebhook(ctx, input)),
|
||||||
const wh = await loadWebhookOrThrow(ctx.db, input.id);
|
|
||||||
const result = await sendWebhookTestRequest(wh);
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "Webhook",
|
|
||||||
entityId: wh.id,
|
|
||||||
entityName: wh.name,
|
|
||||||
action: "UPDATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
summary: `Tested webhook (result: ${result.success ? "success" : "failed"})`,
|
|
||||||
metadata: result as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user