diff --git a/backend/src/database/repositories/memberRepository.ts b/backend/src/database/repositories/memberRepository.ts index d9f30b62d..44360fd69 100644 --- a/backend/src/database/repositories/memberRepository.ts +++ b/backend/src/database/repositories/memberRepository.ts @@ -1513,7 +1513,10 @@ class MemberRepository { if (memberResponse.count === 0) { // try it again without segment information (no aggregates) // for members without activities - memberResponse = await MemberRepository.findAndCountAll( + memberResponse = await queryMembersAdvanced( + optionsQx(options), + options.redis, + options.currentTenant.id, { filter: { id: { eq: id } }, limit: 1, @@ -1528,7 +1531,6 @@ class MemberRepository { ...include, }, }, - options, ) if (memberResponse.count === 0) { @@ -1945,7 +1947,12 @@ class MemberRepository { } } - const lastActivities = await getLastActivitiesForMembers(options.qdb, memberIds) + const lastActivities = await getLastActivitiesForMembers( + options.qdb, + memberIds, + options.currentTenant.id, + segments, + ) for (const row of translatedRows) { const r = row as any @@ -2151,9 +2158,10 @@ class MemberRepository { SELECT ${fields} FROM members m - ${withAggregates - ? ` JOIN "memberSegmentsAgg" msa ON msa."memberId" = m.id AND msa."segmentId" = $(segmentId)` - : '' + ${ + withAggregates + ? ` JOIN "memberSegmentsAgg" msa ON msa."memberId" = m.id AND msa."segmentId" = $(segmentId)` + : '' } LEFT JOIN member_orgs mo ON mo."memberId" = m.id ${searchJoin} @@ -2175,36 +2183,36 @@ class MemberRepository { qx.select( ` ${createQuery( - (function prepareFields(fields) { - return `${fields - .map((f) => { - const mappedField = MemberRepository.QUERY_FILTER_COLUMN_MAP.get(f) - if (!mappedField) { - throw new Error400(options.language, `Invalid field: ${f}`) - } + (function prepareFields(fields) { + return `${fields + .map((f) => { + const mappedField = MemberRepository.QUERY_FILTER_COLUMN_MAP.get(f) + if (!mappedField) { + throw new Error400(options.language, `Invalid field: ${f}`) + } - return { - alias: f, - ...mappedField, - } - }) - .filter((mappedField) => mappedField.queryable !== false) - .filter((mappedField) => { - if (!withAggregates && mappedField.name.includes('msa.')) { - return false - } - if (!include.memberOrganizations && mappedField.name.includes('mo.')) { - return false - } - if (!include.attributes && mappedField.name === 'm.attributes') { - return false - } - return true - }) - .map((mappedField) => `${mappedField.name} AS "${mappedField.alias}"`) - .join(',\n')}` - })(fields), - )} + return { + alias: f, + ...mappedField, + } + }) + .filter((mappedField) => mappedField.queryable !== false) + .filter((mappedField) => { + if (!withAggregates && mappedField.name.includes('msa.')) { + return false + } + if (!include.memberOrganizations && mappedField.name.includes('mo.')) { + return false + } + if (!include.attributes && mappedField.name === 'm.attributes') { + return false + } + return true + }) + .map((mappedField) => `${mappedField.name} AS "${mappedField.alias}"`) + .join(',\n')}` + })(fields), + )} ORDER BY ${order} NULLS LAST LIMIT $(limit) OFFSET $(offset) @@ -2232,13 +2240,13 @@ class MemberRepository { ) const orgExtra = orgIds.length ? await queryOrgs(qx, { - filter: { - [OrganizationField.ID]: { - in: orgIds, + filter: { + [OrganizationField.ID]: { + in: orgIds, + }, }, - }, - fields: [OrganizationField.ID, OrganizationField.DISPLAY_NAME, OrganizationField.LOGO], - }) + fields: [OrganizationField.ID, OrganizationField.DISPLAY_NAME, OrganizationField.LOGO], + }) : [] rows.forEach((member) => { @@ -2333,7 +2341,12 @@ class MemberRepository { }) if (memberIds.length > 0) { - const lastActivities = await getLastActivitiesForMembers(options.qdb, memberIds) + const lastActivities = await getLastActivitiesForMembers( + options.qdb, + memberIds, + options.currentTenant.id, + [segmentId], + ) rows.forEach((r) => { r.lastActivity = lastActivities.find((a) => a.memberId === r.id) @@ -2345,9 +2358,9 @@ class MemberRepository { ) } }) - - return { rows, count, limit, offset } } + + return { rows, count, limit, offset } } /** diff --git a/backend/src/database/repositories/taskRepository.ts b/backend/src/database/repositories/taskRepository.ts index 44a3048cd..3db3ab3fc 100644 --- a/backend/src/database/repositories/taskRepository.ts +++ b/backend/src/database/repositories/taskRepository.ts @@ -491,7 +491,13 @@ class TaskRepository { ) const activityIds = results.map((r) => (r as any).activityId) if (activityIds.length > 0) { - output.activities = await getActivitiesById(options.qdb, activityIds) + const segmentIds = SequelizeRepository.getSegmentIds(options) + output.activities = await getActivitiesById( + options.qdb, + activityIds, + options.currentTenant.id, + segmentIds, + ) } else { output.activities = [] } diff --git a/services/libs/conversations/src/service/conversation.service.ts b/services/libs/conversations/src/service/conversation.service.ts index 1f72283a4..7e2b0b4d1 100644 --- a/services/libs/conversations/src/service/conversation.service.ts +++ b/services/libs/conversations/src/service/conversation.service.ts @@ -128,7 +128,7 @@ export class ConversationService extends LoggerBase { let results: IQueryActivityResult[] = [activity] if (activity.parentId) { - results = await getActivitiesById(this.qdbStore, [activity.parentId]) + results = await getActivitiesById(this.qdbStore, [activity.parentId], tenantId, [segmentId]) if (results.length !== 1) { throw new Error(`Parent activity ${activity.parentId} does not exist!`) } diff --git a/services/libs/data-access-layer/src/activities/sql.ts b/services/libs/data-access-layer/src/activities/sql.ts index eaf290fd7..1d9df9ed8 100644 --- a/services/libs/data-access-layer/src/activities/sql.ts +++ b/services/libs/data-access-layer/src/activities/sql.ts @@ -51,6 +51,8 @@ const s3Url = `https://${ export async function getActivitiesById( conn: DbConnOrTx, ids: string[], + tenantId: string, + segmentIds: string[], ): Promise { if (ids.length === 0) { return [] @@ -59,6 +61,8 @@ export async function getActivitiesById( const data = await queryActivities(conn, { filter: { and: [{ id: { in: ids } }] }, limit: ids.length, + tenantId, + segmentIds, }) return data.rows @@ -1396,6 +1400,8 @@ export async function getNewActivityPlatforms( export async function getLastActivitiesForMembers( qdbConn: DbConnOrTx, memberIds: string[], + tenantId: string, + segmentIds?: string[], ): Promise { const query = ` select id from activities where "deletedAt" is null and "memberId" in ($(memberIds:csv)) @@ -1410,5 +1416,7 @@ export async function getLastActivitiesForMembers( return getActivitiesById( qdbConn, results.map((r) => r.id), + tenantId, + segmentIds, ) } diff --git a/services/libs/data-access-layer/src/members/base.ts b/services/libs/data-access-layer/src/members/base.ts index d78cc70f8..1154bf20d 100644 --- a/services/libs/data-access-layer/src/members/base.ts +++ b/services/libs/data-access-layer/src/members/base.ts @@ -403,7 +403,9 @@ export async function queryMembersAdvanced( }) if (memberIds.length > 0 && qdbConn) { - const lastActivities = await getLastActivitiesForMembers(qdbConn, memberIds) + const lastActivities = await getLastActivitiesForMembers(qdbConn, memberIds, tenantId, [ + segmentId, + ]) rows.forEach((r) => { r.lastActivity = lastActivities.find((a) => (a as any).memberId === r.id) if (r.lastActivity) {