Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/commerce/commerce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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'
Expand Down
6 changes: 4 additions & 2 deletions src/commerce/commerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
export const updateCart = (payload: UpdateCartRequestParams) => {
/* a customer could potentially send these up if they're not using TypeScript */
if (payload.user) {
delete (payload as any).user.userId;

Check warning on line 13 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 13 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / e2e-tests (firefox)

Unexpected any. Specify a different type

Check warning on line 13 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / e2e-tests (chromium)

Unexpected any. Specify a different type

Check warning on line 13 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / e2e-tests (webkit)

Unexpected any. Specify a different type
delete (payload as any).user.email;

Check warning on line 14 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 14 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / e2e-tests (firefox)

Unexpected any. Specify a different type

Check warning on line 14 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / e2e-tests (chromium)

Unexpected any. Specify a different type

Check warning on line 14 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / e2e-tests (webkit)

Unexpected any. Specify a different type
}
if (canTrackUnknownUser()) {
const unknownUserEventManager = new UnknownUserEventManager();
Expand All @@ -26,7 +26,8 @@
...payload,
user: {
...payload.user,
preferUserId: true
/* default to true, but allow the caller to override */
preferUserId: payload.user?.preferUserId ?? true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
},
validation: {
Expand All @@ -38,8 +39,8 @@
export const trackPurchase = (payload: TrackPurchaseRequestParams) => {
/* a customer could potentially send these up if they're not using TypeScript */
if (payload.user) {
delete (payload as any).user.userId;

Check warning on line 42 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 42 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / e2e-tests (firefox)

Unexpected any. Specify a different type

Check warning on line 42 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / e2e-tests (chromium)

Unexpected any. Specify a different type

Check warning on line 42 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / e2e-tests (webkit)

Unexpected any. Specify a different type
delete (payload as any).user.email;

Check warning on line 43 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 43 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / e2e-tests (firefox)

Unexpected any. Specify a different type

Check warning on line 43 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / e2e-tests (chromium)

Unexpected any. Specify a different type

Check warning on line 43 in src/commerce/commerce.ts

View workflow job for this annotation

GitHub Actions / e2e-tests (webkit)

Unexpected any. Specify a different type
}
if (canTrackUnknownUser()) {
const unknownUserEventManager = new UnknownUserEventManager();
Expand All @@ -54,7 +55,8 @@
...payload,
user: {
...payload.user,
preferUserId: true
/* default to true, but allow the caller to override */
preferUserId: payload.user?.preferUserId ?? true
}
},
validation: {
Expand Down
9 changes: 6 additions & 3 deletions src/unknownUserTracking/unknownUserEventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/users/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface GetUserResponse {

export interface UpdateUserParams {
dataFields?: Record<string, any>;
preferUserId?: boolean;
mergeNestedObjects?: boolean;
}

Expand Down
21 changes: 21 additions & 0 deletions src/users/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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"`.',
Expand Down
3 changes: 2 additions & 1 deletion src/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading