Skip to content

Commit 969567e

Browse files
committed
fix: comments
Signed-off-by: Uroš Marolt <uros@marolt.me>
1 parent ec60e44 commit 969567e

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

services/apps/cron_service/src/jobs/incomingWebhooksCheck.job.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,33 @@ const job: IJobDefinition = {
3434

3535
const dbConnection = await getDbConnection(WRITE_DB_CONFIG())
3636

37+
// Clean up orphaned webhooks whose integration was deleted (hard or soft).
38+
// incomingWebhooks has no FK constraint on integrationId so these accumulate silently.
39+
const deleted = await dbConnection.result(
40+
`
41+
delete from "incomingWebhooks" iw
42+
where not exists (
43+
select 1 from integrations i
44+
where i.id = iw."integrationId"
45+
and i."deletedAt" is null
46+
)
47+
`,
48+
)
49+
if (deleted.rowCount > 0) {
50+
ctx.log.info(
51+
`Deleted ${deleted.rowCount} orphaned webhooks with missing or deleted integrations!`,
52+
)
53+
}
54+
3755
const count = (
3856
await dbConnection.one(
39-
`select count(*)::int as count from "incomingWebhooks" where state = $(state) and "createdAt" < now() - interval '1 day'`,
57+
`
58+
select count(*)::int as count
59+
from "incomingWebhooks" iw
60+
join integrations i on iw."integrationId" = i.id and i."deletedAt" is null
61+
where iw.state = $(state)
62+
and iw."createdAt" < now() - interval '1 day'
63+
`,
4064
{ state: WebhookState.PENDING },
4165
)
4266
).count
@@ -50,7 +74,7 @@ const job: IJobDefinition = {
5074
`
5175
select iw.id, i.platform
5276
from "incomingWebhooks" iw
53-
join integrations i on iw."integrationId" = i.id
77+
join integrations i on iw."integrationId" = i.id and i."deletedAt" is null
5478
where iw.state = $(state)
5579
and iw."createdAt" < now() - interval '1 day'
5680
order by iw."createdAt" asc

services/libs/data-access-layer/src/old/apps/data_sink_worker/repo/requestedForErasureMemberIdentities.repo.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { singleOrDefault } from '@crowd/common'
21
import { DbStore, RepositoryBase } from '@crowd/database'
32
import { Logger } from '@crowd/logging'
43
import { IMemberIdentity, MemberIdentityType } from '@crowd/types'
@@ -75,13 +74,16 @@ export default class RequestedForErasureMemberIdentitiesRepository extends Repos
7574

7675
if (identity.type === MemberIdentityType.EMAIL) {
7776
// SQL matches case-insensitively (lower(value) = lower(?)), so mirror that here.
78-
const row = singleOrDefault(data, (r) => {
79-
return (
80-
r.type === identity.type &&
81-
r.value.toLowerCase() === identity.value.toLowerCase() &&
82-
r.platform === identity.platform
83-
)
84-
})
77+
// Using find instead of singleOrDefault: case-insensitive matching could produce
78+
// multiple hits if the DB has rows with differing casing for the same email,
79+
// causing singleOrDefault to throw.
80+
const row =
81+
data.find(
82+
(r) =>
83+
r.type === identity.type &&
84+
r.value.toLowerCase() === identity.value.toLowerCase() &&
85+
r.platform === identity.platform,
86+
) ?? null
8587

8688
if (row) {
8789
results.set(identity, true)

0 commit comments

Comments
 (0)