fa87a3f8a3732f98a6086816346e6d7b370d7115
[motion2.git] / server / api / cannedAnswer / cannedAnswer.controller.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0xcee8=['destroy','get','CannedAnswers','UserProfileResource','sendStatus','stack','send','index','map','CannedAnswer','rawAttributes','fieldName','type','key','name','query','keys','filters','intersection','model','attributes','fields','hasOwnProperty','nolimit','order','sort','pick','filter','merge','where','options','includeAll','include','catch','show','params','length','update','body','find','SmsAccountId','OpenchannelAccountId','ChatWebsiteId','Sequelize','ValidationError','create','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','UserProfileSection','userProfileId','autoAssociation','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','rimraf','zip-dir','request-promise','moment','bluebird','util','to-csv','ejs','fs-extra','lodash','squel','jsforce','desk.js','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','then'];(function(_0x2e7a43,_0x3b3df4){var _0x52e9e1=function(_0x2c08cf){while(--_0x2c08cf){_0x2e7a43['push'](_0x2e7a43['shift']());}};_0x52e9e1(++_0x3b3df4);}(_0xcee8,0x1a2));var _0x8cee=function(_0x1f82a0,_0x4498b1){_0x1f82a0=_0x1f82a0-0x0;var _0x376bab=_0xcee8[_0x1f82a0];return _0x376bab;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x8cee('0x0'));var zipdir=require(_0x8cee('0x1'));var jsonpatch=require('fast-json-patch');var rp=require(_0x8cee('0x2'));var moment=require(_0x8cee('0x3'));var BPromise=require(_0x8cee('0x4'));var Mustache=require('mustache');var util=require(_0x8cee('0x5'));var path=require('path');var sox=require('sox');var csv=require(_0x8cee('0x6'));var ejs=require(_0x8cee('0x7'));var fs=require('fs');var fs_extra=require(_0x8cee('0x8'));var _=require(_0x8cee('0x9'));var squel=require(_0x8cee('0xa'));var crypto=require('crypto');var jsforce=require(_0x8cee('0xb'));var deskjs=require(_0x8cee('0xc'));var toCsv=require('to-csv');var querystring=require(_0x8cee('0xd'));var Papa=require(_0x8cee('0xe'));var Redis=require(_0x8cee('0xf'));var authService=require('../../components/auth/service');var qs=require(_0x8cee('0x10'));var as=require(_0x8cee('0x11'));var hardwareService=require(_0x8cee('0x12'));var logger=require(_0x8cee('0x13'))(_0x8cee('0x14'));var utils=require(_0x8cee('0x15'));var config=require('../../config/environment');var licenseUtil=require(_0x8cee('0x16'));var db=require(_0x8cee('0x17'))['db'];function respondWithStatusCode(_0x5bcb01,_0x473302){_0x473302=_0x473302||0xcc;return function(_0x26e224){if(_0x26e224){return _0x5bcb01['sendStatus'](_0x473302);}return _0x5bcb01[_0x8cee('0x18')](_0x473302)[_0x8cee('0x19')]();};}function respondWithResult(_0x2a8e73,_0x2c70d2){_0x2c70d2=_0x2c70d2||0xc8;return function(_0x4a4178){if(_0x4a4178){return _0x2a8e73[_0x8cee('0x18')](_0x2c70d2)[_0x8cee('0x1a')](_0x4a4178);}};}function respondWithFilteredResult(_0x5796eb,_0x4bc6b8){return function(_0x4c67dd){if(_0x4c67dd){var _0x344f42=typeof _0x4bc6b8[_0x8cee('0x1b')]===_0x8cee('0x1c')&&typeof _0x4bc6b8[_0x8cee('0x1d')]==='undefined';var _0x277ab9=_0x4c67dd['count'];var _0x57e3a7=_0x344f42?0x0:_0x4bc6b8['offset'];var _0xc41778=_0x344f42?_0x4c67dd[_0x8cee('0x1e')]:_0x4bc6b8['offset']+_0x4bc6b8[_0x8cee('0x1d')];var _0x3607da;if(_0xc41778>=_0x277ab9){_0xc41778=_0x277ab9;_0x3607da=0xc8;}else{_0x3607da=0xce;}_0x5796eb['status'](_0x3607da);return _0x5796eb[_0x8cee('0x1f')](_0x8cee('0x20'),_0x57e3a7+'-'+_0xc41778+'/'+_0x277ab9)['json'](_0x4c67dd);}return null;};}function patchUpdates(_0xadb845){return function(_0x31de4c){try{jsonpatch[_0x8cee('0x21')](_0x31de4c,_0xadb845,!![]);}catch(_0xd7cd8f){return BPromise[_0x8cee('0x22')](_0xd7cd8f);}return _0x31de4c[_0x8cee('0x23')]();};}function saveUpdates(_0xe898ae,_0x44037){return function(_0x186f6a){if(_0x186f6a){return _0x186f6a['update'](_0xe898ae)[_0x8cee('0x24')](function(_0x1384d1){return _0x1384d1;});}return null;};}function removeEntity(_0x16607b,_0x4fc406){return function(_0x1476a3){if(_0x1476a3){return _0x1476a3[_0x8cee('0x25')]()[_0x8cee('0x24')](function(){var _0x547761=_0x1476a3[_0x8cee('0x26')]({'plain':!![]});var _0x436ed4=_0x8cee('0x27');return db[_0x8cee('0x28')]['destroy']({'where':{'type':_0x436ed4,'resourceId':_0x547761['id']}})[_0x8cee('0x24')](function(){return _0x1476a3;});})['then'](function(){_0x16607b[_0x8cee('0x18')](0xcc)[_0x8cee('0x19')]();});}};}function handleEntityNotFound(_0x3edcbb,_0x410ce8){return function(_0x250ee8){if(!_0x250ee8){_0x3edcbb[_0x8cee('0x29')](0x194);}return _0x250ee8;};}function handleError(_0x3e71d6,_0x200ef9){_0x200ef9=_0x200ef9||0x1f4;return function(_0x57cb20){logger['error'](_0x57cb20[_0x8cee('0x2a')]);if(_0x57cb20['name']){delete _0x57cb20['name'];}_0x3e71d6[_0x8cee('0x18')](_0x200ef9)[_0x8cee('0x2b')](_0x57cb20);};}exports[_0x8cee('0x2c')]=function(_0x509dd6,_0xb7328f){var _0x226c04={},_0xdd92e6={},_0x18dbcc={'count':0x0,'rows':[]};var _0xf1f7bf=_[_0x8cee('0x2d')](db[_0x8cee('0x2e')][_0x8cee('0x2f')],function(_0x1806fc){return{'name':_0x1806fc[_0x8cee('0x30')],'type':_0x1806fc[_0x8cee('0x31')][_0x8cee('0x32')]};});_0xdd92e6['model']=_[_0x8cee('0x2d')](_0xf1f7bf,_0x8cee('0x33'));_0xdd92e6[_0x8cee('0x34')]=_[_0x8cee('0x35')](_0x509dd6[_0x8cee('0x34')]);_0xdd92e6[_0x8cee('0x36')]=_[_0x8cee('0x37')](_0xdd92e6[_0x8cee('0x38')],_0xdd92e6[_0x8cee('0x34')]);_0x226c04[_0x8cee('0x39')]=_[_0x8cee('0x37')](_0xdd92e6['model'],qs[_0x8cee('0x3a')](_0x509dd6[_0x8cee('0x34')][_0x8cee('0x3a')]));_0x226c04[_0x8cee('0x39')]=_0x226c04['attributes']['length']?_0x226c04[_0x8cee('0x39')]:_0xdd92e6['model'];if(!_0x509dd6[_0x8cee('0x34')][_0x8cee('0x3b')](_0x8cee('0x3c'))){_0x226c04[_0x8cee('0x1d')]=qs[_0x8cee('0x1d')](_0x509dd6['query'][_0x8cee('0x1d')]);_0x226c04[_0x8cee('0x1b')]=qs[_0x8cee('0x1b')](_0x509dd6[_0x8cee('0x34')]['offset']);}_0x226c04[_0x8cee('0x3d')]=qs[_0x8cee('0x3e')](_0x509dd6[_0x8cee('0x34')][_0x8cee('0x3e')]);_0x226c04['where']=qs[_0x8cee('0x36')](_[_0x8cee('0x3f')](_0x509dd6[_0x8cee('0x34')],_0xdd92e6['filters']),_0xf1f7bf);if(_0x509dd6['query'][_0x8cee('0x40')]){_0x226c04['where']=_[_0x8cee('0x41')](_0x226c04[_0x8cee('0x42')],{'$or':[{'key':{'$like':'%'+_0x509dd6[_0x8cee('0x34')][_0x8cee('0x40')]+'%'}},{'value':{'$like':'%'+_0x509dd6[_0x8cee('0x34')][_0x8cee('0x40')]+'%'}}]});}_0x226c04=_['merge']({},_0x226c04,_0x509dd6[_0x8cee('0x43')]);var _0x4a553c={'where':_0x226c04['where']};return db[_0x8cee('0x2e')][_0x8cee('0x1e')](_0x4a553c)['then'](function(_0x3c130e){_0x18dbcc[_0x8cee('0x1e')]=_0x3c130e;if(_0x509dd6[_0x8cee('0x34')][_0x8cee('0x44')]){_0x226c04[_0x8cee('0x45')]=[{'all':!![]}];}return db[_0x8cee('0x2e')]['findAll'](_0x226c04);})[_0x8cee('0x24')](function(_0x4b6b33){_0x18dbcc['rows']=_0x4b6b33;return _0x18dbcc;})[_0x8cee('0x24')](respondWithFilteredResult(_0xb7328f,_0x226c04))[_0x8cee('0x46')](handleError(_0xb7328f,null));};exports[_0x8cee('0x47')]=function(_0x2d2f25,_0x3676ac){var _0x1ed344={'raw':!![],'where':{'id':_0x2d2f25[_0x8cee('0x48')]['id']}},_0x473a14={};_0x473a14[_0x8cee('0x38')]=_[_0x8cee('0x35')](db[_0x8cee('0x2e')][_0x8cee('0x2f')]);_0x473a14['query']=_[_0x8cee('0x35')](_0x2d2f25[_0x8cee('0x34')]);_0x473a14['filters']=_[_0x8cee('0x37')](_0x473a14[_0x8cee('0x38')],_0x473a14['query']);_0x1ed344['attributes']=_[_0x8cee('0x37')](_0x473a14[_0x8cee('0x38')],qs[_0x8cee('0x3a')](_0x2d2f25['query'][_0x8cee('0x3a')]));_0x1ed344[_0x8cee('0x39')]=_0x1ed344['attributes'][_0x8cee('0x49')]?_0x1ed344[_0x8cee('0x39')]:_0x473a14[_0x8cee('0x38')];if(_0x2d2f25[_0x8cee('0x34')][_0x8cee('0x44')]){_0x1ed344[_0x8cee('0x45')]=[{'all':!![]}];}_0x1ed344=_['merge']({},_0x1ed344,_0x2d2f25[_0x8cee('0x43')]);return db[_0x8cee('0x2e')]['find'](_0x1ed344)[_0x8cee('0x24')](handleEntityNotFound(_0x3676ac,null))['then'](respondWithResult(_0x3676ac,null))[_0x8cee('0x46')](handleError(_0x3676ac,null));};exports[_0x8cee('0x4a')]=function(_0x2bf539,_0x3b7274){if(_0x2bf539[_0x8cee('0x4b')]['id']){delete _0x2bf539[_0x8cee('0x4b')]['id'];}return db['CannedAnswer'][_0x8cee('0x4c')]({'where':{'id':_0x2bf539[_0x8cee('0x48')]['id']}})['then'](handleEntityNotFound(_0x3b7274,null))[_0x8cee('0x24')](saveUpdates(_0x2bf539['body'],null))[_0x8cee('0x24')](respondWithResult(_0x3b7274,null))['catch'](handleError(_0x3b7274,null));};exports[_0x8cee('0x25')]=function(_0x59dafd,_0xe6843c){return db[_0x8cee('0x2e')][_0x8cee('0x4c')]({'where':{'id':_0x59dafd['params']['id']}})[_0x8cee('0x24')](handleEntityNotFound(_0xe6843c,null))['then'](removeEntity(_0xe6843c,null))[_0x8cee('0x46')](handleError(_0xe6843c,null));};exports['create']=function(_0x4ee537,_0x5e0abe){var _0x18a9f2={'key':_0x4ee537[_0x8cee('0x4b')][_0x8cee('0x32')]};if(_0x4ee537[_0x8cee('0x4b')]['MailAccountId']){_0x18a9f2['MailAccountId']=_0x4ee537[_0x8cee('0x4b')]['MailAccountId'];}else if(_0x4ee537['body'][_0x8cee('0x4d')]){_0x18a9f2[_0x8cee('0x4d')]=_0x4ee537[_0x8cee('0x4b')][_0x8cee('0x4d')];}else if(_0x4ee537[_0x8cee('0x4b')][_0x8cee('0x4e')]){_0x18a9f2[_0x8cee('0x4e')]=_0x4ee537[_0x8cee('0x4b')]['OpenchannelAccountId'];}else if(_0x4ee537['body'][_0x8cee('0x4f')]){_0x18a9f2[_0x8cee('0x4f')]=_0x4ee537[_0x8cee('0x4b')]['ChatWebsiteId'];}return db['CannedAnswer']['find']({'where':_0x18a9f2})[_0x8cee('0x24')](function(_0xe6e185){if(_0xe6e185){throw new db[(_0x8cee('0x50'))][(_0x8cee('0x51'))]('The\x20inserted\x20key\x20already\x20exists.');}return db[_0x8cee('0x2e')][_0x8cee('0x52')](_0x4ee537['body']);})[_0x8cee('0x24')](function(_0x1cc80e){var _0xeada09=_0x4ee537['user']['get']({'plain':!![]});if(!_0xeada09)throw new Error(_0x8cee('0x53'));if(_0xeada09[_0x8cee('0x54')]===_0x8cee('0x55')){var _0x5291f6=_0x1cc80e[_0x8cee('0x26')]({'plain':!![]});return db[_0x8cee('0x56')][_0x8cee('0x4c')]({'where':{'name':_0x8cee('0x27'),'userProfileId':_0xeada09[_0x8cee('0x57')]},'raw':!![]})[_0x8cee('0x24')](function(_0x2b49c8){if(_0x2b49c8&&_0x2b49c8[_0x8cee('0x58')]===0x0){return db['UserProfileResource'][_0x8cee('0x52')]({'name':_0x5291f6[_0x8cee('0x33')],'resourceId':_0x5291f6['id'],'type':_0x2b49c8['name'],'sectionId':_0x2b49c8['id']},{})['then'](function(){return _0x1cc80e;});}else{return _0x1cc80e;}})[_0x8cee('0x46')](function(_0x19a968){logger[_0x8cee('0x59')](_0x8cee('0x5a'),_0x19a968);throw _0x19a968;});}return _0x1cc80e;})[_0x8cee('0x24')](respondWithResult(_0x5e0abe,0xc9))[_0x8cee('0x46')](handleError(_0x5e0abe,null));};