feat: add payload for feed event integration and send function

for easy to fetch origin data and debug later
This commit is contained in:
moonrailgun 2024-10-03 22:16:17 +08:00
parent 88fa90c2ca
commit 572d96babb
6 changed files with 40 additions and 2 deletions

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "FeedEvent" ADD COLUMN "payload" JSON;

View File

@ -567,6 +567,9 @@ model FeedEvent {
url String? // url link url String? // url link
important Boolean important Boolean
archived Boolean @default(false) @db.Boolean archived Boolean @default(false) @db.Boolean
/// [Nullable<PrismaJson.CommonPayload>]
/// @zod.custom(imports.CommonPayloadSchema.nullish())
payload Json? @db.Json
channel FeedChannel @relation(fields: [channelId], references: [id], onUpdate: Cascade, onDelete: Cascade) channel FeedChannel @relation(fields: [channelId], references: [id], onUpdate: Cascade, onDelete: Cascade)

View File

@ -2,6 +2,12 @@ import * as z from "zod"
import * as imports from "./schemas/index.js" import * as imports from "./schemas/index.js"
import { CompleteFeedChannel, RelatedFeedChannelModelSchema } from "./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<Json> = z.lazy(() => z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)]))
export const FeedEventModelSchema = z.object({ export const FeedEventModelSchema = z.object({
id: z.string(), id: z.string(),
channelId: z.string(), channelId: z.string(),
@ -16,6 +22,10 @@ export const FeedEventModelSchema = z.object({
url: z.string().nullish(), url: z.string().nullish(),
important: z.boolean(), important: z.boolean(),
archived: z.boolean(), archived: z.boolean(),
/**
* [Nullable<PrismaJson.CommonPayload>]
*/
payload: imports.CommonPayloadSchema.nullish(),
}) })
export interface CompleteFeedEvent extends z.infer<typeof FeedEventModelSchema> { export interface CompleteFeedEvent extends z.infer<typeof FeedEventModelSchema> {

View File

@ -194,6 +194,21 @@ export const feedRouter = router({
channelId, channelId,
archived, 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, limit,
cursor, cursor,
}); });
@ -294,6 +309,7 @@ export const feedRouter = router({
senderId: true, senderId: true,
senderName: true, senderName: true,
important: true, important: true,
payload: true,
}).merge( }).merge(
z.object({ z.object({
channelId: z.string(), channelId: z.string(),
@ -311,7 +327,7 @@ export const feedRouter = router({
}, },
}); });
return event; return event as z.infer<typeof FeedEventModelSchema>;
}), }),
archiveEvent: workspaceAdminProcedure archiveEvent: workspaceAdminProcedure
.meta( .meta(

View File

@ -95,13 +95,14 @@ export const feedIntegrationRouter = router({
await createFeedEvent(workspaceId, { await createFeedEvent(workspaceId, {
channelId: channelId, channelId: channelId,
eventName: eventType, 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: [], tags: [],
source: 'github', source: 'github',
senderId, senderId,
senderName, senderName,
important: false, important: false,
url, url,
payload: data,
}); });
return 'ok'; return 'ok';
@ -126,6 +127,7 @@ export const feedIntegrationRouter = router({
senderName, senderName,
important: false, important: false,
url, url,
payload: data,
}); });
} else if (action === 'deleted') { } else if (action === 'deleted') {
await createFeedEvent(workspaceId, { await createFeedEvent(workspaceId, {
@ -138,6 +140,7 @@ export const feedIntegrationRouter = router({
senderName, senderName,
important: false, important: false,
url, url,
payload: data,
}); });
} }
@ -172,6 +175,7 @@ export const feedIntegrationRouter = router({
senderName, senderName,
important: false, important: false,
url, url,
payload: data,
}); });
return 'ok'; return 'ok';
@ -244,6 +248,7 @@ export const feedIntegrationRouter = router({
senderId: alarm.alarmObjInfo.appId, senderId: alarm.alarmObjInfo.appId,
senderName: alarm.alarmPolicyInfo.policyName, senderName: alarm.alarmPolicyInfo.policyName,
important: alarm.alarmStatus === '1', important: alarm.alarmStatus === '1',
payload: data,
}); });
return 'ok'; return 'ok';
@ -265,6 +270,7 @@ export const feedIntegrationRouter = router({
senderId: alarm.alarmObjInfo.appId, senderId: alarm.alarmObjInfo.appId,
senderName: alarm.alarmPolicyInfo.policyName, senderName: alarm.alarmPolicyInfo.policyName,
important: alarm.alarmStatus === '1', important: alarm.alarmStatus === '1',
payload: data,
}); });
return 'ok'; return 'ok';

View File

@ -12,6 +12,7 @@ declare global {
namespace PrismaJson { namespace PrismaJson {
type CommonPayload = Record<string, any>; type CommonPayload = Record<string, any>;
type Nullable<T> = Record<string, any> | null | undefined;
type DashboardLayout = { type DashboardLayout = {
layouts: Record<string, any[]>; layouts: Record<string, any[]>;
items: any[]; items: any[];