Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion apps/server/src/routes/rpc/services/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import { getCheckerPayload, getCheckerUrl } from "@/libs/checker";
import { tb } from "@/libs/clients";
import type { ServiceImpl } from "@connectrpc/connect";
import { and, db, eq, gte, inArray, isNull, sql } from "@openstatus/db";
import { monitor, monitorRun } from "@openstatus/db/src/schema";
import {
maintenancesToMonitors,
monitor,
monitorRun,
monitorTagsToMonitors,
monitorsToPages,
monitorsToStatusReport,
notificationsToMonitors,
pageComponent,
} from "@openstatus/db/src/schema";
import { monitorStatusTable } from "@openstatus/db/src/schema/monitor_status/monitor_status";
import { selectMonitorSchema } from "@openstatus/db/src/schema/monitors/validation";
import type {
Expand Down Expand Up @@ -572,6 +581,28 @@ export const monitorServiceImpl: ServiceImpl<typeof MonitorService> = {
})
.where(eq(monitor.id, dbMon.id));

// Clean up related junction tables and page components
await db.transaction(async (tx) => {
await tx
.delete(monitorsToPages)
.where(eq(monitorsToPages.monitorId, dbMon.id));
await tx
.delete(monitorTagsToMonitors)
.where(eq(monitorTagsToMonitors.monitorId, dbMon.id));
await tx
.delete(monitorsToStatusReport)
.where(eq(monitorsToStatusReport.monitorId, dbMon.id));
await tx
.delete(notificationsToMonitors)
.where(eq(notificationsToMonitors.monitorId, dbMon.id));
await tx
.delete(maintenancesToMonitors)
.where(eq(maintenancesToMonitors.monitorId, dbMon.id));
await tx
.delete(pageComponent)
.where(eq(pageComponent.monitorId, dbMon.id));
});

return { success: true };
},

Expand Down
7 changes: 7 additions & 0 deletions packages/api/src/router/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
monitorsToStatusReport,
notification,
notificationsToMonitors,
pageComponent,
privateLocation,
privateLocationToMonitors,
selectIncidentSchema,
Expand Down Expand Up @@ -85,6 +86,9 @@ export const monitorRouter = createTRPCRouter({
await tx
.delete(maintenancesToMonitors)
.where(eq(maintenancesToMonitors.monitorId, monitorToDelete.id));
await tx
.delete(pageComponent)
.where(eq(pageComponent.monitorId, monitorToDelete.id));
});
}),

Expand Down Expand Up @@ -131,6 +135,9 @@ export const monitorRouter = createTRPCRouter({
await tx
.delete(maintenancesToMonitors)
.where(inArray(maintenancesToMonitors.monitorId, opts.input.ids));
await tx
.delete(pageComponent)
.where(inArray(pageComponent.monitorId, opts.input.ids));
});
Comment on lines 132 to 137
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same atomicity concern as the single-delete path: the soft-delete update is executed before the cleanup transaction. Wrapping both the monitor update and the related deletes in one transaction would avoid partially-applied deletions if the cleanup fails.

Copilot uses AI. Check for mistakes.
}),

Expand Down
19 changes: 12 additions & 7 deletions packages/api/src/router/pageComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ export const pageComponentRouter = createTRPCRouter({

const pageComponentLimit = opts.ctx.workspace.limits["page-components"];

// Validate the incoming component count against the limit
const newComponentCount =
opts.input.components.length +
opts.input.groups.reduce((sum, g) => sum + g.components.length, 0);

if (newComponentCount > pageComponentLimit) {
throw new TRPCError({
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new limit validation changes behavior but there’s no test covering the boundary cases (exactly at the limit should succeed; exceeding the limit via components+group components should throw). Please add/adjust tests for pageComponent.updateOrder to lock in this logic and prevent regressions.

Copilot uses AI. Check for mistakes.
code: "FORBIDDEN",
message: "You reached your page component limits.",
});
}

// Get existing state
const existingComponents = await tx
.select()
Expand All @@ -169,13 +181,6 @@ export const pageComponentRouter = createTRPCRouter({
)
.all();

if (existingComponents.length >= pageComponentLimit) {
throw new TRPCError({
code: "FORBIDDEN",
message: "You reached your page component limits.",
});
}

const existingGroups = await tx
.select()
.from(pageComponentGroup)
Expand Down