|
1 | 1 | import { subHours } from 'date-fns'; |
2 | 2 | import { Cron } from './cron'; |
3 | 3 | import { UserExperience } from '../entity/user/experiences/UserExperience'; |
4 | | -import { MoreThan } from 'typeorm'; |
| 4 | +import { In, MoreThan } from 'typeorm'; |
5 | 5 | import { processStream } from '../common/streaming'; |
6 | 6 | import { logger } from '../logger'; |
7 | 7 | import { getSecondsTimestamp, triggerTypedEvent } from '../common'; |
@@ -39,22 +39,45 @@ export const userProfileUpdatedSync: Cron = { |
39 | 39 | const userExperiences = await queryReadReplica( |
40 | 40 | con, |
41 | 41 | 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 | + }); |
50 | 71 | }, |
51 | | - }); |
| 72 | + ); |
| 73 | + |
| 74 | + return userExperiences; |
52 | 75 | }, |
53 | 76 | ); |
54 | 77 |
|
55 | 78 | const experiencesByUser = new Map<string, UserExperience[]>(); |
56 | 79 |
|
57 | | - for await (const experience of userExperiences) { |
| 80 | + for (const experience of userExperiences) { |
58 | 81 | let userExperiences = experiencesByUser.get(experience.userId); |
59 | 82 |
|
60 | 83 | if (!userExperiences) { |
|
0 commit comments