import { User } from '../models/user'; import { UserProfile } from '../types/user'; const UPDATE_USER_PROFILE_FIELDS = ['id', 'email', 'secondaryEmail', 'password', 'title', 'firstName', 'lastName', 'primaryTelephone', 'jobTitle', 'graduationDate', 'organisationName', 'organisationId', 'isEmailVerified']; /** * @attention * The `externalUserId` property is added only to the "create profile" property list, * because it's not considered by Membership API on "profile update". It requires * a separate call to a "update externalUserId" specific endpoint. * * Follow up story to apply externalUserId on profile updating: * https://financialtimes.atlassian.net/browse/ENT-978 */ const NEW_USER_PROFILE_FIELDS = ['email', 'password', 'secondaryEmail', 'title', 'firstName', 'lastName', 'primaryTelephone', 'jobTitle', 'graduationDate', 'allowanceSize', 'source', 'organisationName', 'isEmailVerified', 'externalUserId']; const HOME_ADDRESS_FIELDS = ['country', 'line1', 'line2', 'townCity', 'state', 'postcode']; export function verifyNewUserFields(user: User) { if (user.email == null) { throw new Error('Email cannot be null'); } if (user.password == null) { throw new Error('Password cannot be null'); } if (user.country == null) { throw new Error('Country cannot be null'); } } export function getNewUserRequestBody(user: User): Partial { verifyNewUserFields(user); const profile: Partial = extractParameters(NEW_USER_PROFILE_FIELDS, user); const homeAddress = extractParameters(HOME_ADDRESS_FIELDS, user); homeAddress.postcode = user.postcode; profile.homeAddress = homeAddress; const demographics = extractDemographics(user); if (Object.keys(demographics).length > 0) { profile.demographics = demographics; } return profile; } export function verifyUpdateUserFields(user: User) { if (user.id == null) { throw new Error('User ID cannot be null'); } if (user.email == null) { throw new Error('Email cannot be null'); } if (user.country == null) { throw new Error('Country cannot be null'); } } export function getUpdateUserRequestBody(user: User): Partial { verifyUpdateUserFields(user); const profile: Partial = extractParameters(UPDATE_USER_PROFILE_FIELDS, user); const homeAddress = extractParameters(HOME_ADDRESS_FIELDS, user); homeAddress.postcode = user.postcode; profile.homeAddress = homeAddress; const demographics = extractDemographics(user); if (Object.keys(demographics).length > 0) { profile.demographics = demographics; } return profile; } export function extractDemographics(user: User) { const demographics:any = {}; if (user.industry) { demographics.industry = { code: user.industry }; } if (user.responsibility) { demographics.responsibility = { code: user.responsibility }; } if (user.position) { demographics.position = { code: user.position }; } return demographics; } export function extractParameters(parameters: Array, user: User) { const obj:any = {}; parameters.forEach( // @ts-ignore (optionalParam) => user[optionalParam] != null ? obj[optionalParam] = user[optionalParam] : undefined); return obj; }