From b6b5d8657947b8510f9d23530d709e757e5e3ecf Mon Sep 17 00:00:00 2001 From: Guillermo Cacheda Date: Fri, 12 Jan 2024 10:06:53 +0100 Subject: [PATCH] feat(search): handle display tagging from search response (#1388) --- .../src/__tests__/__fixtures__/search.response.ts | 2 ++ .../src/mappers/__tests__/url.spec.ts | 12 ++++++++---- .../search-response.mapper.spec.ts.snap | 14 ++++++++++++++ .../x-adapter-platform/src/mappers/url.utils.ts | 12 ++++++------ .../src/schemas/models/result.schema.ts | 4 ++-- .../schemas/responses/search-response.schema.ts | 5 +++-- .../src/types/responses/search-response.model.ts | 1 + .../empty-search-response-stubs.factory.ts | 4 ++++ .../src/__stubs__/search-response-stubs.factory.ts | 1 + .../x-components/src/adapter/mocked-responses.ts | 4 ++++ .../store/actions/save-search-response.action.ts | 8 +++++++- .../src/x-modules/search/store/emitters.ts | 1 + .../src/x-modules/search/store/module.ts | 7 +++++++ .../src/x-modules/search/store/types.ts | 8 ++++++++ .../x-types/src/response/search-response.model.ts | 1 + 15 files changed, 69 insertions(+), 15 deletions(-) diff --git a/packages/x-adapter-platform/src/__tests__/__fixtures__/search.response.ts b/packages/x-adapter-platform/src/__tests__/__fixtures__/search.response.ts index 72c7ec3496..a37c3425d7 100644 --- a/packages/x-adapter-platform/src/__tests__/__fixtures__/search.response.ts +++ b/packages/x-adapter-platform/src/__tests__/__fixtures__/search.response.ts @@ -101,6 +101,8 @@ export const platformSearchResponse = { } ], tagging: { + display: + 'https://api.staging.empathy.co/tagging/v1/track/empathy/display?q=jeans&lang=en&scope=desktop&totalHits=686&page=1&origin=url%3Aexternal&filtered=true&spellcheck=false', query: 'https://api.staging.empathy.co/tagging/v1/track/empathy/query?q=jeans&lang=en&scope=desktop&totalHits=686&page=1&origin=url%3Aexternal&filtered=true&spellcheck=false' }, diff --git a/packages/x-adapter-platform/src/mappers/__tests__/url.spec.ts b/packages/x-adapter-platform/src/mappers/__tests__/url.spec.ts index 6e2895cda0..8bff8346dc 100644 --- a/packages/x-adapter-platform/src/mappers/__tests__/url.spec.ts +++ b/packages/x-adapter-platform/src/mappers/__tests__/url.spec.ts @@ -1,4 +1,8 @@ -import { extractUrlParameters, getDisplayClickTagging, getTaggingInfoFromUrl } from '../url.utils'; +import { + extractUrlParameters, + getDisplayTaggingInfoFromUrl, + getTaggingInfoFromUrl +} from '../url.utils'; describe('url utils methods tests', () => { describe('extractUrlParameters', () => { @@ -45,14 +49,14 @@ describe('url utils methods tests', () => { describe('getDisplayClickTagging', () => { it('should not break when dealing with bad urls', () => { - expect(getDisplayClickTagging('null')).toStrictEqual({ + expect(getDisplayTaggingInfoFromUrl('null')).toStrictEqual({ url: 'null', params: { displayId: 'no_query', follow: false } }); }); it('should retrieve the tagging info from the url, replacing q with displayId', () => { - const { url, params } = getDisplayClickTagging( + const { url, params } = getDisplayTaggingInfoFromUrl( 'https://api.empathy.co/?q=chips&env=mobile&lang=english&lang=spanish' ); expect(url).toBe('https://api.empathy.co/'); @@ -65,7 +69,7 @@ describe('url utils methods tests', () => { }); it('should set no_query tagging info when no q param exist as in topclicked response', () => { - const { url, params } = getDisplayClickTagging( + const { url, params } = getDisplayTaggingInfoFromUrl( 'https://api.empathy.co/?env=mobile&lang=english&lang=spanish' ); expect(url).toBe('https://api.empathy.co/'); diff --git a/packages/x-adapter-platform/src/mappers/responses/__tests__/__snapshots__/search-response.mapper.spec.ts.snap b/packages/x-adapter-platform/src/mappers/responses/__tests__/__snapshots__/search-response.mapper.spec.ts.snap index dcca26cf5a..a12ebfbae5 100644 --- a/packages/x-adapter-platform/src/mappers/responses/__tests__/__snapshots__/search-response.mapper.spec.ts.snap +++ b/packages/x-adapter-platform/src/mappers/responses/__tests__/__snapshots__/search-response.mapper.spec.ts.snap @@ -30,6 +30,20 @@ Object { "url": "https://assets.empathy.co/", }, ], + "displayTagging": Object { + "params": Object { + "displayId": "jeans", + "filtered": "true", + "follow": false, + "lang": "en", + "origin": "url:external", + "page": "1", + "scope": "desktop", + "spellcheck": "false", + "totalHits": "686", + }, + "url": "https://api.staging.empathy.co/tagging/v1/track/empathy/display", + }, "facets": Array [ Object { "filters": Array [ diff --git a/packages/x-adapter-platform/src/mappers/url.utils.ts b/packages/x-adapter-platform/src/mappers/url.utils.ts index 9f2aa88fef..1ece334b3a 100644 --- a/packages/x-adapter-platform/src/mappers/url.utils.ts +++ b/packages/x-adapter-platform/src/mappers/url.utils.ts @@ -28,14 +28,14 @@ export function getTaggingInfoFromUrl(taggingUrl: string): TaggingRequest { * * @public */ -export function getDisplayClickTagging(displayTaggingUrl: string): TaggingRequest { - const displayClickTagging = getTaggingInfoFromUrl(displayTaggingUrl); - const displayClickTaggingParams = displayClickTagging.params; +export function getDisplayTaggingInfoFromUrl(displayTaggingUrl: string): TaggingRequest { + const displayTagging = getTaggingInfoFromUrl(displayTaggingUrl); + const displayTaggingParams = displayTagging.params; - displayClickTaggingParams.displayId = displayClickTaggingParams.q ?? 'no_query'; - delete displayClickTaggingParams.q; + displayTaggingParams.displayId = displayTaggingParams.q ?? 'no_query'; + delete displayTaggingParams.q; - return displayClickTagging; + return displayTagging; } /** diff --git a/packages/x-adapter-platform/src/schemas/models/result.schema.ts b/packages/x-adapter-platform/src/schemas/models/result.schema.ts index 365a61574d..8bdcb6e97d 100644 --- a/packages/x-adapter-platform/src/schemas/models/result.schema.ts +++ b/packages/x-adapter-platform/src/schemas/models/result.schema.ts @@ -1,6 +1,6 @@ import { createMutableSchema } from '@empathyco/x-adapter'; import { Result } from '@empathyco/x-types'; -import { getDisplayClickTagging, getTaggingInfoFromUrl } from '../../mappers/url.utils'; +import { getDisplayTaggingInfoFromUrl, getTaggingInfoFromUrl } from '../../mappers/url.utils'; import { PlatformResult } from '../../types/models/result.model'; /** @@ -36,7 +36,7 @@ export const resultSchema = createMutableSchema({ add2cart: ({ add2cart }) => getTaggingInfoFromUrl(add2cart), checkout: ({ checkout }) => getTaggingInfoFromUrl(checkout), click: ({ click }) => getTaggingInfoFromUrl(click), - displayClick: ({ displayClick }) => getDisplayClickTagging(displayClick) + displayClick: ({ displayClick }) => getDisplayTaggingInfoFromUrl(displayClick) } } }); diff --git a/packages/x-adapter-platform/src/schemas/responses/search-response.schema.ts b/packages/x-adapter-platform/src/schemas/responses/search-response.schema.ts index 5dd2867302..eee6ceb8e4 100644 --- a/packages/x-adapter-platform/src/schemas/responses/search-response.schema.ts +++ b/packages/x-adapter-platform/src/schemas/responses/search-response.schema.ts @@ -1,6 +1,6 @@ import { createMutableSchema } from '@empathyco/x-adapter'; import { SearchResponse } from '@empathyco/x-types'; -import { getTaggingInfoFromUrl } from '../../mappers/url.utils'; +import { getDisplayTaggingInfoFromUrl, getTaggingInfoFromUrl } from '../../mappers/url.utils'; import { PlatformSearchResponse } from '../../types/responses/search-response.model'; import { bannerSchema } from '../models/banner.schema'; import { facetSchema } from '../models/facet.schema'; @@ -41,5 +41,6 @@ export const searchResponseSchema = createMutableSchema getTaggingInfoFromUrl(catalog?.tagging?.query) + queryTagging: ({ catalog }) => getTaggingInfoFromUrl(catalog?.tagging?.query), + displayTagging: ({ catalog }) => getDisplayTaggingInfoFromUrl(catalog?.tagging?.display) }); diff --git a/packages/x-adapter-platform/src/types/responses/search-response.model.ts b/packages/x-adapter-platform/src/types/responses/search-response.model.ts index fb3563c45b..a93b9c9bb3 100644 --- a/packages/x-adapter-platform/src/types/responses/search-response.model.ts +++ b/packages/x-adapter-platform/src/types/responses/search-response.model.ts @@ -22,6 +22,7 @@ export interface PlatformSearchResponse { partials: PlatformPartialResult[]; tagging: { query: string; + display: string; }; }; direct: { diff --git a/packages/x-components/src/__stubs__/empty-search-response-stubs.factory.ts b/packages/x-components/src/__stubs__/empty-search-response-stubs.factory.ts index 660642c3a1..96303db2ad 100644 --- a/packages/x-components/src/__stubs__/empty-search-response-stubs.factory.ts +++ b/packages/x-components/src/__stubs__/empty-search-response-stubs.factory.ts @@ -17,6 +17,10 @@ export function getEmptySearchResponseStub(): SearchResponse { params: {}, url: '' }, + displayTagging: { + params: {}, + url: '' + }, redirections: [], results: [], spellcheck: '', diff --git a/packages/x-components/src/__stubs__/search-response-stubs.factory.ts b/packages/x-components/src/__stubs__/search-response-stubs.factory.ts index ac5b1c100e..ae0117e99e 100644 --- a/packages/x-components/src/__stubs__/search-response-stubs.factory.ts +++ b/packages/x-components/src/__stubs__/search-response-stubs.factory.ts @@ -20,6 +20,7 @@ export function getSearchResponseStub(): SearchResponse { partialResults: [], promoteds: getPromotedsStub(), queryTagging: getTaggingResponseStub(), + displayTagging: getTaggingResponseStub(), redirections: getRedirectionsStub(), results: getResultsStub(), spellcheck: '', diff --git a/packages/x-components/src/adapter/mocked-responses.ts b/packages/x-components/src/adapter/mocked-responses.ts index 6d05f12937..aebfb2890e 100644 --- a/packages/x-components/src/adapter/mocked-responses.ts +++ b/packages/x-components/src/adapter/mocked-responses.ts @@ -221,6 +221,10 @@ export function createSearchResponse(partial?: Partial): SearchR url: `${trackEndpoint}/query`, params: { page: 1 } }, + displayTagging: { + url: `${trackEndpoint}/display`, + params: { page: 1 } + }, spellcheck: '', ...partial }; diff --git a/packages/x-components/src/x-modules/search/store/actions/save-search-response.action.ts b/packages/x-components/src/x-modules/search/store/actions/save-search-response.action.ts index 464ca11fc3..9774a2f4c0 100644 --- a/packages/x-components/src/x-modules/search/store/actions/save-search-response.action.ts +++ b/packages/x-components/src/x-modules/search/store/actions/save-search-response.action.ts @@ -20,7 +20,8 @@ export const saveSearchResponse: SearchXStoreModule['actions']['saveSearchRespon totalResults, spellcheck, redirections, - queryTagging + queryTagging, + displayTagging } ) => { if (totalResults === 0) { @@ -50,6 +51,11 @@ export const saveSearchResponse: SearchXStoreModule['actions']['saveSearchRespon if (queryTagging) { commit('setQueryTagging', queryTagging); } + + if (displayTagging) { + commit('setDisplayTagging', displayTagging); + } + commit('setTotalResults', totalResults); commit('setSpellcheck', spellcheck ?? ''); }; diff --git a/packages/x-components/src/x-modules/search/store/emitters.ts b/packages/x-components/src/x-modules/search/store/emitters.ts index 119dbfdf20..3e0fc5fc5d 100644 --- a/packages/x-components/src/x-modules/search/store/emitters.ts +++ b/packages/x-components/src/x-modules/search/store/emitters.ts @@ -28,6 +28,7 @@ export const searchEmitters = createStoreEmitters(searchXStoreModule, { partialResults: state.partialResults, promoteds: state.promoteds, queryTagging: state.queryTagging, + displayTagging: state.displayTagging, redirections: state.redirections, results: state.results, spellcheck: state.spellcheckedQuery, diff --git a/packages/x-components/src/x-modules/search/store/module.ts b/packages/x-components/src/x-modules/search/store/module.ts index 20dcf349e6..8430c7920b 100644 --- a/packages/x-components/src/x-modules/search/store/module.ts +++ b/packages/x-components/src/x-modules/search/store/module.ts @@ -106,6 +106,9 @@ export const searchXStoreModule: SearchXStoreModule = { setQueryTagging(state, queryTagging) { state.queryTagging = queryTagging; }, + setDisplayTagging(state, displayTagging) { + state.displayTagging = displayTagging; + }, updateResult(state, result) { const stateResult = state.results.find(stateResult => result.id === stateResult.id); if (stateResult) { @@ -153,6 +156,10 @@ export function resettableState() { queryTagging: { url: '', params: {} + }, + displayTagging: { + url: '', + params: {} } }; } diff --git a/packages/x-components/src/x-modules/search/store/types.ts b/packages/x-components/src/x-modules/search/store/types.ts index d8ad39084a..848532f4da 100644 --- a/packages/x-components/src/x-modules/search/store/types.ts +++ b/packages/x-components/src/x-modules/search/store/types.ts @@ -52,6 +52,8 @@ export interface SearchState extends StatusState, QueryState { promoteds: Promoted[]; /** The query tagging used to track the search events. */ queryTagging: TaggingRequest; + /** The display tagging used to track the search events. */ + displayTagging: TaggingRequest; /** The redirections associated to the `query`. */ redirections: Redirection[]; /** The list of the related tags, related to the `query` property of the state. */ @@ -172,6 +174,12 @@ export interface SearchMutations * @param queryTagging - The new query tagging object to save to the state. */ setQueryTagging(queryTagging: TaggingRequest): void; + /** + * Sets the display tagging of the module. + * + * @param displayTagging - The new display tagging object to save to the state. + */ + setDisplayTagging(DisplayTagging: TaggingRequest): void; /** * Sets the redirection of the module. * diff --git a/packages/x-types/src/response/search-response.model.ts b/packages/x-types/src/response/search-response.model.ts index 7c22354b76..7b6a32ff36 100644 --- a/packages/x-types/src/response/search-response.model.ts +++ b/packages/x-types/src/response/search-response.model.ts @@ -17,6 +17,7 @@ export interface SearchResponse { partialResults?: PartialResult[]; promoteds?: Promoted[]; queryTagging?: TaggingRequest; + displayTagging?: TaggingRequest; redirections?: Redirection[]; results: Result[]; spellcheck?: string;