Skip to content

Commit

Permalink
feat: add activePromotions field to search response (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaho authored Dec 7, 2021
1 parent 2d4cca5 commit afb79c2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,11 @@ class QueryPipeline extends EventEmitter {
return obj;
}, {});

let activePromotions: ActivePromotion[] = [];
const activePins: Record<string, Set<string>> = {};
if (jsonProto.activePromotions) {
jsonProto.activePromotions.forEach((promotion) => {
activePromotions = jsonProto.activePromotions;
activePromotions.forEach((promotion) => {
if (promotion.activePins) {
promotion.activePins.forEach(({ key }) => {
if (!activePins[key.field]) {
Expand Down Expand Up @@ -498,6 +500,7 @@ class QueryPipeline extends EventEmitter {
aggregates: aggregates,
aggregateFilters: aggregateFilters,
redirects: redirects,
activePromotions: activePromotions,
},
jsonProto.values || {},
];
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/user-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ");
53 changes: 53 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe("Pipeline", () => {
aggregates: {},
aggregateFilters: {},
redirects: {},
activePromotions: [],
});

expect(fetchMock.mock.calls.length).toEqual(1);
Expand Down Expand Up @@ -107,6 +108,7 @@ describe("Pipeline", () => {
token: "https://re.sajari.com/token/12345abcd",
},
},
activePromotions: [],
});

expect(fetchMock.mock.calls.length).toEqual(1);
Expand Down Expand Up @@ -158,6 +160,7 @@ describe("Pipeline", () => {
aggregates: {},
aggregateFilters: {},
redirects: {},
activePromotions: [],
});
});

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit afb79c2

Please sign in to comment.