From f812039de76bf3d318507b1de90ecace9d1723eb Mon Sep 17 00:00:00 2001 From: annarhughes Date: Tue, 28 May 2024 16:58:15 +0100 Subject: [PATCH] minor refactors --- src/api/crisp/crisp-api.ts | 60 +++++++++++++++--------------- src/api/mailchimp/mailchimp-api.ts | 2 +- src/user/user.service.spec.ts | 6 +-- src/utils/profileData.ts | 4 +- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/api/crisp/crisp-api.ts b/src/api/crisp/crisp-api.ts index 20aab4c0..259398ba 100644 --- a/src/api/crisp/crisp-api.ts +++ b/src/api/crisp/crisp-api.ts @@ -21,15 +21,7 @@ const headers = { const logger = new Logger('UserService'); -export const getCrispPeopleData = async (email: string): Promise> => { - return await apiCall({ - url: `${baseUrl}/people/data/${email}`, - type: 'get', - headers, - }); -}; - -export const addCrispProfile = async ( +export const createMailchimpProfile = async ( newPeopleProfile: NewPeopleProfile, ): Promise> => { try { @@ -46,22 +38,22 @@ export const addCrispProfile = async ( } }; -export const updateCrispProfileData = async ( - peopleData: PeopleData, +export const getCrispProfile = async ( email: string, -): Promise> => { - try { - return await apiCall({ - url: `${baseUrl}/people/data/${email}`, - type: 'patch', - data: { data: peopleData }, - headers, - }); - } catch (error) { - logger.error(`Could not update crisp profile for user: ${email}`); +): Promise> => { + return await apiCall({ + url: `${baseUrl}/people/profile/${email}`, + type: 'get', + headers, + }); +}; - throw error; - } +export const getCrispPeopleData = async (email: string): Promise> => { + return await apiCall({ + url: `${baseUrl}/people/data/${email}`, + type: 'get', + headers, + }); }; export const updateCrispProfile = async ( @@ -76,14 +68,22 @@ export const updateCrispProfile = async ( }); }; -export const getCrispProfile = async ( +export const updateCrispProfileData = async ( + peopleData: PeopleData, email: string, -): Promise> => { - return await apiCall({ - url: `${baseUrl}/people/profile/${email}`, - type: 'get', - headers, - }); +): Promise> => { + try { + return await apiCall({ + url: `${baseUrl}/people/data/${email}`, + type: 'patch', + data: { data: peopleData }, + headers, + }); + } catch (error) { + logger.error(`Could not update crisp profile for user: ${email}`); + + throw error; + } }; export const deleteCrispProfile = async (email: string) => { diff --git a/src/api/mailchimp/mailchimp-api.ts b/src/api/mailchimp/mailchimp-api.ts index 5c53a2fb..6cf00a41 100644 --- a/src/api/mailchimp/mailchimp-api.ts +++ b/src/api/mailchimp/mailchimp-api.ts @@ -22,7 +22,7 @@ export async function ping() { console.log(response); } -export async function createContact(user: UserEntity) { +export async function createMailchimpProfile(user: UserEntity) { const response = await mailchimp.lists.addListMember(mailchimpAudienceId, { email_address: user.email, status: 'subscribed', diff --git a/src/user/user.service.spec.ts b/src/user/user.service.spec.ts index a70dd94b..ba81eea2 100644 --- a/src/user/user.service.spec.ts +++ b/src/user/user.service.spec.ts @@ -3,7 +3,7 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'; import { HttpException, HttpStatus } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; -import { addCrispProfile } from 'src/api/crisp/crisp-api'; +import { createMailchimpProfile } from 'src/api/crisp/crisp-api'; import { PartnerAccessEntity } from 'src/entities/partner-access.entity'; import { PartnerEntity } from 'src/entities/partner.entity'; import { PartnerAccessCodeStatusEnum } from 'src/utils/constants'; @@ -110,7 +110,7 @@ describe('UserService', () => { expect(user.partnerAdmin).toBeNull(); expect(user.partnerAccesses).toBeNull(); expect(repoSaveSpy).toHaveBeenCalledWith(createUserRepositoryDto); - expect(addCrispProfile).toHaveBeenCalledWith({ + expect(createMailchimpProfile).toHaveBeenCalledWith({ email: user.user.email, person: { nickname: 'name' }, segments: ['public'], @@ -140,7 +140,7 @@ describe('UserService', () => { { ...partnerAccessData, therapySessions: therapySession }, ]); - expect(addCrispProfile).toHaveBeenCalledWith({ + expect(createMailchimpProfile).toHaveBeenCalledWith({ email: user.user.email, person: { nickname: 'name' }, segments: ['bumble'], diff --git a/src/utils/profileData.ts b/src/utils/profileData.ts index 95381702..672cf567 100644 --- a/src/utils/profileData.ts +++ b/src/utils/profileData.ts @@ -1,4 +1,4 @@ -import { addCrispProfile, updateCrispProfileData } from 'src/api/crisp/crisp-api'; +import { createMailchimpProfile, updateCrispProfileData } from 'src/api/crisp/crisp-api'; import { CourseUserEntity } from 'src/entities/course-user.entity'; import { PartnerAccessEntity } from 'src/entities/partner-access.entity'; import { PartnerEntity } from 'src/entities/partner.entity'; @@ -17,7 +17,7 @@ export const createServicesProfiles = async ( partner: PartnerEntity, partnerAccess: PartnerAccessEntity, ) => { - await addCrispProfile({ + await createMailchimpProfile({ email: user.email, person: { nickname: user.name }, segments: [partner?.name.toLowerCase() || 'public'],