UNPKG

asksuite-core

Version:
107 lines (90 loc) 3.04 kB
require('datejs'); const API_AI_PARAMETER = 'Subst_feriados'; const packageUtil = {}; const KeywordUtil = require('./keyword_interpreter/keyword-util'); packageUtil.companyHasPackages = function(sessionContext, companyId, functionFindPackage) { const dialog = companyId + '.' + 'pacotes'; const findPaccote = functionFindPackage(dialog, sessionContext); // console.log("Dialog"+dialog); // console.log("Pacote Encontrado"+JSON.stringify(findPaccote)); return findPaccote; }; packageUtil.findPackageByTags = function(sessionContext, companyId, functionFindPackage, text) { 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); } const 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]; 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; }; module.exports = packageUtil;