f7bd6b20ddfa591d9c9489a81fa99cfdf7540e8e
[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 _0xda3f=['choices','capitalize','test','statusCode','getFields,\x20%s,\x20%s,\x20%s','getFields','stringify','send','rimraf','zip-dir','bluebird','util','path','to-csv','ejs','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','offset','undefined','limit','count','status','set','Content-Range','apply','save','update','then','destroy','error','stack','name','index','rawAttributes','type','key','model','map','query','keys','filters','attributes','intersection','fields','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','FreshdeskAccount','includeAll','include','params','length','find','catch','create','body','getConfigurations','findOne','FreshdeskConfiguration','options','AccountId','Subjects','getSubjects','Descriptions','getDescriptions','channel','remoteUri','apiKey','slice','substring','lastIndexOf','GET','%s/%s','api/v2/ticket_fields','format','Basic\x20%s','%s:X','toString','base64','requester','subject','description','priority','group','source','ticket_type','company','remove','includes','custom_text','custom_dropdown','label','default'];(function(_0x36a1a0,_0x31c0f7){var _0x34192c=function(_0x3772dc){while(--_0x3772dc){_0x36a1a0['push'](_0x36a1a0['shift']());}};_0x34192c(++_0x31c0f7);}(_0xda3f,0xec));var _0xfda3=function(_0x530867,_0x2663af){_0x530867=_0x530867-0x0;var _0x4c78f3=_0xda3f[_0x530867];return _0x4c78f3;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0xfda3('0x0'));var zipdir=require(_0xfda3('0x1'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0xfda3('0x2'));var Mustache=require('mustache');var util=require(_0xfda3('0x3'));var path=require(_0xfda3('0x4'));var sox=require('sox');var csv=require(_0xfda3('0x5'));var ejs=require(_0xfda3('0x6'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require('lodash');var squel=require(_0xfda3('0x7'));var crypto=require(_0xfda3('0x8'));var jsforce=require(_0xfda3('0x9'));var deskjs=require(_0xfda3('0xa'));var toCsv=require(_0xfda3('0x5'));var querystring=require(_0xfda3('0xb'));var Papa=require(_0xfda3('0xc'));var Redis=require(_0xfda3('0xd'));var authService=require(_0xfda3('0xe'));var qs=require(_0xfda3('0xf'));var as=require(_0xfda3('0x10'));var hardwareService=require(_0xfda3('0x11'));var logger=require('../../config/logger')(_0xfda3('0x12'));var utils=require(_0xfda3('0x13'));var config=require(_0xfda3('0x14'));var licenseUtil=require(_0xfda3('0x15'));var db=require(_0xfda3('0x16'))['db'];var integrations=require('../../components/integrations/configuration');function respondWithStatusCode(_0x583125,_0x5d7d5b){_0x5d7d5b=_0x5d7d5b||0xcc;return function(_0x3fd031){if(_0x3fd031){return _0x583125[_0xfda3('0x17')](_0x5d7d5b);}return _0x583125['status'](_0x5d7d5b)['end']();};}function respondWithResult(_0x587e9b,_0x227d72){_0x227d72=_0x227d72||0xc8;return function(_0x477fc0){if(_0x477fc0){return _0x587e9b['status'](_0x227d72)['json'](_0x477fc0);}};}function respondWithFilteredResult(_0x533365,_0x80ea80){return function(_0x474707){if(_0x474707){var _0x249dc6=typeof _0x80ea80[_0xfda3('0x18')]===_0xfda3('0x19')&&typeof _0x80ea80[_0xfda3('0x1a')]===_0xfda3('0x19');var _0x93c8ef=_0x474707[_0xfda3('0x1b')];var _0xc62ae6=_0x249dc6?0x0:_0x80ea80['offset'];var _0x15ddb9=_0x249dc6?_0x474707[_0xfda3('0x1b')]:_0x80ea80[_0xfda3('0x18')]+_0x80ea80[_0xfda3('0x1a')];var _0x59a887;if(_0x15ddb9>=_0x93c8ef){_0x15ddb9=_0x93c8ef;_0x59a887=0xc8;}else{_0x59a887=0xce;}_0x533365[_0xfda3('0x1c')](_0x59a887);return _0x533365[_0xfda3('0x1d')](_0xfda3('0x1e'),_0xc62ae6+'-'+_0x15ddb9+'/'+_0x93c8ef)['json'](_0x474707);}return null;};}function patchUpdates(_0x5ad52a){return function(_0xa0449d){try{jsonpatch[_0xfda3('0x1f')](_0xa0449d,_0x5ad52a,!![]);}catch(_0x4ea9f5){return BPromise['reject'](_0x4ea9f5);}return _0xa0449d[_0xfda3('0x20')]();};}function saveUpdates(_0xef2535,_0x1e15a8){return function(_0x2c0ae9){if(_0x2c0ae9){return _0x2c0ae9[_0xfda3('0x21')](_0xef2535)[_0xfda3('0x22')](function(_0x2677f6){return _0x2677f6;});}return null;};}function removeEntity(_0xc6fb9a,_0x16eaa6){return function(_0x5499f5){if(_0x5499f5){return _0x5499f5[_0xfda3('0x23')]()[_0xfda3('0x22')](function(){_0xc6fb9a[_0xfda3('0x1c')](0xcc)['end']();});}};}function handleEntityNotFound(_0x45a977,_0x39e246){return function(_0x172cfb){if(!_0x172cfb){_0x45a977[_0xfda3('0x17')](0x194);}return _0x172cfb;};}function handleError(_0x1cb831,_0xad9f7b){_0xad9f7b=_0xad9f7b||0x1f4;return function(_0x514d2a){logger[_0xfda3('0x24')](_0x514d2a[_0xfda3('0x25')]);if(_0x514d2a[_0xfda3('0x26')]){delete _0x514d2a[_0xfda3('0x26')];}_0x1cb831[_0xfda3('0x1c')](_0xad9f7b)['send'](_0x514d2a);};}exports[_0xfda3('0x27')]=function(_0x4101b1,_0x301509){var _0x474992={},_0xe21ec6={},_0x3cfeb2={'count':0x0,'rows':[]};var _0x26e544=_['map'](db['FreshdeskAccount'][_0xfda3('0x28')],function(_0x4fd277){return{'name':_0x4fd277['fieldName'],'type':_0x4fd277[_0xfda3('0x29')][_0xfda3('0x2a')]};});_0xe21ec6[_0xfda3('0x2b')]=_[_0xfda3('0x2c')](_0x26e544,_0xfda3('0x26'));_0xe21ec6[_0xfda3('0x2d')]=_[_0xfda3('0x2e')](_0x4101b1[_0xfda3('0x2d')]);_0xe21ec6[_0xfda3('0x2f')]=_['intersection'](_0xe21ec6[_0xfda3('0x2b')],_0xe21ec6[_0xfda3('0x2d')]);_0x474992[_0xfda3('0x30')]=_[_0xfda3('0x31')](_0xe21ec6[_0xfda3('0x2b')],qs[_0xfda3('0x32')](_0x4101b1[_0xfda3('0x2d')][_0xfda3('0x32')]));_0x474992[_0xfda3('0x30')]=_0x474992['attributes']['length']?_0x474992[_0xfda3('0x30')]:_0xe21ec6[_0xfda3('0x2b')];if(!_0x4101b1['query'][_0xfda3('0x33')](_0xfda3('0x34'))){_0x474992[_0xfda3('0x1a')]=qs['limit'](_0x4101b1[_0xfda3('0x2d')][_0xfda3('0x1a')]);_0x474992[_0xfda3('0x18')]=qs[_0xfda3('0x18')](_0x4101b1[_0xfda3('0x2d')]['offset']);}_0x474992[_0xfda3('0x35')]=qs[_0xfda3('0x36')](_0x4101b1[_0xfda3('0x2d')]['sort']);_0x474992[_0xfda3('0x37')]=qs['filters'](_[_0xfda3('0x38')](_0x4101b1[_0xfda3('0x2d')],_0xe21ec6[_0xfda3('0x2f')]),_0x26e544);if(_0x4101b1[_0xfda3('0x2d')][_0xfda3('0x39')]){_0x474992[_0xfda3('0x37')]=_[_0xfda3('0x3a')](_0x474992[_0xfda3('0x37')],{'$or':_['map'](_0x26e544,function(_0x24c7ad){if(_0x24c7ad[_0xfda3('0x29')]!==_0xfda3('0x3b')){var _0x8ed2ba={};_0x8ed2ba[_0x24c7ad['name']]={'$like':'%'+_0x4101b1[_0xfda3('0x2d')][_0xfda3('0x39')]+'%'};return _0x8ed2ba;}})});}_0x474992=_[_0xfda3('0x3a')]({},_0x474992,_0x4101b1['options']);var _0x5ac61e={'where':_0x474992[_0xfda3('0x37')]};return db[_0xfda3('0x3c')][_0xfda3('0x1b')](_0x5ac61e)[_0xfda3('0x22')](function(_0x2b415d){_0x3cfeb2[_0xfda3('0x1b')]=_0x2b415d;if(_0x4101b1[_0xfda3('0x2d')][_0xfda3('0x3d')]){_0x474992[_0xfda3('0x3e')]=[{'all':!![]}];}return db[_0xfda3('0x3c')]['findAll'](_0x474992);})[_0xfda3('0x22')](function(_0x22b848){_0x3cfeb2['rows']=_0x22b848;return _0x3cfeb2;})[_0xfda3('0x22')](respondWithFilteredResult(_0x301509,_0x474992))['catch'](handleError(_0x301509,null));};exports['show']=function(_0x4e6886,_0x2208bc){var _0x107fb1={'raw':![],'where':{'id':_0x4e6886[_0xfda3('0x3f')]['id']}},_0x260977={};_0x260977['model']=_[_0xfda3('0x2e')](db[_0xfda3('0x3c')][_0xfda3('0x28')]);_0x260977[_0xfda3('0x2d')]=_['keys'](_0x4e6886[_0xfda3('0x2d')]);_0x260977[_0xfda3('0x2f')]=_[_0xfda3('0x31')](_0x260977['model'],_0x260977['query']);_0x107fb1[_0xfda3('0x30')]=_[_0xfda3('0x31')](_0x260977[_0xfda3('0x2b')],qs[_0xfda3('0x32')](_0x4e6886[_0xfda3('0x2d')][_0xfda3('0x32')]));_0x107fb1[_0xfda3('0x30')]=_0x107fb1[_0xfda3('0x30')][_0xfda3('0x40')]?_0x107fb1[_0xfda3('0x30')]:_0x260977[_0xfda3('0x2b')];if(_0x4e6886[_0xfda3('0x2d')][_0xfda3('0x3d')]){_0x107fb1[_0xfda3('0x3e')]=[{'all':!![]}];}_0x107fb1=_['merge']({},_0x107fb1,_0x4e6886['options']);return db[_0xfda3('0x3c')][_0xfda3('0x41')](_0x107fb1)[_0xfda3('0x22')](handleEntityNotFound(_0x2208bc,null))[_0xfda3('0x22')](respondWithResult(_0x2208bc,null))[_0xfda3('0x42')](handleError(_0x2208bc,null));};exports[_0xfda3('0x43')]=function(_0x192133,_0x125f5a){return db[_0xfda3('0x3c')][_0xfda3('0x43')](_0x192133['body'],{})[_0xfda3('0x22')](respondWithResult(_0x125f5a,0xc9))[_0xfda3('0x42')](handleError(_0x125f5a,null));};exports[_0xfda3('0x21')]=function(_0x36aa3a,_0x26496a){if(_0x36aa3a['body']['id']){delete _0x36aa3a[_0xfda3('0x44')]['id'];}return db[_0xfda3('0x3c')][_0xfda3('0x41')]({'where':{'id':_0x36aa3a['params']['id']}})[_0xfda3('0x22')](handleEntityNotFound(_0x26496a,null))[_0xfda3('0x22')](saveUpdates(_0x36aa3a[_0xfda3('0x44')],null))['then'](respondWithResult(_0x26496a,null))[_0xfda3('0x42')](handleError(_0x26496a,null));};exports[_0xfda3('0x23')]=function(_0x2f54eb,_0x2f6e62){return db[_0xfda3('0x3c')]['find']({'where':{'id':_0x2f54eb[_0xfda3('0x3f')]['id']}})['then'](handleEntityNotFound(_0x2f6e62,null))[_0xfda3('0x22')](removeEntity(_0x2f6e62,null))['catch'](handleError(_0x2f6e62,null));};exports[_0xfda3('0x45')]=function(_0x6447,_0x322b66,_0x26ffee){var _0x574f02={};var _0x244c24={};var _0x3b430d;var _0x2f2c32;return db[_0xfda3('0x3c')][_0xfda3('0x46')]({'where':{'id':_0x6447[_0xfda3('0x3f')]['id']}})[_0xfda3('0x22')](handleEntityNotFound(_0x322b66,null))['then'](function(_0x4b1410){if(_0x4b1410){_0x3b430d=_0x4b1410;_0x244c24[_0xfda3('0x2b')]=_[_0xfda3('0x2e')](db[_0xfda3('0x47')]['rawAttributes']);_0x244c24['query']=_[_0xfda3('0x2e')](_0x6447[_0xfda3('0x2d')]);_0x244c24[_0xfda3('0x2f')]=_[_0xfda3('0x31')](_0x244c24[_0xfda3('0x2b')],_0x244c24['query']);_0x574f02[_0xfda3('0x30')]=_[_0xfda3('0x31')](_0x244c24[_0xfda3('0x2b')],qs[_0xfda3('0x32')](_0x6447[_0xfda3('0x2d')][_0xfda3('0x32')]));_0x574f02[_0xfda3('0x30')]=_0x574f02[_0xfda3('0x30')][_0xfda3('0x40')]?_0x574f02[_0xfda3('0x30')]:_0x244c24[_0xfda3('0x2b')];_0x574f02[_0xfda3('0x35')]=qs['sort'](_0x6447[_0xfda3('0x2d')][_0xfda3('0x36')]);_0x574f02[_0xfda3('0x37')]=qs[_0xfda3('0x2f')](_[_0xfda3('0x38')](_0x6447[_0xfda3('0x2d')],_0x244c24[_0xfda3('0x2f')]));if(_0x6447['query']['filter']){_0x574f02[_0xfda3('0x37')]=_[_0xfda3('0x3a')](_0x574f02['where'],{'$or':_[_0xfda3('0x2c')](_0x574f02[_0xfda3('0x30')],function(_0x2a5efe){var _0x38dbc7={};_0x38dbc7[_0x2a5efe]={'$like':'%'+_0x6447['query']['filter']+'%'};return _0x38dbc7;})});}_0x574f02=_[_0xfda3('0x3a')]({},_0x574f02,_0x6447[_0xfda3('0x48')]);return _0x3b430d[_0xfda3('0x45')](_0x574f02);}})['then'](function(_0x23fd20){if(_0x23fd20){_0x2f2c32=_0x23fd20['length'];if(!_0x6447[_0xfda3('0x2d')][_0xfda3('0x33')](_0xfda3('0x34'))){_0x574f02[_0xfda3('0x1a')]=qs[_0xfda3('0x1a')](_0x6447[_0xfda3('0x2d')]['limit']);_0x574f02[_0xfda3('0x18')]=qs[_0xfda3('0x18')](_0x6447[_0xfda3('0x2d')][_0xfda3('0x18')]);}return _0x3b430d[_0xfda3('0x45')](_0x574f02);}})['then'](function(_0x5e9309){if(_0x5e9309){return _0x5e9309?{'count':_0x2f2c32,'rows':_0x5e9309}:null;}})[_0xfda3('0x22')](respondWithResult(_0x322b66,null))[_0xfda3('0x42')](handleError(_0x322b66,null));};exports['addConfiguration']=function(_0x550822,_0x512d7b,_0x248174){if(_0x550822[_0xfda3('0x44')]['id']){delete _0x550822['body']['id'];}return db[_0xfda3('0x3c')][_0xfda3('0x46')]({'where':{'id':_0x550822[_0xfda3('0x3f')]['id']}})[_0xfda3('0x22')](handleEntityNotFound(_0x512d7b,null))['then'](function(_0x1cd818){if(_0x1cd818){_0x550822[_0xfda3('0x44')][_0xfda3('0x49')]=_0x1cd818['id'];_0x550822[_0xfda3('0x44')][_0xfda3('0x4a')]=integrations[_0xfda3('0x4b')](_0x550822['body']['channel'],_0x550822[_0xfda3('0x44')]['type']);_0x550822[_0xfda3('0x44')][_0xfda3('0x4c')]=integrations[_0xfda3('0x4d')](_0x550822[_0xfda3('0x44')][_0xfda3('0x4e')],_0x550822[_0xfda3('0x44')][_0xfda3('0x29')]);return db[_0xfda3('0x47')][_0xfda3('0x43')](_0x550822[_0xfda3('0x44')],{'include':[{'model':db['FreshdeskField'],'as':_0xfda3('0x4a')},{'model':db['FreshdeskField'],'as':'Descriptions'}]});}return null;})[_0xfda3('0x22')](respondWithResult(_0x512d7b,null))[_0xfda3('0x42')](handleError(_0x512d7b,null));};exports['getFields']=function(_0xa3b2e0,_0x26e1f5,_0x42e387){var _0x589c7a='';return db[_0xfda3('0x3c')][_0xfda3('0x46')]({'where':{'id':_0xa3b2e0[_0xfda3('0x3f')]['id']},'attributes':['id',_0xfda3('0x4f'),_0xfda3('0x50')]})['then'](handleEntityNotFound(_0x26e1f5,null))['then'](function(_0x53c2e9){if(_0x53c2e9){_0x589c7a=_0x53c2e9[_0xfda3('0x4f')];var _0xa032b5=_0x589c7a[_0xfda3('0x51')](-0x1);if(_0xa032b5==='/'){_0x589c7a=_0x589c7a[_0xfda3('0x52')](0x0,_0x589c7a[_0xfda3('0x53')](_0xa032b5));}return rp({'method':_0xfda3('0x54'),'uri':util['format'](_0xfda3('0x55'),_0x589c7a,_0xfda3('0x56')),'headers':{'Authorization':util[_0xfda3('0x57')](_0xfda3('0x58'),new Buffer(util['format'](_0xfda3('0x59'),_0x53c2e9['apiKey']))[_0xfda3('0x5a')](_0xfda3('0x5b')))},'json':!![]});}})[_0xfda3('0x22')](function(_0x16e8f0){if(_0x16e8f0){var _0x328806=[_0xfda3('0x5c'),_0xfda3('0x5d'),'agent',_0xfda3('0x5e'),_0xfda3('0x1c'),_0xfda3('0x5f'),_0xfda3('0x60'),_0xfda3('0x61'),_0xfda3('0x62'),'group',_0xfda3('0x63')];_[_0xfda3('0x64')](_0x16e8f0,function(_0x17161f){return _0x328806[_0xfda3('0x65')](_0x17161f[_0xfda3('0x26')])||!_0x17161f['default']&&_0x17161f[_0xfda3('0x29')]!==_0xfda3('0x66')&&_0x17161f[_0xfda3('0x29')]!==_0xfda3('0x67');});return{'count':_0x16e8f0[_0xfda3('0x40')],'rows':_[_0xfda3('0x2c')](_0x16e8f0,function(_0x1aacc1){return{'id':_0x1aacc1[_0xfda3('0x26')],'name':_0x1aacc1[_0xfda3('0x68')],'custom':!_0x1aacc1[_0xfda3('0x69')],'options':_0x1aacc1['choices']?_[_0xfda3('0x2c')](_0x1aacc1[_0xfda3('0x6a')],function(_0x46210d){return{'name':_[_0xfda3('0x6b')](_0x46210d),'value':_0x46210d};}):[]};})};}})[_0xfda3('0x22')](respondWithResult(_0x26e1f5,null))['catch'](function(_0x5e54e5){var _0x232cd2=_0xa3b2e0[_0xfda3('0x2d')][_0xfda3('0x6c')]?0x1f4:_0x5e54e5[_0xfda3('0x6d')]||0x1f4;logger[_0xfda3('0x24')](_0xfda3('0x6e'),_0xfda3('0x6f'),_0x232cd2,JSON[_0xfda3('0x70')](_0x5e54e5));delete _0x5e54e5[_0xfda3('0x26')];if(_0x232cd2===0x191){_0x232cd2=0x190;}_0x26e1f5[_0xfda3('0x1c')](_0x232cd2)[_0xfda3('0x71')](_0xa3b2e0['query'][_0xfda3('0x6c')]?{'message':'Wrong\x20credentials','statusCode':_0x5e54e5['statusCode']}:_0x5e54e5);});};