diff --git a/src/connections/connections.service.ts b/src/connections/connections.service.ts index 5bb8666..eed8449 100644 --- a/src/connections/connections.service.ts +++ b/src/connections/connections.service.ts @@ -154,19 +154,26 @@ export class ConnectionsService { // If the account exists, we just return the user associated with it. const profileUrl = Buffer.from(profileUrlBase64, 'base64').toString(); + // Fetch the profile details + const profileData = await new ProfileFetcherDelegator( + profileUrl, + ).getProfileDetails(); + const socialAccount = await this.prisma.socialAccount.findFirst({ - where: { profileUrl }, + where: { + OR: [ + { profileUrl }, + { + socialId: profileData.socialId, + }, + ], + }, include: { user: { include: this.includeWithUserConnection() }, }, }); if (socialAccount) return this.convertConnectionToDto(socialAccount.user); - // Fetch the profile details - const profileData = await new ProfileFetcherDelegator( - profileUrl, - ).getProfileDetails(); - // Else, we create a new user, associate the social account with it, and return the user. const newUserId = v4(); @@ -188,6 +195,7 @@ export class ConnectionsService { data: { platform: profileData.socialAccountType, profileUrl, + socialId: profileData.socialId, userId: newUserId, }, }), diff --git a/src/user/profile-fetcher/base.profile-fetcher.ts b/src/user/profile-fetcher/base.profile-fetcher.ts index ea1ddc8..2f3f852 100644 --- a/src/user/profile-fetcher/base.profile-fetcher.ts +++ b/src/user/profile-fetcher/base.profile-fetcher.ts @@ -12,4 +12,5 @@ export interface ProfileDetails { name?: string; headline?: string; profilePictureUrl?: string; + socialId?: string; } diff --git a/src/user/profile-fetcher/linkedin.profile-fetcher.ts b/src/user/profile-fetcher/linkedin.profile-fetcher.ts index 54f3123..42bb023 100644 --- a/src/user/profile-fetcher/linkedin.profile-fetcher.ts +++ b/src/user/profile-fetcher/linkedin.profile-fetcher.ts @@ -24,6 +24,7 @@ export class LinkedInProfileFetcher extends ProfileFetcher { socialAccountType: SocialAccountType.LINKEDIN, profileUrl: this.profileUrl, headline: data.data.job_title, + socialId: data.data.profile_id, profilePictureUrl: data.data.profile_image_url, }; } else {