Skip to content

Commit

Permalink
chore(contentful): SearchResults to use CommonFields
Browse files Browse the repository at this point in the history
Now commonFields's keywords & partitions are never null for simplicity
  • Loading branch information
dpinol authored and ericmarcos committed Feb 3, 2020
1 parent 2e95744 commit 0673202
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 46 deletions.
12 changes: 9 additions & 3 deletions packages/botonic-plugin-contentful/src/cms/cms-dummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,15 @@ export class DummyCMS implements CMS {
const contents = this.buttonCallbacks.map(cb => {
const button = DummyCMS.buttonFromCallback(cb);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return new SearchResult(cb, button.name, button.text, [
'keyword for ' + (button.callback.payload || button.callback.url!)
]);
return new SearchResult(
cb,
new CommonFields(button.name, {
shortText: button.text,
keywords: [
'keyword for ' + (button.callback.payload || button.callback.url!)
]
})
);
});
return Promise.resolve(contents);
}
Expand Down
11 changes: 7 additions & 4 deletions packages/botonic-plugin-contentful/src/cms/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ export abstract class TopContent extends Content {
*/
export class CommonFields {
readonly shortText?: string;
readonly keywords?: string[];
readonly keywords: string[];
readonly searchableBy?: SearchableBy;
/** Useful when contents need to be replicated according to some criteria. Eg. country, company,...
*/
readonly partitions?: string[];
readonly partitions: string[];
readonly dateRange?: DateRangeContent;
constructor(
readonly name: string,
Expand All @@ -72,10 +72,13 @@ export class CommonFields {
) {
if (opt) {
this.shortText = opt.shortText;
this.keywords = opt.keywords;
this.keywords = opt.keywords || [];
this.searchableBy = opt.searchableBy;
this.partitions = opt.partitions;
this.partitions = opt.partitions || [];
this.dateRange = opt.dateRange;
} else {
this.keywords = [];
this.partitions = [];
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions packages/botonic-plugin-contentful/src/contentful/keywords.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entry, EntryCollection } from 'contentful';
import { Context } from '../cms';
import { CommonFields, Context } from '../cms';
import * as cms from '../cms';
import { ModelType } from '../cms';
import { SearchResult } from '../search';
Expand Down Expand Up @@ -29,9 +29,10 @@ export class KeywordsDelivery {
const callback = DeliveryApi.callbackFromEntry(entry);
return new SearchResult(
callback,
entry.fields.name,
entry.fields.shortText,
keywords,
new CommonFields(entry.fields.name, {
shortText: entry.fields.shortText,
keywords
}),
priority
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class SearchByKeywords {
this.keywordsOptions[locale] || new KeywordsOptions()
);
contentsWithKeywords.forEach(content =>
kws.addCandidate(content, content.keywords!)
kws.addCandidate(content, content.common.keywords!)
);
const results = kws.findCandidatesWithKeywordsAt(inputText);
return results.map(res => {
Expand Down
15 changes: 7 additions & 8 deletions packages/botonic-plugin-contentful/src/search/search-result.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Button,
Callback,
CommonFields,
ContentCallback,
ModelType,
PRIORITY_MAX,
Expand All @@ -17,25 +18,23 @@ export class SearchResult {
*/
constructor(
readonly callback: Callback,
readonly name: string,
readonly shortText?: string,
readonly keywords: string[] = [],
readonly common: CommonFields,
readonly priority = PRIORITY_MAX,
readonly score = SCORE_MAX,
readonly match?: string
) {}

toButton(): Button {
let shortText = this.shortText;
let shortText = this.common.shortText;
if (!shortText) {
shortText = this.name;
shortText = this.common.name;
console.error(
`${JSON.stringify(this.callback)} ${
this.name
this.common.name
} without shortText. Assigning name to button text`
);
}
return new Button(this.name, shortText, this.callback);
return new Button(this.common.name, shortText, this.callback);
}

getCallbackIfContentIs(modelType: ModelType): ContentCallback | undefined {
Expand All @@ -53,7 +52,7 @@ export class SearchResult {
return undefined;
}
if (
this.shortText !== SearchResult.CHITCHAT_SHORT_TEXT &&
this.common.shortText !== SearchResult.CHITCHAT_SHORT_TEXT &&
this.callback.model !== ModelType.CHITCHAT
) {
return undefined;
Expand Down
4 changes: 2 additions & 2 deletions packages/botonic-plugin-contentful/src/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export class Search {
if (urlCallback) {
const url = await this.cms.url(urlCallback.id, context);
return new Button(
result.name,
result.shortText!,
result.common.name,
result.common.shortText!,
Callback.ofUrl(url.url)
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/botonic-plugin-contentful/src/tools/keyword-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export class KeywordsTool {
const context = { locale: this.locale };
const results = await this.cms.contentsWithKeywords(context);
for (const res of results) {
const stemmed = res.keywords.map(
const stemmed = res.common.keywords.map(
kw =>
new StemmedKeyword(
kw,
this.normalizer.normalize(context.locale, kw).stems
)
);
keywords.set(res.name, stemmed);
keywords.set(res.common.name, stemmed);
}
return keywords;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ test('TEST: contentful contentsWithKeywords', async () => {
expect(queues).toHaveLength(2);
const keywordsByPrio: { [priority: number]: string[] } = {};
for (const queue of queues) {
expect(queue.name).toEqual('TEST_QUEUE');
expect(queue.shortText).toEqual('Short Text');
keywordsByPrio[queue.priority] = queue.keywords;
expect(queue.common.name).toEqual('TEST_QUEUE');
expect(queue.common.shortText).toEqual('Short Text');
keywordsByPrio[queue.priority] = queue.common.keywords;
}
expect(keywordsByPrio[10]).toEqual(['low1', 'low2']);
expect(keywordsByPrio[99]).toEqual(['high1', 'high2']);
Expand All @@ -38,10 +38,10 @@ test('TEST: contentful contentsWithKeywords', async () => {
const postFaq1 = contentsWithKeywords.find(
content => (content.callback as ContentCallback).id == TEST_POST_FAQ1_ID
);
expect(postFaq1!.name).toEqual('POST_FAQ1');
expect(postFaq1!.shortText).toEqual('Encontrar mi pedido');
expect(postFaq1!.common.name).toEqual('POST_FAQ1');
expect(postFaq1!.common.shortText).toEqual('Encontrar mi pedido');
expect(postFaq1!.priority).toEqual(100);
expect(postFaq1!.keywords).toIncludeSameMembers([
expect(postFaq1!.common.keywords).toIncludeSameMembers([
'no encuentro mi pedido',
'donde esta mi pedido'
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
DummyCMS,
ModelType,
SearchByKeywords,
Context
Context,
CommonFields
} from '../../src';
import { SearchResult as CallbackToContentWithKeywords1 } from '../../src/search/search-result';
import { Normalizer, StemmingBlackList, MatchType } from '../../src/nlp';
Expand Down Expand Up @@ -100,19 +101,18 @@ test('TEST: searchContentsFromInput with stem blacklist', async () => {
export function contentWithKeyword(callback: Callback, keywords: string[]) {
return new SearchResult(
callback,
callback.payload!,
'shortText' + callback.payload,
keywords
new CommonFields(callback.payload!, {
shortText: 'shortText' + callback.payload,
keywords
})
);
}

export function chitchatContent(keywords: string[]) {
const id = Math.random().toString();
return new SearchResult(
new ContentCallback(ModelType.TEXT, id),
id,
'chitchat',
keywords
new CommonFields(id, { shortText: 'chitchat', keywords })
);
}

Expand Down
12 changes: 3 additions & 9 deletions packages/botonic-plugin-contentful/tests/search/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ test('TEST: respondFoundContents text with buttons', async () => {

const urlContent = new SearchResult(
new ContentCallback(ModelType.URL, 'urlCmsId'),
'name',
'url shortText',
[]
new CommonFields('name', { shortText: 'url shortText' })
);

const textContent = new SearchResult(
new ContentCallback(ModelType.TEXT, 'textCmsId'),
'name',
'text shortText',
[]
new CommonFields('name', { shortText: 'text shortText' })
);

// sut
Expand Down Expand Up @@ -64,9 +60,7 @@ test('TEST: respondFoundContents text with chitchat', async () => {
when(cms.chitchat('chitchatCmsId', CONTEXT)).thenResolve(chitchat);
const chitchatCallback = new SearchResult(
new ContentCallback(ModelType.CHITCHAT, 'chitchatCmsId'),
'name',
'chitchat',
[]
new CommonFields('name', { shortText: 'chitchat' })
);

when(cms.text('foundId', CONTEXT)).thenResolve(
Expand Down

0 comments on commit 0673202

Please sign in to comment.