Built motion from commit 85328c08.|2.5.50
[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 _0x96d9=['capitalize','test','statusCode','stringify','send','Wrong\x20credentials','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','path','sox','to-csv','ejs','fs-extra','lodash','squel','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../components/integrations/configuration','sendStatus','end','status','offset','undefined','limit','count','Content-Range','json','apply','save','then','destroy','error','stack','name','index','rawAttributes','fieldName','type','key','model','map','keys','filters','intersection','query','attributes','fields','length','hasOwnProperty','nolimit','sort','where','filter','merge','VIRTUAL','options','findAll','rows','catch','show','params','FreshdeskAccount','includeAll','find','create','body','update','getConfigurations','findOne','FreshdeskConfiguration','pick','addConfiguration','AccountId','getSubjects','Descriptions','FreshdeskField','Subjects','getFields','remoteUri','apiKey','slice','substring','lastIndexOf','GET','api/v2/ticket_fields','Basic\x20%s','format','%s:X','toString','requester','subject','agent','description','ticket_type','group','includes','custom_dropdown','label','default','choices'];(function(_0x3298f1,_0x3794f3){var _0x40fd7c=function(_0x1a9e2f){while(--_0x1a9e2f){_0x3298f1['push'](_0x3298f1['shift']());}};_0x40fd7c(++_0x3794f3);}(_0x96d9,0xea));var _0x996d=function(_0x52d13a,_0x3bdd8e){_0x52d13a=_0x52d13a-0x0;var _0x2a67b1=_0x96d9[_0x52d13a];return _0x2a67b1;};'use strict';var emlformat=require(_0x996d('0x0'));var rimraf=require(_0x996d('0x1'));var zipdir=require(_0x996d('0x2'));var jsonpatch=require(_0x996d('0x3'));var rp=require(_0x996d('0x4'));var moment=require(_0x996d('0x5'));var BPromise=require(_0x996d('0x6'));var Mustache=require(_0x996d('0x7'));var util=require('util');var path=require(_0x996d('0x8'));var sox=require(_0x996d('0x9'));var csv=require(_0x996d('0xa'));var ejs=require(_0x996d('0xb'));var fs=require('fs');var fs_extra=require(_0x996d('0xc'));var _=require(_0x996d('0xd'));var squel=require(_0x996d('0xe'));var crypto=require('crypto');var jsforce=require(_0x996d('0xf'));var deskjs=require(_0x996d('0x10'));var toCsv=require(_0x996d('0xa'));var querystring=require(_0x996d('0x11'));var Papa=require(_0x996d('0x12'));var Redis=require('ioredis');var authService=require(_0x996d('0x13'));var qs=require(_0x996d('0x14'));var as=require(_0x996d('0x15'));var hardwareService=require(_0x996d('0x16'));var logger=require('../../config/logger')(_0x996d('0x17'));var utils=require(_0x996d('0x18'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];var integrations=require(_0x996d('0x19'));function respondWithStatusCode(_0x2a585a,_0x54d85b){_0x54d85b=_0x54d85b||0xcc;return function(_0xa19195){if(_0xa19195){return _0x2a585a[_0x996d('0x1a')](_0x54d85b);}return _0x2a585a['status'](_0x54d85b)[_0x996d('0x1b')]();};}function respondWithResult(_0x269179,_0x18c2b8){_0x18c2b8=_0x18c2b8||0xc8;return function(_0x47cd95){if(_0x47cd95){return _0x269179[_0x996d('0x1c')](_0x18c2b8)['json'](_0x47cd95);}};}function respondWithFilteredResult(_0x1da7b0,_0xd8bcba){return function(_0x281281){if(_0x281281){var _0x15a985=typeof _0xd8bcba[_0x996d('0x1d')]===_0x996d('0x1e')&&typeof _0xd8bcba[_0x996d('0x1f')]==='undefined';var _0x5dacb7=_0x281281[_0x996d('0x20')];var _0x17ce93=_0x15a985?0x0:_0xd8bcba[_0x996d('0x1d')];var _0x26d8c6=_0x15a985?_0x281281[_0x996d('0x20')]:_0xd8bcba[_0x996d('0x1d')]+_0xd8bcba[_0x996d('0x1f')];var _0x2fd906;if(_0x26d8c6>=_0x5dacb7){_0x26d8c6=_0x5dacb7;_0x2fd906=0xc8;}else{_0x2fd906=0xce;}_0x1da7b0['status'](_0x2fd906);return _0x1da7b0['set'](_0x996d('0x21'),_0x17ce93+'-'+_0x26d8c6+'/'+_0x5dacb7)[_0x996d('0x22')](_0x281281);}return null;};}function patchUpdates(_0x2da682){return function(_0x109662){try{jsonpatch[_0x996d('0x23')](_0x109662,_0x2da682,!![]);}catch(_0x2d0979){return BPromise['reject'](_0x2d0979);}return _0x109662[_0x996d('0x24')]();};}function saveUpdates(_0x76a7e,_0x379165){return function(_0x5bbf4c){if(_0x5bbf4c){return _0x5bbf4c['update'](_0x76a7e)[_0x996d('0x25')](function(_0x42fae4){return _0x42fae4;});}return null;};}function removeEntity(_0x53c4d9,_0x2a9da1){return function(_0x273aad){if(_0x273aad){return _0x273aad[_0x996d('0x26')]()[_0x996d('0x25')](function(){_0x53c4d9['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x31fbb0,_0x22defb){return function(_0x559030){if(!_0x559030){_0x31fbb0[_0x996d('0x1a')](0x194);}return _0x559030;};}function handleError(_0x54a040,_0x435f27){_0x435f27=_0x435f27||0x1f4;return function(_0x279285){logger[_0x996d('0x27')](_0x279285[_0x996d('0x28')]);if(_0x279285[_0x996d('0x29')]){delete _0x279285[_0x996d('0x29')];}_0x54a040[_0x996d('0x1c')](_0x435f27)['send'](_0x279285);};}exports[_0x996d('0x2a')]=function(_0x150e7d,_0x45a158){var _0x365cc5={},_0x33814d={},_0xdbf420={'count':0x0,'rows':[]};var _0x4b45bc=_['map'](db['FreshdeskAccount'][_0x996d('0x2b')],function(_0x6b3221){return{'name':_0x6b3221[_0x996d('0x2c')],'type':_0x6b3221[_0x996d('0x2d')][_0x996d('0x2e')]};});_0x33814d[_0x996d('0x2f')]=_[_0x996d('0x30')](_0x4b45bc,'name');_0x33814d['query']=_[_0x996d('0x31')](_0x150e7d['query']);_0x33814d[_0x996d('0x32')]=_[_0x996d('0x33')](_0x33814d['model'],_0x33814d[_0x996d('0x34')]);_0x365cc5[_0x996d('0x35')]=_[_0x996d('0x33')](_0x33814d['model'],qs[_0x996d('0x36')](_0x150e7d[_0x996d('0x34')][_0x996d('0x36')]));_0x365cc5['attributes']=_0x365cc5['attributes'][_0x996d('0x37')]?_0x365cc5[_0x996d('0x35')]:_0x33814d['model'];if(!_0x150e7d[_0x996d('0x34')][_0x996d('0x38')](_0x996d('0x39'))){_0x365cc5[_0x996d('0x1f')]=qs[_0x996d('0x1f')](_0x150e7d['query'][_0x996d('0x1f')]);_0x365cc5[_0x996d('0x1d')]=qs[_0x996d('0x1d')](_0x150e7d[_0x996d('0x34')][_0x996d('0x1d')]);}_0x365cc5['order']=qs[_0x996d('0x3a')](_0x150e7d[_0x996d('0x34')][_0x996d('0x3a')]);_0x365cc5[_0x996d('0x3b')]=qs[_0x996d('0x32')](_['pick'](_0x150e7d['query'],_0x33814d['filters']),_0x4b45bc);if(_0x150e7d['query'][_0x996d('0x3c')]){_0x365cc5[_0x996d('0x3b')]=_[_0x996d('0x3d')](_0x365cc5['where'],{'$or':_[_0x996d('0x30')](_0x4b45bc,function(_0x576b99){if(_0x576b99[_0x996d('0x2d')]!==_0x996d('0x3e')){var _0x3750e8={};_0x3750e8[_0x576b99[_0x996d('0x29')]]={'$like':'%'+_0x150e7d[_0x996d('0x34')][_0x996d('0x3c')]+'%'};return _0x3750e8;}})});}_0x365cc5=_[_0x996d('0x3d')]({},_0x365cc5,_0x150e7d[_0x996d('0x3f')]);var _0x226039={'where':_0x365cc5[_0x996d('0x3b')]};return db['FreshdeskAccount'][_0x996d('0x20')](_0x226039)[_0x996d('0x25')](function(_0x4b8586){_0xdbf420[_0x996d('0x20')]=_0x4b8586;if(_0x150e7d['query']['includeAll']){_0x365cc5['include']=[{'all':!![]}];}return db['FreshdeskAccount'][_0x996d('0x40')](_0x365cc5);})[_0x996d('0x25')](function(_0x4a7e07){_0xdbf420[_0x996d('0x41')]=_0x4a7e07;return _0xdbf420;})[_0x996d('0x25')](respondWithFilteredResult(_0x45a158,_0x365cc5))[_0x996d('0x42')](handleError(_0x45a158,null));};exports[_0x996d('0x43')]=function(_0x2f708b,_0x5853f5){var _0x113997={'raw':![],'where':{'id':_0x2f708b[_0x996d('0x44')]['id']}},_0x2fc99b={};_0x2fc99b[_0x996d('0x2f')]=_[_0x996d('0x31')](db[_0x996d('0x45')]['rawAttributes']);_0x2fc99b[_0x996d('0x34')]=_[_0x996d('0x31')](_0x2f708b[_0x996d('0x34')]);_0x2fc99b['filters']=_[_0x996d('0x33')](_0x2fc99b[_0x996d('0x2f')],_0x2fc99b['query']);_0x113997['attributes']=_[_0x996d('0x33')](_0x2fc99b['model'],qs[_0x996d('0x36')](_0x2f708b[_0x996d('0x34')]['fields']));_0x113997[_0x996d('0x35')]=_0x113997[_0x996d('0x35')][_0x996d('0x37')]?_0x113997[_0x996d('0x35')]:_0x2fc99b[_0x996d('0x2f')];if(_0x2f708b['query'][_0x996d('0x46')]){_0x113997['include']=[{'all':!![]}];}_0x113997=_['merge']({},_0x113997,_0x2f708b['options']);return db['FreshdeskAccount'][_0x996d('0x47')](_0x113997)[_0x996d('0x25')](handleEntityNotFound(_0x5853f5,null))[_0x996d('0x25')](respondWithResult(_0x5853f5,null))['catch'](handleError(_0x5853f5,null));};exports['create']=function(_0x9365a,_0x3513fc){return db[_0x996d('0x45')][_0x996d('0x48')](_0x9365a[_0x996d('0x49')],{})['then'](respondWithResult(_0x3513fc,0xc9))[_0x996d('0x42')](handleError(_0x3513fc,null));};exports[_0x996d('0x4a')]=function(_0xed150d,_0x42c2f0){if(_0xed150d[_0x996d('0x49')]['id']){delete _0xed150d[_0x996d('0x49')]['id'];}return db[_0x996d('0x45')]['find']({'where':{'id':_0xed150d['params']['id']}})[_0x996d('0x25')](handleEntityNotFound(_0x42c2f0,null))[_0x996d('0x25')](saveUpdates(_0xed150d['body'],null))['then'](respondWithResult(_0x42c2f0,null))[_0x996d('0x42')](handleError(_0x42c2f0,null));};exports['destroy']=function(_0x446fa1,_0xcd988b){return db[_0x996d('0x45')][_0x996d('0x47')]({'where':{'id':_0x446fa1['params']['id']}})[_0x996d('0x25')](handleEntityNotFound(_0xcd988b,null))['then'](removeEntity(_0xcd988b,null))[_0x996d('0x42')](handleError(_0xcd988b,null));};exports[_0x996d('0x4b')]=function(_0x37dcac,_0x378efd,_0x536472){var _0x146f47={};var _0x36f354={};var _0xa5ad1;var _0x1107c1;return db[_0x996d('0x45')][_0x996d('0x4c')]({'where':{'id':_0x37dcac[_0x996d('0x44')]['id']}})[_0x996d('0x25')](handleEntityNotFound(_0x378efd,null))['then'](function(_0x590549){if(_0x590549){_0xa5ad1=_0x590549;_0x36f354['model']=_[_0x996d('0x31')](db[_0x996d('0x4d')][_0x996d('0x2b')]);_0x36f354[_0x996d('0x34')]=_[_0x996d('0x31')](_0x37dcac['query']);_0x36f354[_0x996d('0x32')]=_[_0x996d('0x33')](_0x36f354[_0x996d('0x2f')],_0x36f354[_0x996d('0x34')]);_0x146f47[_0x996d('0x35')]=_[_0x996d('0x33')](_0x36f354[_0x996d('0x2f')],qs[_0x996d('0x36')](_0x37dcac[_0x996d('0x34')][_0x996d('0x36')]));_0x146f47['attributes']=_0x146f47[_0x996d('0x35')][_0x996d('0x37')]?_0x146f47[_0x996d('0x35')]:_0x36f354['model'];_0x146f47['order']=qs[_0x996d('0x3a')](_0x37dcac[_0x996d('0x34')][_0x996d('0x3a')]);_0x146f47['where']=qs['filters'](_[_0x996d('0x4e')](_0x37dcac[_0x996d('0x34')],_0x36f354[_0x996d('0x32')]));if(_0x37dcac['query'][_0x996d('0x3c')]){_0x146f47['where']=_[_0x996d('0x3d')](_0x146f47[_0x996d('0x3b')],{'$or':_[_0x996d('0x30')](_0x146f47[_0x996d('0x35')],function(_0x2d7652){var _0x13da79={};_0x13da79[_0x2d7652]={'$like':'%'+_0x37dcac[_0x996d('0x34')][_0x996d('0x3c')]+'%'};return _0x13da79;})});}_0x146f47=_[_0x996d('0x3d')]({},_0x146f47,_0x37dcac['options']);return _0xa5ad1['getConfigurations'](_0x146f47);}})['then'](function(_0x1debd5){if(_0x1debd5){_0x1107c1=_0x1debd5[_0x996d('0x37')];if(!_0x37dcac[_0x996d('0x34')][_0x996d('0x38')](_0x996d('0x39'))){_0x146f47[_0x996d('0x1f')]=qs[_0x996d('0x1f')](_0x37dcac[_0x996d('0x34')]['limit']);_0x146f47[_0x996d('0x1d')]=qs[_0x996d('0x1d')](_0x37dcac[_0x996d('0x34')][_0x996d('0x1d')]);}return _0xa5ad1[_0x996d('0x4b')](_0x146f47);}})['then'](function(_0x2babdc){if(_0x2babdc){return _0x2babdc?{'count':_0x1107c1,'rows':_0x2babdc}:null;}})['then'](respondWithResult(_0x378efd,null))[_0x996d('0x42')](handleError(_0x378efd,null));};exports[_0x996d('0x4f')]=function(_0x466bac,_0x5d7959,_0xecb07b){if(_0x466bac[_0x996d('0x49')]['id']){delete _0x466bac[_0x996d('0x49')]['id'];}return db[_0x996d('0x45')][_0x996d('0x4c')]({'where':{'id':_0x466bac[_0x996d('0x44')]['id']}})[_0x996d('0x25')](handleEntityNotFound(_0x5d7959,null))[_0x996d('0x25')](function(_0x3eb579){if(_0x3eb579){_0x466bac[_0x996d('0x49')][_0x996d('0x50')]=_0x3eb579['id'];_0x466bac[_0x996d('0x49')]['Subjects']=integrations[_0x996d('0x51')](_0x466bac[_0x996d('0x49')]['channel'],_0x466bac[_0x996d('0x49')][_0x996d('0x2d')]);_0x466bac[_0x996d('0x49')][_0x996d('0x52')]=integrations['getDescriptions'](_0x466bac['body']['channel'],_0x466bac['body']['type']);return db['FreshdeskConfiguration'][_0x996d('0x48')](_0x466bac['body'],{'include':[{'model':db[_0x996d('0x53')],'as':_0x996d('0x54')},{'model':db['FreshdeskField'],'as':_0x996d('0x52')}]});}return null;})['then'](respondWithResult(_0x5d7959,null))[_0x996d('0x42')](handleError(_0x5d7959,null));};exports[_0x996d('0x55')]=function(_0x51aa5f,_0x38dd68,_0x24bd7a){var _0x1d71ef='';return db[_0x996d('0x45')][_0x996d('0x4c')]({'where':{'id':_0x51aa5f[_0x996d('0x44')]['id']},'attributes':['id',_0x996d('0x56'),_0x996d('0x57')]})['then'](handleEntityNotFound(_0x38dd68,null))[_0x996d('0x25')](function(_0x912fb0){if(_0x912fb0){_0x1d71ef=_0x912fb0[_0x996d('0x56')];var _0xc48526=_0x1d71ef[_0x996d('0x58')](-0x1);if(_0xc48526==='/'){_0x1d71ef=_0x1d71ef[_0x996d('0x59')](0x0,_0x1d71ef[_0x996d('0x5a')](_0xc48526));}return rp({'method':_0x996d('0x5b'),'uri':util['format']('%s/%s',_0x1d71ef,_0x996d('0x5c')),'headers':{'Authorization':util['format'](_0x996d('0x5d'),new Buffer(util[_0x996d('0x5e')](_0x996d('0x5f'),_0x912fb0[_0x996d('0x57')]))[_0x996d('0x60')]('base64'))},'json':!![]});}})['then'](function(_0x3868bc){if(_0x3868bc){var _0x288690=[_0x996d('0x61'),_0x996d('0x62'),_0x996d('0x63'),_0x996d('0x64'),'status','priority','group','source',_0x996d('0x65'),_0x996d('0x66'),'company'];_['remove'](_0x3868bc,function(_0x316021){return _0x288690[_0x996d('0x67')](_0x316021[_0x996d('0x29')])||!_0x316021['default']&&_0x316021[_0x996d('0x2d')]!=='custom_text'&&_0x316021[_0x996d('0x2d')]!==_0x996d('0x68');});return{'count':_0x3868bc[_0x996d('0x37')],'rows':_[_0x996d('0x30')](_0x3868bc,function(_0x997d1){return{'id':_0x997d1[_0x996d('0x29')],'name':_0x997d1[_0x996d('0x69')],'custom':!_0x997d1[_0x996d('0x6a')],'options':_0x997d1[_0x996d('0x6b')]?_['map'](_0x997d1['choices'],function(_0xf5aafa){return{'name':_[_0x996d('0x6c')](_0xf5aafa),'value':_0xf5aafa};}):[]};})};}})['then'](respondWithResult(_0x38dd68,null))[_0x996d('0x42')](function(_0x452732){var _0x4815b5=_0x51aa5f[_0x996d('0x34')][_0x996d('0x6d')]?0x1f4:_0x452732[_0x996d('0x6e')]||0x1f4;logger['error']('getFields,\x20%s,\x20%s,\x20%s',_0x996d('0x55'),_0x4815b5,JSON[_0x996d('0x6f')](_0x452732));delete _0x452732['name'];if(_0x4815b5===0x191){_0x4815b5=0x190;}_0x38dd68[_0x996d('0x1c')](_0x4815b5)[_0x996d('0x70')](_0x51aa5f['query'][_0x996d('0x6d')]?{'message':_0x996d('0x71'),'statusCode':_0x452732[_0x996d('0x6e')]}:_0x452732);});};