1e756fe79f886ff5d74cffa5160f4abdc7329cfc
[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 _0x8f37=['sort','where','filters','pick','filter','VIRTUAL','options','includeAll','findAll','show','merge','find','catch','create','body','params','findOne','length','order','getConfigurations','addConfiguration','AccountId','Subjects','getSubjects','Descriptions','getDescriptions','channel','FreshdeskConfiguration','FreshdeskField','getFields','remoteUri','apiKey','slice','substring','lastIndexOf','GET','%s/%s','api/v2/ticket_fields','format','Basic\x20%s','%s:X','toString','base64','requester','priority','source','ticket_type','group','remove','default','custom_dropdown','label','choices','statusCode','getFields,\x20%s,\x20%s,\x20%s','stringify','test','Wrong\x20credentials','eml-format','rimraf','zip-dir','moment','bluebird','mustache','path','to-csv','fs-extra','lodash','crypto','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','reject','save','update','then','destroy','error','stack','name','send','index','map','FreshdeskAccount','rawAttributes','fieldName','type','key','model','query','keys','intersection','attributes','fields','hasOwnProperty','nolimit'];(function(_0x317bf0,_0x277c03){var _0x225932=function(_0x2c280e){while(--_0x2c280e){_0x317bf0['push'](_0x317bf0['shift']());}};_0x225932(++_0x277c03);}(_0x8f37,0x190));var _0x78f3=function(_0x325972,_0x3addd4){_0x325972=_0x325972-0x0;var _0x45c18e=_0x8f37[_0x325972];return _0x45c18e;};'use strict';var emlformat=require(_0x78f3('0x0'));var rimraf=require(_0x78f3('0x1'));var zipdir=require(_0x78f3('0x2'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x78f3('0x3'));var BPromise=require(_0x78f3('0x4'));var Mustache=require(_0x78f3('0x5'));var util=require('util');var path=require(_0x78f3('0x6'));var sox=require('sox');var csv=require(_0x78f3('0x7'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x78f3('0x8'));var _=require(_0x78f3('0x9'));var squel=require('squel');var crypto=require(_0x78f3('0xa'));var jsforce=require('jsforce');var deskjs=require(_0x78f3('0xb'));var toCsv=require(_0x78f3('0x7'));var querystring=require('querystring');var Papa=require(_0x78f3('0xc'));var Redis=require(_0x78f3('0xd'));var authService=require(_0x78f3('0xe'));var qs=require(_0x78f3('0xf'));var as=require(_0x78f3('0x10'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x78f3('0x11'))(_0x78f3('0x12'));var utils=require('../../config/utils');var config=require(_0x78f3('0x13'));var licenseUtil=require(_0x78f3('0x14'));var db=require(_0x78f3('0x15'))['db'];var integrations=require('../../components/integrations/configuration');function respondWithStatusCode(_0x134063,_0x217945){_0x217945=_0x217945||0xcc;return function(_0x199fdb){if(_0x199fdb){return _0x134063[_0x78f3('0x16')](_0x217945);}return _0x134063[_0x78f3('0x17')](_0x217945)[_0x78f3('0x18')]();};}function respondWithResult(_0x33944e,_0x38db12){_0x38db12=_0x38db12||0xc8;return function(_0x1b0f4c){if(_0x1b0f4c){return _0x33944e[_0x78f3('0x17')](_0x38db12)[_0x78f3('0x19')](_0x1b0f4c);}};}function respondWithFilteredResult(_0x1e7a83,_0x3e2a15){return function(_0x453270){if(_0x453270){var _0x17172c=typeof _0x3e2a15[_0x78f3('0x1a')]===_0x78f3('0x1b')&&typeof _0x3e2a15[_0x78f3('0x1c')]===_0x78f3('0x1b');var _0x33d1c5=_0x453270[_0x78f3('0x1d')];var _0x1c25f2=_0x17172c?0x0:_0x3e2a15[_0x78f3('0x1a')];var _0x4a5db4=_0x17172c?_0x453270[_0x78f3('0x1d')]:_0x3e2a15[_0x78f3('0x1a')]+_0x3e2a15['limit'];var _0x4de869;if(_0x4a5db4>=_0x33d1c5){_0x4a5db4=_0x33d1c5;_0x4de869=0xc8;}else{_0x4de869=0xce;}_0x1e7a83['status'](_0x4de869);return _0x1e7a83[_0x78f3('0x1e')](_0x78f3('0x1f'),_0x1c25f2+'-'+_0x4a5db4+'/'+_0x33d1c5)[_0x78f3('0x19')](_0x453270);}return null;};}function patchUpdates(_0x11ee05){return function(_0x14d4ae){try{jsonpatch['apply'](_0x14d4ae,_0x11ee05,!![]);}catch(_0x369704){return BPromise[_0x78f3('0x20')](_0x369704);}return _0x14d4ae[_0x78f3('0x21')]();};}function saveUpdates(_0x3f623f,_0x2049a3){return function(_0x11de3e){if(_0x11de3e){return _0x11de3e[_0x78f3('0x22')](_0x3f623f)[_0x78f3('0x23')](function(_0x18189e){return _0x18189e;});}return null;};}function removeEntity(_0x3c6bf8,_0x5ea7fd){return function(_0x46f3a0){if(_0x46f3a0){return _0x46f3a0[_0x78f3('0x24')]()['then'](function(){_0x3c6bf8[_0x78f3('0x17')](0xcc)[_0x78f3('0x18')]();});}};}function handleEntityNotFound(_0x2d6df8,_0x43a8c8){return function(_0x1e36cf){if(!_0x1e36cf){_0x2d6df8['sendStatus'](0x194);}return _0x1e36cf;};}function handleError(_0x35ac35,_0x197837){_0x197837=_0x197837||0x1f4;return function(_0xf0607f){logger[_0x78f3('0x25')](_0xf0607f[_0x78f3('0x26')]);if(_0xf0607f['name']){delete _0xf0607f[_0x78f3('0x27')];}_0x35ac35[_0x78f3('0x17')](_0x197837)[_0x78f3('0x28')](_0xf0607f);};}exports[_0x78f3('0x29')]=function(_0x32e52f,_0x31f6c5){var _0x2de1bb={},_0x170d63={},_0x1b9c69={'count':0x0,'rows':[]};var _0x2b7308=_[_0x78f3('0x2a')](db[_0x78f3('0x2b')][_0x78f3('0x2c')],function(_0x9c0636){return{'name':_0x9c0636[_0x78f3('0x2d')],'type':_0x9c0636[_0x78f3('0x2e')][_0x78f3('0x2f')]};});_0x170d63[_0x78f3('0x30')]=_[_0x78f3('0x2a')](_0x2b7308,_0x78f3('0x27'));_0x170d63[_0x78f3('0x31')]=_[_0x78f3('0x32')](_0x32e52f[_0x78f3('0x31')]);_0x170d63['filters']=_[_0x78f3('0x33')](_0x170d63['model'],_0x170d63[_0x78f3('0x31')]);_0x2de1bb[_0x78f3('0x34')]=_[_0x78f3('0x33')](_0x170d63['model'],qs[_0x78f3('0x35')](_0x32e52f[_0x78f3('0x31')][_0x78f3('0x35')]));_0x2de1bb[_0x78f3('0x34')]=_0x2de1bb['attributes']['length']?_0x2de1bb[_0x78f3('0x34')]:_0x170d63[_0x78f3('0x30')];if(!_0x32e52f[_0x78f3('0x31')][_0x78f3('0x36')](_0x78f3('0x37'))){_0x2de1bb['limit']=qs[_0x78f3('0x1c')](_0x32e52f[_0x78f3('0x31')][_0x78f3('0x1c')]);_0x2de1bb['offset']=qs['offset'](_0x32e52f[_0x78f3('0x31')][_0x78f3('0x1a')]);}_0x2de1bb['order']=qs[_0x78f3('0x38')](_0x32e52f[_0x78f3('0x31')][_0x78f3('0x38')]);_0x2de1bb[_0x78f3('0x39')]=qs[_0x78f3('0x3a')](_[_0x78f3('0x3b')](_0x32e52f['query'],_0x170d63[_0x78f3('0x3a')]),_0x2b7308);if(_0x32e52f['query'][_0x78f3('0x3c')]){_0x2de1bb[_0x78f3('0x39')]=_['merge'](_0x2de1bb['where'],{'$or':_[_0x78f3('0x2a')](_0x2b7308,function(_0x2c26c7){if(_0x2c26c7[_0x78f3('0x2e')]!==_0x78f3('0x3d')){var _0x2e4200={};_0x2e4200[_0x2c26c7[_0x78f3('0x27')]]={'$like':'%'+_0x32e52f[_0x78f3('0x31')]['filter']+'%'};return _0x2e4200;}})});}_0x2de1bb=_['merge']({},_0x2de1bb,_0x32e52f[_0x78f3('0x3e')]);var _0x34ccb2={'where':_0x2de1bb['where']};return db['FreshdeskAccount'][_0x78f3('0x1d')](_0x34ccb2)[_0x78f3('0x23')](function(_0xdf42f7){_0x1b9c69[_0x78f3('0x1d')]=_0xdf42f7;if(_0x32e52f['query'][_0x78f3('0x3f')]){_0x2de1bb['include']=[{'all':!![]}];}return db[_0x78f3('0x2b')][_0x78f3('0x40')](_0x2de1bb);})[_0x78f3('0x23')](function(_0x5925a9){_0x1b9c69['rows']=_0x5925a9;return _0x1b9c69;})['then'](respondWithFilteredResult(_0x31f6c5,_0x2de1bb))['catch'](handleError(_0x31f6c5,null));};exports[_0x78f3('0x41')]=function(_0x4a86d8,_0x585204){var _0x29daf0={'raw':![],'where':{'id':_0x4a86d8['params']['id']}},_0x1e7935={};_0x1e7935[_0x78f3('0x30')]=_['keys'](db[_0x78f3('0x2b')][_0x78f3('0x2c')]);_0x1e7935[_0x78f3('0x31')]=_[_0x78f3('0x32')](_0x4a86d8['query']);_0x1e7935[_0x78f3('0x3a')]=_[_0x78f3('0x33')](_0x1e7935[_0x78f3('0x30')],_0x1e7935[_0x78f3('0x31')]);_0x29daf0[_0x78f3('0x34')]=_[_0x78f3('0x33')](_0x1e7935[_0x78f3('0x30')],qs[_0x78f3('0x35')](_0x4a86d8[_0x78f3('0x31')][_0x78f3('0x35')]));_0x29daf0[_0x78f3('0x34')]=_0x29daf0[_0x78f3('0x34')]['length']?_0x29daf0[_0x78f3('0x34')]:_0x1e7935[_0x78f3('0x30')];if(_0x4a86d8['query']['includeAll']){_0x29daf0['include']=[{'all':!![]}];}_0x29daf0=_[_0x78f3('0x42')]({},_0x29daf0,_0x4a86d8[_0x78f3('0x3e')]);return db['FreshdeskAccount'][_0x78f3('0x43')](_0x29daf0)[_0x78f3('0x23')](handleEntityNotFound(_0x585204,null))[_0x78f3('0x23')](respondWithResult(_0x585204,null))[_0x78f3('0x44')](handleError(_0x585204,null));};exports[_0x78f3('0x45')]=function(_0x179268,_0x2abb9e){return db[_0x78f3('0x2b')][_0x78f3('0x45')](_0x179268[_0x78f3('0x46')],{})['then'](respondWithResult(_0x2abb9e,0xc9))[_0x78f3('0x44')](handleError(_0x2abb9e,null));};exports['update']=function(_0x5cc5b1,_0x1f53fb){if(_0x5cc5b1[_0x78f3('0x46')]['id']){delete _0x5cc5b1[_0x78f3('0x46')]['id'];}return db['FreshdeskAccount'][_0x78f3('0x43')]({'where':{'id':_0x5cc5b1[_0x78f3('0x47')]['id']}})[_0x78f3('0x23')](handleEntityNotFound(_0x1f53fb,null))['then'](saveUpdates(_0x5cc5b1['body'],null))[_0x78f3('0x23')](respondWithResult(_0x1f53fb,null))[_0x78f3('0x44')](handleError(_0x1f53fb,null));};exports[_0x78f3('0x24')]=function(_0x3fc4ad,_0x372297){return db[_0x78f3('0x2b')]['find']({'where':{'id':_0x3fc4ad[_0x78f3('0x47')]['id']}})['then'](handleEntityNotFound(_0x372297,null))[_0x78f3('0x23')](removeEntity(_0x372297,null))[_0x78f3('0x44')](handleError(_0x372297,null));};exports['getConfigurations']=function(_0x30b5f4,_0x171c9b,_0x162e26){var _0x16b6e6={};var _0x418374={};var _0x3b0844;var _0x33904f;return db[_0x78f3('0x2b')][_0x78f3('0x48')]({'where':{'id':_0x30b5f4[_0x78f3('0x47')]['id']}})[_0x78f3('0x23')](handleEntityNotFound(_0x171c9b,null))[_0x78f3('0x23')](function(_0x1fbdb3){if(_0x1fbdb3){_0x3b0844=_0x1fbdb3;_0x418374[_0x78f3('0x30')]=_[_0x78f3('0x32')](db['FreshdeskConfiguration'][_0x78f3('0x2c')]);_0x418374[_0x78f3('0x31')]=_[_0x78f3('0x32')](_0x30b5f4[_0x78f3('0x31')]);_0x418374['filters']=_['intersection'](_0x418374[_0x78f3('0x30')],_0x418374[_0x78f3('0x31')]);_0x16b6e6[_0x78f3('0x34')]=_['intersection'](_0x418374['model'],qs[_0x78f3('0x35')](_0x30b5f4[_0x78f3('0x31')][_0x78f3('0x35')]));_0x16b6e6[_0x78f3('0x34')]=_0x16b6e6[_0x78f3('0x34')][_0x78f3('0x49')]?_0x16b6e6[_0x78f3('0x34')]:_0x418374['model'];_0x16b6e6[_0x78f3('0x4a')]=qs[_0x78f3('0x38')](_0x30b5f4[_0x78f3('0x31')][_0x78f3('0x38')]);_0x16b6e6[_0x78f3('0x39')]=qs[_0x78f3('0x3a')](_[_0x78f3('0x3b')](_0x30b5f4[_0x78f3('0x31')],_0x418374[_0x78f3('0x3a')]));if(_0x30b5f4[_0x78f3('0x31')][_0x78f3('0x3c')]){_0x16b6e6[_0x78f3('0x39')]=_[_0x78f3('0x42')](_0x16b6e6[_0x78f3('0x39')],{'$or':_[_0x78f3('0x2a')](_0x16b6e6[_0x78f3('0x34')],function(_0x366b35){var _0x1f9101={};_0x1f9101[_0x366b35]={'$like':'%'+_0x30b5f4[_0x78f3('0x31')][_0x78f3('0x3c')]+'%'};return _0x1f9101;})});}_0x16b6e6=_[_0x78f3('0x42')]({},_0x16b6e6,_0x30b5f4[_0x78f3('0x3e')]);return _0x3b0844[_0x78f3('0x4b')](_0x16b6e6);}})[_0x78f3('0x23')](function(_0x4b06d7){if(_0x4b06d7){_0x33904f=_0x4b06d7[_0x78f3('0x49')];if(!_0x30b5f4[_0x78f3('0x31')]['hasOwnProperty'](_0x78f3('0x37'))){_0x16b6e6['limit']=qs['limit'](_0x30b5f4[_0x78f3('0x31')]['limit']);_0x16b6e6[_0x78f3('0x1a')]=qs[_0x78f3('0x1a')](_0x30b5f4[_0x78f3('0x31')][_0x78f3('0x1a')]);}return _0x3b0844['getConfigurations'](_0x16b6e6);}})[_0x78f3('0x23')](function(_0x53feab){if(_0x53feab){return _0x53feab?{'count':_0x33904f,'rows':_0x53feab}:null;}})[_0x78f3('0x23')](respondWithResult(_0x171c9b,null))[_0x78f3('0x44')](handleError(_0x171c9b,null));};exports[_0x78f3('0x4c')]=function(_0x2d9a7c,_0x4375d8,_0x199004){if(_0x2d9a7c[_0x78f3('0x46')]['id']){delete _0x2d9a7c[_0x78f3('0x46')]['id'];}return db['FreshdeskAccount'][_0x78f3('0x48')]({'where':{'id':_0x2d9a7c['params']['id']}})[_0x78f3('0x23')](handleEntityNotFound(_0x4375d8,null))[_0x78f3('0x23')](function(_0x1489d2){if(_0x1489d2){_0x2d9a7c[_0x78f3('0x46')][_0x78f3('0x4d')]=_0x1489d2['id'];_0x2d9a7c[_0x78f3('0x46')][_0x78f3('0x4e')]=integrations[_0x78f3('0x4f')](_0x2d9a7c[_0x78f3('0x46')]['channel'],_0x2d9a7c[_0x78f3('0x46')][_0x78f3('0x2e')]);_0x2d9a7c[_0x78f3('0x46')][_0x78f3('0x50')]=integrations[_0x78f3('0x51')](_0x2d9a7c[_0x78f3('0x46')][_0x78f3('0x52')],_0x2d9a7c[_0x78f3('0x46')][_0x78f3('0x2e')]);return db[_0x78f3('0x53')]['create'](_0x2d9a7c[_0x78f3('0x46')],{'include':[{'model':db['FreshdeskField'],'as':'Subjects'},{'model':db[_0x78f3('0x54')],'as':_0x78f3('0x50')}]});}return null;})[_0x78f3('0x23')](respondWithResult(_0x4375d8,null))[_0x78f3('0x44')](handleError(_0x4375d8,null));};exports[_0x78f3('0x55')]=function(_0x449ca9,_0x4eb9a5,_0x39338d){var _0x2e1358='';return db[_0x78f3('0x2b')][_0x78f3('0x48')]({'where':{'id':_0x449ca9[_0x78f3('0x47')]['id']},'attributes':['id',_0x78f3('0x56'),_0x78f3('0x57')]})[_0x78f3('0x23')](handleEntityNotFound(_0x4eb9a5,null))[_0x78f3('0x23')](function(_0xb1bdc9){if(_0xb1bdc9){_0x2e1358=_0xb1bdc9[_0x78f3('0x56')];var _0x1da6c1=_0x2e1358[_0x78f3('0x58')](-0x1);if(_0x1da6c1==='/'){_0x2e1358=_0x2e1358[_0x78f3('0x59')](0x0,_0x2e1358[_0x78f3('0x5a')](_0x1da6c1));}return rp({'method':_0x78f3('0x5b'),'uri':util['format'](_0x78f3('0x5c'),_0x2e1358,_0x78f3('0x5d')),'headers':{'Authorization':util[_0x78f3('0x5e')](_0x78f3('0x5f'),new Buffer(util[_0x78f3('0x5e')](_0x78f3('0x60'),_0xb1bdc9[_0x78f3('0x57')]))[_0x78f3('0x61')](_0x78f3('0x62')))},'json':!![]});}})[_0x78f3('0x23')](function(_0xe0e9ed){if(_0xe0e9ed){var _0x3a69b7=[_0x78f3('0x63'),'subject','agent','description',_0x78f3('0x17'),_0x78f3('0x64'),'group',_0x78f3('0x65'),_0x78f3('0x66'),_0x78f3('0x67'),'company'];_[_0x78f3('0x68')](_0xe0e9ed,function(_0x218be3){return _0x3a69b7['includes'](_0x218be3['name'])||!_0x218be3[_0x78f3('0x69')]&&_0x218be3['type']!=='custom_text'&&_0x218be3[_0x78f3('0x2e')]!==_0x78f3('0x6a');});return{'count':_0xe0e9ed[_0x78f3('0x49')],'rows':_[_0x78f3('0x2a')](_0xe0e9ed,function(_0x55a627){return{'id':_0x55a627[_0x78f3('0x27')],'name':_0x55a627[_0x78f3('0x6b')],'custom':!_0x55a627[_0x78f3('0x69')],'options':_0x55a627[_0x78f3('0x6c')]?_[_0x78f3('0x2a')](_0x55a627[_0x78f3('0x6c')],function(_0xdbbcb3){return{'name':_['capitalize'](_0xdbbcb3),'value':_0xdbbcb3};}):[]};})};}})[_0x78f3('0x23')](respondWithResult(_0x4eb9a5,null))[_0x78f3('0x44')](function(_0x2c93f6){var _0xca83b2=_0x449ca9['query']['test']?0x1f4:_0x2c93f6[_0x78f3('0x6d')]||0x1f4;logger['error'](_0x78f3('0x6e'),_0x78f3('0x55'),_0xca83b2,JSON[_0x78f3('0x6f')](_0x2c93f6));delete _0x2c93f6['name'];if(_0xca83b2===0x191){_0xca83b2=0x190;}_0x4eb9a5['status'](_0xca83b2)['send'](_0x449ca9['query'][_0x78f3('0x70')]?{'message':_0x78f3('0x71'),'statusCode':_0x2c93f6[_0x78f3('0x6d')]}:_0x2c93f6);});};