diff --git a/src/services/inplayer.account.service.ts b/src/services/inplayer.account.service.ts index 08d57717a..d88bfe090 100644 --- a/src/services/inplayer.account.service.ts +++ b/src/services/inplayer.account.service.ts @@ -1,6 +1,6 @@ import InPlayer, { AccountData, Env } from '@inplayer-org/inplayer.js'; -import type { AuthData, Customer, CustomerConsent, Login, UpdateCustomer } from '#types/account'; +import type { AuthData, Customer, Login, UpdateCustomer } from '#types/account'; import type { Config } from '#types/Config'; import type { InPlayerAuthData } from '#types/inplayer'; @@ -25,8 +25,8 @@ export const login: Login = async ({ config, email, password }) => { }); return { - auth: processInPlayerAuth(data), - user: processInplayerAccount(data.account), + auth: processAuth(data), + user: processAccount(data.account), }; } catch { throw new Error('Failed to authenticate user.'); @@ -44,7 +44,7 @@ export const logout = async () => { export const getUser = async (): Promise => { try { const { data } = await InPlayer.Account.getAccountInfo(); - return processInplayerAccount(data); + return processAccount(data); } catch { throw new Error('Failed to fetch user data.'); } @@ -54,50 +54,44 @@ export const getFreshJwtToken = async ({ auth }: { auth: AuthData }) => auth; export const updateCustomer: UpdateCustomer = async (values) => { try { - const consents: { [key: string]: string } = {}; - values.consents?.map((consent: CustomerConsent) => { - if (consent.label) { - const { customerId, date, newestVersion, needsUpdate, ...rest } = consent; - consents[`consents_${consent.name}`] = JSON.stringify(rest); - } - }); + const fullName = `${values.firstName} ${values.lastName}`; const response = await InPlayer.Account.updateAccount({ - fullName: `${values.firstName} ${values.lastName}`, + fullName, metadata: { - first_name: values.firstName as string, - last_name: values.lastName as string, - ...consents, + ...(values?.consents && { consents: JSON.stringify(values.consents) }), }, }); return { errors: [], // @ts-ignore - // wrong data type from InPlayer SDK - responseData: processInplayerAccount(response.data), + // wrong data type from InPlayer SDK (will be updated in the SDK) + responseData: processAccount(response.data), }; } catch { - throw new Error('Failed to fetch user data.'); + throw new Error('Failed to update user data.'); } }; + // responsible to convert the InPlayer object to be compatible to the store -function processInplayerAccount(account: AccountData): Customer { - const { id, email, full_name: fullName, metadata, created_at: createdAt } = account; +function processAccount(account: AccountData): Customer { + const { id, email, full_name: fullName, created_at: createdAt } = account; const regDate = new Date(createdAt * 1000).toLocaleString(); + const nameParts = fullName.split(' '); return { id: id.toString(), email, fullName, - firstName: metadata?.first_name as string, - lastName: metadata?.last_name as string, + firstName: nameParts[0] || '', + lastName: nameParts.slice(1).join(' '), regDate, country: '', lastUserIp: '', }; } -function processInPlayerAuth(auth: InPlayerAuthData): AuthData { +function processAuth(auth: InPlayerAuthData): AuthData { const { access_token: jwt } = auth; return { jwt,