Skip to content

Commit 7eb6a89

Browse files
authored
fix: remove dangling affiliation overrides on memberOrganization deletion (#3631)
1 parent fc859cc commit 7eb6a89

3 files changed

Lines changed: 31 additions & 54 deletions

File tree

services/apps/members_enrichment_worker/src/activities/enrichment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ export async function updateMemberUsingSquashedPayload(
411411
if (results.toDelete.length > 0) {
412412
for (const org of results.toDelete) {
413413
updated = true
414-
await deleteMemberOrgById(tx.transaction(), memberId, org.id)
414+
await deleteMemberOrgById(tx.transaction(), org.id)
415415
}
416416
}
417417

services/libs/data-access-layer/src/members/organizations.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,43 @@ export async function createOrUpdateMemberOrganizations(
133133
dateEnd: string | null | undefined,
134134
): Promise<void> {
135135
if (dateStart) {
136+
const whereClause = `
137+
"memberId" = $(memberId)
138+
AND "title" = $(title)
139+
AND "organizationId" = $(organizationId)
140+
AND "dateStart" IS NULL
141+
AND "dateEnd" IS NULL
142+
`
143+
136144
// clean up organizations without dates if we're getting ones with dates
137145
await qx.result(
138146
`
139147
UPDATE "memberOrganizations"
140148
SET "deletedAt" = NOW()
141-
WHERE "memberId" = $(memberId)
142-
AND "title" = $(title)
143-
AND "organizationId" = $(organizationId)
144-
AND "dateStart" IS NULL
145-
AND "dateEnd" IS NULL
149+
WHERE ${whereClause}
146150
`,
147151
{
148152
memberId,
149153
title,
150154
organizationId,
151155
},
152156
)
157+
158+
// always clean up affiliation overrides for any organization we soft-delete
159+
// to prevent stale override data pointing to soft-deleted organizations
160+
await qx.result(
161+
`
162+
DELETE FROM "memberOrganizationAffiliationOverrides"
163+
WHERE "memberOrganizationId" IN (
164+
SELECT id FROM "memberOrganizations" WHERE ${whereClause}
165+
)
166+
`,
167+
{
168+
memberId,
169+
title,
170+
organizationId,
171+
},
172+
)
153173
} else {
154174
const rows = await qx.select(
155175
`

services/libs/data-access-layer/src/old/apps/members_enrichment_worker/index.ts

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -433,47 +433,7 @@ export async function insertOrgIdentity(
433433
)
434434
}
435435

436-
export async function deleteMemberOrg(db: DbConnOrTx, memberId: string, organizationId: string) {
437-
await db.tx(async (tx) => {
438-
await tx.none(
439-
`
440-
DELETE FROM "memberOrganizationAffiliationOverrides"
441-
WHERE "memberOrganizationId" IN (
442-
SELECT id FROM "memberOrganizations"
443-
WHERE "memberId" = $(memberId)
444-
AND "organizationId" = $(organizationId)
445-
AND "dateStart" IS NULL
446-
AND "dateEnd" IS NULL
447-
)
448-
`,
449-
{
450-
memberId,
451-
organizationId,
452-
},
453-
)
454-
455-
await tx.none(
456-
`
457-
UPDATE "memberOrganizations"
458-
SET "deletedAt" = NOW()
459-
WHERE "memberId" = $(memberId)
460-
AND "organizationId" = $(organizationId)
461-
AND "dateStart" IS NULL
462-
AND "dateEnd" IS NULL
463-
`,
464-
{
465-
memberId,
466-
organizationId,
467-
},
468-
)
469-
})
470-
}
471-
472-
export async function deleteMemberOrgById(
473-
tx: DbTransaction,
474-
memberId: string,
475-
id: string,
476-
): Promise<void> {
436+
export async function deleteMemberOrgById(tx: DbTransaction, id: string): Promise<void> {
477437
// Execute directly on the provided transaction to avoid creating nested savepoints
478438
await tx.none(
479439
`
@@ -487,9 +447,9 @@ export async function deleteMemberOrgById(
487447
`
488448
UPDATE "memberOrganizations"
489449
SET "deletedAt" = NOW()
490-
WHERE "memberId" = $(memberId) and id = $(id);
450+
WHERE id = $(id);
491451
`,
492-
{ memberId, id },
452+
{ id },
493453
)
494454
}
495455

@@ -506,7 +466,7 @@ export async function findMemberOrgs(db: DbStore, memberId: string, orgId: strin
506466
}
507467

508468
export async function updateMemberOrg(
509-
tx: DbConnOrTx,
469+
tx: DbTransaction,
510470
memberId: string,
511471
original: IMemberOrganizationData,
512472
toUpdate: Record<string, unknown>,
@@ -554,10 +514,7 @@ export async function updateMemberOrg(
554514

555515
if (existing) {
556516
// we should just delete the row
557-
await tx.none(
558-
`update "memberOrganizations" set "deletedAt" = now() where "memberId" = $(memberId) and id = $(id)`,
559-
{ id: original.id, memberId },
560-
)
517+
await deleteMemberOrgById(tx, original.id)
561518
} else {
562519
const sets = keys.map((k) => `"${k}" = $(${k})`)
563520
await tx.none(

0 commit comments

Comments
 (0)