Built motion from commit 67e5df37.|2.0.66
[motion2.git] / server / api / intFreshdeskAccount / intFreshdeskAccount.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 _0x3f9b=['index','keys','FreshdeskAccount','query','dateEnd','filters','intersection','fields','attributes','length','model','nolimit','order','sort','where','pick','dateStart','add','day','merge','filter','options','includeAll','include','rows','catch','show','params','rawAttributes','find','create','body','getConfigurations','findOne','AccountId','channel','type','getDescriptions','FreshdeskConfiguration','FreshdeskField','Subjects','remoteUri','apiKey','GET','%s%s','format','Basic\x20%s','%s:X','toString','base64','requester','description','priority','source','ticket_type','group','company','remove','default','custom_text','map','label','choices','capitalize','test','getFields','stringify','statusCode','moment','mustache','util','ejs','lodash','squel','jsforce','desk.js','to-csv','querystring','papaparse','api','../../config/utils','../../mysqldb','client','http','request','then','info','FreshdeskAccount,\x20%s,\x20%s','request\x20sent','FreshdeskAccount,\x20%s,\x20%s,\x20%s','error','code','message','result','status','end','json','count','offset','limit','set','Content-Range','apply','reject','save','update','sendStatus','name','send'];(function(_0x3c04ef,_0x9d110c){var _0x6a3a69=function(_0x333514){while(--_0x333514){_0x3c04ef['push'](_0x3c04ef['shift']());}};_0x6a3a69(++_0x9d110c);}(_0x3f9b,0x11e));var _0xb3f9=function(_0x575400,_0x34f199){_0x575400=_0x575400-0x0;var _0x29cb5c=_0x3f9b[_0x575400];return _0x29cb5c;};'use strict';var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0xb3f9('0x0'));var BPromise=require('bluebird');var Mustache=require(_0xb3f9('0x1'));var util=require(_0xb3f9('0x2'));var path=require('path');var sox=require('sox');var csv=require('to-csv');var ejs=require(_0xb3f9('0x3'));var fs=require('fs');var _=require(_0xb3f9('0x4'));var squel=require(_0xb3f9('0x5'));var crypto=require('crypto');var jsforce=require(_0xb3f9('0x6'));var deskjs=require(_0xb3f9('0x7'));var toCsv=require(_0xb3f9('0x8'));var querystring=require(_0xb3f9('0x9'));var Papa=require(_0xb3f9('0xa'));var qs=require('../../components/parsers/qs');var logger=require('../../config/logger')(_0xb3f9('0xb'));var utils=require(_0xb3f9('0xc'));var config=require('../../config/environment');var db=require(_0xb3f9('0xd'))['db'];var integrations=require('../../components/integrations/configuration');var jayson=require('jayson/promise');var client=jayson[_0xb3f9('0xe')][_0xb3f9('0xf')]({'port':0x232a});function respondWithRpcPromise(_0x22981c,_0x18d43f,_0x21de5a,_0x21f87f){return new BPromise(function(_0x535f8b,_0x1cdce2){var _0xbbc1cb=_0x21f87f||client;return _0xbbc1cb[_0xb3f9('0x10')](_0x22981c,_0x21de5a)[_0xb3f9('0x11')](function(_0x1af6c0){logger[_0xb3f9('0x12')](_0xb3f9('0x13'),_0x18d43f,_0xb3f9('0x14'));logger['debug'](_0xb3f9('0x15'),_0x18d43f,'request\x20sent',JSON['stringify'](_0x1af6c0));if(_0x1af6c0[_0xb3f9('0x16')]){if(_0x1af6c0[_0xb3f9('0x16')][_0xb3f9('0x17')]===0x1f4){logger[_0xb3f9('0x16')](_0xb3f9('0x13'),_0x18d43f,_0x1af6c0[_0xb3f9('0x16')][_0xb3f9('0x18')]);return _0x1cdce2(_0x1af6c0[_0xb3f9('0x16')]['message']);}logger[_0xb3f9('0x16')]('FreshdeskAccount,\x20%s,\x20%s',_0x18d43f,_0x1af6c0['error'][_0xb3f9('0x18')]);return _0x535f8b(_0x1af6c0['error'][_0xb3f9('0x18')]);}else{logger[_0xb3f9('0x12')](_0xb3f9('0x13'),_0x18d43f,_0xb3f9('0x14'));_0x535f8b(_0x1af6c0[_0xb3f9('0x19')][_0xb3f9('0x18')]);}})['catch'](function(_0x51b607){logger['error'](_0xb3f9('0x13'),_0x18d43f,_0x51b607);_0x1cdce2(_0x51b607);});});}function respondWithStatusCode(_0x56610c,_0x2b65cf){_0x2b65cf=_0x2b65cf||0xcc;return function(_0x4bffc7){if(_0x4bffc7){return _0x56610c['sendStatus'](_0x2b65cf);}return _0x56610c[_0xb3f9('0x1a')](_0x2b65cf)[_0xb3f9('0x1b')]();};}function respondWithResult(_0x30d548,_0x560077){_0x560077=_0x560077||0xc8;return function(_0x3b8738){if(_0x3b8738){return _0x30d548['status'](_0x560077)[_0xb3f9('0x1c')](_0x3b8738);}};}function respondWithFilteredResult(_0x1e0ab3,_0x1d3683){return function(_0x539f7a){if(_0x539f7a){var _0x4a20ea=_0x539f7a[_0xb3f9('0x1d')],_0x1ffd67=_0x1d3683[_0xb3f9('0x1e')],_0x378271=_0x1d3683['offset']+_0x1d3683[_0xb3f9('0x1f')],_0x4d70e6;if(_0x378271>=_0x4a20ea){_0x378271=_0x4a20ea;_0x4d70e6=0xc8;}else{_0x4d70e6=0xce;}_0x1e0ab3[_0xb3f9('0x1a')](_0x4d70e6);return _0x1e0ab3[_0xb3f9('0x20')](_0xb3f9('0x21'),_0x1ffd67+'-'+_0x378271+'/'+_0x4a20ea)[_0xb3f9('0x1c')](_0x539f7a);}return null;};}function patchUpdates(_0x4f3a61){return function(_0x73d0a3){try{jsonpatch[_0xb3f9('0x22')](_0x73d0a3,_0x4f3a61,!![]);}catch(_0x2939f8){return BPromise[_0xb3f9('0x23')](_0x2939f8);}return _0x73d0a3[_0xb3f9('0x24')]();};}function saveUpdates(_0x1b952f,_0xd634f7){return function(_0xb1013f){if(_0xb1013f){return _0xb1013f[_0xb3f9('0x25')](_0x1b952f)[_0xb3f9('0x11')](function(_0x2752c5){return _0x2752c5;});}return null;};}function removeEntity(_0xacbe01,_0x3e990d){return function(_0x564977){if(_0x564977){return _0x564977['destroy']()['then'](function(){_0xacbe01[_0xb3f9('0x1a')](0xcc)['end']();});}};}function handleEntityNotFound(_0x47b88a,_0x5c810c){return function(_0x2728ea){if(!_0x2728ea){_0x47b88a[_0xb3f9('0x26')](0x194);}return _0x2728ea;};}function handleError(_0x47905d,_0x1db8a6){_0x1db8a6=_0x1db8a6||0x1f4;return function(_0x19919a){logger[_0xb3f9('0x16')](_0x19919a['stack']);if(_0x19919a['name']){delete _0x19919a[_0xb3f9('0x27')];}_0x47905d['status'](_0x1db8a6)[_0xb3f9('0x28')](_0x19919a);};}exports[_0xb3f9('0x29')]=function(_0x3d50f4,_0x546796){var _0x443b8d={},_0x1e7d62={},_0x293d29={'count':0x0,'rows':[]};_0x1e7d62['model']=_[_0xb3f9('0x2a')](db[_0xb3f9('0x2b')]['rawAttributes']);var _0x21f16a={'dateStart':_0x3d50f4['query']['dateStart'],'dateEnd':_0x3d50f4[_0xb3f9('0x2c')][_0xb3f9('0x2d')]};delete _0x3d50f4['query']['dateStart'];delete _0x3d50f4[_0xb3f9('0x2c')]['dateEnd'];_0x1e7d62[_0xb3f9('0x2c')]=_[_0xb3f9('0x2a')](_0x3d50f4[_0xb3f9('0x2c')]);_0x1e7d62[_0xb3f9('0x2e')]=_['intersection'](_0x1e7d62['model'],_0x1e7d62[_0xb3f9('0x2c')]);_0x443b8d['attributes']=_[_0xb3f9('0x2f')](_0x1e7d62['model'],qs[_0xb3f9('0x30')](_0x3d50f4[_0xb3f9('0x2c')][_0xb3f9('0x30')]));_0x443b8d[_0xb3f9('0x31')]=_0x443b8d[_0xb3f9('0x31')][_0xb3f9('0x32')]?_0x443b8d[_0xb3f9('0x31')]:_0x1e7d62[_0xb3f9('0x33')];if(!_0x3d50f4[_0xb3f9('0x2c')]['hasOwnProperty'](_0xb3f9('0x34'))){_0x443b8d[_0xb3f9('0x1f')]=qs[_0xb3f9('0x1f')](_0x3d50f4[_0xb3f9('0x2c')][_0xb3f9('0x1f')]);_0x443b8d[_0xb3f9('0x1e')]=qs[_0xb3f9('0x1e')](_0x3d50f4['query'][_0xb3f9('0x1e')]);}_0x443b8d[_0xb3f9('0x35')]=qs[_0xb3f9('0x36')](_0x3d50f4[_0xb3f9('0x2c')][_0xb3f9('0x36')]);_0x443b8d[_0xb3f9('0x37')]=qs[_0xb3f9('0x2e')](_[_0xb3f9('0x38')](_0x3d50f4['query'],_0x1e7d62[_0xb3f9('0x2e')]));if(_0x21f16a[_0xb3f9('0x39')]){if(_0x21f16a[_0xb3f9('0x2d')]){_0x21f16a['dateEnd']=moment(_0x21f16a[_0xb3f9('0x2d')])[_0xb3f9('0x3a')](0x1,_0xb3f9('0x3b'));}else{_0x21f16a[_0xb3f9('0x2d')]=moment(_0x21f16a['dateStart'])[_0xb3f9('0x3a')](0x1,'day');}_[_0xb3f9('0x3c')](_0x443b8d[_0xb3f9('0x37')],{'createdAt':{'$gte':_0x21f16a[_0xb3f9('0x39')],'$lte':_0x21f16a[_0xb3f9('0x2d')]}});}if(_0x3d50f4[_0xb3f9('0x2c')][_0xb3f9('0x3d')]){_0x443b8d[_0xb3f9('0x37')]=_[_0xb3f9('0x3c')](_0x443b8d[_0xb3f9('0x37')],{'$or':_['map'](_0x443b8d['attributes'],function(_0x8c9632){var _0x348139={};_0x348139[_0x8c9632]={'$like':'%'+_0x3d50f4['query'][_0xb3f9('0x3d')]+'%'};return _0x348139;})});}_0x443b8d=_[_0xb3f9('0x3c')]({},_0x443b8d,_0x3d50f4[_0xb3f9('0x3e')]);var _0x4c5d29={'where':_0x443b8d[_0xb3f9('0x37')]};return db['FreshdeskAccount'][_0xb3f9('0x1d')](_0x4c5d29)['then'](function(_0x1e37d4){_0x293d29[_0xb3f9('0x1d')]=_0x1e37d4;if(_0x3d50f4[_0xb3f9('0x2c')][_0xb3f9('0x3f')]){_0x443b8d[_0xb3f9('0x40')]=[{'all':!![]}];}return db[_0xb3f9('0x2b')]['findAll'](_0x443b8d);})[_0xb3f9('0x11')](function(_0xe78dc5){_0x293d29[_0xb3f9('0x41')]=_0xe78dc5;return _0x293d29;})[_0xb3f9('0x11')](respondWithFilteredResult(_0x546796,_0x443b8d))[_0xb3f9('0x42')](handleError(_0x546796,null));};exports[_0xb3f9('0x43')]=function(_0x183b48,_0x16709b){var _0x245ddd={'raw':![],'where':{'id':_0x183b48[_0xb3f9('0x44')]['id']}},_0x5d0e91={};_0x5d0e91[_0xb3f9('0x33')]=_[_0xb3f9('0x2a')](db['FreshdeskAccount'][_0xb3f9('0x45')]);_0x5d0e91['query']=_[_0xb3f9('0x2a')](_0x183b48[_0xb3f9('0x2c')]);_0x5d0e91[_0xb3f9('0x2e')]=_[_0xb3f9('0x2f')](_0x5d0e91['model'],_0x5d0e91[_0xb3f9('0x2c')]);_0x245ddd[_0xb3f9('0x31')]=_[_0xb3f9('0x2f')](_0x5d0e91[_0xb3f9('0x33')],qs[_0xb3f9('0x30')](_0x183b48['query'][_0xb3f9('0x30')]));_0x245ddd[_0xb3f9('0x31')]=_0x245ddd[_0xb3f9('0x31')][_0xb3f9('0x32')]?_0x245ddd[_0xb3f9('0x31')]:_0x5d0e91[_0xb3f9('0x33')];if(_0x183b48[_0xb3f9('0x2c')]['includeAll']){_0x245ddd[_0xb3f9('0x40')]=[{'all':!![]}];}_0x245ddd=_[_0xb3f9('0x3c')]({},_0x245ddd,_0x183b48[_0xb3f9('0x3e')]);return db[_0xb3f9('0x2b')][_0xb3f9('0x46')](_0x245ddd)['then'](handleEntityNotFound(_0x16709b,null))[_0xb3f9('0x11')](respondWithResult(_0x16709b,null))[_0xb3f9('0x42')](handleError(_0x16709b,null));};exports[_0xb3f9('0x47')]=function(_0x1214ce,_0x565513){return db[_0xb3f9('0x2b')][_0xb3f9('0x47')](_0x1214ce[_0xb3f9('0x48')],{})['then'](respondWithResult(_0x565513,0xc9))[_0xb3f9('0x42')](handleError(_0x565513,null));};exports[_0xb3f9('0x25')]=function(_0x966388,_0x513c02){if(_0x966388[_0xb3f9('0x48')]['id']){delete _0x966388[_0xb3f9('0x48')]['id'];}return db[_0xb3f9('0x2b')][_0xb3f9('0x46')]({'where':{'id':_0x966388[_0xb3f9('0x44')]['id']}})[_0xb3f9('0x11')](handleEntityNotFound(_0x513c02,null))[_0xb3f9('0x11')](saveUpdates(_0x966388[_0xb3f9('0x48')],null))[_0xb3f9('0x11')](respondWithResult(_0x513c02,null))[_0xb3f9('0x42')](handleError(_0x513c02,null));};exports['destroy']=function(_0x161509,_0x329658){return db[_0xb3f9('0x2b')][_0xb3f9('0x46')]({'where':{'id':_0x161509[_0xb3f9('0x44')]['id']}})[_0xb3f9('0x11')](handleEntityNotFound(_0x329658,null))[_0xb3f9('0x11')](removeEntity(_0x329658,null))['catch'](handleError(_0x329658,null));};exports[_0xb3f9('0x49')]=function(_0x8c1afa,_0x3815bc,_0x1dd3f3){var _0x412f46={};var _0x5d8c9f={};var _0x203afc;var _0x5a0e65;return db[_0xb3f9('0x2b')][_0xb3f9('0x4a')]({'where':{'id':_0x8c1afa[_0xb3f9('0x44')]['id']}})[_0xb3f9('0x11')](handleEntityNotFound(_0x3815bc,null))[_0xb3f9('0x11')](function(_0x43f449){if(_0x43f449){_0x203afc=_0x43f449;_0x5d8c9f['model']=_[_0xb3f9('0x2a')](db['FreshdeskConfiguration'][_0xb3f9('0x45')]);_0x5d8c9f[_0xb3f9('0x2c')]=_[_0xb3f9('0x2a')](_0x8c1afa[_0xb3f9('0x2c')]);_0x5d8c9f[_0xb3f9('0x2e')]=_[_0xb3f9('0x2f')](_0x5d8c9f[_0xb3f9('0x33')],_0x5d8c9f['query']);_0x412f46[_0xb3f9('0x31')]=_[_0xb3f9('0x2f')](_0x5d8c9f[_0xb3f9('0x33')],qs['fields'](_0x8c1afa[_0xb3f9('0x2c')][_0xb3f9('0x30')]));_0x412f46[_0xb3f9('0x31')]=_0x412f46[_0xb3f9('0x31')][_0xb3f9('0x32')]?_0x412f46['attributes']:_0x5d8c9f['model'];_0x412f46[_0xb3f9('0x35')]=qs['sort'](_0x8c1afa['query'][_0xb3f9('0x36')]);_0x412f46['where']=qs['filters'](_['pick'](_0x8c1afa[_0xb3f9('0x2c')],_0x5d8c9f['filters']));if(_0x8c1afa['query']['filter']){_0x412f46['where']=_['merge'](_0x412f46[_0xb3f9('0x37')],{'$or':_['map'](_0x412f46[_0xb3f9('0x31')],function(_0x11ebac){var _0x5e5ade={};_0x5e5ade[_0x11ebac]={'$like':'%'+_0x8c1afa[_0xb3f9('0x2c')]['filter']+'%'};return _0x5e5ade;})});}_0x412f46=_[_0xb3f9('0x3c')]({},_0x412f46,_0x8c1afa[_0xb3f9('0x3e')]);return _0x203afc[_0xb3f9('0x49')](_0x412f46);}})[_0xb3f9('0x11')](function(_0x199077){if(_0x199077){_0x5a0e65=_0x199077['length'];if(!_0x8c1afa[_0xb3f9('0x2c')]['hasOwnProperty'](_0xb3f9('0x34'))){_0x412f46['limit']=qs[_0xb3f9('0x1f')](_0x8c1afa[_0xb3f9('0x2c')][_0xb3f9('0x1f')]);_0x412f46[_0xb3f9('0x1e')]=qs['offset'](_0x8c1afa[_0xb3f9('0x2c')][_0xb3f9('0x1e')]);}return _0x203afc[_0xb3f9('0x49')](_0x412f46);}})[_0xb3f9('0x11')](function(_0x2b1688){if(_0x2b1688){return _0x2b1688?{'count':_0x5a0e65,'rows':_0x2b1688}:null;}})['then'](respondWithResult(_0x3815bc,null))[_0xb3f9('0x42')](handleError(_0x3815bc,null));};exports['addConfiguration']=function(_0x14c39c,_0x484379,_0x162a09){if(_0x14c39c[_0xb3f9('0x48')]['id']){delete _0x14c39c[_0xb3f9('0x48')]['id'];}return db[_0xb3f9('0x2b')][_0xb3f9('0x4a')]({'where':{'id':_0x14c39c[_0xb3f9('0x44')]['id']}})[_0xb3f9('0x11')](handleEntityNotFound(_0x484379,null))[_0xb3f9('0x11')](function(_0x4a2183){if(_0x4a2183){_0x14c39c[_0xb3f9('0x48')][_0xb3f9('0x4b')]=_0x4a2183['id'];_0x14c39c['body']['Subjects']=integrations['getSubjects'](_0x14c39c[_0xb3f9('0x48')][_0xb3f9('0x4c')],_0x14c39c['body'][_0xb3f9('0x4d')]);_0x14c39c['body']['Descriptions']=integrations[_0xb3f9('0x4e')](_0x14c39c[_0xb3f9('0x48')]['channel'],_0x14c39c[_0xb3f9('0x48')][_0xb3f9('0x4d')]);return db[_0xb3f9('0x4f')][_0xb3f9('0x47')](_0x14c39c[_0xb3f9('0x48')],{'include':[{'model':db[_0xb3f9('0x50')],'as':_0xb3f9('0x51')},{'model':db[_0xb3f9('0x50')],'as':'Descriptions'}]});}return null;})[_0xb3f9('0x11')](respondWithResult(_0x484379,null))[_0xb3f9('0x42')](handleError(_0x484379,null));};exports['getFields']=function(_0x467bd2,_0x20c882,_0x2f9117){return db[_0xb3f9('0x2b')]['findOne']({'where':{'id':_0x467bd2[_0xb3f9('0x44')]['id']},'attributes':['id',_0xb3f9('0x52'),_0xb3f9('0x53')]})[_0xb3f9('0x11')](handleEntityNotFound(_0x20c882,null))[_0xb3f9('0x11')](function(_0x24bccf){if(_0x24bccf){return rp({'method':_0xb3f9('0x54'),'uri':util['format'](_0xb3f9('0x55'),_0x24bccf['remoteUri'],'api/v2/ticket_fields'),'headers':{'Authorization':util[_0xb3f9('0x56')](_0xb3f9('0x57'),new Buffer(util[_0xb3f9('0x56')](_0xb3f9('0x58'),_0x24bccf[_0xb3f9('0x53')]))[_0xb3f9('0x59')](_0xb3f9('0x5a')))},'json':!![]});}})['then'](function(_0x6f3d0c){if(_0x6f3d0c){var _0x24d0ab=[_0xb3f9('0x5b'),'subject','agent',_0xb3f9('0x5c'),_0xb3f9('0x1a'),_0xb3f9('0x5d'),'group',_0xb3f9('0x5e'),_0xb3f9('0x5f'),_0xb3f9('0x60'),_0xb3f9('0x61')];_[_0xb3f9('0x62')](_0x6f3d0c,function(_0x3c1ac5){return _0x24d0ab['includes'](_0x3c1ac5[_0xb3f9('0x27')])||!_0x3c1ac5[_0xb3f9('0x63')]&&_0x3c1ac5[_0xb3f9('0x4d')]!==_0xb3f9('0x64');});return{'count':_0x6f3d0c[_0xb3f9('0x32')],'rows':_[_0xb3f9('0x65')](_0x6f3d0c,function(_0x8b84c6){return{'id':_0x8b84c6[_0xb3f9('0x27')],'name':_0x8b84c6[_0xb3f9('0x66')],'custom':!_0x8b84c6[_0xb3f9('0x63')],'options':_0x8b84c6[_0xb3f9('0x67')]?_['map'](_0x8b84c6['choices'],function(_0x13790a){return{'name':_[_0xb3f9('0x68')](_0x13790a),'value':_0x13790a};}):[]};})};}})[_0xb3f9('0x11')](respondWithResult(_0x20c882,null))['catch'](function(_0x567db5){var _0x4aebad=_0x467bd2[_0xb3f9('0x2c')][_0xb3f9('0x69')]?0x1f4:_0x567db5['statusCode']||0x1f4;logger[_0xb3f9('0x16')]('getFields,\x20%s,\x20%s,\x20%s',_0xb3f9('0x6a'),_0x4aebad,JSON[_0xb3f9('0x6b')](_0x567db5));delete _0x567db5['name'];if(_0x4aebad===0x191){_0x4aebad=0x190;}_0x20c882[_0xb3f9('0x1a')](_0x4aebad)[_0xb3f9('0x28')](_0x467bd2[_0xb3f9('0x2c')][_0xb3f9('0x69')]?{'message':'Wrong\x20credentials','statusCode':_0x567db5[_0xb3f9('0x6c')]}:_0x567db5);});};