diff --git a/src/containers/AccountModal/forms/Registration.tsx b/src/containers/AccountModal/forms/Registration.tsx index 4fb717425..9d4060736 100644 --- a/src/containers/AccountModal/forms/Registration.tsx +++ b/src/containers/AccountModal/forms/Registration.tsx @@ -57,7 +57,7 @@ const Registration = () => { return; } - await register(email, password); + await register(email, password, consentValues); await updateConsents(customerConsents).catch(() => { // error caught while updating the consents, but continue the registration flow diff --git a/src/services/inplayer.account.service.ts b/src/services/inplayer.account.service.ts index 852b5cde6..184b81589 100644 --- a/src/services/inplayer.account.service.ts +++ b/src/services/inplayer.account.service.ts @@ -86,8 +86,33 @@ export const login: Login = async ({ config, email, password }) => { } }; -export const register: Register = async ({ config, email, password }) => { +export const register: Register = async ({ config, email, password, customFields }) => { try { + const metadata = { + ...customFields, + ...Object.entries(customFields).reduce((acc, [name, val]) => { + const isBoolean = val === true || val === false; + + if (isBoolean) { + if (name === 'terms') { + return { ...acc, [name]: val }; + } + + return { ...acc, [name]: val === true ? 'on' : 'off' }; + } + + if (name === 'us_state') { + if (customFields.country === 'us') { + return { ...acc, [name]: val === 'n/a' ? '' : val }; + } + + return { ...acc, [name]: 'n/a' }; + } + + return { ...acc, [name]: val }; + }, {}), + }; + const { data } = await InPlayer.Account.signUpV2({ email, password, @@ -96,6 +121,7 @@ export const register: Register = async ({ config, email, password }) => { metadata: { first_name: ' ', surname: ' ', + ...metadata, }, type: 'consumer', clientId: config.integrations.jwp?.clientId || '', diff --git a/src/stores/AccountController.ts b/src/stores/AccountController.ts index 843a0dc0c..46052dd09 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) => { +export const register = async (email: string, password: string, customFields: Record) => { await useService(async ({ accountService, accessModel, config }) => { useAccountStore.setState({ loading: true }); - const response = await accountService.register({ config, email, password }); + const response = await accountService.register({ config, email, password, customFields }); if (response) { const { user, customerConsents } = response; await afterLogin(user, customerConsents, accessModel); diff --git a/types/account.d.ts b/types/account.d.ts index fbc9ae842..99b8de4d8 100644 --- a/types/account.d.ts +++ b/types/account.d.ts @@ -20,12 +20,16 @@ export type PayloadWithIPOverride = { customerIP?: string; }; -export type AuthArgs = { +export type LoginArgs = { config: Config; email: string; password: string; }; +export type RegistrationArgs = LoginArgs & { + customFields: Record; +}; + export type AuthResponse = { auth: AuthData; user: Customer; @@ -360,8 +364,8 @@ export type DeleteAccountPayload = { password: string; }; -type Login = PromiseRequest; -type Register = PromiseRequest; +type Login = PromiseRequest; +type Register = PromiseRequest; type GetCustomer = EnvironmentServiceRequest; type UpdateCustomer = EnvironmentServiceRequest; type GetPublisherConsents = PromiseRequest;