40ac1f4a33ffb1f221dc5fe39af92ddfa3a2355c
[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 _0xffdc=['default','custom_text','custom_dropdown','label','choices','capitalize','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','stringify','eml-format','rimraf','fast-json-patch','request-promise','bluebird','mustache','util','path','sox','ejs','fs-extra','lodash','squel','crypto','jsforce','to-csv','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../mysqldb','../../components/integrations/configuration','end','status','json','offset','undefined','limit','count','set','apply','reject','save','update','then','destroy','sendStatus','error','stack','send','FreshdeskAccount','rawAttributes','type','key','map','name','keys','filters','intersection','query','model','fields','attributes','length','nolimit','order','sort','filter','where','merge','VIRTUAL','options','includeAll','include','findAll','rows','show','params','find','catch','create','body','findOne','FreshdeskConfiguration','getConfigurations','hasOwnProperty','Subjects','getSubjects','channel','getDescriptions','FreshdeskField','getFields','remoteUri','apiKey','substring','lastIndexOf','GET','format','%s/%s','api/v2/ticket_fields','Basic\x20%s','%s:X','toString','base64','requester','subject','description','priority','group','source','company','remove','includes'];(function(_0x47be3e,_0x114ec9){var _0x25b97b=function(_0x4a8c48){while(--_0x4a8c48){_0x47be3e['push'](_0x47be3e['shift']());}};_0x25b97b(++_0x114ec9);}(_0xffdc,0x7f));var _0xcffd=function(_0x228540,_0x31248d){_0x228540=_0x228540-0x0;var _0x6e8c86=_0xffdc[_0x228540];return _0x6e8c86;};'use strict';var emlformat=require(_0xcffd('0x0'));var rimraf=require(_0xcffd('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0xcffd('0x2'));var rp=require(_0xcffd('0x3'));var moment=require('moment');var BPromise=require(_0xcffd('0x4'));var Mustache=require(_0xcffd('0x5'));var util=require(_0xcffd('0x6'));var path=require(_0xcffd('0x7'));var sox=require(_0xcffd('0x8'));var csv=require('to-csv');var ejs=require(_0xcffd('0x9'));var fs=require('fs');var fs_extra=require(_0xcffd('0xa'));var _=require(_0xcffd('0xb'));var squel=require(_0xcffd('0xc'));var crypto=require(_0xcffd('0xd'));var jsforce=require(_0xcffd('0xe'));var deskjs=require('desk.js');var toCsv=require(_0xcffd('0xf'));var querystring=require(_0xcffd('0x10'));var Papa=require('papaparse');var Redis=require(_0xcffd('0x11'));var authService=require(_0xcffd('0x12'));var qs=require(_0xcffd('0x13'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xcffd('0x14'));var logger=require(_0xcffd('0x15'))(_0xcffd('0x16'));var utils=require(_0xcffd('0x17'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0xcffd('0x18'))['db'];var integrations=require(_0xcffd('0x19'));function respondWithStatusCode(_0x1340b0,_0x4ec693){_0x4ec693=_0x4ec693||0xcc;return function(_0x25156f){if(_0x25156f){return _0x1340b0['sendStatus'](_0x4ec693);}return _0x1340b0['status'](_0x4ec693)[_0xcffd('0x1a')]();};}function respondWithResult(_0x20e134,_0x4edde4){_0x4edde4=_0x4edde4||0xc8;return function(_0x1cda29){if(_0x1cda29){return _0x20e134[_0xcffd('0x1b')](_0x4edde4)[_0xcffd('0x1c')](_0x1cda29);}};}function respondWithFilteredResult(_0x32f579,_0x4ec386){return function(_0x4921d1){if(_0x4921d1){var _0xb5ffdc=typeof _0x4ec386[_0xcffd('0x1d')]===_0xcffd('0x1e')&&typeof _0x4ec386[_0xcffd('0x1f')]===_0xcffd('0x1e');var _0x55b5db=_0x4921d1[_0xcffd('0x20')];var _0x5c3701=_0xb5ffdc?0x0:_0x4ec386[_0xcffd('0x1d')];var _0x33c713=_0xb5ffdc?_0x4921d1[_0xcffd('0x20')]:_0x4ec386[_0xcffd('0x1d')]+_0x4ec386[_0xcffd('0x1f')];var _0x1b6037;if(_0x33c713>=_0x55b5db){_0x33c713=_0x55b5db;_0x1b6037=0xc8;}else{_0x1b6037=0xce;}_0x32f579[_0xcffd('0x1b')](_0x1b6037);return _0x32f579[_0xcffd('0x21')]('Content-Range',_0x5c3701+'-'+_0x33c713+'/'+_0x55b5db)[_0xcffd('0x1c')](_0x4921d1);}return null;};}function patchUpdates(_0x1f36f4){return function(_0x3d337d){try{jsonpatch[_0xcffd('0x22')](_0x3d337d,_0x1f36f4,!![]);}catch(_0x44f32d){return BPromise[_0xcffd('0x23')](_0x44f32d);}return _0x3d337d[_0xcffd('0x24')]();};}function saveUpdates(_0x5d2d3c,_0x52fc51){return function(_0x2cc75){if(_0x2cc75){return _0x2cc75[_0xcffd('0x25')](_0x5d2d3c)[_0xcffd('0x26')](function(_0x3935ff){return _0x3935ff;});}return null;};}function removeEntity(_0x3c3839,_0x345a4d){return function(_0x2723db){if(_0x2723db){return _0x2723db[_0xcffd('0x27')]()[_0xcffd('0x26')](function(){_0x3c3839[_0xcffd('0x1b')](0xcc)[_0xcffd('0x1a')]();});}};}function handleEntityNotFound(_0x1f232a,_0x544ae7){return function(_0x102f93){if(!_0x102f93){_0x1f232a[_0xcffd('0x28')](0x194);}return _0x102f93;};}function handleError(_0x2c66a7,_0x547bdb){_0x547bdb=_0x547bdb||0x1f4;return function(_0x21bad3){logger[_0xcffd('0x29')](_0x21bad3[_0xcffd('0x2a')]);if(_0x21bad3['name']){delete _0x21bad3['name'];}_0x2c66a7[_0xcffd('0x1b')](_0x547bdb)[_0xcffd('0x2b')](_0x21bad3);};}exports['index']=function(_0x3cb0dd,_0x6641e){var _0x5a97cf={},_0x3f46ea={},_0x3ab051={'count':0x0,'rows':[]};var _0x5783d3=_['map'](db[_0xcffd('0x2c')][_0xcffd('0x2d')],function(_0x318cdf){return{'name':_0x318cdf['fieldName'],'type':_0x318cdf[_0xcffd('0x2e')][_0xcffd('0x2f')]};});_0x3f46ea['model']=_[_0xcffd('0x30')](_0x5783d3,_0xcffd('0x31'));_0x3f46ea['query']=_[_0xcffd('0x32')](_0x3cb0dd['query']);_0x3f46ea[_0xcffd('0x33')]=_[_0xcffd('0x34')](_0x3f46ea['model'],_0x3f46ea[_0xcffd('0x35')]);_0x5a97cf['attributes']=_['intersection'](_0x3f46ea[_0xcffd('0x36')],qs[_0xcffd('0x37')](_0x3cb0dd[_0xcffd('0x35')]['fields']));_0x5a97cf['attributes']=_0x5a97cf[_0xcffd('0x38')][_0xcffd('0x39')]?_0x5a97cf['attributes']:_0x3f46ea['model'];if(!_0x3cb0dd[_0xcffd('0x35')]['hasOwnProperty'](_0xcffd('0x3a'))){_0x5a97cf[_0xcffd('0x1f')]=qs[_0xcffd('0x1f')](_0x3cb0dd[_0xcffd('0x35')][_0xcffd('0x1f')]);_0x5a97cf[_0xcffd('0x1d')]=qs['offset'](_0x3cb0dd[_0xcffd('0x35')][_0xcffd('0x1d')]);}_0x5a97cf[_0xcffd('0x3b')]=qs[_0xcffd('0x3c')](_0x3cb0dd[_0xcffd('0x35')][_0xcffd('0x3c')]);_0x5a97cf['where']=qs[_0xcffd('0x33')](_['pick'](_0x3cb0dd[_0xcffd('0x35')],_0x3f46ea['filters']),_0x5783d3);if(_0x3cb0dd[_0xcffd('0x35')][_0xcffd('0x3d')]){_0x5a97cf[_0xcffd('0x3e')]=_[_0xcffd('0x3f')](_0x5a97cf[_0xcffd('0x3e')],{'$or':_[_0xcffd('0x30')](_0x5783d3,function(_0x392ee3){if(_0x392ee3['type']!==_0xcffd('0x40')){var _0x24f534={};_0x24f534[_0x392ee3[_0xcffd('0x31')]]={'$like':'%'+_0x3cb0dd['query'][_0xcffd('0x3d')]+'%'};return _0x24f534;}})});}_0x5a97cf=_[_0xcffd('0x3f')]({},_0x5a97cf,_0x3cb0dd[_0xcffd('0x41')]);var _0x47ea69={'where':_0x5a97cf['where']};return db['FreshdeskAccount'][_0xcffd('0x20')](_0x47ea69)[_0xcffd('0x26')](function(_0x3d3650){_0x3ab051['count']=_0x3d3650;if(_0x3cb0dd[_0xcffd('0x35')][_0xcffd('0x42')]){_0x5a97cf[_0xcffd('0x43')]=[{'all':!![]}];}return db[_0xcffd('0x2c')][_0xcffd('0x44')](_0x5a97cf);})[_0xcffd('0x26')](function(_0x5ec655){_0x3ab051[_0xcffd('0x45')]=_0x5ec655;return _0x3ab051;})[_0xcffd('0x26')](respondWithFilteredResult(_0x6641e,_0x5a97cf))['catch'](handleError(_0x6641e,null));};exports[_0xcffd('0x46')]=function(_0x587a53,_0x5cb0a9){var _0x23e8fc={'raw':![],'where':{'id':_0x587a53[_0xcffd('0x47')]['id']}},_0xeebe28={};_0xeebe28[_0xcffd('0x36')]=_[_0xcffd('0x32')](db[_0xcffd('0x2c')][_0xcffd('0x2d')]);_0xeebe28[_0xcffd('0x35')]=_[_0xcffd('0x32')](_0x587a53[_0xcffd('0x35')]);_0xeebe28['filters']=_[_0xcffd('0x34')](_0xeebe28[_0xcffd('0x36')],_0xeebe28['query']);_0x23e8fc['attributes']=_[_0xcffd('0x34')](_0xeebe28[_0xcffd('0x36')],qs[_0xcffd('0x37')](_0x587a53[_0xcffd('0x35')][_0xcffd('0x37')]));_0x23e8fc['attributes']=_0x23e8fc['attributes']['length']?_0x23e8fc[_0xcffd('0x38')]:_0xeebe28[_0xcffd('0x36')];if(_0x587a53[_0xcffd('0x35')][_0xcffd('0x42')]){_0x23e8fc[_0xcffd('0x43')]=[{'all':!![]}];}_0x23e8fc=_[_0xcffd('0x3f')]({},_0x23e8fc,_0x587a53[_0xcffd('0x41')]);return db[_0xcffd('0x2c')][_0xcffd('0x48')](_0x23e8fc)[_0xcffd('0x26')](handleEntityNotFound(_0x5cb0a9,null))[_0xcffd('0x26')](respondWithResult(_0x5cb0a9,null))[_0xcffd('0x49')](handleError(_0x5cb0a9,null));};exports[_0xcffd('0x4a')]=function(_0x3f23c1,_0x52f2cd){return db[_0xcffd('0x2c')]['create'](_0x3f23c1['body'],{})[_0xcffd('0x26')](respondWithResult(_0x52f2cd,0xc9))['catch'](handleError(_0x52f2cd,null));};exports[_0xcffd('0x25')]=function(_0x42b396,_0x589e6c){if(_0x42b396[_0xcffd('0x4b')]['id']){delete _0x42b396[_0xcffd('0x4b')]['id'];}return db[_0xcffd('0x2c')][_0xcffd('0x48')]({'where':{'id':_0x42b396[_0xcffd('0x47')]['id']}})['then'](handleEntityNotFound(_0x589e6c,null))[_0xcffd('0x26')](saveUpdates(_0x42b396[_0xcffd('0x4b')],null))[_0xcffd('0x26')](respondWithResult(_0x589e6c,null))[_0xcffd('0x49')](handleError(_0x589e6c,null));};exports[_0xcffd('0x27')]=function(_0x2cc53f,_0x8c341){return db['FreshdeskAccount']['find']({'where':{'id':_0x2cc53f['params']['id']}})['then'](handleEntityNotFound(_0x8c341,null))['then'](removeEntity(_0x8c341,null))[_0xcffd('0x49')](handleError(_0x8c341,null));};exports['getConfigurations']=function(_0x53b879,_0x40c270,_0x44945a){var _0xef5848={};var _0x23b75e={};var _0x229766;var _0x8f3139;return db[_0xcffd('0x2c')][_0xcffd('0x4c')]({'where':{'id':_0x53b879[_0xcffd('0x47')]['id']}})[_0xcffd('0x26')](handleEntityNotFound(_0x40c270,null))[_0xcffd('0x26')](function(_0x347d99){if(_0x347d99){_0x229766=_0x347d99;_0x23b75e[_0xcffd('0x36')]=_[_0xcffd('0x32')](db[_0xcffd('0x4d')]['rawAttributes']);_0x23b75e[_0xcffd('0x35')]=_[_0xcffd('0x32')](_0x53b879[_0xcffd('0x35')]);_0x23b75e[_0xcffd('0x33')]=_[_0xcffd('0x34')](_0x23b75e[_0xcffd('0x36')],_0x23b75e[_0xcffd('0x35')]);_0xef5848[_0xcffd('0x38')]=_[_0xcffd('0x34')](_0x23b75e[_0xcffd('0x36')],qs[_0xcffd('0x37')](_0x53b879[_0xcffd('0x35')][_0xcffd('0x37')]));_0xef5848[_0xcffd('0x38')]=_0xef5848[_0xcffd('0x38')][_0xcffd('0x39')]?_0xef5848[_0xcffd('0x38')]:_0x23b75e['model'];_0xef5848[_0xcffd('0x3b')]=qs[_0xcffd('0x3c')](_0x53b879['query']['sort']);_0xef5848[_0xcffd('0x3e')]=qs[_0xcffd('0x33')](_['pick'](_0x53b879[_0xcffd('0x35')],_0x23b75e[_0xcffd('0x33')]));if(_0x53b879[_0xcffd('0x35')]['filter']){_0xef5848[_0xcffd('0x3e')]=_[_0xcffd('0x3f')](_0xef5848[_0xcffd('0x3e')],{'$or':_[_0xcffd('0x30')](_0xef5848[_0xcffd('0x38')],function(_0x41e801){var _0x289ecf={};_0x289ecf[_0x41e801]={'$like':'%'+_0x53b879[_0xcffd('0x35')][_0xcffd('0x3d')]+'%'};return _0x289ecf;})});}_0xef5848=_[_0xcffd('0x3f')]({},_0xef5848,_0x53b879[_0xcffd('0x41')]);return _0x229766[_0xcffd('0x4e')](_0xef5848);}})[_0xcffd('0x26')](function(_0x5c0edf){if(_0x5c0edf){_0x8f3139=_0x5c0edf[_0xcffd('0x39')];if(!_0x53b879[_0xcffd('0x35')][_0xcffd('0x4f')](_0xcffd('0x3a'))){_0xef5848[_0xcffd('0x1f')]=qs[_0xcffd('0x1f')](_0x53b879[_0xcffd('0x35')][_0xcffd('0x1f')]);_0xef5848[_0xcffd('0x1d')]=qs[_0xcffd('0x1d')](_0x53b879[_0xcffd('0x35')][_0xcffd('0x1d')]);}return _0x229766[_0xcffd('0x4e')](_0xef5848);}})[_0xcffd('0x26')](function(_0x378ec9){if(_0x378ec9){return _0x378ec9?{'count':_0x8f3139,'rows':_0x378ec9}:null;}})['then'](respondWithResult(_0x40c270,null))[_0xcffd('0x49')](handleError(_0x40c270,null));};exports['addConfiguration']=function(_0x406dc5,_0x269e15,_0x7e39b0){if(_0x406dc5[_0xcffd('0x4b')]['id']){delete _0x406dc5[_0xcffd('0x4b')]['id'];}return db[_0xcffd('0x2c')][_0xcffd('0x4c')]({'where':{'id':_0x406dc5[_0xcffd('0x47')]['id']}})[_0xcffd('0x26')](handleEntityNotFound(_0x269e15,null))[_0xcffd('0x26')](function(_0x15b097){if(_0x15b097){_0x406dc5[_0xcffd('0x4b')]['AccountId']=_0x15b097['id'];_0x406dc5['body'][_0xcffd('0x50')]=integrations[_0xcffd('0x51')](_0x406dc5[_0xcffd('0x4b')][_0xcffd('0x52')],_0x406dc5[_0xcffd('0x4b')]['type']);_0x406dc5[_0xcffd('0x4b')]['Descriptions']=integrations[_0xcffd('0x53')](_0x406dc5[_0xcffd('0x4b')][_0xcffd('0x52')],_0x406dc5[_0xcffd('0x4b')][_0xcffd('0x2e')]);return db[_0xcffd('0x4d')][_0xcffd('0x4a')](_0x406dc5[_0xcffd('0x4b')],{'include':[{'model':db[_0xcffd('0x54')],'as':_0xcffd('0x50')},{'model':db[_0xcffd('0x54')],'as':'Descriptions'}]});}return null;})[_0xcffd('0x26')](respondWithResult(_0x269e15,null))[_0xcffd('0x49')](handleError(_0x269e15,null));};exports[_0xcffd('0x55')]=function(_0x2a44d0,_0x1cc2a7,_0x40b8fa){var _0x50b3fe='';return db[_0xcffd('0x2c')]['findOne']({'where':{'id':_0x2a44d0['params']['id']},'attributes':['id',_0xcffd('0x56'),_0xcffd('0x57')]})[_0xcffd('0x26')](handleEntityNotFound(_0x1cc2a7,null))['then'](function(_0x104206){if(_0x104206){_0x50b3fe=_0x104206[_0xcffd('0x56')];var _0x15637c=_0x50b3fe['slice'](-0x1);if(_0x15637c==='/'){_0x50b3fe=_0x50b3fe[_0xcffd('0x58')](0x0,_0x50b3fe[_0xcffd('0x59')](_0x15637c));}return rp({'method':_0xcffd('0x5a'),'uri':util[_0xcffd('0x5b')](_0xcffd('0x5c'),_0x50b3fe,_0xcffd('0x5d')),'headers':{'Authorization':util[_0xcffd('0x5b')](_0xcffd('0x5e'),new Buffer(util[_0xcffd('0x5b')](_0xcffd('0x5f'),_0x104206[_0xcffd('0x57')]))[_0xcffd('0x60')](_0xcffd('0x61')))},'json':!![]});}})[_0xcffd('0x26')](function(_0x539329){if(_0x539329){var _0x1044ec=[_0xcffd('0x62'),_0xcffd('0x63'),'agent',_0xcffd('0x64'),_0xcffd('0x1b'),_0xcffd('0x65'),_0xcffd('0x66'),_0xcffd('0x67'),'ticket_type',_0xcffd('0x66'),_0xcffd('0x68')];_[_0xcffd('0x69')](_0x539329,function(_0x483da9){return _0x1044ec[_0xcffd('0x6a')](_0x483da9[_0xcffd('0x31')])||!_0x483da9[_0xcffd('0x6b')]&&_0x483da9['type']!==_0xcffd('0x6c')&&_0x483da9[_0xcffd('0x2e')]!==_0xcffd('0x6d');});return{'count':_0x539329['length'],'rows':_[_0xcffd('0x30')](_0x539329,function(_0x57dc3d){return{'id':_0x57dc3d[_0xcffd('0x31')],'name':_0x57dc3d[_0xcffd('0x6e')],'custom':!_0x57dc3d['default'],'options':_0x57dc3d[_0xcffd('0x6f')]?_[_0xcffd('0x30')](_0x57dc3d[_0xcffd('0x6f')],function(_0x1876a3){return{'name':_[_0xcffd('0x70')](_0x1876a3),'value':_0x1876a3};}):[]};})};}})[_0xcffd('0x26')](respondWithResult(_0x1cc2a7,null))[_0xcffd('0x49')](function(_0xe8fcc2){var _0x4e11be=_0x2a44d0[_0xcffd('0x35')][_0xcffd('0x71')]?0x1f4:_0xe8fcc2[_0xcffd('0x72')]||0x1f4;logger['error'](_0xcffd('0x73'),_0xcffd('0x55'),_0x4e11be,JSON[_0xcffd('0x74')](_0xe8fcc2));delete _0xe8fcc2[_0xcffd('0x31')];if(_0x4e11be===0x191){_0x4e11be=0x190;}_0x1cc2a7[_0xcffd('0x1b')](_0x4e11be)[_0xcffd('0x2b')](_0x2a44d0[_0xcffd('0x35')][_0xcffd('0x71')]?{'message':'Wrong\x20credentials','statusCode':_0xe8fcc2['statusCode']}:_0xe8fcc2);});};