From 2476a9dae1160a2f36e09c34899eb566cac820a1 Mon Sep 17 00:00:00 2001 From: mirovladimitrovski Date: Thu, 5 Oct 2023 16:09:01 +0200 Subject: [PATCH] fix: contain register logic in one service step --- src/containers/AccountModal/forms/Registration.tsx | 12 +++--------- src/services/cleeng.account.service.ts | 6 +++++- src/services/inplayer.account.service.ts | 4 ++-- src/stores/AccountController.ts | 4 ++-- types/account.d.ts | 2 +- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/containers/AccountModal/forms/Registration.tsx b/src/containers/AccountModal/forms/Registration.tsx index 63f4c93af..0b1e80e8b 100644 --- a/src/containers/AccountModal/forms/Registration.tsx +++ b/src/containers/AccountModal/forms/Registration.tsx @@ -6,10 +6,10 @@ import { useQuery, useQueryClient } from 'react-query'; import useForm, { UseFormOnSubmitHandler } from '#src/hooks/useForm'; import RegistrationForm from '#components/RegistrationForm/RegistrationForm'; -import { extractConsentValues, checkConsentsFromValues, formatConsentsToRegisterFields } from '#src/utils/collection'; +import { extractConsentValues, checkConsentsFromValues } from '#src/utils/collection'; import { addQueryParam } from '#src/utils/location'; import type { RegistrationFormData } from '#types/account'; -import { getPublisherConsents, register, updateConsents } from '#src/stores/AccountController'; +import { getPublisherConsents, register } from '#src/stores/AccountController'; import { useConfigStore } from '#src/stores/ConfigStore'; const Registration = () => { @@ -57,13 +57,7 @@ const Registration = () => { return; } - const cleanConsentValues = formatConsentsToRegisterFields(customerConsents); - - await register(email, password, cleanConsentValues); - - await updateConsents(customerConsents).catch(() => { - // error caught while updating the consents, but continue the registration flow - }); + await register(email, password, customerConsents); await queryClient.invalidateQueries('listProfiles'); diff --git a/src/services/cleeng.account.service.ts b/src/services/cleeng.account.service.ts index a80cfaece..391146f0c 100644 --- a/src/services/cleeng.account.service.ts +++ b/src/services/cleeng.account.service.ts @@ -74,7 +74,7 @@ export const login: Login = async ({ config, email, password }) => { }; }; -export const register: Register = async ({ config, email, password }) => { +export const register: Register = async ({ config, email, password, consents }) => { const localesResponse = await getLocales(!!config.integrations.cleeng?.useSandbox); handleErrors(localesResponse.errors); @@ -96,6 +96,10 @@ export const register: Register = async ({ config, email, password }) => { const { user, customerConsents } = await getUser({ config }); + await updateCustomerConsents({ config, consents, customer: user }).catch(() => { + // error caught while updating the consents, but continue the registration process + }); + return { user, auth, diff --git a/src/services/inplayer.account.service.ts b/src/services/inplayer.account.service.ts index 6260cc88d..527ddcd83 100644 --- a/src/services/inplayer.account.service.ts +++ b/src/services/inplayer.account.service.ts @@ -87,7 +87,7 @@ export const login: Login = async ({ config, email, password }) => { } }; -export const register: Register = async ({ config, email, password, customFields }) => { +export const register: Register = async ({ config, email, password, consents }) => { try { const { data } = await InPlayer.Account.signUpV2({ email, @@ -97,7 +97,7 @@ export const register: Register = async ({ config, email, password, customFields metadata: { first_name: ' ', surname: ' ', - ...customFields, + ...formatConsentsToRegisterFields(consents), }, type: 'consumer', clientId: config.integrations.jwp?.clientId || '', diff --git a/src/stores/AccountController.ts b/src/stores/AccountController.ts index 46052dd09..2b029dada 100644 --- a/src/stores/AccountController.ts +++ b/src/stores/AccountController.ts @@ -192,10 +192,10 @@ export async function logout(logoutOptions: { includeNetworkRequest: boolean } = }); } -export const register = async (email: string, password: string, customFields: Record) => { +export const register = async (email: string, password: string, consents: CustomerConsent[]) => { await useService(async ({ accountService, accessModel, config }) => { useAccountStore.setState({ loading: true }); - const response = await accountService.register({ config, email, password, customFields }); + const response = await accountService.register({ config, email, password, consents }); if (response) { const { user, customerConsents } = response; await afterLogin(user, customerConsents, accessModel); diff --git a/types/account.d.ts b/types/account.d.ts index 99b8de4d8..4b2fdd345 100644 --- a/types/account.d.ts +++ b/types/account.d.ts @@ -27,7 +27,7 @@ export type LoginArgs = { }; export type RegistrationArgs = LoginArgs & { - customFields: Record; + consents: CustomerConsent[]; }; export type AuthResponse = {