diff --git a/src/commerce/commerce.test.ts b/src/commerce/commerce.test.ts index e580814e..fb6e1822 100644 --- a/src/commerce/commerce.test.ts +++ b/src/commerce/commerce.test.ts @@ -48,6 +48,29 @@ describe('Users Requests', () => { expect(response.data.msg).toBe('hello'); }); + it('should respect a caller-provided preferUserId of false for updateCart', async () => { + mockRequest.onPost('/commerce/updateCart').reply(200, { + msg: 'hello' + }); + + const response = await updateCart({ + user: { + preferUserId: false + }, + items: [ + { + id: 'fdsafds', + name: 'banana', + quantity: 2, + price: 12 + } + ] + }); + + expect(JSON.parse(response.config.data).user.preferUserId).toBe(false); + expect(response.data.msg).toBe('hello'); + }); + it('should reject updateCart on bad params', async () => { try { await updateCart({ @@ -93,6 +116,23 @@ describe('Users Requests', () => { expect(response.data.msg).toBe('hello'); }); + it('should respect a caller-provided preferUserId of false for trackPurchase', async () => { + mockRequest.onPost('/commerce/trackPurchase').reply(200, { + msg: 'hello' + }); + + const response = await trackPurchase({ + user: { + preferUserId: false + }, + items: [], + total: 100 + }); + + expect(JSON.parse(response.config.data).user.preferUserId).toBe(false); + expect(response.data.msg).toBe('hello'); + }); + it('should not allow a passed userId or email for API methods', async () => { mockRequest.onPost('/commerce/trackPurchase').reply(200, { msg: 'hello' diff --git a/src/commerce/commerce.ts b/src/commerce/commerce.ts index f09c2524..31a2ab34 100644 --- a/src/commerce/commerce.ts +++ b/src/commerce/commerce.ts @@ -26,7 +26,8 @@ export const updateCart = (payload: UpdateCartRequestParams) => { ...payload, user: { ...payload.user, - preferUserId: true + /* default to true, but allow the caller to override */ + preferUserId: payload.user?.preferUserId ?? true } }, validation: { @@ -54,7 +55,8 @@ export const trackPurchase = (payload: TrackPurchaseRequestParams) => { ...payload, user: { ...payload.user, - preferUserId: true + /* default to true, but allow the caller to override */ + preferUserId: payload.user?.preferUserId ?? true } }, validation: { diff --git a/src/unknownUserTracking/unknownUserEventManager.ts b/src/unknownUserTracking/unknownUserEventManager.ts index 85c4f97b..af7dbb1e 100644 --- a/src/unknownUserTracking/unknownUserEventManager.ts +++ b/src/unknownUserTracking/unknownUserEventManager.ts @@ -504,7 +504,8 @@ export class UnknownUserEventManager { ...payload, user: { ...payload.user, - preferUserId: true + /* default to true, but allow the caller to override */ + preferUserId: payload.user?.preferUserId ?? true } }, validation: { @@ -520,7 +521,8 @@ export class UnknownUserEventManager { ...payload, user: { ...payload.user, - preferUserId: true + /* default to true, but allow the caller to override */ + preferUserId: payload.user?.preferUserId ?? true } }, validation: { @@ -535,7 +537,8 @@ export class UnknownUserEventManager { url: ENDPOINTS.users_update.route, data: { ...payload, - preferUserId: true + /* default to true, but allow the caller to override */ + preferUserId: payload.preferUserId ?? true }, validation: { data: updateUserSchema diff --git a/src/users/types.ts b/src/users/types.ts index 79c43e2a..6dd5a0c3 100644 --- a/src/users/types.ts +++ b/src/users/types.ts @@ -12,6 +12,7 @@ export interface GetUserResponse { export interface UpdateUserParams { dataFields?: Record; + preferUserId?: boolean; mergeNestedObjects?: boolean; } diff --git a/src/users/users.test.ts b/src/users/users.test.ts index 2fb189b3..01728b4a 100644 --- a/src/users/users.test.ts +++ b/src/users/users.test.ts @@ -36,6 +36,22 @@ describe('Users Requests', () => { expect(response && response.data.msg).toBe('hello'); }); + it('should respect a caller-provided preferUserId of false for updateUser', async () => { + mockRequest.onPost('/users/update').reply(200, { + msg: 'hello' + }); + + const response = await updateUser({ + dataFields: {}, + preferUserId: false + }); + + expect(JSON.parse(response && response.config.data).preferUserId).toBe( + false + ); + expect(response && response.data.msg).toBe('hello'); + }); + it('should reject updateUser on bad params', async () => { try { await updateUser({ @@ -52,6 +68,11 @@ describe('Users Requests', () => { ' If "null" is intended as an empty value be sure to mark the schema as `.nullable()`', field: 'dataFields' }, + { + error: + 'preferUserId must be a `boolean` type, but the final value was: `"string"`.', + field: 'preferUserId' + }, { error: 'mergeNestedObjects must be a `boolean` type, but the final value was: `"string"`.', diff --git a/src/users/users.ts b/src/users/users.ts index 46c11620..c5a30209 100644 --- a/src/users/users.ts +++ b/src/users/users.ts @@ -38,7 +38,8 @@ export const updateUser = (payloadParam: UpdateUserParams = {}) => { url: ENDPOINTS.users_update.route, data: { ...payload, - preferUserId: true + /* default to true, but allow the caller to override */ + preferUserId: payload.preferUserId ?? true }, validation: { data: updateUserSchema