Skip to content

Commit

Permalink
fix: send crf metadata on update account
Browse files Browse the repository at this point in the history
  • Loading branch information
mirovladimitrovski committed Oct 3, 2023
1 parent b1273ca commit 26539ad
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 49 deletions.
13 changes: 10 additions & 3 deletions src/components/Account/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Checkbox from '#components/Checkbox/Checkbox';
import HelperText from '#components/HelperText/HelperText';
import CustomRegisterField from '#components/CustomRegisterField/CustomRegisterField';
import useToggle from '#src/hooks/useToggle';
import { formatConsentsFromValues, formatConsents, formatConsentValues } from '#src/utils/collection';
import { formatConsentsFromValues, formatConsents, formatConsentValues, noEmptyStringEntries, formatCrfEntries } from '#src/utils/collection';
import { addQueryParam } from '#src/utils/location';
import { useAccountStore } from '#src/stores/AccountStore';
import { isTruthy, logDev } from '#src/utils/common';
Expand Down Expand Up @@ -262,7 +262,10 @@ const Account = ({ panelClassName, panelHeaderClassName, canUpdateEmail = true }
formSection({
label: t('account.terms_and_tracking'),
saveButton: t('account.update_consents'),
onSubmit: (values) => updateConsents(formatConsentsFromValues(publisherConsents, { ...values.consentsValues, terms: true })),
onSubmit: (values) => {
const cleanConsentValues = Object.fromEntries(Object.entries(values.consentsValues).filter(noEmptyStringEntries).map(formatCrfEntries));
return updateConsents(formatConsentsFromValues(publisherConsents, { ...cleanConsentValues, terms: true }), cleanConsentValues);
},
content: (section) => (
<>
{termsConsents?.map((consent, index) => (
Expand All @@ -282,7 +285,11 @@ const Account = ({ panelClassName, panelHeaderClassName, canUpdateEmail = true }
formSection({
label: t('account.other_registration_details'),
saveButton: t('account.update_consents'),
onSubmit: (values) => updateConsents(formatConsentsFromValues(publisherConsents, values.consentsValues)),
onSubmit: (values) => {
const cleanConsentValues = Object.fromEntries(Object.entries(values.consentsValues).filter(noEmptyStringEntries).map(formatCrfEntries));
return updateConsents(formatConsentsFromValues(publisherConsents, cleanConsentValues), cleanConsentValues);
},

content: (section) => (
<div className={styles.customFields}>
{nonTermsConsents.map((consent) => (
Expand Down
8 changes: 5 additions & 3 deletions src/containers/AccountModal/forms/Registration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useQuery, useQueryClient } from 'react-query';

import useForm, { UseFormOnSubmitHandler } from '#src/hooks/useForm';
import RegistrationForm from '#components/RegistrationForm/RegistrationForm';
import { extractConsentValues, checkConsentsFromValues } from '#src/utils/collection';
import { extractConsentValues, checkConsentsFromValues, noEmptyStringEntries, formatCrfEntries } from '#src/utils/collection';
import { addQueryParam } from '#src/utils/location';
import type { RegistrationFormData } from '#types/account';
import { getPublisherConsents, register, updateConsents } from '#src/stores/AccountController';
Expand Down Expand Up @@ -57,9 +57,11 @@ const Registration = () => {
return;
}

await register(email, password, consentValues);
const cleanConsentValues = Object.fromEntries(Object.entries(consentValues).filter(noEmptyStringEntries).map(formatCrfEntries));

await updateConsents(customerConsents).catch(() => {
await register(email, password, cleanConsentValues);

await updateConsents(customerConsents, cleanConsentValues).catch(() => {
// error caught while updating the consents, but continue the registration flow
});

Expand Down
45 changes: 3 additions & 42 deletions src/services/inplayer.account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,6 @@ export const login: Login = 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 value = (() => {
if (name === 'us_state') {
if (customFields.country === 'us') {
return val === 'n/a' ? '' : val;
}

return 'n/a';
}

const isBoolean = val === true || val === false;

if (isBoolean && name !== 'terms') {
return val ? 'on' : 'off';
}

return val;
})();

return { ...acc, [name]: value };
}, {}),
};

const { data } = await InPlayer.Account.signUpV2({
email,
password,
Expand All @@ -121,7 +96,7 @@ export const register: Register = async ({ config, email, password, customFields
metadata: {
first_name: ' ',
surname: ' ',
...metadata,
...customFields,
},
type: 'consumer',
clientId: config.integrations.jwp?.clientId || '',
Expand Down Expand Up @@ -236,29 +211,15 @@ export const getCustomerConsents: GetCustomerConsents = async (payload) => {

export const updateCustomerConsents: UpdateCustomerConsents = async (payload) => {
try {
const { customer, consents } = payload;
const { customer, consents, consentValues } = payload;

const existingAccountData = formatUpdateAccount(customer);

const newMetadata = Object.fromEntries(
consents
.filter(({ value }) => value !== '')
.map(({ name, value }) => {
const isBoolean = value === true || value === false;

if (isBoolean) {
return [name, value === true ? 'on' : 'off'];
}

return [name, value];
}),
);

const params = {
...existingAccountData,
metadata: {
...existingAccountData.metadata,
...newMetadata,
...consentValues,
consents: JSON.stringify(consents),
},
};
Expand Down
6 changes: 5 additions & 1 deletion src/stores/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ export const updatePersonalShelves = async () => {
});
};

export const updateConsents = async (customerConsents: CustomerConsent[]): Promise<ServiceResponse<CustomerConsent[]>> => {
export const updateConsents = async (
customerConsents: CustomerConsent[],
consentValues: Record<string, string | boolean>,
): Promise<ServiceResponse<CustomerConsent[]>> => {
return await useAccount(async ({ customer }) => {
return await useService(async ({ accountService, config }) => {
useAccountStore.setState({ loading: true });
Expand All @@ -238,6 +241,7 @@ export const updateConsents = async (customerConsents: CustomerConsent[]): Promi
config,
customer,
consents: customerConsents,
consentValues,
});

if (response?.consents) {
Expand Down
26 changes: 26 additions & 0 deletions src/utils/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ const checkConsentsFromValues = (publisherConsents: Consent[], consents: Record<
return { customerConsents, consentsErrors };
};

const noEmptyStringEntries = <T>([, value]: [string, T]) => value !== '';

const formatCrfEntries = <T>([name, value]: [string, T], _: number, collection: [string, T][]) => {
const val = (() => {
if (name === 'us_state') {
if (Object.fromEntries(collection).country === 'us') {
return value === 'n/a' ? '' : value;
}

return 'n/a';
}

const isBoolean = value === true || value === false;

if (isBoolean && name !== 'terms') {
return value ? 'on' : 'off';
}

return value;
})();

return [name, val] as const;
};

const deepCopy = (obj: unknown) => {
if (Array.isArray(obj) || (typeof obj === 'object' && obj !== null)) {
return JSON.parse(JSON.stringify(obj));
Expand Down Expand Up @@ -177,4 +201,6 @@ export {
deepCopy,
parseAspectRatio,
parseTilesDelta,
noEmptyStringEntries,
formatCrfEntries,
};
1 change: 1 addition & 0 deletions types/account.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export type UpdateCustomerConsentsArgs = {
config: Config;
customer: Customer;
consents: CustomerConsent[];
consentValues: Record<string, string | boolean>;
};

export type LocalesData = {
Expand Down

0 comments on commit 26539ad

Please sign in to comment.