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

Don't override UI changes in member affiliation v2 #1444

Merged
merged 6 commits into from
Sep 7, 2023
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 @@ -3217,7 +3217,7 @@ describe('MemberRepository tests', () => {
// member1 is expected to have [org1,org2] after update
member1 = await MemberRepository.update(
member1.id,
{ organizations: [org1.id, org2.id] },
{ organizations: [org1.id, org2.id], organizationsReplace: true },
mockIRepositoryOptions,
)

Expand Down
56 changes: 30 additions & 26 deletions backend/src/database/repositories/memberRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
OrganizationSource,
} from '@crowd/types'
import lodash, { chunk } from 'lodash'
import moment from 'moment'
import Sequelize, { QueryTypes } from 'sequelize'

import { FieldTranslatorFactory, OpensearchQueryParser } from '@crowd/opensearch'
Expand Down Expand Up @@ -137,6 +138,7 @@ class MemberRepository {
await MemberRepository.updateMemberOrganizations(
record,
data.organizations,
true,
transaction,
options,
)
Expand Down Expand Up @@ -565,7 +567,8 @@ class MemberRepository {
'dateEnd', "dateEnd",
'createdAt', "createdAt",
'updatedAt', "updatedAt",
'title', title
'title', title,
'source', source
)
)
) AS orgs
Expand Down Expand Up @@ -688,6 +691,7 @@ class MemberRepository {
await MemberRepository.updateMemberOrganizations(
record,
data.organizations,
data.organizationsReplace,
transaction,
options,
)
Expand Down Expand Up @@ -963,7 +967,7 @@ class MemberRepository {
as: 'organizations',
order: [['createdAt', 'ASC']],
through: {
attributes: ['memberId', 'organizationId', 'dateStart', 'dateEnd', 'title'],
attributes: ['memberId', 'organizationId', 'dateStart', 'dateEnd', 'title', 'source'],
where: {
deletedAt: null,
},
Expand Down Expand Up @@ -3199,7 +3203,7 @@ class MemberRepository {
output.organizations = await record.getOrganizations({
transaction,
order: [['createdAt', 'ASC']],
joinTableAttributes: ['dateStart', 'dateEnd', 'title'],
joinTableAttributes: ['dateStart', 'dateEnd', 'title', 'source'],
through: {
where: {
deletedAt: null,
Expand Down Expand Up @@ -3268,33 +3272,38 @@ class MemberRepository {
static async updateMemberOrganizations(
record,
organizations,
replace,
transaction,
options: IRepositoryOptions,
) {
if (!organizations) {
return
}

const sourceUi = !!organizations.find((org) => org.source === OrganizationSource.UI)
function iso(v) {
return moment(v).toISOString()
}

const originalOrgs = await MemberRepository.fetchWorkExperiences(record.id, sourceUi, options)
if (replace) {
const originalOrgs = await MemberRepository.fetchWorkExperiences(record.id, options)

const toDelete = originalOrgs.filter(
(originalOrg: any) =>
!organizations.find(
(newOrg) =>
originalOrg.organizationId === newOrg.id &&
originalOrg.title === (newOrg.title || null) &&
originalOrg.dateStart === (newOrg.startDate || null) &&
originalOrg.dateEnd === (newOrg.endDate || null),
),
)
const toDelete = originalOrgs.filter(
(originalOrg: any) =>
!organizations.find(
(newOrg) =>
originalOrg.organizationId === newOrg.id &&
originalOrg.title === (newOrg.title || null) &&
iso(originalOrg.dateStart) === iso(newOrg.startDate || null) &&
iso(originalOrg.dateEnd) === iso(newOrg.endDate || null),
),
)

for (const item of toDelete) {
await MemberRepository.deleteWorkExperience((item as any).id, {
transaction,
...options,
})
for (const item of toDelete) {
await MemberRepository.deleteWorkExperience((item as any).id, {
transaction,
...options,
})
}
}

for (const item of organizations) {
Expand Down Expand Up @@ -3438,18 +3447,13 @@ class MemberRepository {
)
}

static async fetchWorkExperiences(
memberId: string,
sourceUi: boolean,
options: IRepositoryOptions,
) {
static async fetchWorkExperiences(memberId: string, options: IRepositoryOptions) {
const seq = SequelizeRepository.getSequelize(options)
const transaction = SequelizeRepository.getTransaction(options)

const query = `
SELECT * FROM "memberOrganizations"
WHERE "memberId" = :memberId
${sourceUi ? '' : "AND (source IS NULL OR source != 'ui')"}
AND "deletedAt" IS NULL
`

Expand Down
2 changes: 1 addition & 1 deletion backend/src/serverless/integrations/types/messageTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type Member = {
displayName?: string
attributes?: any
emails?: string[]
organizations?: [any]
organizations?: any[]
bio?: string
reach?: number | any
location?: string
Expand Down
4 changes: 4 additions & 0 deletions backend/src/services/__tests__/memberService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ describe('MemberService tests', () => {
dateEnd: null,
dateStart: null,
title: null,
source: null,
},
tags: null,
twitter: null,
Expand Down Expand Up @@ -580,6 +581,7 @@ describe('MemberService tests', () => {
dateEnd: null,
dateStart: null,
title: null,
source: null,
},
tags: null,
twitter: null,
Expand Down Expand Up @@ -678,6 +680,7 @@ describe('MemberService tests', () => {
dateEnd: null,
dateStart: null,
title: null,
source: null,
},
tags: null,
twitter: null,
Expand Down Expand Up @@ -776,6 +779,7 @@ describe('MemberService tests', () => {
dateEnd: null,
dateStart: null,
title: null,
source: null,
},
tags: [],
twitter: {
Expand Down
26 changes: 0 additions & 26 deletions backend/src/services/memberService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,32 +698,6 @@ export default class MemberService extends LoggerBase {

return toKeep
},
organizations: (oldOrganizations, newOrganizations) => {
const convertOrgs = (orgs) =>
orgs
? orgs
.map((o) => (o.dataValues ? o.get({ plain: true }) : o))
.map((o) => {
if (typeof o === 'string') {
return {
id: o,
}
}
const memberOrg = o.memberOrganizations
return {
id: o.id,
title: memberOrg?.title,
startDate: memberOrg?.dateStart,
endDate: memberOrg?.dateEnd,
}
})
: []

oldOrganizations = convertOrgs(oldOrganizations)
newOrganizations = convertOrgs(newOrganizations)

return lodash.uniqWith([...oldOrganizations, ...newOrganizations], lodash.isEqual)
},
})
}

Expand Down
34 changes: 0 additions & 34 deletions backend/src/services/premium/enrichment/memberEnrichmentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
MemberEnrichmentAttributeName,
MemberEnrichmentAttributes,
PlatformType,
IOrganization,
OrganizationSource,
} from '@crowd/types'
import { ENRICHMENT_CONFIG, REDIS_CONFIG } from '../../../conf'
Expand Down Expand Up @@ -354,39 +353,6 @@ export default class MemberEnrichmentService extends LoggerBase {
member.emails = Array.from(emailSet)
}

if (enrichmentData.company) {
const organization: IOrganization = {
name: enrichmentData.company,
}

// check for more info about the company in work experiences
if (enrichmentData.work_experiences && enrichmentData.work_experiences.length > 0) {
const organizationsByWorkExperience = enrichmentData.work_experiences.filter(
(w) => w.company === enrichmentData.company && w.current,
)
if (organizationsByWorkExperience.length > 0) {
organization.location = organizationsByWorkExperience[0].location
const linkedinUrl = organizationsByWorkExperience[0].companyLinkedInUrl
if (linkedinUrl) {
organization.linkedin = {
handle: linkedinUrl.split('/').pop(),
// remove https/http if exists
url: linkedinUrl.replace(/(^\w+:|^)\/\//, ''),
}
}
organization.url = organizationsByWorkExperience[0].companyUrl

// fetch jobTitle from most recent work experience
member.attributes.jobTitle = {
custom: organizationsByWorkExperience[0].title,
default: organizationsByWorkExperience[0].title,
}
}
}

member.organizations = [organization]
}

member.contributions = enrichmentData.oss_contributions?.map(
(contribution: EnrichmentAPIContribution) => ({
id: contribution.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const save = () => {
doUpdate({
id: props.member.id,
values: {
organizationsReplace: true,
organizations: organizations.value.map(
(o) => ({
id: o.id,
Expand Down
1 change: 1 addition & 0 deletions frontend/src/modules/member/pages/member-form-page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ async function onSubmit() {
tags: formModel.value.tags.map((t) => t.id),
},
...formModel.value.organizations.length && {
organizationsReplace: true,
organizations: formModel.value.organizations.map(
(o) => ({
id: o.id,
Expand Down