Built motion from commit 67e5df37.|2.0.66
[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 _0xf0f6=['includeAll','options','find','body','key','MailAccountId','SmsAccountId','OpenchannelAccountId','ChatWebsiteId','Sequelize','ValidationError','The\x20inserted\x20key\x20already\x20exists.','create','request-promise','moment','bluebird','util','path','sox','to-csv','ejs','lodash','crypto','jsforce','desk.js','papaparse','../../components/parsers/qs','../../config/logger','api','../../config/utils','../../mysqldb','sendStatus','end','status','json','count','offset','limit','set','Content-Range','apply','update','destroy','error','stack','name','send','index','model','keys','CannedAnswer','rawAttributes','dateStart','query','dateEnd','filters','intersection','attributes','fields','nolimit','sort','where','pick','add','day','merge','filter','then','include','rows','catch','show','params'];(function(_0x27cbc4,_0xec2fa6){var _0x31ca88=function(_0x56145e){while(--_0x56145e){_0x27cbc4['push'](_0x27cbc4['shift']());}};_0x31ca88(++_0xec2fa6);}(_0xf0f6,0xe8));var _0x6f0f=function(_0x2bcac7,_0x27467d){_0x2bcac7=_0x2bcac7-0x0;var _0xe39587=_0xf0f6[_0x2bcac7];return _0xe39587;};'use strict';var jsonpatch=require('fast-json-patch');var rp=require(_0x6f0f('0x0'));var moment=require(_0x6f0f('0x1'));var BPromise=require(_0x6f0f('0x2'));var Mustache=require('mustache');var util=require(_0x6f0f('0x3'));var path=require(_0x6f0f('0x4'));var sox=require(_0x6f0f('0x5'));var csv=require(_0x6f0f('0x6'));var ejs=require(_0x6f0f('0x7'));var fs=require('fs');var _=require(_0x6f0f('0x8'));var squel=require('squel');var crypto=require(_0x6f0f('0x9'));var jsforce=require(_0x6f0f('0xa'));var deskjs=require(_0x6f0f('0xb'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require(_0x6f0f('0xc'));var qs=require(_0x6f0f('0xd'));var logger=require(_0x6f0f('0xe'))(_0x6f0f('0xf'));var utils=require(_0x6f0f('0x10'));var config=require('../../config/environment');var db=require(_0x6f0f('0x11'))['db'];function respondWithStatusCode(_0x203e3b,_0x43f396){_0x43f396=_0x43f396||0xcc;return function(_0x18011e){if(_0x18011e){return _0x203e3b[_0x6f0f('0x12')](_0x43f396);}return _0x203e3b['status'](_0x43f396)[_0x6f0f('0x13')]();};}function respondWithResult(_0x59a066,_0x1419e2){_0x1419e2=_0x1419e2||0xc8;return function(_0xb44ef0){if(_0xb44ef0){return _0x59a066[_0x6f0f('0x14')](_0x1419e2)[_0x6f0f('0x15')](_0xb44ef0);}};}function respondWithFilteredResult(_0x14f4d4,_0xda8e2a){return function(_0x42c927){if(_0x42c927){var _0x4652ea=_0x42c927[_0x6f0f('0x16')],_0xf3b183=_0xda8e2a[_0x6f0f('0x17')],_0x7f2980=_0xda8e2a['offset']+_0xda8e2a[_0x6f0f('0x18')],_0x4426bb;if(_0x7f2980>=_0x4652ea){_0x7f2980=_0x4652ea;_0x4426bb=0xc8;}else{_0x4426bb=0xce;}_0x14f4d4[_0x6f0f('0x14')](_0x4426bb);return _0x14f4d4[_0x6f0f('0x19')](_0x6f0f('0x1a'),_0xf3b183+'-'+_0x7f2980+'/'+_0x4652ea)[_0x6f0f('0x15')](_0x42c927);}return null;};}function patchUpdates(_0x11dad2){return function(_0x16d74d){try{jsonpatch[_0x6f0f('0x1b')](_0x16d74d,_0x11dad2,!![]);}catch(_0x2aeb11){return BPromise['reject'](_0x2aeb11);}return _0x16d74d['save']();};}function saveUpdates(_0x7fdc02,_0x3a6512){return function(_0x499145){if(_0x499145){return _0x499145[_0x6f0f('0x1c')](_0x7fdc02)['then'](function(_0x22fb87){return _0x22fb87;});}return null;};}function removeEntity(_0x130b7e,_0x1f8025){return function(_0x54cbde){if(_0x54cbde){return _0x54cbde[_0x6f0f('0x1d')]()['then'](function(){_0x130b7e[_0x6f0f('0x14')](0xcc)['end']();});}};}function handleEntityNotFound(_0x230baa,_0x555f56){return function(_0x2e34a1){if(!_0x2e34a1){_0x230baa['sendStatus'](0x194);}return _0x2e34a1;};}function handleError(_0x2efe5b,_0x5df092){_0x5df092=_0x5df092||0x1f4;return function(_0x5d3ee1){logger[_0x6f0f('0x1e')](_0x5d3ee1[_0x6f0f('0x1f')]);if(_0x5d3ee1[_0x6f0f('0x20')]){delete _0x5d3ee1[_0x6f0f('0x20')];}_0x2efe5b[_0x6f0f('0x14')](_0x5df092)[_0x6f0f('0x21')](_0x5d3ee1);};}exports[_0x6f0f('0x22')]=function(_0x2b2ea5,_0xc7d6fe){var _0x226756={},_0x32441e={},_0x6004ab={'count':0x0,'rows':[]};_0x32441e[_0x6f0f('0x23')]=_[_0x6f0f('0x24')](db[_0x6f0f('0x25')][_0x6f0f('0x26')]);var _0x5686dc={'dateStart':_0x2b2ea5['query'][_0x6f0f('0x27')],'dateEnd':_0x2b2ea5[_0x6f0f('0x28')][_0x6f0f('0x29')]};delete _0x2b2ea5[_0x6f0f('0x28')][_0x6f0f('0x27')];delete _0x2b2ea5[_0x6f0f('0x28')][_0x6f0f('0x29')];_0x32441e[_0x6f0f('0x28')]=_[_0x6f0f('0x24')](_0x2b2ea5[_0x6f0f('0x28')]);_0x32441e[_0x6f0f('0x2a')]=_[_0x6f0f('0x2b')](_0x32441e[_0x6f0f('0x23')],_0x32441e[_0x6f0f('0x28')]);_0x226756[_0x6f0f('0x2c')]=_[_0x6f0f('0x2b')](_0x32441e[_0x6f0f('0x23')],qs[_0x6f0f('0x2d')](_0x2b2ea5[_0x6f0f('0x28')]['fields']));_0x226756['attributes']=_0x226756['attributes']['length']?_0x226756['attributes']:_0x32441e['model'];if(!_0x2b2ea5[_0x6f0f('0x28')]['hasOwnProperty'](_0x6f0f('0x2e'))){_0x226756[_0x6f0f('0x18')]=qs['limit'](_0x2b2ea5[_0x6f0f('0x28')][_0x6f0f('0x18')]);_0x226756[_0x6f0f('0x17')]=qs[_0x6f0f('0x17')](_0x2b2ea5['query'][_0x6f0f('0x17')]);}_0x226756['order']=qs[_0x6f0f('0x2f')](_0x2b2ea5[_0x6f0f('0x28')][_0x6f0f('0x2f')]);_0x226756[_0x6f0f('0x30')]=qs['filters'](_[_0x6f0f('0x31')](_0x2b2ea5[_0x6f0f('0x28')],_0x32441e[_0x6f0f('0x2a')]));if(_0x5686dc[_0x6f0f('0x27')]){if(_0x5686dc[_0x6f0f('0x29')]){_0x5686dc['dateEnd']=moment(_0x5686dc[_0x6f0f('0x29')])[_0x6f0f('0x32')](0x1,_0x6f0f('0x33'));}else{_0x5686dc[_0x6f0f('0x29')]=moment(_0x5686dc[_0x6f0f('0x27')])[_0x6f0f('0x32')](0x1,_0x6f0f('0x33'));}_[_0x6f0f('0x34')](_0x226756[_0x6f0f('0x30')],{'createdAt':{'$gte':_0x5686dc[_0x6f0f('0x27')],'$lte':_0x5686dc[_0x6f0f('0x29')]}});}if(_0x2b2ea5[_0x6f0f('0x28')]['filter']){_0x226756[_0x6f0f('0x30')]=_[_0x6f0f('0x34')](_0x226756[_0x6f0f('0x30')],{'$or':_['map'](_0x226756[_0x6f0f('0x2c')],function(_0x877d6e){var _0x439b={};_0x439b[_0x877d6e]={'$like':'%'+_0x2b2ea5[_0x6f0f('0x28')][_0x6f0f('0x35')]+'%'};return _0x439b;})});}_0x226756=_[_0x6f0f('0x34')]({},_0x226756,_0x2b2ea5['options']);var _0x3d81b2={'where':_0x226756[_0x6f0f('0x30')]};return db[_0x6f0f('0x25')][_0x6f0f('0x16')](_0x3d81b2)[_0x6f0f('0x36')](function(_0x29a8fb){_0x6004ab[_0x6f0f('0x16')]=_0x29a8fb;if(_0x2b2ea5[_0x6f0f('0x28')]['includeAll']){_0x226756[_0x6f0f('0x37')]=[{'all':!![]}];}return db['CannedAnswer']['findAll'](_0x226756);})[_0x6f0f('0x36')](function(_0x220fac){_0x6004ab[_0x6f0f('0x38')]=_0x220fac;return _0x6004ab;})[_0x6f0f('0x36')](respondWithFilteredResult(_0xc7d6fe,_0x226756))[_0x6f0f('0x39')](handleError(_0xc7d6fe,null));};exports[_0x6f0f('0x3a')]=function(_0x31f149,_0x1c41e6){var _0x40dc1d={'raw':!![],'where':{'id':_0x31f149[_0x6f0f('0x3b')]['id']}},_0x2be5e1={};_0x2be5e1[_0x6f0f('0x23')]=_[_0x6f0f('0x24')](db[_0x6f0f('0x25')][_0x6f0f('0x26')]);_0x2be5e1[_0x6f0f('0x28')]=_['keys'](_0x31f149['query']);_0x2be5e1[_0x6f0f('0x2a')]=_['intersection'](_0x2be5e1[_0x6f0f('0x23')],_0x2be5e1['query']);_0x40dc1d[_0x6f0f('0x2c')]=_['intersection'](_0x2be5e1['model'],qs[_0x6f0f('0x2d')](_0x31f149[_0x6f0f('0x28')][_0x6f0f('0x2d')]));_0x40dc1d[_0x6f0f('0x2c')]=_0x40dc1d[_0x6f0f('0x2c')]['length']?_0x40dc1d[_0x6f0f('0x2c')]:_0x2be5e1[_0x6f0f('0x23')];if(_0x31f149[_0x6f0f('0x28')][_0x6f0f('0x3c')]){_0x40dc1d[_0x6f0f('0x37')]=[{'all':!![]}];}_0x40dc1d=_[_0x6f0f('0x34')]({},_0x40dc1d,_0x31f149[_0x6f0f('0x3d')]);return db[_0x6f0f('0x25')][_0x6f0f('0x3e')](_0x40dc1d)['then'](handleEntityNotFound(_0x1c41e6,null))[_0x6f0f('0x36')](respondWithResult(_0x1c41e6,null))[_0x6f0f('0x39')](handleError(_0x1c41e6,null));};exports[_0x6f0f('0x1c')]=function(_0x157980,_0x32b5f5){if(_0x157980['body']['id']){delete _0x157980[_0x6f0f('0x3f')]['id'];}return db['CannedAnswer'][_0x6f0f('0x3e')]({'where':{'id':_0x157980['params']['id']}})[_0x6f0f('0x36')](handleEntityNotFound(_0x32b5f5,null))[_0x6f0f('0x36')](saveUpdates(_0x157980[_0x6f0f('0x3f')],null))['then'](respondWithResult(_0x32b5f5,null))[_0x6f0f('0x39')](handleError(_0x32b5f5,null));};exports[_0x6f0f('0x1d')]=function(_0x5012b3,_0x1fcafc){return db[_0x6f0f('0x25')][_0x6f0f('0x3e')]({'where':{'id':_0x5012b3[_0x6f0f('0x3b')]['id']}})[_0x6f0f('0x36')](handleEntityNotFound(_0x1fcafc,null))[_0x6f0f('0x36')](removeEntity(_0x1fcafc,null))[_0x6f0f('0x39')](handleError(_0x1fcafc,null));};exports['create']=function(_0x4096f7,_0x2c91ea){var _0xb4353f={'key':_0x4096f7[_0x6f0f('0x3f')][_0x6f0f('0x40')]};if(_0x4096f7[_0x6f0f('0x3f')][_0x6f0f('0x41')]){_0xb4353f[_0x6f0f('0x41')]=_0x4096f7[_0x6f0f('0x3f')][_0x6f0f('0x41')];}else if(_0x4096f7[_0x6f0f('0x3f')]['SmsAccountId']){_0xb4353f[_0x6f0f('0x42')]=_0x4096f7[_0x6f0f('0x3f')]['SmsAccountId'];}else if(_0x4096f7[_0x6f0f('0x3f')][_0x6f0f('0x43')]){_0xb4353f[_0x6f0f('0x43')]=_0x4096f7[_0x6f0f('0x3f')][_0x6f0f('0x43')];}else if(_0x4096f7[_0x6f0f('0x3f')][_0x6f0f('0x44')]){_0xb4353f[_0x6f0f('0x44')]=_0x4096f7[_0x6f0f('0x3f')][_0x6f0f('0x44')];}return db[_0x6f0f('0x25')][_0x6f0f('0x3e')]({'where':_0xb4353f})[_0x6f0f('0x36')](function(_0x2631a4){if(_0x2631a4){throw new db[(_0x6f0f('0x45'))][(_0x6f0f('0x46'))](_0x6f0f('0x47'));}return db[_0x6f0f('0x25')][_0x6f0f('0x48')](_0x4096f7[_0x6f0f('0x3f')]);})[_0x6f0f('0x36')](respondWithResult(_0x2c91ea,0xc9))[_0x6f0f('0x39')](handleError(_0x2c91ea,null));};