From afb79c2b2ce5203a5e8580027a4a4c7d3797ffb5 Mon Sep 17 00:00:00 2001 From: Jessica Date: Tue, 7 Dec 2021 12:28:47 +1100 Subject: [PATCH] feat: add activePromotions field to search response (#133) --- package.json | 2 +- src/index.ts | 11 +++++++++- src/user-agent.ts | 2 +- test/index.test.ts | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 0ae77e1..c26a44c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@sajari/sdk-js", "description": "Sajari JavaScript SDK", - "version": "2.6.0", + "version": "2.7.0", "main": "dist/index.js", "umd:main": "dist/sajarisdk.umd.production.js", "module": "dist/sajarisdk.esm.production.js", diff --git a/src/index.ts b/src/index.ts index 54d5f59..78e156d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -419,9 +419,11 @@ class QueryPipeline extends EventEmitter { return obj; }, {}); + let activePromotions: ActivePromotion[] = []; const activePins: Record> = {}; if (jsonProto.activePromotions) { - jsonProto.activePromotions.forEach((promotion) => { + activePromotions = jsonProto.activePromotions; + activePromotions.forEach((promotion) => { if (promotion.activePins) { promotion.activePins.forEach(({ key }) => { if (!activePins[key.field]) { @@ -498,6 +500,7 @@ class QueryPipeline extends EventEmitter { aggregates: aggregates, aggregateFilters: aggregateFilters, redirects: redirects, + activePromotions: activePromotions, }, jsonProto.values || {}, ]; @@ -529,10 +532,16 @@ export interface SearchResponse { * AggregateFilters computed on the query results (see [[Aggregates]]). */ aggregateFilters: Aggregates; + /** * All Redirects for which the current query is a starting substring (see [[Redirects]]). */ redirects: Redirects; + + /** + * All Promotions activated by the current query (see [[ActivePromotion]]). + */ + activePromotions: ActivePromotion[]; } export interface Result { diff --git a/src/user-agent.ts b/src/user-agent.ts index 0f67328..cf1dab8 100644 --- a/src/user-agent.ts +++ b/src/user-agent.ts @@ -16,6 +16,6 @@ if (scriptTag) { * user agent of sdk * @hidden */ -export const USER_AGENT = ["sajari-sdk-js/2.6.0", suffix] +export const USER_AGENT = ["sajari-sdk-js/2.7.0", suffix] .filter(Boolean) .join(" "); diff --git a/test/index.test.ts b/test/index.test.ts index 82a20b0..c7bca1f 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -64,6 +64,7 @@ describe("Pipeline", () => { aggregates: {}, aggregateFilters: {}, redirects: {}, + activePromotions: [], }); expect(fetchMock.mock.calls.length).toEqual(1); @@ -107,6 +108,7 @@ describe("Pipeline", () => { token: "https://re.sajari.com/token/12345abcd", }, }, + activePromotions: [], }); expect(fetchMock.mock.calls.length).toEqual(1); @@ -158,6 +160,7 @@ describe("Pipeline", () => { aggregates: {}, aggregateFilters: {}, redirects: {}, + activePromotions: [], }); }); @@ -233,6 +236,21 @@ describe("Pipeline", () => { aggregates: {}, aggregateFilters: {}, redirects: {}, + activePromotions: [ + { + activeExclusions: [], + activePins: [ + { + key: { + field: "_id", + value: "876120cc-c9c6-95f2-bafb-58b8e2aa962f", + }, + position: 1, + }, + ], + promotionId: "1", + }, + ], }); expect(fetchMock.mock.calls.length).toEqual(1); @@ -361,6 +379,41 @@ describe("Pipeline", () => { aggregates: {}, aggregateFilters: {}, redirects: {}, + activePromotions: [ + { + activeExclusions: [], + activePins: [ + { + key: { + field: "_id", + value: "876120cc-c9c6-95f2-bafb-58b8e2aa962f", + }, + position: 1, + }, + { + key: { + field: "sku", + value: "sku-2", + }, + position: 4, + }, + ], + promotionId: "1", + }, + { + activeExclusions: [], + activePins: [ + { + key: { + field: "name", + value: "pear", + }, + position: 2, + }, + ], + promotionId: "2", + }, + ], }); expect(fetchMock.mock.calls.length).toEqual(1);