Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MemberAffiliationRepository {
(ARRAY_REMOVE(ARRAY_AGG(CASE WHEN msa.id IS NULL THEN '00000000-0000-0000-0000-000000000000' ELSE msa."organizationId" END), '00000000-0000-0000-0000-000000000000')
|| ARRAY_REMOVE(ARRAY_AGG(mo."organizationId" ORDER BY mo."dateStart" DESC, mo.id), NULL)
|| ARRAY_REMOVE(ARRAY_AGG(mo1."organizationId" ORDER BY mo1."createdAt" DESC, mo1.id), NULL)
|| ARRAY_REMOVE(ARRAY_AGG(mo2."organizationId" ORDER BY mo2."createdAt", mo2.id), NULL)
)[1] AS new_org
FROM activities a
LEFT JOIN "memberSegmentAffiliations" msa ON msa."memberId" = a."memberId" AND a."segmentId" = msa."segmentId" AND (
Expand All @@ -30,6 +31,7 @@ class MemberAffiliationRepository {
OR (a.timestamp >= mo."dateStart" AND mo."dateEnd" IS NULL)
)
LEFT JOIN "memberOrganizations" mo1 ON mo1."memberId" = a."memberId" AND mo1."createdAt" <= a.timestamp
LEFT JOIN "memberOrganizations" mo2 ON mo2."memberId" = a."memberId"
WHERE a."memberId" = :memberId
GROUP BY a.id
)
Expand Down
28 changes: 28 additions & 0 deletions backend/src/database/repositories/memberRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3461,6 +3461,34 @@ class MemberRepository {
return records[0]
}

static async findMostRecentOrganizationEver(
memberId: string,
options: IRepositoryOptions,
): Promise<any> {
const seq = SequelizeRepository.getSequelize(options)
const transaction = SequelizeRepository.getTransaction(options)

const query = `
SELECT * FROM "memberOrganizations"
WHERE "memberId" = :memberId
ORDER BY "createdAt", id
LIMIT 1
`
const records = await seq.query(query, {
replacements: {
memberId,
},
type: QueryTypes.SELECT,
transaction,
})

if (records.length === 0) {
return null
}

return records[0]
}

static sortOrganizations(organizations) {
organizations.sort((a, b) => {
a = a.dataValues ? a.get({ plain: true }) : a
Expand Down
4 changes: 2 additions & 2 deletions backend/src/services/__tests__/activityService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3042,7 +3042,7 @@ describe('ActivityService tests', () => {
expect(activity.organization.id).toBe(org3.id)
})

it('Should affiliate nothing if there is no matching work experience', async () => {
it('Should affiliate first created org to past activities', async () => {
const member = await createMember()

let activity = await createActivity(member.id, {
Expand All @@ -3065,7 +3065,7 @@ describe('ActivityService tests', () => {

activity = await findActivity(activity.id)

expect(activity.organization).toBeNull()
expect(activity.organization.id).toBe(org1.id)
})

it('Should prefer manual affiliation over work experience', async () => {
Expand Down
8 changes: 8 additions & 0 deletions backend/src/services/memberAffiliationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ export default class MemberAffiliationService extends LoggerBase {
return mostRecentOrg.organizationId
}

const mostRecentOrgEver: any = await MemberRepository.findMostRecentOrganizationEver(
memberId,
this.options,
)
if (mostRecentOrgEver) {
return mostRecentOrgEver.organizationId
}

return null
}

Expand Down
18 changes: 18 additions & 0 deletions services/apps/data_sink_worker/src/repo/memberAffiliation.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,22 @@ export default class MemberAffiliationRepository extends RepositoryBase<MemberAf

return result
}

public async findMostRecentOrganizationEver(
memberId: string,
): Promise<IWorkExperienceData | null> {
const result = await this.db().oneOrNone(
`
SELECT * FROM "memberOrganizations"
WHERE "memberId" = $(memberId)
ORDER BY "createdAt", id
LIMIT 1
`,
{
memberId,
},
)

return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export default class MemberAffiliationService extends LoggerBase {
return mostRecentOrg.organizationId
}

const mostRecentOrgEver = await this.repo.findMostRecentOrganizationEver(memberId)
if (mostRecentOrgEver) {
return mostRecentOrgEver.organizationId
}

return null
}
}