Skip to content

Commit

Permalink
Fix creation of parent and grandparent segment documents for members/…
Browse files Browse the repository at this point in the history
…orgs (#1443)
  • Loading branch information
Uros Marolt authored Sep 7, 2023
1 parent 83abc92 commit f2ea9df
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 66 deletions.
4 changes: 2 additions & 2 deletions backend/src/database/repositories/organizationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ class OrganizationRepository {
segment_level as (select case
when "parentSlug" is not null and "grandparentSlug" is not null
then 'child'
when "parentSlug" is null and "grandparentSlug" is not null
when "parentSlug" is not null and "grandparentSlug" is null
then 'parent'
when "parentSlug" is null and "grandparentSlug" is null
then 'grandparent'
Expand All @@ -495,7 +495,7 @@ class OrganizationRepository {
segment_level sl
on
(sl.level = 'child' and s.id = sl.id) or
(sl.level = 'parent' and s."parentSlug" = sl.slug) or
(sl.level = 'parent' and s."parentSlug" = sl.slug and s."grandparentSlug" is not null) or
(sl.level = 'grandparent' and s."grandparentSlug" = sl.slug)`
}

Expand Down
60 changes: 30 additions & 30 deletions services/apps/search_sync_worker/src/service/member.sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,38 +300,38 @@ export class MemberSyncService extends LoggerBase {
id: `${memberId}-${member.segmentId}`,
body: prepared,
})
}

const relevantSegmentInfos = segmentInfos.filter((s) => s.id === memberDocs[0].segmentId)

// and for each parent and grandparent
const parentIds = distinct(relevantSegmentInfos.map((s) => s.parentId))
for (const parentId of parentIds) {
const aggregated = MemberSyncService.aggregateData(
memberDocs,
relevantSegmentInfos,
parentId,
)
const prepared = MemberSyncService.prefixData(aggregated, attributes)
forSync.push({
id: `${memberId}-${parentId}`,
body: prepared,
})
}
const relevantSegmentInfos = segmentInfos.filter((s) => s.id === member.segmentId)

// and for each parent and grandparent
const parentIds = distinct(relevantSegmentInfos.map((s) => s.parentId))
for (const parentId of parentIds) {
const aggregated = MemberSyncService.aggregateData(
memberDocs,
relevantSegmentInfos,
parentId,
)
const prepared = MemberSyncService.prefixData(aggregated, attributes)
forSync.push({
id: `${memberId}-${parentId}`,
body: prepared,
})
}

const grandParentIds = distinct(relevantSegmentInfos.map((s) => s.grandParentId))
for (const grandParentId of grandParentIds) {
const aggregated = MemberSyncService.aggregateData(
memberDocs,
relevantSegmentInfos,
undefined,
grandParentId,
)
const prepared = MemberSyncService.prefixData(aggregated, attributes)
forSync.push({
id: `${memberId}-${grandParentId}`,
body: prepared,
})
const grandParentIds = distinct(relevantSegmentInfos.map((s) => s.grandParentId))
for (const grandParentId of grandParentIds) {
const aggregated = MemberSyncService.aggregateData(
memberDocs,
relevantSegmentInfos,
undefined,
grandParentId,
)
const prepared = MemberSyncService.prefixData(aggregated, attributes)
forSync.push({
id: `${memberId}-${grandParentId}`,
body: prepared,
})
}
}
} else {
if (memberDocs.length > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,47 +269,45 @@ export class OrganizationSyncService extends LoggerBase {
for (const organizationId of organizationIds) {
const organizationDocs = grouped.get(organizationId)
if (isMultiSegment) {
// index each of them individually
for (const organization of organizationDocs) {
// index each of them individually because it's per leaf segment
const prepared = OrganizationSyncService.prefixData(organization)
forSync.push({
id: `${organizationId}-${organization.segmentId}`,
body: prepared,
})
}

const relevantSegmentInfos = segmentInfos.filter(
(s) => s.id === organizationDocs[0].segmentId,
)

// and for each parent and grandparent
const parentIds = distinct(relevantSegmentInfos.map((s) => s.parentId))
for (const parentId of parentIds) {
const aggregated = OrganizationSyncService.aggregateData(
organizationDocs,
relevantSegmentInfos,
parentId,
)
const prepared = OrganizationSyncService.prefixData(aggregated)
forSync.push({
id: `${organizationId}-${parentId}`,
body: prepared,
})
}

const grandParentIds = distinct(relevantSegmentInfos.map((s) => s.grandParentId))
for (const grandParentId of grandParentIds) {
const aggregated = OrganizationSyncService.aggregateData(
organizationDocs,
relevantSegmentInfos,
undefined,
grandParentId,
)
const prepared = OrganizationSyncService.prefixData(aggregated)
forSync.push({
id: `${organizationId}-${grandParentId}`,
body: prepared,
})
const relevantSegmentInfos = segmentInfos.filter((s) => s.id === organization.segmentId)

// and for each parent and grandparent
const parentIds = distinct(relevantSegmentInfos.map((s) => s.parentId))
for (const parentId of parentIds) {
const aggregated = OrganizationSyncService.aggregateData(
organizationDocs,
relevantSegmentInfos,
parentId,
)
const prepared = OrganizationSyncService.prefixData(aggregated)
forSync.push({
id: `${organizationId}-${parentId}`,
body: prepared,
})
}

const grandParentIds = distinct(relevantSegmentInfos.map((s) => s.grandParentId))
for (const grandParentId of grandParentIds) {
const aggregated = OrganizationSyncService.aggregateData(
organizationDocs,
relevantSegmentInfos,
undefined,
grandParentId,
)
const prepared = OrganizationSyncService.prefixData(aggregated)
forSync.push({
id: `${organizationId}-${grandParentId}`,
body: prepared,
})
}
}
} else {
if (organizationDocs.length > 1) {
Expand Down

0 comments on commit f2ea9df

Please sign in to comment.