@scayle/storefront-core 8.26.2 → 8.27.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @scayle/storefront-core
2
2
 
3
+ ## 8.27.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Added a new `getApplicablePromotionsByCode` RPC method which can be used to fetch applicable promotions for a basket.
8
+
9
+ ### Patch Changes
10
+
11
+ **Dependencies**
12
+
13
+ - Updated dependency to @scayle/storefront-api@18.7.0
14
+
3
15
  ## 8.26.2
4
16
 
5
17
  ### Patch Changes
@@ -1,6 +1,6 @@
1
1
  import { ExistingItemHandling } from '@scayle/storefront-api';
2
- import type { AddOrUpdateItemError, UpdateBasketItemQuantity } from '@scayle/storefront-api';
3
- import type { AddOrUpdateItemType, BasketResponseData, BasketWithOptions, Product, RpcHandler, Variant } from '../../../types';
2
+ import type { AddOrUpdateItemError, UpdateBasketItemQuantity, GetApplicablePromotionsByCodeParameters } from '@scayle/storefront-api';
3
+ import type { AddOrUpdateItemType, BasketResponseData, BasketWithOptions, Product, RpcHandler, ParamRpcHandler, Variant } from '../../../types';
4
4
  /**
5
5
  * Adds an item to the basket.
6
6
  *
@@ -104,7 +104,7 @@ export declare const clearBasket: RpcHandler<boolean>;
104
104
  *
105
105
  * @returns The new merged basket as result of the merge operation.
106
106
  */
107
- export declare const mergeBaskets: RpcHandler<{
107
+ export declare const mergeBaskets: ParamRpcHandler<{
108
108
  fromBasketKey: string;
109
109
  toBasketKey: string;
110
110
  with?: BasketWithOptions;
@@ -150,3 +150,17 @@ export interface UpdateBasketItemRequestParameter {
150
150
  export declare const updateBasketItem: RpcHandler<UpdateBasketItemRequestParameter, {
151
151
  basket: BasketResponseData<Product, Variant>;
152
152
  }>;
153
+ /**
154
+ * Gets the applicable promotions for a basket based on a promotion code.
155
+ *
156
+ * @param params The parameters for updating a basket item.
157
+ * @param params.promotionCode The promotion code to apply.
158
+ * @param params.with An object describing which data the returned basket should contain.
159
+ * @param context The RPC context.
160
+ *
161
+ * @returns A promise that resolves with the updated basket, including the `applicablePromotions` property.
162
+ * It will return an `ErrorResponse` if no session is found.
163
+ */
164
+ export declare const getApplicablePromotionsByCode: RpcHandler<Omit<GetApplicablePromotionsByCodeParameters, 'basketKey'>, {
165
+ basket: BasketResponseData<Product, Variant>;
166
+ }>;
@@ -9,6 +9,7 @@ import {
9
9
  import { mergeBaskets as mergeBasketFunction } from "../../../utils/user.mjs";
10
10
  import { unwrap } from "../../../utils/response.mjs";
11
11
  import { wasAddedWithReducedQuantity } from "../../../utils/index.mjs";
12
+ import { mapSAPIFetchErrorToResponse } from "../../../utils/sapi.mjs";
12
13
  const SAPI_ERROR_NAME = "SAPI ERROR";
13
14
  function getWithParams(params, context) {
14
15
  return params.with ?? context.withParams?.basket ?? MIN_WITH_PARAMS_BASKET;
@@ -267,6 +268,41 @@ export const updateBasketItem = async function updateBasketItem2({ basketItemKey
267
268
  );
268
269
  }
269
270
  };
271
+ export const getApplicablePromotionsByCode = async function getApplicablePromotionsByCode2({ promotionCode, with: _with }, context) {
272
+ if (!hasSession(context)) {
273
+ return new ErrorResponse(
274
+ HttpStatusCode.BAD_REQUEST,
275
+ HttpStatusMessage.BAD_REQUEST,
276
+ "No Session found"
277
+ );
278
+ }
279
+ const { basketKey, sapiClient } = context;
280
+ const resolvedWith = getWithParams(
281
+ { with: _with },
282
+ context
283
+ );
284
+ const campaignKey = await context.callRpc?.("getCampaignKey");
285
+ const result = await mapSAPIFetchErrorToResponse(
286
+ sapiClient.basket.getApplicablePromotionsByCode
287
+ )(
288
+ basketKey,
289
+ promotionCode,
290
+ { campaignKey, with: resolvedWith }
291
+ );
292
+ if (result instanceof Response || result.type === "failure") {
293
+ context.log.info("Getting applicable promotion codes failed", {
294
+ basketKey,
295
+ promotionCode
296
+ });
297
+ return new ErrorResponse(
298
+ HttpStatusCode.BAD_REQUEST,
299
+ SAPI_ERROR_NAME,
300
+ "Getting applicable promotion codes failed"
301
+ );
302
+ } else {
303
+ return { basket: result.basket };
304
+ }
305
+ };
270
306
  function parseBasketError(response) {
271
307
  const parsedError = {
272
308
  message: "",
@@ -36,7 +36,7 @@ export const getCheckoutToken = async function getCheckoutToken2(jwtPayload = {}
36
36
  carrier,
37
37
  basketId: context.basketKey,
38
38
  campaignKey
39
- }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"8.26.2"}`).setProtectedHeader({ alg: "HS256", typ: "JWT" }).sign(secret);
39
+ }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"8.27.0"}`).setProtectedHeader({ alg: "HS256", typ: "JWT" }).sign(secret);
40
40
  return {
41
41
  accessToken: refreshedAccessToken,
42
42
  checkoutJwt
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scayle/storefront-core",
3
- "version": "8.26.2",
3
+ "version": "8.27.0",
4
4
  "description": "Collection of essential utilities to work with the Storefront API",
5
5
  "author": "SCAYLE Commerce Engine",
6
6
  "license": "MIT",
@@ -64,7 +64,7 @@
64
64
  "fishery": "^2.2.3"
65
65
  },
66
66
  "dependencies": {
67
- "@scayle/storefront-api": "18.6.1",
67
+ "@scayle/storefront-api": "18.7.0",
68
68
  "@scayle/unstorage-scayle-kv-driver": "0.1.2",
69
69
  "crypto-js": "^4.2.0",
70
70
  "hookable": "^5.5.3",