Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update getActivitiesById and getLastActivitiesForMembers #2641

Merged
101 changes: 57 additions & 44 deletions backend/src/database/repositories/memberRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -1528,7 +1531,6 @@ class MemberRepository {
...include,
},
},
options,
)

if (memberResponse.count === 0) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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)
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
Expand All @@ -2345,9 +2358,9 @@ class MemberRepository {
)
}
})

return { rows, count, limit, offset }
}

return { rows, count, limit, offset }
}

/**
Expand Down
8 changes: 7 additions & 1 deletion backend/src/database/repositories/taskRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!`)
}
Expand Down
8 changes: 8 additions & 0 deletions services/libs/data-access-layer/src/activities/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const s3Url = `https://${
export async function getActivitiesById(
conn: DbConnOrTx,
ids: string[],
tenantId: string,
segmentIds: string[],
): Promise<IQueryActivityResult[]> {
if (ids.length === 0) {
return []
Expand All @@ -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
Expand Down Expand Up @@ -1396,6 +1400,8 @@ export async function getNewActivityPlatforms(
export async function getLastActivitiesForMembers(
qdbConn: DbConnOrTx,
memberIds: string[],
tenantId: string,
segmentIds?: string[],
): Promise<IQueryActivityResult[]> {
const query = `
select id from activities where "deletedAt" is null and "memberId" in ($(memberIds:csv))
Expand All @@ -1410,5 +1416,7 @@ export async function getLastActivitiesForMembers(
return getActivitiesById(
qdbConn,
results.map((r) => r.id),
tenantId,
segmentIds,
)
}
4 changes: 3 additions & 1 deletion services/libs/data-access-layer/src/members/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading