Skip to content

Commit 4823fcc

Browse files
committed
refactor(profile-updated): cron sends full user experiences on each change
1 parent 2c7a425 commit 4823fcc

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

src/cron/userProfileUpdatedSync.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { subHours } from 'date-fns';
22
import { Cron } from './cron';
33
import { UserExperience } from '../entity/user/experiences/UserExperience';
4-
import { MoreThan } from 'typeorm';
4+
import { In, MoreThan } from 'typeorm';
55
import { processStream } from '../common/streaming';
66
import { logger } from '../logger';
77
import { getSecondsTimestamp, triggerTypedEvent } from '../common';
@@ -39,22 +39,45 @@ export const userProfileUpdatedSync: Cron = {
3939
const userExperiences = await queryReadReplica(
4040
con,
4141
async ({ queryRunner }) => {
42-
return queryRunner.manager.getRepository(UserExperience).find({
43-
where: {
44-
updatedAt: MoreThan(timeThreshold),
45-
},
46-
relations: {
47-
skills: true,
48-
company: true,
49-
location: true,
42+
const changedUserProfiles: Pick<UserExperience, 'userId'>[] =
43+
await queryRunner.manager.getRepository(UserExperience).find({
44+
select: ['userId'],
45+
where: {
46+
updatedAt: MoreThan(timeThreshold),
47+
},
48+
relations: {
49+
skills: true,
50+
company: true,
51+
location: true,
52+
},
53+
});
54+
55+
// get all experiences for the changed user profiles so we can send full profile updates
56+
const userExperiences = await queryReadReplica(
57+
con,
58+
async ({ queryRunner }) => {
59+
return queryRunner.manager.getRepository(UserExperience).find({
60+
where: {
61+
userId: In(
62+
changedUserProfiles.map((profile) => profile.userId),
63+
),
64+
},
65+
relations: {
66+
skills: true,
67+
company: true,
68+
location: true,
69+
},
70+
});
5071
},
51-
});
72+
);
73+
74+
return userExperiences;
5275
},
5376
);
5477

5578
const experiencesByUser = new Map<string, UserExperience[]>();
5679

57-
for await (const experience of userExperiences) {
80+
for (const experience of userExperiences) {
5881
let userExperiences = experiencesByUser.get(experience.userId);
5982

6083
if (!userExperiences) {

0 commit comments

Comments
 (0)