Skip to content

Commit 8aa32dc

Browse files
fix: link experience when company is manually approved (#3321)
1 parent 109f597 commit 8aa32dc

2 files changed

Lines changed: 231 additions & 0 deletions

File tree

__tests__/workers/cdc/primary.ts

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5126,6 +5126,213 @@ describe('user company approved', () => {
51265126
expect(experiences.every((exp) => exp.verified)).toBe(true);
51275127
});
51285128
});
5129+
5130+
describe('links experiences by customCompanyName', () => {
5131+
beforeEach(async () => {
5132+
await saveFixtures(con, User, usersFixture);
5133+
await con.getRepository(Company).save({
5134+
id: 'comp1',
5135+
name: 'Test Company',
5136+
altName: 'Test Alt Name',
5137+
image: 'https://example.com/image.png',
5138+
domains: ['testcompany.com'],
5139+
createdAt: new Date(),
5140+
updatedAt: new Date(),
5141+
});
5142+
});
5143+
5144+
it('should link experience with matching customCompanyName (exact match)', async () => {
5145+
const experience = await con.getRepository(UserExperienceWork).save({
5146+
userId: '1',
5147+
companyId: null,
5148+
customCompanyName: 'Test Company',
5149+
title: 'Software Engineer',
5150+
startedAt: new Date('2020-01-01'),
5151+
verified: false,
5152+
type: UserExperienceType.Work,
5153+
});
5154+
5155+
const after: ChangeObject<ObjectType> = { ...base, companyId: 'comp1' };
5156+
await expectSuccessfulBackground(
5157+
worker,
5158+
mockChangeMessage<ObjectType>({
5159+
after,
5160+
before: null,
5161+
op: 'c',
5162+
table: 'user_company',
5163+
}),
5164+
);
5165+
5166+
const updatedExperience = await con
5167+
.getRepository(UserExperienceWork)
5168+
.findOneBy({ id: experience.id });
5169+
expect(updatedExperience?.companyId).toBe('comp1');
5170+
});
5171+
5172+
it('should link experience with matching customCompanyName (case insensitive)', async () => {
5173+
const experience = await con.getRepository(UserExperienceWork).save({
5174+
userId: '1',
5175+
companyId: null,
5176+
customCompanyName: 'TEST COMPANY',
5177+
title: 'Software Engineer',
5178+
startedAt: new Date('2020-01-01'),
5179+
verified: false,
5180+
type: UserExperienceType.Work,
5181+
});
5182+
5183+
const after: ChangeObject<ObjectType> = { ...base, companyId: 'comp1' };
5184+
await expectSuccessfulBackground(
5185+
worker,
5186+
mockChangeMessage<ObjectType>({
5187+
after,
5188+
before: null,
5189+
op: 'c',
5190+
table: 'user_company',
5191+
}),
5192+
);
5193+
5194+
const updatedExperience = await con
5195+
.getRepository(UserExperienceWork)
5196+
.findOneBy({ id: experience.id });
5197+
expect(updatedExperience?.companyId).toBe('comp1');
5198+
});
5199+
5200+
it('should link experience with matching altName (case insensitive)', async () => {
5201+
const experience = await con.getRepository(UserExperienceWork).save({
5202+
userId: '1',
5203+
companyId: null,
5204+
customCompanyName: 'test alt name',
5205+
title: 'Software Engineer',
5206+
startedAt: new Date('2020-01-01'),
5207+
verified: false,
5208+
type: UserExperienceType.Work,
5209+
});
5210+
5211+
const after: ChangeObject<ObjectType> = { ...base, companyId: 'comp1' };
5212+
await expectSuccessfulBackground(
5213+
worker,
5214+
mockChangeMessage<ObjectType>({
5215+
after,
5216+
before: null,
5217+
op: 'c',
5218+
table: 'user_company',
5219+
}),
5220+
);
5221+
5222+
const updatedExperience = await con
5223+
.getRepository(UserExperienceWork)
5224+
.findOneBy({ id: experience.id });
5225+
expect(updatedExperience?.companyId).toBe('comp1');
5226+
});
5227+
5228+
it('should not link experience with non-matching customCompanyName', async () => {
5229+
const experience = await con.getRepository(UserExperienceWork).save({
5230+
userId: '1',
5231+
companyId: null,
5232+
customCompanyName: 'Some Other Company',
5233+
title: 'Software Engineer',
5234+
startedAt: new Date('2020-01-01'),
5235+
verified: false,
5236+
type: UserExperienceType.Work,
5237+
});
5238+
5239+
const after: ChangeObject<ObjectType> = { ...base, companyId: 'comp1' };
5240+
await expectSuccessfulBackground(
5241+
worker,
5242+
mockChangeMessage<ObjectType>({
5243+
after,
5244+
before: null,
5245+
op: 'c',
5246+
table: 'user_company',
5247+
}),
5248+
);
5249+
5250+
const updatedExperience = await con
5251+
.getRepository(UserExperienceWork)
5252+
.findOneBy({ id: experience.id });
5253+
expect(updatedExperience?.companyId).toBeNull();
5254+
});
5255+
5256+
it('should not overwrite experience that already has a companyId', async () => {
5257+
await con.getRepository(Company).save({
5258+
id: 'comp2',
5259+
name: 'Other Company',
5260+
image: 'https://example.com/image2.png',
5261+
domains: ['othercompany.com'],
5262+
createdAt: new Date(),
5263+
updatedAt: new Date(),
5264+
});
5265+
5266+
const experience = await con.getRepository(UserExperienceWork).save({
5267+
userId: '1',
5268+
companyId: 'comp2',
5269+
customCompanyName: 'Test Company',
5270+
title: 'Software Engineer',
5271+
startedAt: new Date('2020-01-01'),
5272+
verified: false,
5273+
type: UserExperienceType.Work,
5274+
});
5275+
5276+
const after: ChangeObject<ObjectType> = { ...base, companyId: 'comp1' };
5277+
await expectSuccessfulBackground(
5278+
worker,
5279+
mockChangeMessage<ObjectType>({
5280+
after,
5281+
before: null,
5282+
op: 'c',
5283+
table: 'user_company',
5284+
}),
5285+
);
5286+
5287+
const updatedExperience = await con
5288+
.getRepository(UserExperienceWork)
5289+
.findOneBy({ id: experience.id });
5290+
expect(updatedExperience?.companyId).toBe('comp2');
5291+
});
5292+
5293+
it('should only link experiences for the same user', async () => {
5294+
const experience1 = await con.getRepository(UserExperienceWork).save({
5295+
userId: '1',
5296+
companyId: null,
5297+
customCompanyName: 'Test Company',
5298+
title: 'Software Engineer',
5299+
startedAt: new Date('2020-01-01'),
5300+
verified: false,
5301+
type: UserExperienceType.Work,
5302+
});
5303+
5304+
const experience2 = await con.getRepository(UserExperienceWork).save({
5305+
userId: '2',
5306+
companyId: null,
5307+
customCompanyName: 'Test Company',
5308+
title: 'Software Engineer',
5309+
startedAt: new Date('2020-01-01'),
5310+
verified: false,
5311+
type: UserExperienceType.Work,
5312+
});
5313+
5314+
const after: ChangeObject<ObjectType> = { ...base, companyId: 'comp1' };
5315+
await expectSuccessfulBackground(
5316+
worker,
5317+
mockChangeMessage<ObjectType>({
5318+
after,
5319+
before: null,
5320+
op: 'c',
5321+
table: 'user_company',
5322+
}),
5323+
);
5324+
5325+
const updatedExperience1 = await con
5326+
.getRepository(UserExperienceWork)
5327+
.findOneBy({ id: experience1.id });
5328+
expect(updatedExperience1?.companyId).toBe('comp1');
5329+
5330+
const updatedExperience2 = await con
5331+
.getRepository(UserExperienceWork)
5332+
.findOneBy({ id: experience2.id });
5333+
expect(updatedExperience2?.companyId).toBeNull();
5334+
});
5335+
});
51295336
});
51305337

51315338
describe('bookmark change', () => {

src/workers/cdc/primary.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ import { UserExperience } from '../../entity/user/experiences/UserExperience';
165165
import { UserExperienceType } from '../../entity/user/experiences/types';
166166
import { cio, identifyUserOpportunities } from '../../cio';
167167
import { enrichCompanyForExperience } from '../../common/companyEnrichment';
168+
import { Company } from '../../entity/Company';
168169

169170
const isFreeformPostLongEnough = (
170171
freeform: ChangeMessage<FreeformPost>,
@@ -1206,6 +1207,29 @@ const onUserCompanyCompanyChange = async (
12061207
await con
12071208
.getRepository(UserExperienceWork)
12081209
.update({ userId, companyId: companyId! }, { verified: true });
1210+
1211+
// Link experiences that have matching customCompanyName but no companyId yet
1212+
const company = await con
1213+
.getRepository(Company)
1214+
.findOneBy({ id: companyId! });
1215+
if (company) {
1216+
const companyNames = [company.name.toLowerCase()];
1217+
if (company.altName) {
1218+
companyNames.push(company.altName.toLowerCase());
1219+
}
1220+
1221+
await con
1222+
.getRepository(UserExperience)
1223+
.createQueryBuilder()
1224+
.update()
1225+
.set({ companyId: companyId! })
1226+
.where('userId = :userId', { userId })
1227+
.andWhere('companyId IS NULL')
1228+
.andWhere('LOWER("customCompanyName") IN (:...companyNames)', {
1229+
companyNames,
1230+
})
1231+
.execute();
1232+
}
12091233
}
12101234
};
12111235

0 commit comments

Comments
 (0)