Skip to content

Commit 18ae414

Browse files
authored
fix: soft-delete undated duplicate member organization rows (#4070)
1 parent 4b60b5d commit 18ae414

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
changeMemberOrganizationAffiliationOverrides,
1111
checkOrganizationAffiliationPolicy,
1212
createMemberOrganization,
13+
deleteUndatedMemberOrganizations,
1314
fetchMemberOrganizationsBySource,
1415
updateMemberOrganization,
1516
} from '@crowd/data-access-layer'
@@ -136,6 +137,10 @@ async function applyStintChanges(qx: QueryExecutor, changes: MemberOrgStintChang
136137
})
137138
}
138139
}
140+
141+
await deleteUndatedMemberOrganizations(qx, changes[0].memberId, [
142+
...new Set(changes.map((c) => c.organizationId)),
143+
])
139144
}
140145

141146
export default job

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,47 @@ export async function deleteMemberOrganizations(
482482
})
483483
}
484484

485+
export async function deleteUndatedMemberOrganizations(
486+
qx: QueryExecutor,
487+
memberId: string,
488+
organizationIds: string[],
489+
): Promise<void> {
490+
if (organizationIds.length === 0) {
491+
return
492+
}
493+
494+
const whereClause = `
495+
"memberId" = $(memberId)
496+
AND "organizationId" IN ($(organizationIds:csv))
497+
AND "dateStart" IS NULL
498+
AND "dateEnd" IS NULL
499+
AND "deletedAt" IS NULL
500+
`
501+
502+
const params = { memberId, organizationIds }
503+
504+
await qx.tx(async (tx) => {
505+
await tx.result(
506+
`
507+
DELETE FROM "memberOrganizationAffiliationOverrides"
508+
WHERE "memberOrganizationId" IN (
509+
SELECT "id" FROM "memberOrganizations" WHERE ${whereClause}
510+
)
511+
`,
512+
params,
513+
)
514+
515+
await tx.result(
516+
`
517+
UPDATE "memberOrganizations"
518+
SET "deletedAt" = NOW()
519+
WHERE ${whereClause}
520+
`,
521+
params,
522+
)
523+
})
524+
}
525+
485526
export async function cleanSoftDeletedMemberOrganization(
486527
qx: QueryExecutor,
487528
memberId: string,

0 commit comments

Comments
 (0)