Skip to content

Commit

Permalink
Merge branch 'crowd-linux' into improvement/same-work-experience
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspergrom committed Aug 1, 2024
2 parents faed536 + 1699de3 commit 24c3c3c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 63 deletions.
34 changes: 30 additions & 4 deletions backend/src/database/repositories/memberRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,8 @@ class MemberRepository {
segmentId?: string
} = {},
) {
const { rows, count } = await MemberRepository.findAndCountAll(
let memberResponse = null
memberResponse = await MemberRepository.findAndCountAll(
{
filter: { id: { eq: id } },
limit: 1,
Expand All @@ -1561,11 +1562,36 @@ class MemberRepository {
options,
)

if (count === 0) {
throw new Error404()
if (memberResponse.count === 0) {
// try it again without segment information (no aggregates)
// for members without activities
memberResponse = await MemberRepository.findAndCountAll(
{
filter: { id: { eq: id } },
limit: 1,
offset: 0,
include: {
memberOrganizations: true,
lfxMemberships: true,
identities: true,
segments: true,
},
},
options,
)

if (memberResponse.count === 0) {
throw new Error404()
}

memberResponse.rows[0].activityCount = 0
memberResponse.rows[0].lastActive = null
memberResponse.rows[0].activityTypes = []
memberResponse.rows[0].activeOn = []
memberResponse.rows[0].averageSentiment = null
}

const [data] = rows
const [data] = memberResponse.rows
const affiliations = await MemberRepository.getAffiliations(id, options)

return {
Expand Down
37 changes: 32 additions & 5 deletions backend/src/database/repositories/organizationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,9 @@ class OrganizationRepository {
}

static async findById(id: string, options: IRepositoryOptions, segmentId?: string) {
const { rows, count } = await OrganizationRepository.findAndCountAll(
let orgResponse = null

orgResponse = await OrganizationRepository.findAndCountAll(
{
filter: { id: { eq: id } },
limit: 1,
Expand All @@ -1222,11 +1224,36 @@ class OrganizationRepository {
options,
)

if (count === 0) {
throw new Error404()
if (orgResponse.count === 0) {
// try it again without segment information (no aggregates)
// for orgs without activities
orgResponse = await OrganizationRepository.findAndCountAll(
{
filter: { id: { eq: id } },
limit: 1,
offset: 0,
include: {
attributes: true,
lfxMemberships: true,
identities: true,
},
},
options,
)

if (orgResponse.count === 0) {
throw new Error404()
}

orgResponse.rows[0].joinedAt = null
orgResponse.rows[0].lastActive = null
orgResponse.rows[0].activityCount = 0
orgResponse.rows[0].memberCount = 0
orgResponse.rows[0].avgContributorEngagement = null
orgResponse.rows[0].activeOn = null
}

const organization = rows[0]
const organization = orgResponse.rows[0]

const qx = SequelizeRepository.getQueryExecutor(options)
const attributes = await findOrgAttributes(qx, id)
Expand Down Expand Up @@ -1717,7 +1744,7 @@ class OrganizationRepository {
FROM organizations o
${
withAggregates
? `LEFT JOIN "organizationSegmentsAgg" osa ON osa."organizationId" = o.id AND osa."segmentId" = $(segmentId)`
? ` JOIN "organizationSegmentsAgg" osa ON osa."organizationId" = o.id AND osa."segmentId" = $(segmentId)`
: ''
}
WHERE 1=1
Expand Down
21 changes: 0 additions & 21 deletions backend/src/services/organizationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,27 +571,6 @@ export default class OrganizationService extends LoggerBase {
}),
)

this.log.info(
{ originalId, toMergeId },
'[Merge Organizations] - Sending refresh opensearch messages! ',
)

const searchSyncService = new SearchSyncService(this.options, SyncMode.ASYNCHRONOUS)

await searchSyncService.triggerOrganizationSync(tenantId, originalId)
await searchSyncService.triggerRemoveOrganization(tenantId, toMergeId)

// sync organization members
await searchSyncService.triggerOrganizationMembersSync(tenantId, originalId)

// sync organization activities
await searchSyncService.triggerOrganizationActivitiesSync(tenantId, originalId)

this.log.info(
{ originalId, toMergeId },
'[Merge Organizations] - Sending refresh opensearch messages done! ',
)

await this.options.temporal.workflow.start('finishOrganizationMerging', {
taskQueue: 'entity-merging',
workflowId: `finishOrganizationMerging/${originalId}/${toMergeId}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import {
moveActivitiesToNewOrg,
} from '@crowd/data-access-layer/src/old/apps/entity_merging_worker/orgs'
import { SearchSyncApiClient } from '@crowd/opensearch'
import {
findOrganizationSegments,
markOrganizationAsManuallyCreated,
} from '@crowd/data-access-layer/src/old/apps/entity_merging_worker'
import { WorkflowIdReusePolicy } from '@temporalio/workflow'
import {
cleanupForOganization,
Expand Down Expand Up @@ -63,37 +59,12 @@ export async function recalculateActivityAffiliationsOfOrganizationSynchronous(
})
}

export async function syncOrganization(
organizationId: string,
secondaryOrganizationId: string,
): Promise<void> {
export async function syncOrganization(organizationId: string): Promise<void> {
const syncApi = new SearchSyncApiClient({
baseUrl: process.env['CROWD_SEARCH_SYNC_API_URL'],
})

// check if org has any activities
const result = await findOrganizationSegments(svc.postgres.writer, organizationId)

if (result.segmentIds) {
// segment information can be deduced from activities, no need to send segmentIds explicitly on merging
await syncApi.triggerOrganizationSync(organizationId)
await syncApi.triggerOrganizationMembersSync(null, organizationId)
return
}

// check if secondary org has any activities
const secondaryResult = await findOrganizationSegments(
svc.postgres.writer,
secondaryOrganizationId,
)

if (secondaryResult.segmentIds) {
await markOrganizationAsManuallyCreated(svc.postgres.writer, organizationId)
// organization doesn't have any activity to deduce segmentIds for syncing, use the secondary member's activity segments
await syncApi.triggerOrganizationSync(organizationId, secondaryResult.segmentIds)
}

// also sync organization members
await syncApi.triggerOrganizationSync(organizationId)
await syncApi.triggerOrganizationMembersSync(null, organizationId)
}

Expand Down
5 changes: 3 additions & 2 deletions services/apps/entity_merging_worker/src/workflows/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export async function finishOrganizationMerging(
movedSomething = await moveActivitiesBetweenOrgs(primaryId, secondaryId, tenantId)
} while (movedSomething)

await syncOrganization(primaryId)
await deleteOrganization(secondaryId)
await setMergeActionState(primaryId, secondaryId, tenantId, 'merged' as MergeActionState)
await notifyFrontendOrganizationMergeSuccessful(
Expand All @@ -107,8 +108,8 @@ export async function finishOrganizationUnmerging(
): Promise<void> {
await recalculateActivityAffiliationsOfOrganizationSynchronous(primaryId, tenantId)
await recalculateActivityAffiliationsOfOrganizationSynchronous(secondaryId, tenantId)
await syncOrganization(primaryId, secondaryId)
await syncOrganization(secondaryId, primaryId)
await syncOrganization(primaryId)
await syncOrganization(secondaryId)
await setMergeActionState(primaryId, secondaryId, tenantId, 'unmerged' as MergeActionState)
await notifyFrontendOrganizationUnmergeSuccessful(
primaryId,
Expand Down

0 comments on commit 24c3c3c

Please sign in to comment.