From 572d96babb348858911105659bfe304e869915e4 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Thu, 3 Oct 2024 22:16:17 +0800 Subject: [PATCH] feat: add payload for feed event integration and send function for easy to fetch origin data and debug later --- .../migration.sql | 2 ++ src/server/prisma/schema.prisma | 3 +++ src/server/prisma/zod/feedevent.ts | 10 ++++++++++ src/server/trpc/routers/feed/index.ts | 18 +++++++++++++++++- src/server/trpc/routers/feed/integration.ts | 8 +++++++- src/server/types/global.d.ts | 1 + 6 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/server/prisma/migrations/20241003134353_add_feed_event_payload/migration.sql diff --git a/src/server/prisma/migrations/20241003134353_add_feed_event_payload/migration.sql b/src/server/prisma/migrations/20241003134353_add_feed_event_payload/migration.sql new file mode 100644 index 0000000..10614b7 --- /dev/null +++ b/src/server/prisma/migrations/20241003134353_add_feed_event_payload/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "FeedEvent" ADD COLUMN "payload" JSON; diff --git a/src/server/prisma/schema.prisma b/src/server/prisma/schema.prisma index ea4ddbc..82ab3b6 100644 --- a/src/server/prisma/schema.prisma +++ b/src/server/prisma/schema.prisma @@ -567,6 +567,9 @@ model FeedEvent { url String? // url link important Boolean archived Boolean @default(false) @db.Boolean + /// [Nullable] + /// @zod.custom(imports.CommonPayloadSchema.nullish()) + payload Json? @db.Json channel FeedChannel @relation(fields: [channelId], references: [id], onUpdate: Cascade, onDelete: Cascade) diff --git a/src/server/prisma/zod/feedevent.ts b/src/server/prisma/zod/feedevent.ts index 6cb6fe7..e5850cd 100644 --- a/src/server/prisma/zod/feedevent.ts +++ b/src/server/prisma/zod/feedevent.ts @@ -2,6 +2,12 @@ import * as z from "zod" import * as imports from "./schemas/index.js" import { CompleteFeedChannel, RelatedFeedChannelModelSchema } from "./index.js" +// Helper schema for JSON fields +type Literal = boolean | number | string +type Json = Literal | { [key: string]: Json } | Json[] +const literalSchema = z.union([z.string(), z.number(), z.boolean()]) +const jsonSchema: z.ZodSchema = z.lazy(() => z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])) + export const FeedEventModelSchema = z.object({ id: z.string(), channelId: z.string(), @@ -16,6 +22,10 @@ export const FeedEventModelSchema = z.object({ url: z.string().nullish(), important: z.boolean(), archived: z.boolean(), + /** + * [Nullable] + */ + payload: imports.CommonPayloadSchema.nullish(), }) export interface CompleteFeedEvent extends z.infer { diff --git a/src/server/trpc/routers/feed/index.ts b/src/server/trpc/routers/feed/index.ts index d6b095c..1f55cec 100644 --- a/src/server/trpc/routers/feed/index.ts +++ b/src/server/trpc/routers/feed/index.ts @@ -194,6 +194,21 @@ export const feedRouter = router({ channelId, archived, }, + select: { + id: true, + channelId: true, + createdAt: true, + updatedAt: true, + eventName: true, + eventContent: true, + tags: true, + source: true, + senderId: true, + senderName: true, + url: true, + important: true, + archived: true, + }, limit, cursor, }); @@ -294,6 +309,7 @@ export const feedRouter = router({ senderId: true, senderName: true, important: true, + payload: true, }).merge( z.object({ channelId: z.string(), @@ -311,7 +327,7 @@ export const feedRouter = router({ }, }); - return event; + return event as z.infer; }), archiveEvent: workspaceAdminProcedure .meta( diff --git a/src/server/trpc/routers/feed/integration.ts b/src/server/trpc/routers/feed/integration.ts index 113331e..da4d14e 100644 --- a/src/server/trpc/routers/feed/integration.ts +++ b/src/server/trpc/routers/feed/integration.ts @@ -95,13 +95,14 @@ export const feedIntegrationRouter = router({ await createFeedEvent(workspaceId, { channelId: channelId, eventName: eventType, - eventContent: `[${pusherName}](mailto:${pusherEmail}) push commit **${commits}** to **${ref}** in [${fullName}](${repoUrl})`, + eventContent: `[${pusherName}](mailto:${pusherEmail}) push commit ${commits ? `**${commits}**` : ''} to **${ref}** in [${fullName}](${repoUrl})`, tags: [], source: 'github', senderId, senderName, important: false, url, + payload: data, }); return 'ok'; @@ -126,6 +127,7 @@ export const feedIntegrationRouter = router({ senderName, important: false, url, + payload: data, }); } else if (action === 'deleted') { await createFeedEvent(workspaceId, { @@ -138,6 +140,7 @@ export const feedIntegrationRouter = router({ senderName, important: false, url, + payload: data, }); } @@ -172,6 +175,7 @@ export const feedIntegrationRouter = router({ senderName, important: false, url, + payload: data, }); return 'ok'; @@ -244,6 +248,7 @@ export const feedIntegrationRouter = router({ senderId: alarm.alarmObjInfo.appId, senderName: alarm.alarmPolicyInfo.policyName, important: alarm.alarmStatus === '1', + payload: data, }); return 'ok'; @@ -265,6 +270,7 @@ export const feedIntegrationRouter = router({ senderId: alarm.alarmObjInfo.appId, senderName: alarm.alarmPolicyInfo.policyName, important: alarm.alarmStatus === '1', + payload: data, }); return 'ok'; diff --git a/src/server/types/global.d.ts b/src/server/types/global.d.ts index 5c325ce..9d66f61 100644 --- a/src/server/types/global.d.ts +++ b/src/server/types/global.d.ts @@ -12,6 +12,7 @@ declare global { namespace PrismaJson { type CommonPayload = Record; + type Nullable = Record | null | undefined; type DashboardLayout = { layouts: Record; items: any[];