Skip to content

Commit

Permalink
fix: using fullName insted of first and last name for the users
Browse files Browse the repository at this point in the history
  • Loading branch information
darkoatanasovski committed Dec 5, 2022
1 parent 96f6da3 commit e740cdc
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions src/services/inplayer.account.service.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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.');
Expand All @@ -44,7 +44,7 @@ export const logout = async () => {
export const getUser = async (): Promise<Customer> => {
try {
const { data } = await InPlayer.Account.getAccountInfo();
return processInplayerAccount(data);
return processAccount(data);
} catch {
throw new Error('Failed to fetch user data.');
}
Expand All @@ -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,
Expand Down

0 comments on commit e740cdc

Please sign in to comment.