Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions services/libs/data-access-layer/src/members/organizations.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getServiceChildLogger } from '@crowd/logging'
import {
IMemberOrganization,
IMemberOrganizationAffiliationOverride,
Expand All @@ -16,6 +17,8 @@ import { QueryExecutor } from '../queryExecutor'

/* eslint-disable @typescript-eslint/no-explicit-any */

const logger = getServiceChildLogger('organizations')

export async function fetchMemberOrganizations(
qx: QueryExecutor,
memberId: string,
Expand Down Expand Up @@ -489,6 +492,11 @@ async function moveRolesBetweenEntities(
const primaryAffiliationOverrides = await findAffiliationOverrides(qx, primaryId)
const secondaryAffiliationOverrides = await findAffiliationOverrides(qx, secondaryId)

logger.info(`primaryMemberAffiliationOverrides: ${JSON.stringify(primaryAffiliationOverrides)}`)
logger.info(
`secondaryMemberAffiliationOverrides: ${JSON.stringify(secondaryAffiliationOverrides)}`,
)

await mergeRoles(
qx,
primaryRoles,
Expand All @@ -507,13 +515,19 @@ async function moveRolesBetweenEntities(
mergeStrat.intersectBasedOnField,
)

logger.info(`Found remaining roles to process: ${JSON.stringify(remainingRoles)}`)

for (const role of remainingRoles) {
logger.info(`Processing role: ${JSON.stringify(role)}`)

// delete any existing affiliation override for the role to avoid foreign key conflicts
// and reapply it with the new memberOrganizationId
const existingOverride = secondaryAffiliationOverrides.find(
(o) => o.memberOrganizationId === role.id,
)

logger.info(`Found existing override for role: ${existingOverride}`)

if (existingOverride) {
await deleteAffiliationOverrides(qx, role.memberId, [role.id])
}
Expand Down Expand Up @@ -690,11 +704,14 @@ export async function mergeRoles(
const existingOverrides = [...primaryAffiliationOverrides, ...secondaryAffiliationOverrides]

for (const removeRole of removeRoles) {
logger.info(`Removing role: ${JSON.stringify(removeRole)}`)
// delete affiliation overrides before removing roles to avoid foreign key conflicts
const existingOverride = existingOverrides.find(
(o) => o.memberOrganizationId === removeRole.id,
)

logger.info(`Found existing override for role: ${JSON.stringify(existingOverride)}`)

if (existingOverride) {
await deleteAffiliationOverrides(qx, removeRole.memberId, [removeRole.id])
affiliationOverridesToRecreate.push({
Expand Down
Loading