refactor(api): extract webhook procedures
This commit is contained in:
@@ -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 { createAuditEntry } from "../lib/audit.js";
|
||||
import {
|
||||
buildWebhookCreateData,
|
||||
buildWebhookUpdateData,
|
||||
createWebhookInputSchema,
|
||||
loadWebhookOrThrow,
|
||||
sendWebhookTestRequest,
|
||||
updateWebhookInputSchema,
|
||||
} from "./webhook-support.js";
|
||||
CreateWebhookInputSchema,
|
||||
createWebhook,
|
||||
deleteWebhook,
|
||||
getWebhookById,
|
||||
listWebhooks,
|
||||
testWebhook,
|
||||
UpdateWebhookProcedureInputSchema,
|
||||
updateWebhook,
|
||||
WebhookIdInputSchema,
|
||||
} from "./webhook-procedure-support.js";
|
||||
|
||||
export const webhookRouter = createTRPCRouter({
|
||||
/** List all webhooks. */
|
||||
list: adminProcedure.query(async ({ ctx }) => {
|
||||
return ctx.db.webhook.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
}),
|
||||
list: adminProcedure.query(({ ctx }) => listWebhooks(ctx)),
|
||||
|
||||
/** Get a single webhook by ID. */
|
||||
getById: adminProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.query(async ({ ctx, input }) => loadWebhookOrThrow(ctx.db, input.id)),
|
||||
.input(WebhookIdInputSchema)
|
||||
.query(({ ctx, input }) => getWebhookById(ctx, input)),
|
||||
|
||||
/** Create a new webhook. */
|
||||
create: adminProcedure
|
||||
.input(createWebhookInputSchema)
|
||||
.mutation(async ({ 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;
|
||||
}),
|
||||
.input(CreateWebhookInputSchema)
|
||||
.mutation(({ ctx, input }) => createWebhook(ctx, input)),
|
||||
|
||||
/** Update an existing webhook. */
|
||||
update: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
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;
|
||||
}),
|
||||
.input(UpdateWebhookProcedureInputSchema)
|
||||
.mutation(({ ctx, input }) => updateWebhook(ctx, input)),
|
||||
|
||||
/** Delete a webhook. */
|
||||
delete: adminProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ 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",
|
||||
});
|
||||
}),
|
||||
.input(WebhookIdInputSchema)
|
||||
.mutation(({ ctx, input }) => deleteWebhook(ctx, input)),
|
||||
|
||||
/** Send a test payload to a webhook URL. */
|
||||
test: adminProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ 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;
|
||||
}),
|
||||
.input(WebhookIdInputSchema)
|
||||
.mutation(({ ctx, input }) => testWebhook(ctx, input)),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user