Skip to content

Commit d4f2d7c

Browse files
Backend: Fixed URL fallback & cascade deleting orphaned blockers on URL removal
1 parent 9314b8d commit d4f2d7c

2 files changed

Lines changed: 31 additions & 9 deletions

File tree

apps/backend/routes/auth/getAuditTable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export const getAuditTable = async () => {
291291
id: blocker.id,
292292
short_id: blocker.short_id,
293293
created_at: blocker.created_at,
294-
url: blocker.url?.url || blocker.url_id,
294+
url: blocker.url?.url || "Unknown URL",
295295
type: blocker.url?.type || "unknown",
296296
url_id: blocker.url_id,
297297
content: blocker.content,

apps/backend/routes/internal/syncAuditUrlsFromRemoteCsv.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,37 @@ export const syncAuditUrlsFromRemoteCsv = async (auditId: string) => {
106106
});
107107
}
108108

109-
// removed URLs
110-
for (const url of urlsToRemove) {
111-
await graphqlQuery({
112-
query: `mutation($audit_id:uuid,$url:String) {delete_urls(where: {audit_id: {_eq: $audit_id}, url: {_eq: $url}}) {affected_rows}}`,
113-
variables: {
114-
audit_id: auditId,
115-
url: url.url,
116-
},
109+
// removed URLs, also clean up orphaned blockers and related rows
110+
if (urlsToRemove.length > 0) {
111+
const removedUrlIds = urlsToRemove.map((u: DBUrl) => u.id);
112+
113+
await db.connect();
114+
// delete blocker_messages and ignored_blockers for blockers referencing removed URLs
115+
await db.query({
116+
text: `DELETE FROM "blocker_messages" WHERE "blocker_id" IN (SELECT "id" FROM "blockers" WHERE "url_id" = ANY($1::uuid[]))`,
117+
values: [removedUrlIds],
118+
});
119+
await db.query({
120+
text: `DELETE FROM "ignored_blockers" WHERE "blocker_id" IN (SELECT "id" FROM "blockers" WHERE "url_id" = ANY($1::uuid[]))`,
121+
values: [removedUrlIds],
122+
});
123+
// delete the orphaned blockers themselves
124+
await db.query({
125+
text: `DELETE FROM "blockers" WHERE "url_id" = ANY($1::uuid[])`,
126+
values: [removedUrlIds],
117127
});
128+
await db.clean();
129+
130+
// then delete the URLs
131+
for (const url of urlsToRemove) {
132+
await graphqlQuery({
133+
query: `mutation($audit_id:uuid,$url:String) {delete_urls(where: {audit_id: {_eq: $audit_id}, url: {_eq: $url}}) {affected_rows}}`,
134+
variables: {
135+
audit_id: auditId,
136+
url: url.url,
137+
},
138+
});
139+
}
118140
}
119141

120142
// updated URLs

0 commit comments

Comments
 (0)