UNPKG

asksuite-core

Version:
342 lines (292 loc) 10.6 kB
require('datejs'); const API_AI_PARAMETER = 'Subst_feriados'; const packageUtil = {}; const _ = require('lodash'); const currencyFormatter = require('currency-formatter'); const KeywordUtil = require('./keyword_interpreter/keyword-util'); packageUtil.companyHasPackages = function (sessionContext, companyId, functionFindPackage) { const dialog = companyId + '.' + 'pacotes'; const findPaccote = functionFindPackage(dialog, sessionContext); return findPaccote; }; packageUtil.findPackageByTags = function ( sessionContext, companyId, functionFindPackage, text, v2Options, ) { return new Promise((resolve) => { const findPackage = packageUtil.companyHasPackages( sessionContext, companyId, functionFindPackage, ); if (findPackage && findPackage.value) { if (typeof findPackage.value === 'string') { findPackage.value = JSON.parse(findPackage.value); } let findCurrentPackage = null; if (findPackage.type === 'packages-v2') { findCurrentPackage = packageUtil.createPackageV2(findPackage, text, v2Options); } else { findCurrentPackage = findPackage.value.find((item) => { text = KeywordUtil.removeSpecial(text); if (item.tags) { item.tags = item.tags.map((item) => { return KeywordUtil.removeSpecial(item); }); } return ( item.tags && packageUtil.isValidPackage(item) && KeywordUtil.hasWordInString(text, item.tags) ); }); } if (findCurrentPackage) { resolve(findCurrentPackage); } else { resolve(null); } } else { resolve(null); } }); }; packageUtil.findCurrentPackage = function (sessionContext, intent, companyId, functionFindPackage) { return new Promise((resolve, reject) => { const sub = intent[API_AI_PARAMETER]; // eslint-disable-next-line eqeqeq if (sub && sub != '') { const findPaccote = packageUtil.companyHasPackages( sessionContext, companyId, functionFindPackage, ); if (findPaccote) { const findCurrentPackage = findPaccote.value.find((item) => { return ( item.holiday && item.holiday.toLowerCase().trim() === sub.toLowerCase().trim() && packageUtil.isValidPackage(item) ); }); if (findCurrentPackage) { resolve(findCurrentPackage); } else { const dialog = companyId + '.' + 'pacotes.naoexiste'; const packageDialogNotFound = functionFindPackage(dialog, sessionContext); if (packageDialogNotFound) { reject({ type: 'package_not_found_exists', key: dialog + '.package', holiday: sub, package: packageDialogNotFound, }); } else { reject({ type: 'package_not_found_not_exists' }); } } } else { reject({ type: 'dialog_package_not_found' }); } } else { reject({ type: 'is_not_package' }); } }); }; packageUtil.isValidPackage = function (packageAsk) { return Date.parse(packageAsk.dateEnd) > new Date() && packageAsk.active; }; packageUtil.isValidPackageV2 = function (packageAsk) { const period = (packageAsk.jsonData && packageAsk.jsonData.period) || {}; if (packageAsk.jsonData.enabled && !period.enabled) { // if the package doesnt have a specific period return true; } return Date.parse(period.dateEnd) > new Date() && packageAsk.jsonData.enabled; }; packageUtil.createPackageV2 = (dialog, text, v2Options) => { const packageFound = dialog.value.find((item) => { text = KeywordUtil.removeSpecial(text); const jsonData = item.jsonData || {}; if (jsonData.keywords) { jsonData.keywords = jsonData.keywords.map((item) => { return KeywordUtil.removeSpecial(item); }); } return ( jsonData.keywords && packageUtil.isValidPackageV2(item) && KeywordUtil.hasWordInString(text, jsonData.keywords) ); }); if (packageFound) { packageUtil.formatOffers(packageFound, v2Options); packageFound.dialog = _.cloneDeep(dialog); } return packageFound; }; packageUtil.formatPackageInfo = (packageData, language, labels) => { if (!packageData) { return; } const delimiter = labels.delimiter || ''; let subtitle = ''; if ( packageData.showNumberOfNights && packageData.showNumberOfNights === true && packageData.dailyRate > 0 ) { const dailyInfo = labels.offersDailyInfo.replace(`__days__`, packageData.dailyRate); subtitle += '\n' + `${dailyInfo}`; if ( packageData.period && packageData.period.enabled && packageData.period.dateInit && packageData.period.dateEnd ) { const dateFormat = labels.dateFormat || 'd/m/Y'; const start = Date.parse(packageData.period.dateInit).format(dateFormat); const end = Date.parse(packageData.period.dateEnd).format(dateFormat); subtitle += ' (' + labels.shortPeriod.replace(`__start__`, start).replace(`__end__`, end) + ')'; } } else { if ( packageData.period && packageData.period.enabled && packageData.period.dateInit && packageData.period.dateEnd ) { const dateFormat = labels.dateFormat || 'd/m/Y'; const start = Date.parse(packageData.period.dateInit).format(dateFormat); const end = Date.parse(packageData.period.dateEnd).format(dateFormat); subtitle = delimiter + labels.subtitle.replace(`__start__`, start).replace(`__end__`, end); } } if (packageData.quantityOfPeople && packageData.quantityOfPeople.enabled === true) { subtitle += '\n'; if (packageData.quantityOfPeople.adults === 1) { subtitle += labels.offersAdult.replace(`__adult__`, packageData.quantityOfPeople.adults) + ' '; } if (packageData.quantityOfPeople.adults > 1) { subtitle += labels.offersAdults.replace(`__adults__`, packageData.quantityOfPeople.adults) + ' '; } if (packageData.quantityOfPeople.children === 1) { subtitle += labels.offersChild.replace(`__child__`, packageData.quantityOfPeople.children) + ' '; } if (packageData.quantityOfPeople.children > 1) { subtitle += labels.offersChildren.replace(`__children__`, packageData.quantityOfPeople.children) + ' '; } if ( packageData.quantityOfPeople.includedChildrenAge && packageData.quantityOfPeople.includedChildrenAge.max > 0 ) { subtitle += '\n' + labels.offersChildrenFree .replace(`__startAge__`, packageData.quantityOfPeople.includedChildrenAge.min || 0) .replace(`__endAge__`, packageData.quantityOfPeople.includedChildrenAge.max); } } subtitle += tryToFormatCurrency(packageData, subtitle, language, labels.priceInfo); const differentialsInfo = (labels && labels.differentialsInfo) || ``; let differentials = (packageData.differentials || []).reduce((map, elem) => { return (map += differentialsInfo.replace(`__differential__`, elem) + '\n'); }, ``); if (packageData.additionalInfo) { differentials += delimiter + packageData.additionalInfo; } return { differentials, subtitle }; }; packageUtil.formatOffers = (packageFound, v2Options) => { if (!packageFound || !packageFound.jsonData) { return; } const offers = packageFound.jsonData.offers || []; const delimiter = v2Options.delimiter || ''; offers.forEach((offer) => { let messageOffers = ''; if (offer.showNumberOfNights && offer.showNumberOfNights === true) { if (offer.dailyRate > 0) { const dailyInfo = v2Options.offersDailyInfo.replace(`__days__`, offer.dailyRate); messageOffers += `${dailyInfo}`; } } if (offer.quantityOfPeople && offer.quantityOfPeople.enabled === true) { messageOffers += '\n'; if (offer.quantityOfPeople.adults === 1) { messageOffers += v2Options.offersAdult.replace(`__adult__`, offer.quantityOfPeople.adults) + ' '; } if (offer.quantityOfPeople.adults > 1) { messageOffers += v2Options.offersAdults.replace(`__adults__`, offer.quantityOfPeople.adults) + ' '; } if (offer.quantityOfPeople.children === 1) { messageOffers += v2Options.offersChild.replace(`__child__`, offer.quantityOfPeople.children) + ' '; } if (offer.quantityOfPeople.children > 1) { messageOffers += v2Options.offersChildren.replace(`__children__`, offer.quantityOfPeople.children) + ' '; } if ( offer.quantityOfPeople.includedChildrenAge && offer.quantityOfPeople.includedChildrenAge.max > 0 ) { messageOffers += '\n' + v2Options.offersChildrenFree .replace(`__startAge__`, offer.quantityOfPeople.includedChildrenAge.min || 0) .replace(`__endAge__`, offer.quantityOfPeople.includedChildrenAge.max); } } messageOffers += tryToFormatCurrency( offer, messageOffers, v2Options.language, v2Options.offersPriceInfo, ); messageOffers += delimiter; const offersIncludedItemsText = (v2Options && v2Options.offersIncludedItems) || ``; if (offer.includedItems && offer.includedItems.length) { const offersIncludedItems = (offer.includedItems || []).reduce((map, elem) => { return (map += '\n' + offersIncludedItemsText.replace(`__item__`, elem.label)); }, ``); messageOffers += `${offersIncludedItems}${delimiter}`; } if (offer.additionalInfo) { messageOffers += '\n' + offer.additionalInfo; } offer.messageOffers = messageOffers; }); }; const tryToFormatCurrency = (priceInfo, text, language, textToReplace) => { try { console.log('[tryToFormatCurrency]', JSON.stringify(priceInfo.price), language); if ( priceInfo.price && priceInfo.price.enabled && priceInfo.price.value && priceInfo.price.currency ) { const formattedPrice = formatCurrency(priceInfo.price.value, priceInfo.price.currency); return '\n' + textToReplace.replace(`__price__`, formattedPrice); } } catch (e) { console.log(`error`, e); return ''; } return ''; }; const formatCurrency = (value, currency) => { return currencyFormatter.format(value, { code: currency.toUpperCase() }); }; module.exports = packageUtil;