Skip to content

Commit 88650b6

Browse files
committed
fix: orphaned monitor page components
1 parent 4e694a8 commit 88650b6

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

apps/server/src/routes/rpc/services/monitor/index.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ import { getCheckerPayload, getCheckerUrl } from "@/libs/checker";
33
import { tb } from "@/libs/clients";
44
import type { ServiceImpl } from "@connectrpc/connect";
55
import { and, db, eq, gte, inArray, isNull, sql } from "@openstatus/db";
6-
import { monitor, monitorRun } from "@openstatus/db/src/schema";
6+
import {
7+
maintenancesToMonitors,
8+
monitor,
9+
monitorRun,
10+
monitorTagsToMonitors,
11+
monitorsToPages,
12+
monitorsToStatusReport,
13+
notificationsToMonitors,
14+
pageComponent,
15+
} from "@openstatus/db/src/schema";
716
import { monitorStatusTable } from "@openstatus/db/src/schema/monitor_status/monitor_status";
817
import { selectMonitorSchema } from "@openstatus/db/src/schema/monitors/validation";
918
import type {
@@ -572,6 +581,28 @@ export const monitorServiceImpl: ServiceImpl<typeof MonitorService> = {
572581
})
573582
.where(eq(monitor.id, dbMon.id));
574583

584+
// Clean up related junction tables and page components
585+
await db.transaction(async (tx) => {
586+
await tx
587+
.delete(monitorsToPages)
588+
.where(eq(monitorsToPages.monitorId, dbMon.id));
589+
await tx
590+
.delete(monitorTagsToMonitors)
591+
.where(eq(monitorTagsToMonitors.monitorId, dbMon.id));
592+
await tx
593+
.delete(monitorsToStatusReport)
594+
.where(eq(monitorsToStatusReport.monitorId, dbMon.id));
595+
await tx
596+
.delete(notificationsToMonitors)
597+
.where(eq(notificationsToMonitors.monitorId, dbMon.id));
598+
await tx
599+
.delete(maintenancesToMonitors)
600+
.where(eq(maintenancesToMonitors.monitorId, dbMon.id));
601+
await tx
602+
.delete(pageComponent)
603+
.where(eq(pageComponent.monitorId, dbMon.id));
604+
});
605+
575606
return { success: true };
576607
},
577608

packages/api/src/router/monitor.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
monitorsToStatusReport,
2828
notification,
2929
notificationsToMonitors,
30+
pageComponent,
3031
privateLocation,
3132
privateLocationToMonitors,
3233
selectIncidentSchema,
@@ -85,6 +86,9 @@ export const monitorRouter = createTRPCRouter({
8586
await tx
8687
.delete(maintenancesToMonitors)
8788
.where(eq(maintenancesToMonitors.monitorId, monitorToDelete.id));
89+
await tx
90+
.delete(pageComponent)
91+
.where(eq(pageComponent.monitorId, monitorToDelete.id));
8892
});
8993
}),
9094

@@ -131,6 +135,9 @@ export const monitorRouter = createTRPCRouter({
131135
await tx
132136
.delete(maintenancesToMonitors)
133137
.where(inArray(maintenancesToMonitors.monitorId, opts.input.ids));
138+
await tx
139+
.delete(pageComponent)
140+
.where(inArray(pageComponent.monitorId, opts.input.ids));
134141
});
135142
}),
136143

packages/api/src/router/pageComponent.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ export const pageComponentRouter = createTRPCRouter({
157157

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

160+
// Validate the incoming component count against the limit
161+
const newComponentCount =
162+
opts.input.components.length +
163+
opts.input.groups.reduce((sum, g) => sum + g.components.length, 0);
164+
165+
if (newComponentCount > pageComponentLimit) {
166+
throw new TRPCError({
167+
code: "FORBIDDEN",
168+
message: "You reached your page component limits.",
169+
});
170+
}
171+
160172
// Get existing state
161173
const existingComponents = await tx
162174
.select()
@@ -169,13 +181,6 @@ export const pageComponentRouter = createTRPCRouter({
169181
)
170182
.all();
171183

172-
if (existingComponents.length >= pageComponentLimit) {
173-
throw new TRPCError({
174-
code: "FORBIDDEN",
175-
message: "You reached your page component limits.",
176-
});
177-
}
178-
179184
const existingGroups = await tx
180185
.select()
181186
.from(pageComponentGroup)

0 commit comments

Comments
 (0)