From d2cf5e86ef01b72b976ab4e6401eb564d780f27b Mon Sep 17 00:00:00 2001 From: Rishi Date: Mon, 30 Oct 2023 14:01:11 +0100 Subject: [PATCH] fix: experience in linkedin importer --- .../edit-profile/Account/LinkedinImporter.js | 5 +- apps/web/ui/utilities/utils.js | 78 ++++++++++++------- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/apps/web/components/edit-profile/Account/LinkedinImporter.js b/apps/web/components/edit-profile/Account/LinkedinImporter.js index 24f04473c..7ca9ee9eb 100644 --- a/apps/web/components/edit-profile/Account/LinkedinImporter.js +++ b/apps/web/components/edit-profile/Account/LinkedinImporter.js @@ -37,7 +37,10 @@ const LinkedinImporter = ({ setProfile }) => { const newProfile = { name: linkedinData.name, bio: linkedinData.description, - workExperiences: linkedinData.roles ? linkedinExperienceTransformer(linkedinData.roles) : [], + workExperiences: linkedinData.roles + ? linkedinExperienceTransformer(linkedinData.roles)?.workExperiences + : [], + experience: linkedinData.roles ? linkedinExperienceTransformer(linkedinData.roles)?.experience : [], projects: linkedinData.projects ? linkedinProjectsTransformer(linkedinData.projects) : [], publications: linkedinData.publications ? linkedinPublicationsTransformer(linkedinData.publications) diff --git a/apps/web/ui/utilities/utils.js b/apps/web/ui/utilities/utils.js index db93ee952..18247fddc 100644 --- a/apps/web/ui/utilities/utils.js +++ b/apps/web/ui/utilities/utils.js @@ -40,36 +40,58 @@ export const extractUserNameFromLinkedinUrl = (url) => { return null; }; -export const linkedinExperienceTransformer = (roles) => { - const transformedData = []; - const companies = {}; - - for (const role of roles) { - if (role.org_id && role.org_type !== "school") { - if (!companies[role.org_id]) { - companies[role.org_id] = { - company: role.org_name ?? "", - url: role.org_url ?? "", - roles: [], - }; - } - - const formattedRole = { - title: role.role_job_title ?? "", - start_date: role.role_start_date, - end_date: role.role_end_date === null ? "Present" : role.role_end_date, - description: role.role_description ?? "", - }; - - companies[role.org_id].roles.push(formattedRole); - } - } +const groupBy = function (xs, key) { + return xs.reduce(function (rv, x) { + (rv[x[key]] = rv[x[key]] || []).push(x); + return rv; + }, {}); +}; - for (const companyId in companies) { - transformedData.push(companies[companyId]); - } +export const linkedinExperienceTransformer = (roles) => { + let experience = [], + workExperiences = []; + const groupedRoles = roles?.length ? groupBy(roles, "org_id") : []; + + Object.entries(groupedRoles).forEach(([id, entry]) => { + const key = entry[0]; + experience.push({ + name: key?.org_name, + url: key?.org_url, + description: "", + roles: [ + { + description: key?.role_description || "", + startMonth: parseInt(key?.role_start_date?.split("-")[1]), + startYear: parseInt(key?.role_start_date?.split("-")[0]), + startDay: 1, + endDay: key?.role_end_date ? "01" : null, + endMonth: key.role_end_date ? parseInt(key.role_end_date.split("-")[1]) : null, + endYear: key.role_end_date ? parseInt(key.role_end_date.split("-")[0]) : null, + title: key?.role_job_title, + isCurrentRole: key.role_end_date ? false : true, + }, + ], + }); + + workExperiences.push({ + title: key?.role_job_title, + description: key?.role_description || "", + startMonth: parseInt(key?.role_start_date?.split("-")[1]), + startYear: parseInt(key?.role_start_date?.split("-")[0]), + startDay: 1, + endDay: key?.role_end_date ? "01" : null, + endMonth: key?.role_end_date ? parseInt(key.role_end_date.split("-")[1]) : null, + endYear: key?.role_end_date ? parseInt(key.role_end_date.split("-")[0]) : null, + isCurrentRole: key.role_end_date ? false : true, + company: { + name: key?.org_name, + linkedInId: key?.id, + url: key?.org_url, + }, + }); + }); - return transformedData; + return { experience, workExperiences }; }; export const linkedinProjectsTransformer = (projects) => {