Last active
February 19, 2019 11:36
-
-
Save muttoni/b9f102b1634c1b76c7d6923bdce09967 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // INTERCEPTOR -- add this in your request interceptor to auto-populate ISPs in your session attributes. | |
| let sessionAttributes = handlerInput.attributesManager.getSessionAttributes() || {}; | |
| if (handlerInput.requestEnvelope.session['new'] || !sessionAttributes.inSkillProducts) { | |
| let inSkillProductArray, persistentAttributes, ispObject; | |
| // first populate ISP info | |
| try { | |
| const locale = handlerInput.requestEnvelope.request.locale; | |
| const ms = handlerInput.serviceClientFactory.getMonetizationServiceClient(); | |
| ispObject = await ms.getInSkillProducts(locale); | |
| inSkillProductArray = ispObject.inSkillProducts; | |
| } catch (e) { | |
| console.log('RequestInterceptor:: could not retrieve ISPs'); | |
| } | |
| // then populate persistent attributes | |
| try { | |
| persistentAttributes = await handlerInput.attributesManager.getPersistentAttributes(); | |
| } catch (e) { | |
| console.log('RequestInterceptor:: could not retrieve persistent attributes'); | |
| } | |
| const sessionAttributes = { | |
| ...persistentAttributes, | |
| inSkillProducts: inSkillProductArray | |
| } | |
| // check if user is premium and save it to sessionAttributes | |
| sessionAttributes.premium = utils.isPremium(sessionAttributes); | |
| handlerInput.attributesManager.setSessionAttributes(sessionAttributes); | |
| } | |
| /// ----- | |
| /** | |
| * Checks whether the product exists, and returns the ISP object. | |
| * @param {array} inSkillProducts list of In Skill Products available | |
| * @param {string} productName name of product to look for | |
| * @returns {object|boolean} | |
| */ | |
| function getProductByName(inSkillProducts, productName) { | |
| const filteredProducts = inSkillProducts.filter(record => record.referenceName === productName) | |
| let product; | |
| if(filteredProducts && filteredProducts.length > 0) { | |
| product = filteredProducts[0]; | |
| } | |
| return product || false; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment