diff --git a/src/client/components/modals/monitor/MonitorInfoEditor.tsx b/src/client/components/modals/monitor/MonitorInfoEditor.tsx index 3792aff..4c3fda4 100644 --- a/src/client/components/modals/monitor/MonitorInfoEditor.tsx +++ b/src/client/components/modals/monitor/MonitorInfoEditor.tsx @@ -3,6 +3,7 @@ import type { Monitor } from '@prisma/client'; import { Button, Form, Input, InputNumber, Select } from 'antd'; import { monitorProviders } from './provider'; import { useEvent } from '../../../hooks/useEvent'; +import { NotificationPicker } from '../../notification/NotificationPicker'; export type MonitorInfoEditorValues = Omit< Monitor, @@ -10,6 +11,7 @@ export type MonitorInfoEditorValues = Omit< > & { id?: string; payload: Record; + notificationIds?: string[]; }; const defaultValues: Omit = { @@ -82,6 +84,10 @@ export const MonitorInfoEditor: React.FC = React.memo( {formEl} + + + + diff --git a/src/client/components/monitor/MonitorList.tsx b/src/client/components/monitor/MonitorList.tsx index 0f27e0c..47f2930 100644 --- a/src/client/components/monitor/MonitorList.tsx +++ b/src/client/components/monitor/MonitorList.tsx @@ -56,12 +56,12 @@ export const MonitorList: React.FC = React.memo(() => { } return ( -
+
{monitors.map((monitor) => (
{} +export const NotificationPicker: React.FC = React.memo( + (props) => { + const workspaceId = useCurrentWorkspaceId(); + const { data: allNotification = [] } = trpc.notification.all.useQuery({ + workspaceId, + }); + + return ( + + ); + } +); +NotificationPicker.displayName = 'NotificationPicker'; diff --git a/src/client/pages/Monitor/Edit.tsx b/src/client/pages/Monitor/Edit.tsx index 78c3cc4..7a5e6f0 100644 --- a/src/client/pages/Monitor/Edit.tsx +++ b/src/client/pages/Monitor/Edit.tsx @@ -31,7 +31,12 @@ export const MonitorEdit: React.FC = React.memo(() => { return (
n.id), + } as MonitorInfoEditorValues + } onSave={async (value) => { const monitor = await mutation.mutateAsync({ ...value, diff --git a/src/server/model/monitor/index.ts b/src/server/model/monitor/index.ts index ba455cf..1036503 100644 --- a/src/server/model/monitor/index.ts +++ b/src/server/model/monitor/index.ts @@ -8,7 +8,12 @@ import dayjs from 'dayjs'; export type MonitorUpsertData = Pick< Monitor, 'workspaceId' | 'name' | 'type' | 'interval' -> & { id?: string; active?: boolean; payload: Record }; +> & { + id?: string; + active?: boolean; + notificationIds?: string[]; + payload: Record; +}; type MonitorWithNotification = Monitor & { notifications: Notification[] }; @@ -21,13 +26,19 @@ class MonitorManager { */ async upsert(data: MonitorUpsertData): Promise { let monitor: MonitorWithNotification; - if (data.id) { + const { id, notificationIds = [], ...others } = data; + if (id) { // update monitor = await prisma.monitor.update({ where: { - id: data.id, + id, + }, + data: { + ...others, + notifications: { + set: notificationIds.map((id) => ({ id })), + }, }, - data: { ...data }, include: { notifications: true, }, @@ -37,7 +48,12 @@ class MonitorManager { } else { // create monitor = await prisma.monitor.create({ - data: { ...data }, + data: { + ...others, + notifications: { + connect: notificationIds.map((id) => ({ id })), + }, + }, include: { notifications: true, }, diff --git a/src/server/trpc/routers/monitor.ts b/src/server/trpc/routers/monitor.ts index f3b9398..cce8386 100644 --- a/src/server/trpc/routers/monitor.ts +++ b/src/server/trpc/routers/monitor.ts @@ -2,7 +2,7 @@ import { router, workspaceOwnerProcedure, workspaceProcedure } from '../trpc'; import { prisma } from '../../model/_client'; import { z } from 'zod'; import { monitorManager } from '../../model/monitor'; -import { MonitorInfo } from '../../../types'; +import { MonitorInfoWithNotificationIds } from '../../../types'; import dayjs from 'dayjs'; export const monitorRouter = router({ @@ -12,9 +12,16 @@ export const monitorRouter = router({ where: { workspaceId, }, + include: { + notifications: { + select: { + id: true, + }, + }, + }, }); - return monitors as MonitorInfo[]; + return monitors as MonitorInfoWithNotificationIds[]; }), get: workspaceProcedure .input( @@ -29,9 +36,16 @@ export const monitorRouter = router({ id, workspaceId, }, + include: { + notifications: { + select: { + id: true, + }, + }, + }, }); - return monitor as MonitorInfo; + return monitor as MonitorInfoWithNotificationIds; }), upsert: workspaceOwnerProcedure .input( @@ -41,11 +55,21 @@ export const monitorRouter = router({ type: z.string(), active: z.boolean().default(true), interval: z.number().int().default(20), + notificationIds: z.array(z.string()).default([]), payload: z.object({}).passthrough(), }) ) .mutation(async ({ input }) => { - const { id, workspaceId, name, type, active, interval, payload } = input; + const { + id, + workspaceId, + name, + type, + active, + interval, + notificationIds, + payload, + } = input; const monitor = await monitorManager.upsert({ id, @@ -54,6 +78,7 @@ export const monitorRouter = router({ type, active, interval, + notificationIds, payload, }); diff --git a/src/types/monitor.ts b/src/types/monitor.ts index 12cd2d4..e5ed36f 100644 --- a/src/types/monitor.ts +++ b/src/types/monitor.ts @@ -7,3 +7,7 @@ export type MonitorInfo = ExactType< payload: Record; } >; + +export type MonitorInfoWithNotificationIds = MonitorInfo & { + notifications: { id: string }[]; +};