Skip to content

Commit 6bbb6ce

Browse files
authored
SDK-563 make preferUserId configurable (#578)
* [SDK-563] Make preferUserId caller-configurable, default to true Previously the SDK hardcoded preferUserId: true on users/update, commerce updateCart, and trackPurchase requests, overriding any value the caller passed. Now it uses `payload.preferUserId ?? true` (and `payload.user?.preferUserId ?? true` for the nested commerce user), so callers can opt out while the default stays true. * fixed type gap with updateUser
1 parent 190a168 commit 6bbb6ce

6 files changed

Lines changed: 74 additions & 6 deletions

File tree

src/commerce/commerce.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ describe('Users Requests', () => {
4848
expect(response.data.msg).toBe('hello');
4949
});
5050

51+
it('should respect a caller-provided preferUserId of false for updateCart', async () => {
52+
mockRequest.onPost('/commerce/updateCart').reply(200, {
53+
msg: 'hello'
54+
});
55+
56+
const response = await updateCart({
57+
user: {
58+
preferUserId: false
59+
},
60+
items: [
61+
{
62+
id: 'fdsafds',
63+
name: 'banana',
64+
quantity: 2,
65+
price: 12
66+
}
67+
]
68+
});
69+
70+
expect(JSON.parse(response.config.data).user.preferUserId).toBe(false);
71+
expect(response.data.msg).toBe('hello');
72+
});
73+
5174
it('should reject updateCart on bad params', async () => {
5275
try {
5376
await updateCart({
@@ -93,6 +116,23 @@ describe('Users Requests', () => {
93116
expect(response.data.msg).toBe('hello');
94117
});
95118

119+
it('should respect a caller-provided preferUserId of false for trackPurchase', async () => {
120+
mockRequest.onPost('/commerce/trackPurchase').reply(200, {
121+
msg: 'hello'
122+
});
123+
124+
const response = await trackPurchase({
125+
user: {
126+
preferUserId: false
127+
},
128+
items: [],
129+
total: 100
130+
});
131+
132+
expect(JSON.parse(response.config.data).user.preferUserId).toBe(false);
133+
expect(response.data.msg).toBe('hello');
134+
});
135+
96136
it('should not allow a passed userId or email for API methods', async () => {
97137
mockRequest.onPost('/commerce/trackPurchase').reply(200, {
98138
msg: 'hello'

src/commerce/commerce.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export const updateCart = (payload: UpdateCartRequestParams) => {
2626
...payload,
2727
user: {
2828
...payload.user,
29-
preferUserId: true
29+
/* default to true, but allow the caller to override */
30+
preferUserId: payload.user?.preferUserId ?? true
3031
}
3132
},
3233
validation: {
@@ -54,7 +55,8 @@ export const trackPurchase = (payload: TrackPurchaseRequestParams) => {
5455
...payload,
5556
user: {
5657
...payload.user,
57-
preferUserId: true
58+
/* default to true, but allow the caller to override */
59+
preferUserId: payload.user?.preferUserId ?? true
5860
}
5961
},
6062
validation: {

src/unknownUserTracking/unknownUserEventManager.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,8 @@ export class UnknownUserEventManager {
504504
...payload,
505505
user: {
506506
...payload.user,
507-
preferUserId: true
507+
/* default to true, but allow the caller to override */
508+
preferUserId: payload.user?.preferUserId ?? true
508509
}
509510
},
510511
validation: {
@@ -520,7 +521,8 @@ export class UnknownUserEventManager {
520521
...payload,
521522
user: {
522523
...payload.user,
523-
preferUserId: true
524+
/* default to true, but allow the caller to override */
525+
preferUserId: payload.user?.preferUserId ?? true
524526
}
525527
},
526528
validation: {
@@ -535,7 +537,8 @@ export class UnknownUserEventManager {
535537
url: ENDPOINTS.users_update.route,
536538
data: {
537539
...payload,
538-
preferUserId: true
540+
/* default to true, but allow the caller to override */
541+
preferUserId: payload.preferUserId ?? true
539542
},
540543
validation: {
541544
data: updateUserSchema

src/users/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface GetUserResponse {
1212

1313
export interface UpdateUserParams {
1414
dataFields?: Record<string, any>;
15+
preferUserId?: boolean;
1516
mergeNestedObjects?: boolean;
1617
}
1718

src/users/users.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ describe('Users Requests', () => {
3636
expect(response && response.data.msg).toBe('hello');
3737
});
3838

39+
it('should respect a caller-provided preferUserId of false for updateUser', async () => {
40+
mockRequest.onPost('/users/update').reply(200, {
41+
msg: 'hello'
42+
});
43+
44+
const response = await updateUser({
45+
dataFields: {},
46+
preferUserId: false
47+
});
48+
49+
expect(JSON.parse(response && response.config.data).preferUserId).toBe(
50+
false
51+
);
52+
expect(response && response.data.msg).toBe('hello');
53+
});
54+
3955
it('should reject updateUser on bad params', async () => {
4056
try {
4157
await updateUser({
@@ -52,6 +68,11 @@ describe('Users Requests', () => {
5268
' If "null" is intended as an empty value be sure to mark the schema as `.nullable()`',
5369
field: 'dataFields'
5470
},
71+
{
72+
error:
73+
'preferUserId must be a `boolean` type, but the final value was: `"string"`.',
74+
field: 'preferUserId'
75+
},
5576
{
5677
error:
5778
'mergeNestedObjects must be a `boolean` type, but the final value was: `"string"`.',

src/users/users.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export const updateUser = (payloadParam: UpdateUserParams = {}) => {
3838
url: ENDPOINTS.users_update.route,
3939
data: {
4040
...payload,
41-
preferUserId: true
41+
/* default to true, but allow the caller to override */
42+
preferUserId: payload.preferUserId ?? true
4243
},
4344
validation: {
4445
data: updateUserSchema

0 commit comments

Comments
 (0)