Skip to content

Commit 0806004

Browse files
committed
fix: soft-delete undated duplicate member organization rows in cron job
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
1 parent a493e12 commit 0806004

2 files changed

Lines changed: 28 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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,29 @@ 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+
await qx.result(
495+
`
496+
UPDATE "memberOrganizations"
497+
SET "deletedAt" = NOW()
498+
WHERE "memberId" = $(memberId)
499+
AND "organizationId" IN ($(organizationIds:csv))
500+
AND "dateStart" IS NULL
501+
AND "dateEnd" IS NULL
502+
AND "deletedAt" IS NULL
503+
`,
504+
{ memberId, organizationIds },
505+
)
506+
}
507+
485508
export async function cleanSoftDeletedMemberOrganization(
486509
qx: QueryExecutor,
487510
memberId: string,

0 commit comments

Comments
 (0)