From 625374cafe3e9fd36bc1de65b7582e1bd9a04e98 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 29 Dec 2023 21:15:32 +0800 Subject: [PATCH] feat: add monitor order with updatedAt --- .../migration.sql | 2 + prisma/schema.prisma | 1 + prisma/zod/monitor.ts | 1 + src/server/trpc/routers/monitor.ts | 57 +++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 prisma/migrations/20231229130639_add_monitor_updated_time/migration.sql diff --git a/prisma/migrations/20231229130639_add_monitor_updated_time/migration.sql b/prisma/migrations/20231229130639_add_monitor_updated_time/migration.sql new file mode 100644 index 0000000..9470257 --- /dev/null +++ b/prisma/migrations/20231229130639_add_monitor_updated_time/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Monitor" ADD COLUMN "updatedAt" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index f118a4a..ce3854d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -261,6 +261,7 @@ model Monitor { /// [CommonPayload] payload Json @db.Json createdAt DateTime @default(now()) @db.Timestamptz(6) + updatedAt DateTime @default(now()) @updatedAt @db.Timestamptz(6) workspace Workspace @relation(fields: [workspaceId], references: [id]) diff --git a/prisma/zod/monitor.ts b/prisma/zod/monitor.ts index ae80008..455e784 100644 --- a/prisma/zod/monitor.ts +++ b/prisma/zod/monitor.ts @@ -20,6 +20,7 @@ export const MonitorModelSchema = z.object({ */ payload: jsonSchema, createdAt: z.date(), + updatedAt: z.date(), }) export interface CompleteMonitor extends z.infer { diff --git a/src/server/trpc/routers/monitor.ts b/src/server/trpc/routers/monitor.ts index ebf69b7..0494215 100644 --- a/src/server/trpc/routers/monitor.ts +++ b/src/server/trpc/routers/monitor.ts @@ -45,6 +45,9 @@ export const monitorRouter = router({ }, }, }, + orderBy: { + updatedAt: 'desc', + }, }); return monitors as MonitorInfoWithNotificationIds[]; @@ -426,6 +429,60 @@ export const monitorRouter = router({ return list; }), + clearEvents: workspaceOwnerProcedure + .meta( + buildMonitorOpenapi({ + method: 'DELETE', + path: '/clearEvents', + }) + ) + .input( + z.object({ + monitorId: z.string().cuid2(), + }) + ) + .output(z.number()) + .mutation(async ({ input }) => { + const { workspaceId, monitorId } = input; + + const { count } = await prisma.monitorEvent.deleteMany({ + where: { + monitor: { + id: monitorId, + workspaceId, + }, + }, + }); + + return count; + }), + clearData: workspaceOwnerProcedure + .meta( + buildMonitorOpenapi({ + method: 'DELETE', + path: '/clearData', + }) + ) + .input( + z.object({ + monitorId: z.string().cuid2(), + }) + ) + .output(z.number()) + .mutation(async ({ input }) => { + const { workspaceId, monitorId } = input; + + const { count } = await prisma.monitorData.deleteMany({ + where: { + monitor: { + id: monitorId, + workspaceId, + }, + }, + }); + + return count; + }), getStatus: workspaceProcedure .meta( buildMonitorOpenapi({