Built motion from commit 497d3d92.|2.5.43
[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 _0xaac6=['CannedAnswer','rawAttributes','fieldName','model','query','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','filters','pick','filter','where','merge','options','includeAll','include','findAll','rows','show','keys','find','catch','body','params','destroy','create','key','MailAccountId','SmsAccountId','OpenchannelAccountId','ChatWebsiteId','Sequelize','ValidationError','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','util','path','fs-extra','lodash','squel','crypto','jsforce','desk.js','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','sendStatus','status','end','json','offset','limit','undefined','count','Content-Range','apply','save','update','then','get','CannedAnswers','UserProfileResource','error','stack','name','send','index','map'];(function(_0x3a87c1,_0x260274){var _0x5ba849=function(_0x309a96){while(--_0x309a96){_0x3a87c1['push'](_0x3a87c1['shift']());}};_0x5ba849(++_0x260274);}(_0xaac6,0x13e));var _0x6aac=function(_0x1e2080,_0x53283b){_0x1e2080=_0x1e2080-0x0;var _0x260111=_0xaac6[_0x1e2080];return _0x260111;};'use strict';var emlformat=require(_0x6aac('0x0'));var rimraf=require(_0x6aac('0x1'));var zipdir=require(_0x6aac('0x2'));var jsonpatch=require(_0x6aac('0x3'));var rp=require(_0x6aac('0x4'));var moment=require(_0x6aac('0x5'));var BPromise=require(_0x6aac('0x6'));var Mustache=require('mustache');var util=require(_0x6aac('0x7'));var path=require(_0x6aac('0x8'));var sox=require('sox');var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x6aac('0x9'));var _=require(_0x6aac('0xa'));var squel=require(_0x6aac('0xb'));var crypto=require(_0x6aac('0xc'));var jsforce=require(_0x6aac('0xd'));var deskjs=require(_0x6aac('0xe'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0x6aac('0xf'));var Redis=require(_0x6aac('0x10'));var authService=require('../../components/auth/service');var qs=require(_0x6aac('0x11'));var as=require(_0x6aac('0x12'));var hardwareService=require(_0x6aac('0x13'));var logger=require(_0x6aac('0x14'))(_0x6aac('0x15'));var utils=require(_0x6aac('0x16'));var config=require('../../config/environment');var licenseUtil=require(_0x6aac('0x17'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0xb5d1b5,_0x3c59f6){_0x3c59f6=_0x3c59f6||0xcc;return function(_0x38a62f){if(_0x38a62f){return _0xb5d1b5[_0x6aac('0x18')](_0x3c59f6);}return _0xb5d1b5[_0x6aac('0x19')](_0x3c59f6)[_0x6aac('0x1a')]();};}function respondWithResult(_0x385e0b,_0x112713){_0x112713=_0x112713||0xc8;return function(_0x3b6627){if(_0x3b6627){return _0x385e0b[_0x6aac('0x19')](_0x112713)[_0x6aac('0x1b')](_0x3b6627);}};}function respondWithFilteredResult(_0x36d3dc,_0x4a7bc0){return function(_0x50e9a8){if(_0x50e9a8){var _0xdaef3d=typeof _0x4a7bc0[_0x6aac('0x1c')]==='undefined'&&typeof _0x4a7bc0[_0x6aac('0x1d')]===_0x6aac('0x1e');var _0x3a4d80=_0x50e9a8[_0x6aac('0x1f')];var _0x13f889=_0xdaef3d?0x0:_0x4a7bc0[_0x6aac('0x1c')];var _0x172be5=_0xdaef3d?_0x50e9a8[_0x6aac('0x1f')]:_0x4a7bc0[_0x6aac('0x1c')]+_0x4a7bc0[_0x6aac('0x1d')];var _0x5976ff;if(_0x172be5>=_0x3a4d80){_0x172be5=_0x3a4d80;_0x5976ff=0xc8;}else{_0x5976ff=0xce;}_0x36d3dc[_0x6aac('0x19')](_0x5976ff);return _0x36d3dc['set'](_0x6aac('0x20'),_0x13f889+'-'+_0x172be5+'/'+_0x3a4d80)[_0x6aac('0x1b')](_0x50e9a8);}return null;};}function patchUpdates(_0x456f45){return function(_0x3485a6){try{jsonpatch[_0x6aac('0x21')](_0x3485a6,_0x456f45,!![]);}catch(_0x406093){return BPromise['reject'](_0x406093);}return _0x3485a6[_0x6aac('0x22')]();};}function saveUpdates(_0xb0a941,_0x1f34b8){return function(_0x2563e7){if(_0x2563e7){return _0x2563e7[_0x6aac('0x23')](_0xb0a941)[_0x6aac('0x24')](function(_0x521012){return _0x521012;});}return null;};}function removeEntity(_0x22ff44,_0xa358be){return function(_0x1bcf4c){if(_0x1bcf4c){return _0x1bcf4c['destroy']()['then'](function(){var _0x30719a=_0x1bcf4c[_0x6aac('0x25')]({'plain':!![]});var _0x12bfa6=_0x6aac('0x26');return db[_0x6aac('0x27')]['destroy']({'where':{'type':_0x12bfa6,'resourceId':_0x30719a['id']}})[_0x6aac('0x24')](function(){return _0x1bcf4c;});})[_0x6aac('0x24')](function(){_0x22ff44[_0x6aac('0x19')](0xcc)['end']();});}};}function handleEntityNotFound(_0x596b19,_0x7e1b43){return function(_0x2f8036){if(!_0x2f8036){_0x596b19['sendStatus'](0x194);}return _0x2f8036;};}function handleError(_0xb4896d,_0x3b9363){_0x3b9363=_0x3b9363||0x1f4;return function(_0x52e302){logger[_0x6aac('0x28')](_0x52e302[_0x6aac('0x29')]);if(_0x52e302[_0x6aac('0x2a')]){delete _0x52e302['name'];}_0xb4896d['status'](_0x3b9363)[_0x6aac('0x2b')](_0x52e302);};}exports[_0x6aac('0x2c')]=function(_0x342767,_0xb523cf){var _0x376134={},_0x4878a9={},_0x2d8737={'count':0x0,'rows':[]};var _0x2e677d=_[_0x6aac('0x2d')](db[_0x6aac('0x2e')][_0x6aac('0x2f')],function(_0x422f16){return{'name':_0x422f16[_0x6aac('0x30')],'type':_0x422f16['type']['key']};});_0x4878a9[_0x6aac('0x31')]=_[_0x6aac('0x2d')](_0x2e677d,_0x6aac('0x2a'));_0x4878a9[_0x6aac('0x32')]=_['keys'](_0x342767[_0x6aac('0x32')]);_0x4878a9['filters']=_[_0x6aac('0x33')](_0x4878a9[_0x6aac('0x31')],_0x4878a9['query']);_0x376134[_0x6aac('0x34')]=_['intersection'](_0x4878a9[_0x6aac('0x31')],qs['fields'](_0x342767['query'][_0x6aac('0x35')]));_0x376134[_0x6aac('0x34')]=_0x376134[_0x6aac('0x34')][_0x6aac('0x36')]?_0x376134[_0x6aac('0x34')]:_0x4878a9[_0x6aac('0x31')];if(!_0x342767['query'][_0x6aac('0x37')](_0x6aac('0x38'))){_0x376134['limit']=qs[_0x6aac('0x1d')](_0x342767[_0x6aac('0x32')][_0x6aac('0x1d')]);_0x376134[_0x6aac('0x1c')]=qs[_0x6aac('0x1c')](_0x342767[_0x6aac('0x32')][_0x6aac('0x1c')]);}_0x376134[_0x6aac('0x39')]=qs[_0x6aac('0x3a')](_0x342767[_0x6aac('0x32')][_0x6aac('0x3a')]);_0x376134['where']=qs[_0x6aac('0x3b')](_[_0x6aac('0x3c')](_0x342767[_0x6aac('0x32')],_0x4878a9['filters']),_0x2e677d);if(_0x342767[_0x6aac('0x32')][_0x6aac('0x3d')]){_0x376134[_0x6aac('0x3e')]=_[_0x6aac('0x3f')](_0x376134[_0x6aac('0x3e')],{'$or':[{'key':{'$like':'%'+_0x342767[_0x6aac('0x32')][_0x6aac('0x3d')]+'%'}},{'value':{'$like':'%'+_0x342767[_0x6aac('0x32')][_0x6aac('0x3d')]+'%'}}]});}_0x376134=_['merge']({},_0x376134,_0x342767[_0x6aac('0x40')]);var _0x217253={'where':_0x376134['where']};return db[_0x6aac('0x2e')][_0x6aac('0x1f')](_0x217253)['then'](function(_0x1dc24d){_0x2d8737['count']=_0x1dc24d;if(_0x342767['query'][_0x6aac('0x41')]){_0x376134[_0x6aac('0x42')]=[{'all':!![]}];}return db[_0x6aac('0x2e')][_0x6aac('0x43')](_0x376134);})[_0x6aac('0x24')](function(_0x56964c){_0x2d8737[_0x6aac('0x44')]=_0x56964c;return _0x2d8737;})[_0x6aac('0x24')](respondWithFilteredResult(_0xb523cf,_0x376134))['catch'](handleError(_0xb523cf,null));};exports[_0x6aac('0x45')]=function(_0x38f48e,_0x523fe9){var _0x4a74d1={'raw':!![],'where':{'id':_0x38f48e['params']['id']}},_0x136ec3={};_0x136ec3[_0x6aac('0x31')]=_[_0x6aac('0x46')](db[_0x6aac('0x2e')][_0x6aac('0x2f')]);_0x136ec3[_0x6aac('0x32')]=_[_0x6aac('0x46')](_0x38f48e['query']);_0x136ec3[_0x6aac('0x3b')]=_[_0x6aac('0x33')](_0x136ec3['model'],_0x136ec3['query']);_0x4a74d1[_0x6aac('0x34')]=_['intersection'](_0x136ec3[_0x6aac('0x31')],qs[_0x6aac('0x35')](_0x38f48e[_0x6aac('0x32')][_0x6aac('0x35')]));_0x4a74d1[_0x6aac('0x34')]=_0x4a74d1['attributes'][_0x6aac('0x36')]?_0x4a74d1['attributes']:_0x136ec3[_0x6aac('0x31')];if(_0x38f48e[_0x6aac('0x32')][_0x6aac('0x41')]){_0x4a74d1['include']=[{'all':!![]}];}_0x4a74d1=_['merge']({},_0x4a74d1,_0x38f48e[_0x6aac('0x40')]);return db[_0x6aac('0x2e')][_0x6aac('0x47')](_0x4a74d1)['then'](handleEntityNotFound(_0x523fe9,null))[_0x6aac('0x24')](respondWithResult(_0x523fe9,null))[_0x6aac('0x48')](handleError(_0x523fe9,null));};exports[_0x6aac('0x23')]=function(_0x4a0cd9,_0x269466){if(_0x4a0cd9[_0x6aac('0x49')]['id']){delete _0x4a0cd9[_0x6aac('0x49')]['id'];}return db[_0x6aac('0x2e')][_0x6aac('0x47')]({'where':{'id':_0x4a0cd9[_0x6aac('0x4a')]['id']}})[_0x6aac('0x24')](handleEntityNotFound(_0x269466,null))['then'](saveUpdates(_0x4a0cd9['body'],null))[_0x6aac('0x24')](respondWithResult(_0x269466,null))[_0x6aac('0x48')](handleError(_0x269466,null));};exports[_0x6aac('0x4b')]=function(_0x470884,_0x5b72b2){return db[_0x6aac('0x2e')][_0x6aac('0x47')]({'where':{'id':_0x470884['params']['id']}})['then'](handleEntityNotFound(_0x5b72b2,null))['then'](removeEntity(_0x5b72b2,null))[_0x6aac('0x48')](handleError(_0x5b72b2,null));};exports[_0x6aac('0x4c')]=function(_0x23eb5b,_0x19c9a7){var _0x2b1f76={'key':_0x23eb5b[_0x6aac('0x49')][_0x6aac('0x4d')]};if(_0x23eb5b['body'][_0x6aac('0x4e')]){_0x2b1f76['MailAccountId']=_0x23eb5b['body'][_0x6aac('0x4e')];}else if(_0x23eb5b['body'][_0x6aac('0x4f')]){_0x2b1f76[_0x6aac('0x4f')]=_0x23eb5b['body'][_0x6aac('0x4f')];}else if(_0x23eb5b[_0x6aac('0x49')][_0x6aac('0x50')]){_0x2b1f76[_0x6aac('0x50')]=_0x23eb5b['body'][_0x6aac('0x50')];}else if(_0x23eb5b[_0x6aac('0x49')][_0x6aac('0x51')]){_0x2b1f76[_0x6aac('0x51')]=_0x23eb5b[_0x6aac('0x49')][_0x6aac('0x51')];}return db[_0x6aac('0x2e')]['find']({'where':_0x2b1f76})[_0x6aac('0x24')](function(_0xddf797){if(_0xddf797){throw new db[(_0x6aac('0x52'))][(_0x6aac('0x53'))]('The\x20inserted\x20key\x20already\x20exists.');}return db[_0x6aac('0x2e')][_0x6aac('0x4c')](_0x23eb5b[_0x6aac('0x49')]);})[_0x6aac('0x24')](function(_0x585342){var _0x20d7c1=_0x23eb5b['user'][_0x6aac('0x25')]({'plain':!![]});if(!_0x20d7c1)throw new Error(_0x6aac('0x54'));if(_0x20d7c1[_0x6aac('0x55')]===_0x6aac('0x56')){var _0x595ee9=_0x585342[_0x6aac('0x25')]({'plain':!![]});return db[_0x6aac('0x57')][_0x6aac('0x47')]({'where':{'name':_0x6aac('0x26'),'userProfileId':_0x20d7c1[_0x6aac('0x58')]},'raw':!![]})[_0x6aac('0x24')](function(_0x2b1917){if(_0x2b1917&&_0x2b1917[_0x6aac('0x59')]===0x0){return db[_0x6aac('0x27')]['create']({'name':_0x595ee9[_0x6aac('0x2a')],'resourceId':_0x595ee9['id'],'type':_0x2b1917[_0x6aac('0x2a')],'sectionId':_0x2b1917['id']},{})[_0x6aac('0x24')](function(){return _0x585342;});}else{return _0x585342;}})['catch'](function(_0x544af5){logger[_0x6aac('0x28')](_0x6aac('0x5a'),_0x544af5);throw _0x544af5;});}return _0x585342;})[_0x6aac('0x24')](respondWithResult(_0x19c9a7,0xc9))['catch'](handleError(_0x19c9a7,null));};