Skip to content

Commit

Permalink
chore: add payload for integration and send function
Browse files Browse the repository at this point in the history
for easy to fetch origin data and debug later
  • Loading branch information
moonrailgun committed Oct 3, 2024
1 parent 88fa90c commit ea7e68e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "FeedEvent" ADD COLUMN "payload" JSON;
3 changes: 3 additions & 0 deletions src/server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,9 @@ model FeedEvent {
url String? // url link
important 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)
Expand Down
10 changes: 10 additions & 0 deletions src/server/prisma/zod/feedevent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Json> = z.lazy(() => z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)]))

export const FeedEventModelSchema = z.object({
id: z.string(),
channelId: z.string(),
Expand All @@ -16,6 +22,10 @@ export const FeedEventModelSchema = z.object({
url: z.string().nullish(),
important: z.boolean(),
archived: z.boolean(),
/**
* [Nullable<PrismaJson.CommonPayload>]
*/
payload: imports.CommonPayloadSchema.nullish(),
})

export interface CompleteFeedEvent extends z.infer<typeof FeedEventModelSchema> {
Expand Down
18 changes: 17 additions & 1 deletion src/server/trpc/routers/feed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -294,6 +309,7 @@ export const feedRouter = router({
senderId: true,
senderName: true,
important: true,
payload: true,
}).merge(
z.object({
channelId: z.string(),
Expand All @@ -311,7 +327,7 @@ export const feedRouter = router({
},
});

return event;
return event as z.infer<typeof FeedEventModelSchema>;
}),
archiveEvent: workspaceAdminProcedure
.meta(
Expand Down
8 changes: 7 additions & 1 deletion src/server/trpc/routers/feed/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -126,6 +127,7 @@ export const feedIntegrationRouter = router({
senderName,
important: false,
url,
payload: data,
});
} else if (action === 'deleted') {
await createFeedEvent(workspaceId, {
Expand All @@ -138,6 +140,7 @@ export const feedIntegrationRouter = router({
senderName,
important: false,
url,
payload: data,
});
}

Expand Down Expand Up @@ -172,6 +175,7 @@ export const feedIntegrationRouter = router({
senderName,
important: false,
url,
payload: data,
});

return 'ok';
Expand Down Expand Up @@ -244,6 +248,7 @@ export const feedIntegrationRouter = router({
senderId: alarm.alarmObjInfo.appId,
senderName: alarm.alarmPolicyInfo.policyName,
important: alarm.alarmStatus === '1',
payload: data,
});

return 'ok';
Expand All @@ -265,6 +270,7 @@ export const feedIntegrationRouter = router({
senderId: alarm.alarmObjInfo.appId,
senderName: alarm.alarmPolicyInfo.policyName,
important: alarm.alarmStatus === '1',
payload: data,
});

return 'ok';
Expand Down
1 change: 1 addition & 0 deletions src/server/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare global {

namespace PrismaJson {
type CommonPayload = Record<string, any>;
type Nullable<T> = Record<string, any> | null | undefined;
type DashboardLayout = {
layouts: Record<string, any[]>;
items: any[];
Expand Down

0 comments on commit ea7e68e

Please sign in to comment.