Skip to content

Commit

Permalink
DEVEXP-393: Refactor Product and Choice interfaces + create helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch committed Apr 18, 2024
1 parent f185af1 commit 7e9dc6a
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { MediaMessageItem } from '../media-message';

export interface ChoiceItem {
export interface ChoiceItemWrapper {

/** A message component for interactive messages, containing a choice. */
choice: ChoiceItemItem;
choice: ChoiceItem;
}

export interface ChoiceItemItem {
export interface ChoiceItem {

/** Required parameter. Title for the choice item. */
title: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/conversation/src/models/v1/choice-item/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type { ChoiceItem, ChoiceItemItem } from './choice-item';
export type { ChoiceItemWrapper, ChoiceItem } from './choice-item';
15 changes: 15 additions & 0 deletions packages/conversation/src/models/v1/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { MediaMessage, MediaMessageItem } from './media-message';
import { TemplateMessage, TemplateMessageItem } from './template-message';
import { ListMessage, ListMessageItem } from './list-message';
import { V2TemplateTranslation } from './v2-template-translation';
import { ChoiceItem, ChoiceItemWrapper } from './choice-item';
import { ProductItem, ProductItemWrapper } from './product-item';

export const messageBuilder = {
card: (cardMessageItem: CardMessageItem): CardMessage => {
Expand Down Expand Up @@ -178,6 +180,19 @@ export const templateV2Helper = {
},
};

export const listSectionHelper = {
buildChoiceItem: (choiceItem: ChoiceItem): ChoiceItemWrapper => {
return {
choice: choiceItem,
};
},
buildProductItem: (productItem: ProductItem): ProductItemWrapper => {
return {
product: productItem,
};
},
};

export const enum MessageType {
CARD = 'CardMessage',
CAROUSEL = 'CarouselMessage',
Expand Down
3 changes: 2 additions & 1 deletion packages/conversation/src/models/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export * from './channel-recipient-identity';
export * from './channel-specific-contact-message';
export * from './channel-specific-message';
export * from './choice';
export * from './choice-item';
export * from './choice-message';
export * from './choice-response-message';
export * from './client-credentials';
Expand Down Expand Up @@ -62,7 +63,7 @@ export * from './media-carousel-message';
export * from './media-message';
export * from './merge-contact-request';
export * from './message-retry-settings';
export * from './product';
export * from './product-item';
export * from './product-response-message';
export * from './error-detail';
export * from './lookup-capability-request';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChoiceItem } from '../choice-item';
import { Product } from '../product';
import { ChoiceItemWrapper } from '../choice-item';
import { ProductItemWrapper } from '../product-item';

/**
* Section for interactive WhatsApp messages containing ListItem
Expand All @@ -9,5 +9,5 @@ export interface ListSection {
/** Optional parameter. Title for list section. */
title?: string;
/** List of ListItems */
items?: ChoiceItem[] | Product[];
items?: ChoiceItemWrapper[] | ProductItemWrapper[];
}
1 change: 1 addition & 0 deletions packages/conversation/src/models/v1/product-item/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { ProductItemWrapper, ProductItem } from './product-item';
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* A message component for interactive messages, containing a product.
*/
export interface Product {
export interface ProductItemWrapper {

/** A message component for interactive messages, containing a product. */
product?: ProductItem;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProductItem } from '../product';
import { ProductItem } from '../product-item';

/**
* Represents an interactive WhatsApp message containing ProductItem objects
Expand Down
1 change: 0 additions & 1 deletion packages/conversation/src/models/v1/product/index.ts

This file was deleted.

56 changes: 32 additions & 24 deletions packages/conversation/tests/messages-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,42 +168,50 @@ export const templateMessage: Conversation.TemplateMessage = {
template_message: templateMessageItem,
};

export const strawberryChoiceDetails: Conversation.ChoiceItem = {
title: 'Strawberry',
postback_data: 'Strawberry postback',
};

export const blueberryChoiceDetails: Conversation.ChoiceItem = {
title: 'Blueberry',
postback_data: 'Blueberry postback',
};

export const chocolateChoiceDetails: Conversation.ChoiceItem = {
title: 'Chocolate',
postback_data: 'Chocolate postback',
};

export const vanillaChoiceDetails: Conversation.ChoiceItem = {
title: 'Vanilla',
postback_data: 'Vanilla postback',
};

export const bookProductDetails: Conversation.ProductItem = {
id: '2351786092',
marketplace: 'bashi-bouzouk',
item_price: 12,
currency: 'EUR',
quantity: 1,
};

export const listMessageItem: Conversation.ListMessageItem = {
title: 'Choose your icecream flavor',
description: 'The best icecream in town!',
sections: [
{
title: 'Fruit flavors',
items: [
{
choice: {
title: 'Strawberry',
postback_data: 'Strawberry postback',
},
},
{
choice: {
title: 'Blueberry',
postback_data: 'Blueberry postback',
},
},
Conversation.listSectionHelper.buildChoiceItem(strawberryChoiceDetails),
Conversation.listSectionHelper.buildChoiceItem(blueberryChoiceDetails),
],
},
{
title: 'Other flavors',
items: [
{
choice: {
title: 'Chocolate',
postback_data: 'Chocolate postback',
},
},
{
choice: {
title: 'Vanilla',
postback_data: 'Vanilla postback',
},
},
Conversation.listSectionHelper.buildChoiceItem(chocolateChoiceDetails),
Conversation.listSectionHelper.buildChoiceItem(vanillaChoiceDetails),
],
},
],
Expand Down
29 changes: 29 additions & 0 deletions packages/conversation/tests/models/v1/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
templateMessageItem,
textMessageItem,
textMessage,
strawberryChoiceDetails,
bookProductDetails,
} from '../../messages-mocks';

describe('Conversation models helpers', () => {
Expand Down Expand Up @@ -249,4 +251,31 @@ describe('Conversation models helpers', () => {
});
});

describe('List Section helper', () => {
it('should build a ChoiceItemWrapper', () => {
const strawberryChoiceItem: Conversation.ChoiceItemWrapper = {
choice: {
title: 'Strawberry',
postback_data: 'Strawberry postback',
},
};
const builtItemWrapper = Conversation.listSectionHelper.buildChoiceItem(strawberryChoiceDetails);
expect(builtItemWrapper).toEqual(strawberryChoiceItem);
});

it('should build a ProductItemWrapper', () => {
const bookProductItem: Conversation.ProductItemWrapper = {
product: {
id: '2351786092',
marketplace: 'bashi-bouzouk',
item_price: 12,
currency: 'EUR',
quantity: 1,
},
};
const builtItemWrapper = Conversation.listSectionHelper.buildProductItem(bookProductDetails);
expect(builtItemWrapper).toEqual(bookProductItem);
});
});

});

0 comments on commit 7e9dc6a

Please sign in to comment.