Skip to content

Commit

Permalink
some cleanup and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mprew97 committed Sep 10, 2024
1 parent 198881e commit fb459b7
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 53 deletions.
45 changes: 1 addition & 44 deletions src/authorization/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export interface WithoutJWT {
export const setAnonUserId = async (userId: string) => {
let token: null | string = null;
if (generateJWTGlobal) {
token = await generateJWTGlobal({ userId: userId });
token = await generateJWTGlobal({ userID: userId });
}

baseAxiosRequest.interceptors.request.use((config) => {
Expand Down Expand Up @@ -517,34 +517,11 @@ export function initialize(
},
setUserID: async (userId: string, merge?: boolean) => {
clearMessages();
const tryUser = () => {
let createUserAttempts = 0;
return async function tryUserNTimes(): Promise<any> {
try {
return await updateUser();
} catch (e) {
if (createUserAttempts < RETRY_USER_ATTEMPTS) {
createUserAttempts += 1;
return tryUserNTimes();
}

return Promise.reject(
`could not create user after ${createUserAttempts} tries`
);
}
};
};
try {
merge = getMergeDefaultValue(merge);
const result = await tryMergeUser(userId, false, merge);
if (result) {
initializeUserIdAndSync(userId, merge);
try {
return await tryUser()();
} catch (e) {
/* failed to create a new user. Just silently resolve */
return Promise.resolve();
}
}
} catch (error) {
// here we will not sync events but just bubble up error of merge
Expand Down Expand Up @@ -858,25 +835,6 @@ export function initialize(
},
setUserID: async (userId: string, merge?: boolean) => {
clearMessages();

const tryUser = () => {
let createUserAttempts = 0;

return async function tryUserNTimes(): Promise<any> {
try {
return await updateUser();
} catch (e) {
if (createUserAttempts < RETRY_USER_ATTEMPTS) {
createUserAttempts += 1;
return tryUserNTimes();
}

return Promise.reject(
`could not create user after ${createUserAttempts} tries`
);
}
};
};
try {
merge = getMergeDefaultValue(merge);
const result = await tryMergeUser(userId, false, merge);
Expand All @@ -885,7 +843,6 @@ export function initialize(
try {
return doRequest({ userID: userId })
.then(async (token) => {
await tryUser()();
return token;
})
.catch((e) => {
Expand Down
6 changes: 3 additions & 3 deletions src/commerce/commerce.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-param-reassign */
import { INITIALIZE_ERROR, ENDPOINTS } from '../constants';
import { ENDPOINTS } from '../constants';
import { baseIterableRequest } from '../request';
import { TrackPurchaseRequestParams, UpdateCartRequestParams } from './types';
import { IterableResponse } from '../types';
Expand All @@ -16,7 +16,7 @@ export const updateCart = (payload: UpdateCartRequestParams) => {
if (canTrackAnonUser()) {
const anonymousUserEventManager = new AnonymousUserEventManager();
anonymousUserEventManager.trackAnonUpdateCart(payload);
return Promise.reject(INITIALIZE_ERROR);
return Promise.resolve();
}
return baseIterableRequest<IterableResponse>({
method: 'POST',
Expand All @@ -43,7 +43,7 @@ export const trackPurchase = (payload: TrackPurchaseRequestParams) => {
if (canTrackAnonUser()) {
const anonymousUserEventManager = new AnonymousUserEventManager();
anonymousUserEventManager.trackAnonPurchaseEvent(payload);
return Promise.reject(INITIALIZE_ERROR);
return Promise.resolve();
}
return baseIterableRequest<IterableResponse>({
method: 'POST',
Expand Down
2 changes: 0 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,3 @@ export const UPDATECART_ITEM_PREFIX = 'updateCart.updatedShoppingCartItems.';
export const PURCHASE_ITEM_PREFIX = `${PURCHASE_ITEM}.`;

export const MERGE_SUCCESSFULL = 'MERGE_SUCCESSFULL';
export const INITIALIZE_ERROR =
'Iterable SDK must be initialized with an API key and user email/userId before calling SDK methods';
4 changes: 2 additions & 2 deletions src/events/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-param-reassign */
import { INITIALIZE_ERROR, ENDPOINTS } from '../constants';
import { ENDPOINTS } from '../constants';
import { baseIterableRequest } from '../request';
import { InAppTrackRequestParams } from './inapp/types';
import { IterableResponse } from '../types';
Expand All @@ -14,7 +14,7 @@ export const track = (payload: InAppTrackRequestParams) => {
if (canTrackAnonUser()) {
const anonymousUserEventManager = new AnonymousUserEventManager();
anonymousUserEventManager.trackAnonEvent(payload);
return Promise.reject(INITIALIZE_ERROR);
return Promise.resolve();
}
return baseIterableRequest<IterableResponse>({
method: 'POST',
Expand Down
4 changes: 2 additions & 2 deletions src/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { UpdateSubscriptionParams, UpdateUserParams } from './types';
import { updateSubscriptionsSchema, updateUserSchema } from './users.schema';
import { AnonymousUserEventManager } from '../anonymousUserTracking/anonymousUserEventManager';
import { canTrackAnonUser } from '../utils/commonFunctions';
import { INITIALIZE_ERROR, ENDPOINTS } from '../constants';
import { ENDPOINTS } from '../constants';

export const updateUserEmail = (newEmail: string) =>
baseIterableRequest<IterableResponse>({
Expand All @@ -31,7 +31,7 @@ export const updateUser = (payloadParam: UpdateUserParams = {}) => {
if (canTrackAnonUser()) {
const anonymousUserEventManager = new AnonymousUserEventManager();
anonymousUserEventManager.trackAnonUpdateUser(payload);
return Promise.reject(INITIALIZE_ERROR);
return Promise.resolve();
}
return baseIterableRequest<IterableResponse>({
method: 'POST',
Expand Down

0 comments on commit fb459b7

Please sign in to comment.