Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lambda/main/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports.ElasticPathIntents = {
DESCRIBE_LISTED_PRODUCT: 'DescribeListedProductIntent',
DESCRIBE_PRODUCT: 'DescribeProductIntent',
GET_CART: 'GetCartIntent',
GET_PREVIOUS_PURCHASE_STATUS: 'GetPreviousPurchaseStatusIntent',
GET_WISLIST: 'GetWishlistIntent',
KEYWORD_SEARCH: 'KeywordSearchIntent',
MOVE_TO_CART: 'MoveToCartIntent',
Expand Down
11 changes: 11 additions & 0 deletions lambda/main/cortex.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,15 @@ Cortex.prototype.cortexCheckout = function () {
});
}

Cortex.prototype.cortexGetPurchases = function () {
return new Promise((resolve, reject) => {
this.cortexGet(`${this.cortexBaseUrl}?zoom=defaultprofile:purchases:element`)
.then((data) => {
const purchases = (data._defaultprofile) ? data._defaultprofile[0]._purchases[0]._element : [];
resolve(purchases);

}).catch((err) => reject(err));
});
}

module.exports = Cortex;
54 changes: 54 additions & 0 deletions lambda/main/handlers/getpreviouspurchase.handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright © 2018 Elastic Path Software Inc. All rights reserved.
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this license. If not, see
*
* https://www.gnu.org/licenses/
*
*
*/

const Cortex = require('../cortex');
const SpeechAssets = require('../speech/assets');
const { isIntentRequestOfType } = require('../utils');
const { ElasticPathIntents } = require('../constants');

const GetPreviousPurchaseStatusHandler = {
canHandle({requestEnvelope}) {
return isIntentRequestOfType(requestEnvelope, ElasticPathIntents.GET_PREVIOUS_PURCHASE_STATUS);
},
handle({responseBuilder}) {
return new Promise((resolve, reject) => {
Cortex.getCortexInstance()
.cortexGetPurchases()
.then((items) => {
if (items.length > 0) {
const previousPurchase = items[0];
resolve(responseBuilder
.speak(SpeechAssets.describePurchaseStatus(previousPurchase["purchase-number"], previousPurchase.status))
.reprompt(SpeechAssets.readyToCheckOut())
.getResponse());
} else {
resolve(responseBuilder
.speak(SpeechAssets.emptyPurchases())
.reprompt(SpeechAssets.howElseCanIHelp())
.getResponse());
}
})
.catch((err) => reject(err));
});
}
}

module.exports = GetPreviousPurchaseStatusHandler;
2 changes: 2 additions & 0 deletions lambda/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const DescribeProductHandler = require('./handlers/describeproduct.handler');
const DescribeListedProductHandler = require('./handlers/descibelistedproduct.handler');
const GetCartHandler = require('./handlers/getcart.handler');
const GetWishlistHandler = require('./handlers/getwishlist.handler');
const GetPreviousPurchaseHandler = require('./handlers/getpreviouspurchase.handler');
const HelpIntentHandler = require('./handlers/helpintent.handler');
const KeywordSearchHandler = require('./handlers/keywordsearch.handler');
const MoveToCartHandler= require('./handlers/movetocart.handler');
Expand Down Expand Up @@ -68,6 +69,7 @@ exports.handler = Alexa.SkillBuilders.custom()
DescribeListedProductHandler,
GetCartHandler,
GetWishlistHandler,
GetPreviousPurchaseHandler,
HelpIntentHandler,
KeywordSearchHandler,
MoveToCartHandler,
Expand Down
23 changes: 22 additions & 1 deletion lambda/main/speech/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ const speechAssets = {
noItemsInWishlist: [
'Your wishlist is empty, add some items first. '
],
noPurchases: [
'You have no previous purchases, complete an order first. '
],
purchaseSuccess: [
'Great! Your purchase was successful. ',
'Ok, your order has been placed. ',
Expand All @@ -150,6 +153,12 @@ const speechAssets = {
'Here\'s the description: <<itemDescription>>; The <<itemName>> retails for <<itemPrice>>. ',
'Here\'s the write-up: <<itemDescription>>; The <<itemName>> retails for <<itemPrice>>. '
],
describePurchaseStatus: [
'Your previous order, order number <<orderNumber>>, is <<status>>. ',
'Your previous purchase, order number <<orderNumber>>, is <<status>>. ',
'Your last order, order number <<orderNumber>>, is <<status>>. ',
'Your last purchase, order number <<orderNumber>>, is <<status>>. ',
],
describePrice: [
'<<itemPrice>>. '
],
Expand Down Expand Up @@ -238,7 +247,7 @@ const pickVariation = function(arrayOfResponses) {

const cleanOutput = function(...output) {
// eslint-disable-next-line no-useless-escape
return ` ${output.join(' ').replace(/(?!\.[\d\.])\./g, '. ').replace(/&/g, 'and').replace(/\s\s+/g, ' ')} `;
return ` ${output.join(' ').replace(/(?!\.[\d\.])\./g, '. ').replace(/&/g, 'and').replace(/\s\s+/g, ' ').replace(/_/g, ' ')} `;
}

assets.prototype.greeting = function() {
Expand Down Expand Up @@ -346,6 +355,14 @@ assets.prototype.describeProduct = function(description, item) {
return cleanOutput(this.positiveFiller(), response);
};

assets.prototype.describePurchaseStatus = function(orderNumber, status) {
const response = pickVariation(speechAssets.describePurchaseStatus)
.replace('<<orderNumber>>', orderNumber)
.replace('<<status>>', status);

return cleanOutput(this.positiveFiller(), response);
};

assets.prototype.noProductToDescribe = function() {
return cleanOutput(this.appologeticFiller(), speechAssets.noProductToDescribe, this.howElseCanIHelp());
};
Expand Down Expand Up @@ -453,6 +470,10 @@ assets.prototype.emptyWishlist = function() {
return cleanOutput(pickVariation(speechAssets.noItemsInWishlist), this.howElseCanIHelp());
};

assets.prototype.emptyPurchases = function() {
return cleanOutput(pickVariation(speechAssets.noPurchases), this.howElseCanIHelp());
};

assets.prototype.fullCheckoutMessage = function(quantity, total) {
const checkoutString = speechAssets.itemsInCart[Math.floor(Math.random() * speechAssets.itemsInCart.length)]
.replace('<<quantity>>', quantity)
Expand Down
17 changes: 17 additions & 0 deletions models/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,23 @@
"remove item {ItemNumber} from my wish list",
"delete item {ItemNumber} from my wish list"
]
},
{
"name": "GetPreviousPurchaseStatusIntent",
"samples": [
"What is the status of my last order",
"What is the status of my previous order",
"What is the status of my recent order",
"What is the status of my last purchase",
"What is the status of my previous purchase",
"What is the status of my recent purchase",
"Tell me the status of my last order",
"Tell me the status of my previous order",
"Tell me the status of my recent order",
"Tell me the status of my last purchase",
"Tell me the status of my previous purchase",
"Tell me the status of my recent purchase"
]
}
],
"types": [
Expand Down