@scayle/storefront-core 8.61.1 → 8.61.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/CHANGELOG-V7.md +1 -11
  2. package/CHANGELOG.md +102 -103
  3. package/dist/api/customer.mjs +2 -4
  4. package/dist/api/oauth.mjs +9 -6
  5. package/dist/cache/providers/unstorage.mjs +1 -3
  6. package/dist/constants/withParameters.mjs +2 -16
  7. package/dist/helpers/attributeHelpers.mjs +3 -1
  8. package/dist/helpers/filterHelper.mjs +11 -8
  9. package/dist/helpers/productHelpers.mjs +1 -3
  10. package/dist/helpers/sanitizationHelpers.mjs +1 -4
  11. package/dist/helpers/sortingHelper.mjs +1 -3
  12. package/dist/rpc/methods/basket/basket.d.ts +2 -2
  13. package/dist/rpc/methods/basket/basket.mjs +334 -321
  14. package/dist/rpc/methods/brands.mjs +26 -20
  15. package/dist/rpc/methods/campaign.mjs +32 -23
  16. package/dist/rpc/methods/categories.mjs +156 -135
  17. package/dist/rpc/methods/cbd.mjs +52 -49
  18. package/dist/rpc/methods/checkout/checkout.mjs +42 -39
  19. package/dist/rpc/methods/checkout/order.mjs +46 -40
  20. package/dist/rpc/methods/checkout/shopUser.mjs +112 -106
  21. package/dist/rpc/methods/navigationTrees.mjs +50 -32
  22. package/dist/rpc/methods/oauth/idp.mjs +64 -55
  23. package/dist/rpc/methods/products.mjs +224 -212
  24. package/dist/rpc/methods/promotion.mjs +46 -31
  25. package/dist/rpc/methods/search.mjs +26 -20
  26. package/dist/rpc/methods/session.mjs +291 -262
  27. package/dist/rpc/methods/shopConfiguration.mjs +12 -9
  28. package/dist/rpc/methods/user.mjs +87 -78
  29. package/dist/rpc/methods/variants.mjs +17 -14
  30. package/dist/rpc/methods/wishlist.mjs +71 -62
  31. package/dist/types/api/context.d.ts +2 -2
  32. package/dist/utils/hash.mjs +2 -7
  33. package/dist/utils/sapi.mjs +10 -7
  34. package/package.json +6 -7
@@ -21,221 +21,232 @@ const SAPI_ERROR_NAME = "SAPI ERROR";
21
21
  function getWithParams(params, context) {
22
22
  return params.with ?? context.withParams?.basket ?? MIN_WITH_PARAMS_BASKET;
23
23
  }
24
- export const addItemToBasket = defineRpcHandler(async ({
25
- variantId,
26
- promotionId,
27
- promotions,
28
- quantity,
29
- displayData,
30
- customData,
31
- existingItemHandling = ExistingItemHandling.ADD_QUANTITY_TO_EXISTING,
32
- itemGroup,
33
- with: _with,
34
- orderCustomData
35
- }, context) => {
36
- if (!hasSession(context)) {
37
- return new ErrorResponse(
38
- HttpStatusCode.BAD_REQUEST,
39
- HttpStatusMessage.BAD_REQUEST,
40
- "No Session found"
24
+ export const addItemToBasket = defineRpcHandler(
25
+ async ({
26
+ variantId,
27
+ promotionId,
28
+ promotions,
29
+ quantity,
30
+ displayData,
31
+ customData,
32
+ existingItemHandling = ExistingItemHandling.ADD_QUANTITY_TO_EXISTING,
33
+ itemGroup,
34
+ with: _with,
35
+ orderCustomData
36
+ }, context) => {
37
+ if (!hasSession(context)) {
38
+ return new ErrorResponse(
39
+ HttpStatusCode.BAD_REQUEST,
40
+ HttpStatusMessage.BAD_REQUEST,
41
+ "No Session found"
42
+ );
43
+ }
44
+ const { sapiClient, basketKey } = context;
45
+ const campaignKey = await context.callRpc?.("getCampaignKey");
46
+ const _orderCustomData = await context.callRpc?.("getOrderCustomData");
47
+ const resolvedWith = getWithParams(
48
+ { with: _with },
49
+ context
41
50
  );
42
- }
43
- const { sapiClient, basketKey } = context;
44
- const campaignKey = await context.callRpc?.("getCampaignKey");
45
- const _orderCustomData = await context.callRpc?.("getOrderCustomData");
46
- const resolvedWith = getWithParams(
47
- { with: _with },
48
- context
49
- );
50
- const result = await sapiClient.basket.addOrUpdateItems(
51
- basketKey,
52
- [
53
- {
54
- variantId,
55
- quantity,
56
- params: {
57
- promotionId: promotionId ?? void 0,
58
- promotions: promotions ?? void 0,
59
- campaignKey,
60
- displayData,
61
- pricePromotionKey: resolvedWith?.pricePromotionKey || "",
62
- customData,
63
- itemGroup,
64
- with: resolvedWith,
65
- includeItemsWithoutProductData: resolvedWith?.includeItemsWithoutProductData
51
+ const result = await sapiClient.basket.addOrUpdateItems(
52
+ basketKey,
53
+ [
54
+ {
55
+ variantId,
56
+ quantity,
57
+ params: {
58
+ promotionId: promotionId ?? void 0,
59
+ promotions: promotions ?? void 0,
60
+ campaignKey,
61
+ displayData,
62
+ pricePromotionKey: resolvedWith?.pricePromotionKey || "",
63
+ customData,
64
+ itemGroup,
65
+ with: resolvedWith,
66
+ includeItemsWithoutProductData: resolvedWith?.includeItemsWithoutProductData
67
+ }
66
68
  }
67
- }
68
- ],
69
- {
70
- skipAvailabilityCheck: false,
71
- orderCustomData: mergeOrderCustomData(_orderCustomData, orderCustomData),
72
- customerToken: getValidatedAccessToken(context.accessToken) ?? void 0
73
- },
74
- {
75
- existingItemHandling
76
- }
77
- );
78
- if (result.type === "success") {
79
- return { basket: result.basket };
80
- } else if (result.type === "failure" && wasAddedWithReducedQuantity(result.errors)) {
81
- return {
82
- basket: result.basket,
83
- errors: result.errors
84
- };
85
- } else {
86
- const { message, statusCode } = parseBasketError(result);
87
- context.log.error("Adding item to basket failed", { message, statusCode });
88
- return new ErrorResponse(
89
- HttpStatusCode.BAD_REQUEST,
90
- SAPI_ERROR_NAME,
91
- "Adding item to basket failed",
69
+ ],
92
70
  {
93
- detail: message,
94
- errors: result.errors
71
+ skipAvailabilityCheck: false,
72
+ orderCustomData: mergeOrderCustomData(
73
+ _orderCustomData,
74
+ orderCustomData
75
+ ),
76
+ customerToken: getValidatedAccessToken(context.accessToken) ?? void 0
77
+ },
78
+ {
79
+ existingItemHandling
95
80
  }
96
81
  );
97
- }
98
- }, { method: "PUT" });
99
- export const addItemsToBasket = defineRpcHandler(async (params, context) => {
100
- if (!hasSession(context)) {
101
- return new ErrorResponse(
102
- HttpStatusCode.BAD_REQUEST,
103
- HttpStatusMessage.BAD_REQUEST,
104
- "No Session found"
105
- );
106
- }
107
- const { sapiClient, basketKey } = context;
108
- const campaignKey = await context.callRpc?.("getCampaignKey");
109
- const _orderCustomData = await context.callRpc?.("getOrderCustomData");
110
- const resolvedWith = getWithParams(params, context);
111
- const itemsToBeAddedOrUpdated = params.items.map((item) => ({
112
- variantId: item.variantId,
113
- quantity: item.quantity,
114
- params: {
115
- promotionId: item.promotionId ?? void 0,
116
- promotions: item.promotions ?? void 0,
117
- campaignKey,
118
- displayData: item.displayData,
119
- pricePromotionKey: resolvedWith?.pricePromotionKey || "",
120
- customData: item.customData,
121
- with: resolvedWith,
122
- includeItemsWithoutProductData: item.includeItemsWithoutProductData ?? resolvedWith?.includeItemsWithoutProductData,
123
- itemGroup: item.itemGroup ?? void 0
82
+ if (result.type === "success") {
83
+ return { basket: result.basket };
84
+ } else if (result.type === "failure" && wasAddedWithReducedQuantity(result.errors)) {
85
+ return {
86
+ basket: result.basket,
87
+ errors: result.errors
88
+ };
89
+ } else {
90
+ const { message, statusCode } = parseBasketError(result);
91
+ context.log.error("Adding item to basket failed", { message, statusCode });
92
+ return new ErrorResponse(
93
+ HttpStatusCode.BAD_REQUEST,
94
+ SAPI_ERROR_NAME,
95
+ "Adding item to basket failed",
96
+ {
97
+ detail: message,
98
+ errors: result.errors
99
+ }
100
+ );
124
101
  }
125
- }));
126
- const result = await sapiClient.basket.addOrUpdateItems(
127
- basketKey,
128
- itemsToBeAddedOrUpdated,
129
- {
130
- skipAvailabilityCheck: false,
131
- includeItemsWithoutProductData: resolvedWith?.includeItemsWithoutProductData,
132
- orderCustomData: mergeOrderCustomData(
133
- _orderCustomData,
134
- params.orderCustomData
135
- ),
136
- customerToken: getValidatedAccessToken(context.accessToken) ?? void 0
137
- },
138
- {
139
- existingItemHandling: params.existingItemHandling || ExistingItemHandling.ADD_QUANTITY_TO_EXISTING
102
+ },
103
+ { method: "PUT" }
104
+ );
105
+ export const addItemsToBasket = defineRpcHandler(
106
+ async (params, context) => {
107
+ if (!hasSession(context)) {
108
+ return new ErrorResponse(
109
+ HttpStatusCode.BAD_REQUEST,
110
+ HttpStatusMessage.BAD_REQUEST,
111
+ "No Session found"
112
+ );
140
113
  }
141
- );
142
- if (result.type === "success") {
143
- return { basket: result.basket };
144
- } else if (result.type === "failure" && wasAddedWithReducedQuantity(result.errors)) {
145
- return {
146
- basket: result.basket,
147
- errors: result.errors
148
- };
149
- } else {
150
- const { statusCode, message } = parseBasketError(result);
151
- context.log.error("Adding one or more items to basket failed", {
152
- statusCode,
153
- message
154
- });
155
- return new ErrorResponse(
156
- HttpStatusCode.BAD_REQUEST,
157
- SAPI_ERROR_NAME,
158
- "Adding one or more items to basket failed",
159
- {
160
- detail: message,
161
- errors: result.errors
114
+ const { sapiClient, basketKey } = context;
115
+ const campaignKey = await context.callRpc?.("getCampaignKey");
116
+ const _orderCustomData = await context.callRpc?.("getOrderCustomData");
117
+ const resolvedWith = getWithParams(params, context);
118
+ const itemsToBeAddedOrUpdated = params.items.map((item) => ({
119
+ variantId: item.variantId,
120
+ quantity: item.quantity,
121
+ params: {
122
+ promotionId: item.promotionId ?? void 0,
123
+ promotions: item.promotions ?? void 0,
124
+ campaignKey,
125
+ displayData: item.displayData,
126
+ pricePromotionKey: resolvedWith?.pricePromotionKey || "",
127
+ customData: item.customData,
128
+ with: resolvedWith,
129
+ includeItemsWithoutProductData: item.includeItemsWithoutProductData ?? resolvedWith?.includeItemsWithoutProductData,
130
+ itemGroup: item.itemGroup ?? void 0
162
131
  }
163
- );
164
- }
165
- }, { method: "PUT" });
166
- export const getBasket = defineRpcHandler(async (options, context) => {
167
- if (!hasSession(context)) {
168
- return new ErrorResponse(
169
- HttpStatusCode.BAD_REQUEST,
170
- HttpStatusMessage.BAD_REQUEST,
171
- "No Session found"
172
- );
173
- }
174
- const { sapiClient, basketKey } = context;
175
- const campaignKey = await context.callRpc?.("getCampaignKey");
176
- const _orderCustomData = await context.callRpc?.("getOrderCustomData");
177
- const resolvedWith = getWithParams(
178
- { with: options },
179
- context
180
- );
181
- const response = await sapiClient.basket.get(basketKey, {
182
- with: resolvedWith,
183
- campaignKey,
184
- includeItemsWithoutProductData: resolvedWith.includeItemsWithoutProductData,
185
- orderCustomData: mergeOrderCustomData(
186
- _orderCustomData,
187
- options?.orderCustomData
188
- ),
189
- customerToken: getValidatedAccessToken(context.accessToken) ?? void 0
190
- });
191
- if (response.type === "failure") {
192
- const { statusCode, message } = parseBasketError(response);
193
- return new ErrorResponse(
194
- statusCode,
195
- SAPI_ERROR_NAME,
196
- "Adding item to basket failed",
132
+ }));
133
+ const result = await sapiClient.basket.addOrUpdateItems(
134
+ basketKey,
135
+ itemsToBeAddedOrUpdated,
136
+ {
137
+ skipAvailabilityCheck: false,
138
+ includeItemsWithoutProductData: resolvedWith?.includeItemsWithoutProductData,
139
+ orderCustomData: mergeOrderCustomData(
140
+ _orderCustomData,
141
+ params.orderCustomData
142
+ ),
143
+ customerToken: getValidatedAccessToken(context.accessToken) ?? void 0
144
+ },
197
145
  {
198
- detail: message
146
+ existingItemHandling: params.existingItemHandling || ExistingItemHandling.ADD_QUANTITY_TO_EXISTING
199
147
  }
200
148
  );
201
- }
202
- return { basket: response.basket };
203
- }, { method: "POST" });
204
- export const removeItemFromBasket = defineRpcHandler(async (options, context) => {
205
- if (!hasSession(context)) {
206
- return new ErrorResponse(
207
- HttpStatusCode.BAD_REQUEST,
208
- HttpStatusMessage.BAD_REQUEST,
209
- "No Session found"
149
+ if (result.type === "success") {
150
+ return { basket: result.basket };
151
+ } else if (result.type === "failure" && wasAddedWithReducedQuantity(result.errors)) {
152
+ return {
153
+ basket: result.basket,
154
+ errors: result.errors
155
+ };
156
+ } else {
157
+ const { statusCode, message } = parseBasketError(result);
158
+ context.log.error("Adding one or more items to basket failed", {
159
+ statusCode,
160
+ message
161
+ });
162
+ return new ErrorResponse(
163
+ HttpStatusCode.BAD_REQUEST,
164
+ SAPI_ERROR_NAME,
165
+ "Adding one or more items to basket failed",
166
+ {
167
+ detail: message,
168
+ errors: result.errors
169
+ }
170
+ );
171
+ }
172
+ },
173
+ { method: "PUT" }
174
+ );
175
+ export const getBasket = defineRpcHandler(
176
+ async (options, context) => {
177
+ if (!hasSession(context)) {
178
+ return new ErrorResponse(
179
+ HttpStatusCode.BAD_REQUEST,
180
+ HttpStatusMessage.BAD_REQUEST,
181
+ "No Session found"
182
+ );
183
+ }
184
+ const { sapiClient, basketKey } = context;
185
+ const campaignKey = await context.callRpc?.("getCampaignKey");
186
+ const _orderCustomData = await context.callRpc?.("getOrderCustomData");
187
+ const resolvedWith = getWithParams(
188
+ { with: options },
189
+ context
210
190
  );
211
- }
212
- const { sapiClient, basketKey } = context;
213
- const campaignKey = await context.callRpc?.("getCampaignKey");
214
- const _orderCustomData = await context.callRpc?.("getOrderCustomData");
215
- const resolvedWith = getWithParams(options, context);
216
- const response = await sapiClient.basket.deleteItem(
217
- basketKey,
218
- options.itemKey,
219
- {
191
+ const response = await sapiClient.basket.get(basketKey, {
220
192
  with: resolvedWith,
221
193
  campaignKey,
222
- includeItemsWithoutProductData: resolvedWith?.includeItemsWithoutProductData,
194
+ includeItemsWithoutProductData: resolvedWith.includeItemsWithoutProductData,
223
195
  orderCustomData: mergeOrderCustomData(
224
196
  _orderCustomData,
225
- options.orderCustomData
197
+ options?.orderCustomData
226
198
  ),
227
199
  customerToken: getValidatedAccessToken(context.accessToken) ?? void 0
200
+ });
201
+ if (response.type === "failure") {
202
+ const { statusCode, message } = parseBasketError(response);
203
+ return new ErrorResponse(
204
+ statusCode,
205
+ SAPI_ERROR_NAME,
206
+ "Adding item to basket failed",
207
+ {
208
+ detail: message
209
+ }
210
+ );
211
+ }
212
+ return { basket: response.basket };
213
+ },
214
+ { method: "POST" }
215
+ );
216
+ export const removeItemFromBasket = defineRpcHandler(
217
+ async (options, context) => {
218
+ if (!hasSession(context)) {
219
+ return new ErrorResponse(
220
+ HttpStatusCode.BAD_REQUEST,
221
+ HttpStatusMessage.BAD_REQUEST,
222
+ "No Session found"
223
+ );
228
224
  }
229
- );
230
- if ("code" in response) {
231
- return new ErrorResponse(
232
- response.code,
233
- SAPI_ERROR_NAME,
234
- response.message
225
+ const { sapiClient, basketKey } = context;
226
+ const campaignKey = await context.callRpc?.("getCampaignKey");
227
+ const _orderCustomData = await context.callRpc?.("getOrderCustomData");
228
+ const resolvedWith = getWithParams(options, context);
229
+ const response = await sapiClient.basket.deleteItem(
230
+ basketKey,
231
+ options.itemKey,
232
+ {
233
+ with: resolvedWith,
234
+ campaignKey,
235
+ includeItemsWithoutProductData: resolvedWith?.includeItemsWithoutProductData,
236
+ orderCustomData: mergeOrderCustomData(
237
+ _orderCustomData,
238
+ options.orderCustomData
239
+ ),
240
+ customerToken: getValidatedAccessToken(context.accessToken) ?? void 0
241
+ }
235
242
  );
236
- }
237
- return { basket: response };
238
- }, { method: "DELETE" });
243
+ if ("code" in response) {
244
+ return new ErrorResponse(response.code, SAPI_ERROR_NAME, response.message);
245
+ }
246
+ return { basket: response };
247
+ },
248
+ { method: "DELETE" }
249
+ );
239
250
  export const clearBasket = defineRpcHandler(
240
251
  async (context) => {
241
252
  const getBasketResponse = await getBasket({}, context);
@@ -256,150 +267,152 @@ export const clearBasket = defineRpcHandler(
256
267
  },
257
268
  { method: "DELETE" }
258
269
  );
259
- export const mergeBaskets = defineRpcHandler(async ({ fromBasketKey, toBasketKey, with: _with, orderCustomData }, context) => {
260
- const resolvedWith = getWithParams(
261
- { with: _with },
262
- context
263
- );
264
- const _orderCustomData = await context.callRpc?.("getOrderCustomData");
265
- return await mergeBasketFunction(
266
- fromBasketKey,
267
- toBasketKey,
268
- {
269
- ...resolvedWith,
270
- orderCustomData: mergeOrderCustomData(_orderCustomData, orderCustomData)
271
- },
272
- context
273
- );
274
- }, { method: "POST" });
275
- export const updateBasketItem = defineRpcHandler(async ({ basketItemKey, update, with: _with, orderCustomData }, context) => {
276
- if (!hasSession(context)) {
277
- return new ErrorResponse(
278
- HttpStatusCode.BAD_REQUEST,
279
- HttpStatusMessage.BAD_REQUEST,
280
- "No Session found"
270
+ export const mergeBaskets = defineRpcHandler(
271
+ async ({ fromBasketKey, toBasketKey, with: _with, orderCustomData }, context) => {
272
+ const resolvedWith = getWithParams(
273
+ { with: _with },
274
+ context
281
275
  );
282
- }
283
- const { basketKey, sapiClient } = context;
284
- const resolvedWith = getWithParams(
285
- { with: _with },
286
- context
287
- );
288
- const { quantity, ...updateParams } = update;
289
- const campaignKey = update.campaignKey ?? await context.callRpc?.("getCampaignKey");
290
- const _orderCustomData = await context.callRpc?.("getOrderCustomData");
291
- const result = await sapiClient.basket.updateItem(
292
- basketKey,
293
- basketItemKey,
294
- quantity,
295
- {
296
- ...updateParams,
297
- campaignKey,
298
- with: resolvedWith,
299
- orderCustomData: mergeOrderCustomData(_orderCustomData, orderCustomData),
300
- customerToken: getValidatedAccessToken(context.accessToken) ?? void 0
276
+ const _orderCustomData = await context.callRpc?.("getOrderCustomData");
277
+ return await mergeBasketFunction(
278
+ fromBasketKey,
279
+ toBasketKey,
280
+ {
281
+ ...resolvedWith,
282
+ orderCustomData: mergeOrderCustomData(
283
+ _orderCustomData,
284
+ orderCustomData
285
+ )
286
+ },
287
+ context
288
+ );
289
+ },
290
+ { method: "POST" }
291
+ );
292
+ export const updateBasketItem = defineRpcHandler(
293
+ async ({ basketItemKey, update, with: _with, orderCustomData }, context) => {
294
+ if (!hasSession(context)) {
295
+ return new ErrorResponse(
296
+ HttpStatusCode.BAD_REQUEST,
297
+ HttpStatusMessage.BAD_REQUEST,
298
+ "No Session found"
299
+ );
301
300
  }
302
- );
303
- if (result.type === "success") {
304
- return { basket: result.basket };
305
- } else {
306
- context.log.error("Updating basket item failed", {
307
- kind: result.kind,
301
+ const { basketKey, sapiClient } = context;
302
+ const resolvedWith = getWithParams({ with: _with }, context);
303
+ const { quantity, ...updateParams } = update;
304
+ const campaignKey = update.campaignKey ?? await context.callRpc?.("getCampaignKey");
305
+ const _orderCustomData = await context.callRpc?.("getOrderCustomData");
306
+ const result = await sapiClient.basket.updateItem(
308
307
  basketKey,
309
308
  basketItemKey,
310
- update
311
- });
312
- return new ErrorResponse(
313
- HttpStatusCode.BAD_REQUEST,
314
- SAPI_ERROR_NAME,
315
- "Updating basket item failed",
309
+ quantity,
316
310
  {
317
- failureKind: result.kind
311
+ ...updateParams,
312
+ campaignKey,
313
+ with: resolvedWith,
314
+ orderCustomData: mergeOrderCustomData(
315
+ _orderCustomData,
316
+ orderCustomData
317
+ ),
318
+ customerToken: getValidatedAccessToken(context.accessToken) ?? void 0
318
319
  }
319
320
  );
320
- }
321
- }, { method: "PUT" });
322
- export const getApplicablePromotionsByCode = defineRpcHandler(async ({ promotionCode, with: _with }, context) => {
323
- if (!hasSession(context)) {
324
- return new ErrorResponse(
325
- HttpStatusCode.BAD_REQUEST,
326
- HttpStatusMessage.BAD_REQUEST,
327
- "No Session found"
328
- );
329
- }
330
- const { basketKey, sapiClient } = context;
331
- const resolvedWith = getWithParams(
332
- { with: _with },
333
- context
334
- );
335
- const campaignKey = await context.callRpc?.("getCampaignKey");
336
- const orderCustomData = await context.callRpc?.("getOrderCustomData");
337
- const result = await mapSAPIFetchErrorToResponse(
338
- sapiClient.basket.getApplicablePromotionsByCode
339
- )(
340
- basketKey,
341
- promotionCode,
342
- {
321
+ if (result.type === "success") {
322
+ return { basket: result.basket };
323
+ } else {
324
+ context.log.error("Updating basket item failed", {
325
+ kind: result.kind,
326
+ basketKey,
327
+ basketItemKey,
328
+ update
329
+ });
330
+ return new ErrorResponse(
331
+ HttpStatusCode.BAD_REQUEST,
332
+ SAPI_ERROR_NAME,
333
+ "Updating basket item failed",
334
+ {
335
+ failureKind: result.kind
336
+ }
337
+ );
338
+ }
339
+ },
340
+ { method: "PUT" }
341
+ );
342
+ export const getApplicablePromotionsByCode = defineRpcHandler(
343
+ async ({ promotionCode, with: _with }, context) => {
344
+ if (!hasSession(context)) {
345
+ return new ErrorResponse(
346
+ HttpStatusCode.BAD_REQUEST,
347
+ HttpStatusMessage.BAD_REQUEST,
348
+ "No Session found"
349
+ );
350
+ }
351
+ const { basketKey, sapiClient } = context;
352
+ const resolvedWith = getWithParams({ with: _with }, context);
353
+ const campaignKey = await context.callRpc?.("getCampaignKey");
354
+ const orderCustomData = await context.callRpc?.("getOrderCustomData");
355
+ const result = await mapSAPIFetchErrorToResponse(
356
+ sapiClient.basket.getApplicablePromotionsByCode
357
+ )(basketKey, promotionCode, {
343
358
  campaignKey,
344
359
  with: resolvedWith,
345
360
  orderCustomData,
346
361
  customerToken: getValidatedAccessToken(context.accessToken) ?? void 0
347
- }
348
- );
349
- if (result instanceof Response || result.type === "failure") {
350
- context.log.info("Getting applicable promotion codes failed", {
351
- basketKey,
352
- promotionCode
353
362
  });
354
- return new ErrorResponse(
355
- HttpStatusCode.BAD_REQUEST,
356
- SAPI_ERROR_NAME,
357
- "Getting applicable promotion codes failed"
358
- );
359
- } else {
360
- return { basket: result.basket };
361
- }
362
- }, { method: "POST" });
363
- export const updatePromotions = defineRpcHandler(async (params, context) => {
364
- if (!hasSession(context)) {
365
- return new ErrorResponse(
366
- HttpStatusCode.BAD_REQUEST,
367
- HttpStatusMessage.BAD_REQUEST,
368
- "No Session found"
369
- );
370
- }
371
- const { basketKey, sapiClient } = context;
372
- const campaignKey = params.campaignKey ?? await context.callRpc?.("getCampaignKey");
373
- const orderCustomData = await context.callRpc?.("getOrderCustomData");
374
- const resolvedWith = getWithParams(
375
- { with: params.with },
376
- context
377
- );
378
- const result = await mapSAPIFetchErrorToResponse(
379
- sapiClient.basket.bulkUpdatePromotions
380
- )(
381
- basketKey,
382
- {
363
+ if (result instanceof Response || result.type === "failure") {
364
+ context.log.info("Getting applicable promotion codes failed", {
365
+ basketKey,
366
+ promotionCode
367
+ });
368
+ return new ErrorResponse(
369
+ HttpStatusCode.BAD_REQUEST,
370
+ SAPI_ERROR_NAME,
371
+ "Getting applicable promotion codes failed"
372
+ );
373
+ } else {
374
+ return { basket: result.basket };
375
+ }
376
+ },
377
+ { method: "POST" }
378
+ );
379
+ export const updatePromotions = defineRpcHandler(
380
+ async (params, context) => {
381
+ if (!hasSession(context)) {
382
+ return new ErrorResponse(
383
+ HttpStatusCode.BAD_REQUEST,
384
+ HttpStatusMessage.BAD_REQUEST,
385
+ "No Session found"
386
+ );
387
+ }
388
+ const { basketKey, sapiClient } = context;
389
+ const campaignKey = params.campaignKey ?? await context.callRpc?.("getCampaignKey");
390
+ const orderCustomData = await context.callRpc?.("getOrderCustomData");
391
+ const resolvedWith = getWithParams({ with: params.with }, context);
392
+ const result = await mapSAPIFetchErrorToResponse(
393
+ sapiClient.basket.bulkUpdatePromotions
394
+ )(basketKey, {
383
395
  ...params,
384
396
  campaignKey,
385
397
  with: resolvedWith,
386
398
  orderCustomData,
387
399
  customerToken: getValidatedAccessToken(context.accessToken) ?? void 0
388
- }
389
- );
390
- if (result instanceof Response || result.type === "failure") {
391
- context.log.info("Bulk updating promotions failed", {
392
- basketKey,
393
- items: params.items
394
400
  });
395
- return new ErrorResponse(
396
- HttpStatusCode.BAD_REQUEST,
397
- SAPI_ERROR_NAME,
398
- "Bulk updating promotions failed"
399
- );
400
- }
401
- return { basket: result.basket };
402
- }, { method: "PUT" });
401
+ if (result instanceof Response || result.type === "failure") {
402
+ context.log.info("Bulk updating promotions failed", {
403
+ basketKey,
404
+ items: params.items
405
+ });
406
+ return new ErrorResponse(
407
+ HttpStatusCode.BAD_REQUEST,
408
+ SAPI_ERROR_NAME,
409
+ "Bulk updating promotions failed"
410
+ );
411
+ }
412
+ return { basket: result.basket };
413
+ },
414
+ { method: "PUT" }
415
+ );
403
416
  function parseBasketError(response) {
404
417
  const parsedError = {
405
418
  message: "",