Skip to content

Commit

Permalink
fix: contain register logic in one service step
Browse files Browse the repository at this point in the history
  • Loading branch information
mirovladimitrovski committed Oct 5, 2023
1 parent 988ea46 commit 2476a9d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
12 changes: 3 additions & 9 deletions src/containers/AccountModal/forms/Registration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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');

Expand Down
6 changes: 5 additions & 1 deletion src/services/cleeng.account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/services/inplayer.account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 || '',
Expand Down
4 changes: 2 additions & 2 deletions src/stores/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ export async function logout(logoutOptions: { includeNetworkRequest: boolean } =
});
}

export const register = async (email: string, password: string, customFields: Record<string, string | boolean>) => {
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);
Expand Down
2 changes: 1 addition & 1 deletion types/account.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type LoginArgs = {
};

export type RegistrationArgs = LoginArgs & {
customFields: Record<string, string | boolean>;
consents: CustomerConsent[];
};

export type AuthResponse = {
Expand Down

0 comments on commit 2476a9d

Please sign in to comment.