From 8e7186ebfa97893ca3b781fca1d50eb5a69b3932 Mon Sep 17 00:00:00 2001 From: Maximilian Kaske <56969857+mxkaske@users.noreply.github.com> Date: Thu, 25 Jun 2026 10:00:35 +0200 Subject: [PATCH] chore: audit monitor auto pause job (#2268) * chore: audit monitor auto pause job * fix: review --- apps/workflows/src/cron/monitor.ts | 32 +++-- .../src/monitor/__tests__/monitor.test.ts | 125 +++++++++++++++++- 2 files changed, 146 insertions(+), 11 deletions(-) diff --git a/apps/workflows/src/cron/monitor.ts b/apps/workflows/src/cron/monitor.ts index 0982d0888..e3f86dacc 100644 --- a/apps/workflows/src/cron/monitor.ts +++ b/apps/workflows/src/cron/monitor.ts @@ -11,7 +11,7 @@ import { or, schema, } from "@openstatus/db"; -import { user } from "@openstatus/db/src/schema"; +import { selectWorkspaceSchema, user } from "@openstatus/db/src/schema"; import { monitorDeactivationEmail, monitorPausedEmail, @@ -20,6 +20,7 @@ import { type EmailHtml, sendBatchEmailHtml, } from "@openstatus/emails/src/send"; +import { bulkUpdateMonitors } from "@openstatus/services/monitor"; import { Redis } from "@openstatus/upstash"; import { RateLimiter } from "limiter"; import { z } from "zod"; @@ -289,18 +290,28 @@ export async function StepPaused(userId: number, workFlowRunTimestamp: number) { return; } - const userWorkspace = await getUserWorkspace(userId); - if (userWorkspace) { - await db - .update(schema.monitor) - .set({ active: false }) + const workspace = await getUserWorkspace(userId); + if (workspace) { + const activeMonitors = await db + .select({ id: schema.monitor.id }) + .from(schema.monitor) .where( and( - eq(schema.monitor.workspaceId, userWorkspace.workspaceId), + eq(schema.monitor.workspaceId, workspace.id), eq(schema.monitor.active, true), isNull(schema.monitor.deletedAt), ), - ); + ) + .all(); + if (activeMonitors.length > 0) { + await bulkUpdateMonitors({ + ctx: { + workspace, + actor: { type: "system", job: "monitor-auto-pause" }, + }, + input: { ids: activeMonitors.map((m) => m.id), active: false }, + }); + } } const currentUser = await getUser(userId); @@ -445,8 +456,8 @@ async function sendWorkflowEmail({ } async function getUserWorkspace(userId: number) { - return db - .select({ workspaceId: schema.workspace.id }) + const row = await db + .select({ workspace: schema.workspace }) .from(schema.user) .innerJoin( schema.usersToWorkspaces, @@ -463,4 +474,5 @@ async function getUserWorkspace(userId: number) { ), ) .get(); + return row ? selectWorkspaceSchema.parse(row.workspace) : undefined; } diff --git a/packages/services/src/monitor/__tests__/monitor.test.ts b/packages/services/src/monitor/__tests__/monitor.test.ts index 10ca5d985..9979ea2f7 100644 --- a/packages/services/src/monitor/__tests__/monitor.test.ts +++ b/packages/services/src/monitor/__tests__/monitor.test.ts @@ -17,7 +17,9 @@ import { expectAuditRow, loadSeededWorkspace, makeApiKeyCtx, + makeSystemCtx, makeUserCtx, + readAuditLog, withTestTransaction, } from "../../../test/helpers"; import type { ServiceContext } from "../../context"; @@ -27,7 +29,7 @@ import { createMonitor } from "../create"; import { deleteMonitor, deleteMonitors } from "../delete"; import { getMonitor, listMonitors } from "../list"; import { updateMonitorNotifiers, updateMonitorTags } from "../relations"; -import { updateMonitorGeneral } from "../update"; +import { bulkUpdateMonitors, updateMonitorGeneral } from "../update"; const TEST_PREFIX = "svc-monitor-test"; @@ -579,3 +581,124 @@ describe("updateMonitorGeneral", () => { }); }); }); + +describe("bulkUpdateMonitors", () => { + test("system actor pauses active monitors and audits", async () => { + await withTestTransaction(async (tx) => { + const ctx = { ...teamCtx, db: tx }; + const a = await createMonitor({ + ctx, + input: { + name: `${TEST_PREFIX}-bulk-a`, + jobType: "http", + url: "https://example.com", + method: "GET", + headers: [], + assertions: [], + active: true, + }, + }); + const b = await createMonitor({ + ctx, + input: { + name: `${TEST_PREFIX}-bulk-b`, + jobType: "http", + url: "https://example.com", + method: "GET", + headers: [], + assertions: [], + active: true, + }, + }); + + await bulkUpdateMonitors({ + ctx: { + ...makeSystemCtx(teamCtx.workspace, { job: "monitor-auto-pause" }), + db: tx, + }, + input: { ids: [a.id, b.id], active: false }, + }); + + const rows = await tx + .select() + .from(monitor) + .where(inArray(monitor.id, [a.id, b.id])) + .all(); + expect(rows).toHaveLength(2); + for (const row of rows) { + expect(row.active).toBe(false); + } + + for (const id of [a.id, b.id]) { + const audits = await readAuditLog({ + workspaceId: teamCtx.workspace.id, + entityType: "monitor", + entityId: id, + db: tx, + }); + const hit = audits.find( + (row) => + row.action === "monitor.update" && row.actorType === "system", + ); + expect(hit).toBeDefined(); + expect(hit?.actorId).toBe("monitor-auto-pause"); + } + }); + }); + + test("ignores ids outside the workspace", async () => { + await withTestTransaction(async (tx) => { + const foreign = await createMonitor({ + ctx: { ...teamCtx, db: tx }, + input: { + name: `${TEST_PREFIX}-bulk-foreign`, + jobType: "http", + url: "https://example.com", + method: "GET", + headers: [], + assertions: [], + active: true, + }, + }); + + await bulkUpdateMonitors({ + ctx: { + ...makeSystemCtx(freeCtx.workspace, { job: "monitor-auto-pause" }), + db: tx, + }, + input: { ids: [foreign.id], active: false }, + }); + + const row = await tx + .select() + .from(monitor) + .where(eq(monitor.id, foreign.id)) + .get(); + expect(row?.active).toBe(true); + + const audits = await readAuditLog({ + workspaceId: freeCtx.workspace.id, + entityType: "monitor", + entityId: foreign.id, + db: tx, + }); + expect(audits).toHaveLength(0); + }); + }); + + test("rejects read-only actor", async () => { + await withTestTransaction(async (tx) => { + const ctx = { + ...makeApiKeyCtx(teamCtx.workspace, { + keyId: "k-read", + userId: 1, + scopes: ["read"], + }), + db: tx, + }; + await expect( + bulkUpdateMonitors({ ctx, input: { ids: [1], active: false } }), + ).rejects.toBeInstanceOf(ForbiddenError); + }); + }); +}); -- knotmirror 0.0.1