deb0f440aa5af988c080cf639369b09bc100dbe4
[motion2.git] / server / api / intZendeskAccount / intZendeskAccount.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 _0xa5fa=['send','ZendeskAccount','fieldName','model','query','filters','intersection','attributes','fields','length','nolimit','order','sort','pick','filter','where','merge','map','type','VIRTUAL','options','include','findAll','catch','show','params','keys','rawAttributes','includeAll','find','create','body','getConfigurations','findOne','ZendeskConfiguration','hasOwnProperty','addConfiguration','AccountId','getSubjects','channel','getDescriptions','ZendeskField','Subjects','Descriptions','getFields','username','password','token','remoteUri','lastIndexOf','GET','format','%s/%s','headers','Basic\x20%s','%s:%s','toString','base64','Sequelize','ValidationError','Unknown\x20authentication\x20type','subject','description','group','assignee','remove','ticket_fields','includes','position','title','system_field_options','custom_field_options','test','statusCode','error','getFields,\x20%s,\x20%s,\x20%s','stringify','Wrong\x20credentials','rimraf','zip-dir','fast-json-patch','request-promise','bluebird','mustache','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','papaparse','ioredis','../../components/parsers/advancedSearch','api','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','status','end','offset','undefined','limit','count','set','Content-Range','json','apply','reject','update','destroy','then','stack','name'];(function(_0x2cec00,_0x274462){var _0x590e48=function(_0x39d2fb){while(--_0x39d2fb){_0x2cec00['push'](_0x2cec00['shift']());}};_0x590e48(++_0x274462);}(_0xa5fa,0xc4));var _0xaa5f=function(_0x49d610,_0x3b14e2){_0x49d610=_0x49d610-0x0;var _0x4c6a73=_0xa5fa[_0x49d610];return _0x4c6a73;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0xaa5f('0x0'));var zipdir=require(_0xaa5f('0x1'));var jsonpatch=require(_0xaa5f('0x2'));var rp=require(_0xaa5f('0x3'));var moment=require('moment');var BPromise=require(_0xaa5f('0x4'));var Mustache=require(_0xaa5f('0x5'));var util=require('util');var path=require(_0xaa5f('0x6'));var sox=require(_0xaa5f('0x7'));var csv=require(_0xaa5f('0x8'));var ejs=require(_0xaa5f('0x9'));var fs=require('fs');var fs_extra=require(_0xaa5f('0xa'));var _=require(_0xaa5f('0xb'));var squel=require(_0xaa5f('0xc'));var crypto=require(_0xaa5f('0xd'));var jsforce=require(_0xaa5f('0xe'));var deskjs=require(_0xaa5f('0xf'));var toCsv=require(_0xaa5f('0x8'));var querystring=require('querystring');var Papa=require(_0xaa5f('0x10'));var Redis=require(_0xaa5f('0x11'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0xaa5f('0x12'));var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0xaa5f('0x13'));var utils=require('../../config/utils');var config=require(_0xaa5f('0x14'));var licenseUtil=require(_0xaa5f('0x15'));var db=require(_0xaa5f('0x16'))['db'];var integrations=require(_0xaa5f('0x17'));function respondWithStatusCode(_0x252ae0,_0x48d1d3){_0x48d1d3=_0x48d1d3||0xcc;return function(_0xbd4e10){if(_0xbd4e10){return _0x252ae0['sendStatus'](_0x48d1d3);}return _0x252ae0[_0xaa5f('0x18')](_0x48d1d3)[_0xaa5f('0x19')]();};}function respondWithResult(_0x31eabc,_0x19aa27){_0x19aa27=_0x19aa27||0xc8;return function(_0x2e1ca5){if(_0x2e1ca5){return _0x31eabc['status'](_0x19aa27)['json'](_0x2e1ca5);}};}function respondWithFilteredResult(_0x1f37fb,_0x291042){return function(_0xb0bfb6){if(_0xb0bfb6){var _0x4265ab=typeof _0x291042[_0xaa5f('0x1a')]===_0xaa5f('0x1b')&&typeof _0x291042[_0xaa5f('0x1c')]===_0xaa5f('0x1b');var _0x202ea8=_0xb0bfb6[_0xaa5f('0x1d')];var _0x22175f=_0x4265ab?0x0:_0x291042[_0xaa5f('0x1a')];var _0x4f4cd5=_0x4265ab?_0xb0bfb6[_0xaa5f('0x1d')]:_0x291042[_0xaa5f('0x1a')]+_0x291042[_0xaa5f('0x1c')];var _0x358d2e;if(_0x4f4cd5>=_0x202ea8){_0x4f4cd5=_0x202ea8;_0x358d2e=0xc8;}else{_0x358d2e=0xce;}_0x1f37fb[_0xaa5f('0x18')](_0x358d2e);return _0x1f37fb[_0xaa5f('0x1e')](_0xaa5f('0x1f'),_0x22175f+'-'+_0x4f4cd5+'/'+_0x202ea8)[_0xaa5f('0x20')](_0xb0bfb6);}return null;};}function patchUpdates(_0x2d8fc3){return function(_0xe3d7d){try{jsonpatch[_0xaa5f('0x21')](_0xe3d7d,_0x2d8fc3,!![]);}catch(_0x10f287){return BPromise[_0xaa5f('0x22')](_0x10f287);}return _0xe3d7d['save']();};}function saveUpdates(_0x1e917d,_0x57082c){return function(_0x2e04e6){if(_0x2e04e6){return _0x2e04e6[_0xaa5f('0x23')](_0x1e917d)['then'](function(_0x1baa2e){return _0x1baa2e;});}return null;};}function removeEntity(_0x1032ce,_0x5b9f0b){return function(_0xf6a8c3){if(_0xf6a8c3){return _0xf6a8c3[_0xaa5f('0x24')]()[_0xaa5f('0x25')](function(){_0x1032ce[_0xaa5f('0x18')](0xcc)[_0xaa5f('0x19')]();});}};}function handleEntityNotFound(_0xacbded,_0x3d7bca){return function(_0x2fba7d){if(!_0x2fba7d){_0xacbded['sendStatus'](0x194);}return _0x2fba7d;};}function handleError(_0x2445a6,_0x3a66e8){_0x3a66e8=_0x3a66e8||0x1f4;return function(_0x1ed8eb){logger['error'](_0x1ed8eb[_0xaa5f('0x26')]);if(_0x1ed8eb[_0xaa5f('0x27')]){delete _0x1ed8eb[_0xaa5f('0x27')];}_0x2445a6['status'](_0x3a66e8)[_0xaa5f('0x28')](_0x1ed8eb);};}exports['index']=function(_0x5990d2,_0x27d1a4){var _0x50998f={},_0x1bffec={},_0x4e0ec9={'count':0x0,'rows':[]};var _0x4c5dc0=_['map'](db[_0xaa5f('0x29')]['rawAttributes'],function(_0x310828){return{'name':_0x310828[_0xaa5f('0x2a')],'type':_0x310828['type']['key']};});_0x1bffec[_0xaa5f('0x2b')]=_['map'](_0x4c5dc0,_0xaa5f('0x27'));_0x1bffec[_0xaa5f('0x2c')]=_['keys'](_0x5990d2[_0xaa5f('0x2c')]);_0x1bffec[_0xaa5f('0x2d')]=_[_0xaa5f('0x2e')](_0x1bffec['model'],_0x1bffec[_0xaa5f('0x2c')]);_0x50998f[_0xaa5f('0x2f')]=_['intersection'](_0x1bffec['model'],qs['fields'](_0x5990d2['query'][_0xaa5f('0x30')]));_0x50998f[_0xaa5f('0x2f')]=_0x50998f[_0xaa5f('0x2f')][_0xaa5f('0x31')]?_0x50998f[_0xaa5f('0x2f')]:_0x1bffec[_0xaa5f('0x2b')];if(!_0x5990d2[_0xaa5f('0x2c')]['hasOwnProperty'](_0xaa5f('0x32'))){_0x50998f[_0xaa5f('0x1c')]=qs[_0xaa5f('0x1c')](_0x5990d2[_0xaa5f('0x2c')][_0xaa5f('0x1c')]);_0x50998f[_0xaa5f('0x1a')]=qs['offset'](_0x5990d2[_0xaa5f('0x2c')][_0xaa5f('0x1a')]);}_0x50998f[_0xaa5f('0x33')]=qs[_0xaa5f('0x34')](_0x5990d2[_0xaa5f('0x2c')]['sort']);_0x50998f['where']=qs[_0xaa5f('0x2d')](_[_0xaa5f('0x35')](_0x5990d2[_0xaa5f('0x2c')],_0x1bffec[_0xaa5f('0x2d')]),_0x4c5dc0);if(_0x5990d2['query'][_0xaa5f('0x36')]){_0x50998f[_0xaa5f('0x37')]=_[_0xaa5f('0x38')](_0x50998f[_0xaa5f('0x37')],{'$or':_[_0xaa5f('0x39')](_0x4c5dc0,function(_0x1869c2){if(_0x1869c2[_0xaa5f('0x3a')]!==_0xaa5f('0x3b')){var _0xc6deec={};_0xc6deec[_0x1869c2[_0xaa5f('0x27')]]={'$like':'%'+_0x5990d2[_0xaa5f('0x2c')]['filter']+'%'};return _0xc6deec;}})});}_0x50998f=_[_0xaa5f('0x38')]({},_0x50998f,_0x5990d2[_0xaa5f('0x3c')]);var _0xcd9c30={'where':_0x50998f[_0xaa5f('0x37')]};return db[_0xaa5f('0x29')][_0xaa5f('0x1d')](_0xcd9c30)[_0xaa5f('0x25')](function(_0xa490d9){_0x4e0ec9[_0xaa5f('0x1d')]=_0xa490d9;if(_0x5990d2[_0xaa5f('0x2c')]['includeAll']){_0x50998f[_0xaa5f('0x3d')]=[{'all':!![]}];}return db['ZendeskAccount'][_0xaa5f('0x3e')](_0x50998f);})[_0xaa5f('0x25')](function(_0x17bb4d){_0x4e0ec9['rows']=_0x17bb4d;return _0x4e0ec9;})[_0xaa5f('0x25')](respondWithFilteredResult(_0x27d1a4,_0x50998f))[_0xaa5f('0x3f')](handleError(_0x27d1a4,null));};exports[_0xaa5f('0x40')]=function(_0x528b08,_0x12a8e4){var _0x179be8={'raw':![],'where':{'id':_0x528b08[_0xaa5f('0x41')]['id']}},_0x1ac9b6={};_0x1ac9b6['model']=_[_0xaa5f('0x42')](db[_0xaa5f('0x29')][_0xaa5f('0x43')]);_0x1ac9b6[_0xaa5f('0x2c')]=_[_0xaa5f('0x42')](_0x528b08[_0xaa5f('0x2c')]);_0x1ac9b6[_0xaa5f('0x2d')]=_[_0xaa5f('0x2e')](_0x1ac9b6['model'],_0x1ac9b6[_0xaa5f('0x2c')]);_0x179be8['attributes']=_[_0xaa5f('0x2e')](_0x1ac9b6['model'],qs[_0xaa5f('0x30')](_0x528b08[_0xaa5f('0x2c')]['fields']));_0x179be8[_0xaa5f('0x2f')]=_0x179be8[_0xaa5f('0x2f')][_0xaa5f('0x31')]?_0x179be8['attributes']:_0x1ac9b6['model'];if(_0x528b08['query'][_0xaa5f('0x44')]){_0x179be8['include']=[{'all':!![]}];}_0x179be8=_[_0xaa5f('0x38')]({},_0x179be8,_0x528b08[_0xaa5f('0x3c')]);return db[_0xaa5f('0x29')][_0xaa5f('0x45')](_0x179be8)['then'](handleEntityNotFound(_0x12a8e4,null))[_0xaa5f('0x25')](respondWithResult(_0x12a8e4,null))[_0xaa5f('0x3f')](handleError(_0x12a8e4,null));};exports[_0xaa5f('0x46')]=function(_0x231ad1,_0x27ea4b){return db[_0xaa5f('0x29')][_0xaa5f('0x46')](_0x231ad1[_0xaa5f('0x47')],{})[_0xaa5f('0x25')](respondWithResult(_0x27ea4b,0xc9))[_0xaa5f('0x3f')](handleError(_0x27ea4b,null));};exports[_0xaa5f('0x23')]=function(_0x2de737,_0x2be44d){if(_0x2de737[_0xaa5f('0x47')]['id']){delete _0x2de737[_0xaa5f('0x47')]['id'];}return db[_0xaa5f('0x29')]['find']({'where':{'id':_0x2de737[_0xaa5f('0x41')]['id']}})[_0xaa5f('0x25')](handleEntityNotFound(_0x2be44d,null))[_0xaa5f('0x25')](saveUpdates(_0x2de737[_0xaa5f('0x47')],null))[_0xaa5f('0x25')](respondWithResult(_0x2be44d,null))[_0xaa5f('0x3f')](handleError(_0x2be44d,null));};exports[_0xaa5f('0x24')]=function(_0x4aa556,_0xf5a6b6){return db[_0xaa5f('0x29')][_0xaa5f('0x45')]({'where':{'id':_0x4aa556[_0xaa5f('0x41')]['id']}})['then'](handleEntityNotFound(_0xf5a6b6,null))[_0xaa5f('0x25')](removeEntity(_0xf5a6b6,null))[_0xaa5f('0x3f')](handleError(_0xf5a6b6,null));};exports[_0xaa5f('0x48')]=function(_0x575803,_0x5bbdce,_0x506dce){var _0x56cdd6={};var _0x585559={};var _0x509c61;var _0x1f47a0;return db[_0xaa5f('0x29')][_0xaa5f('0x49')]({'where':{'id':_0x575803[_0xaa5f('0x41')]['id']}})[_0xaa5f('0x25')](handleEntityNotFound(_0x5bbdce,null))[_0xaa5f('0x25')](function(_0x48c473){if(_0x48c473){_0x509c61=_0x48c473;_0x585559['model']=_[_0xaa5f('0x42')](db[_0xaa5f('0x4a')]['rawAttributes']);_0x585559[_0xaa5f('0x2c')]=_[_0xaa5f('0x42')](_0x575803[_0xaa5f('0x2c')]);_0x585559[_0xaa5f('0x2d')]=_[_0xaa5f('0x2e')](_0x585559[_0xaa5f('0x2b')],_0x585559[_0xaa5f('0x2c')]);_0x56cdd6['attributes']=_['intersection'](_0x585559[_0xaa5f('0x2b')],qs[_0xaa5f('0x30')](_0x575803['query']['fields']));_0x56cdd6['attributes']=_0x56cdd6[_0xaa5f('0x2f')]['length']?_0x56cdd6[_0xaa5f('0x2f')]:_0x585559['model'];_0x56cdd6['order']=qs[_0xaa5f('0x34')](_0x575803[_0xaa5f('0x2c')][_0xaa5f('0x34')]);_0x56cdd6['where']=qs[_0xaa5f('0x2d')](_[_0xaa5f('0x35')](_0x575803[_0xaa5f('0x2c')],_0x585559['filters']));if(_0x575803[_0xaa5f('0x2c')][_0xaa5f('0x36')]){_0x56cdd6['where']=_[_0xaa5f('0x38')](_0x56cdd6[_0xaa5f('0x37')],{'$or':_[_0xaa5f('0x39')](_0x56cdd6[_0xaa5f('0x2f')],function(_0x334282){var _0x178d90={};_0x178d90[_0x334282]={'$like':'%'+_0x575803[_0xaa5f('0x2c')][_0xaa5f('0x36')]+'%'};return _0x178d90;})});}_0x56cdd6=_[_0xaa5f('0x38')]({},_0x56cdd6,_0x575803['options']);return _0x509c61[_0xaa5f('0x48')](_0x56cdd6);}})[_0xaa5f('0x25')](function(_0x1d02af){if(_0x1d02af){_0x1f47a0=_0x1d02af[_0xaa5f('0x31')];if(!_0x575803[_0xaa5f('0x2c')][_0xaa5f('0x4b')](_0xaa5f('0x32'))){_0x56cdd6[_0xaa5f('0x1c')]=qs['limit'](_0x575803[_0xaa5f('0x2c')]['limit']);_0x56cdd6[_0xaa5f('0x1a')]=qs[_0xaa5f('0x1a')](_0x575803[_0xaa5f('0x2c')][_0xaa5f('0x1a')]);}return _0x509c61['getConfigurations'](_0x56cdd6);}})[_0xaa5f('0x25')](function(_0x1bc0e7){if(_0x1bc0e7){return _0x1bc0e7?{'count':_0x1f47a0,'rows':_0x1bc0e7}:null;}})[_0xaa5f('0x25')](respondWithResult(_0x5bbdce,null))['catch'](handleError(_0x5bbdce,null));};exports[_0xaa5f('0x4c')]=function(_0x524f42,_0x42ea3b,_0x5e7a74){if(_0x524f42[_0xaa5f('0x47')]['id']){delete _0x524f42[_0xaa5f('0x47')]['id'];}return db[_0xaa5f('0x29')][_0xaa5f('0x49')]({'where':{'id':_0x524f42['params']['id']}})[_0xaa5f('0x25')](handleEntityNotFound(_0x42ea3b,null))[_0xaa5f('0x25')](function(_0x47a160){if(_0x47a160){_0x524f42['body'][_0xaa5f('0x4d')]=_0x47a160['id'];_0x524f42[_0xaa5f('0x47')]['Subjects']=integrations[_0xaa5f('0x4e')](_0x524f42[_0xaa5f('0x47')][_0xaa5f('0x4f')],_0x524f42['body'][_0xaa5f('0x3a')]);_0x524f42[_0xaa5f('0x47')]['Descriptions']=integrations[_0xaa5f('0x50')](_0x524f42[_0xaa5f('0x47')][_0xaa5f('0x4f')],_0x524f42['body'][_0xaa5f('0x3a')]);return db[_0xaa5f('0x4a')]['create'](_0x524f42[_0xaa5f('0x47')],{'include':[{'model':db[_0xaa5f('0x51')],'as':_0xaa5f('0x52')},{'model':db[_0xaa5f('0x51')],'as':_0xaa5f('0x53')}]});}return null;})['then'](respondWithResult(_0x42ea3b,null))[_0xaa5f('0x3f')](handleError(_0x42ea3b,null));};exports[_0xaa5f('0x54')]=function(_0x464ac7,_0x216399,_0x2a1049){var _0x508fe7='';return db[_0xaa5f('0x29')]['findOne']({'where':{'id':_0x464ac7[_0xaa5f('0x41')]['id']},'attributes':['id',_0xaa5f('0x55'),_0xaa5f('0x56'),_0xaa5f('0x57'),'authType',_0xaa5f('0x58')]})[_0xaa5f('0x25')](handleEntityNotFound(_0x216399,null))[_0xaa5f('0x25')](function(_0x424b43){if(_0x424b43){_0x508fe7=_0x424b43[_0xaa5f('0x58')];var _0x201b8b=_0x508fe7['slice'](-0x1);if(_0x201b8b==='/'){_0x508fe7=_0x508fe7['substring'](0x0,_0x508fe7[_0xaa5f('0x59')](_0x201b8b));}var _0x300350={'method':_0xaa5f('0x5a'),'uri':util[_0xaa5f('0x5b')](_0xaa5f('0x5c'),_0x508fe7,'ticket_fields'),'json':!![]};switch(_0x424b43['authType']){case _0xaa5f('0x56'):_0x300350[_0xaa5f('0x5d')]={'Authorization':util['format'](_0xaa5f('0x5e'),new Buffer(util['format'](_0xaa5f('0x5f'),_0x424b43[_0xaa5f('0x55')],_0x424b43['password']))[_0xaa5f('0x60')](_0xaa5f('0x61')))};break;case _0xaa5f('0x57'):_0x300350[_0xaa5f('0x5d')]={'Authorization':util[_0xaa5f('0x5b')](_0xaa5f('0x5e'),new Buffer(util[_0xaa5f('0x5b')]('%s/token:%s',_0x424b43[_0xaa5f('0x55')],_0x424b43[_0xaa5f('0x57')]))[_0xaa5f('0x60')]('base64'))};break;default:throw new db[(_0xaa5f('0x62'))][(_0xaa5f('0x63'))](_0xaa5f('0x64'));}return rp(_0x300350);}})[_0xaa5f('0x25')](function(_0x281004){if(_0x281004){var _0x5d4cab=[_0xaa5f('0x65'),_0xaa5f('0x66'),_0xaa5f('0x67'),_0xaa5f('0x68')];_[_0xaa5f('0x69')](_0x281004[_0xaa5f('0x6a')],function(_0x155eb4){return _0x5d4cab[_0xaa5f('0x6b')](_0x155eb4[_0xaa5f('0x3a')]);});return{'count':_0x281004[_0xaa5f('0x6a')]['length'],'rows':_['map'](_0x281004[_0xaa5f('0x6a')],function(_0xa7ecc9){return{'id':_0xa7ecc9['id'],'name':_0xa7ecc9[_0xaa5f('0x6c')]>0x7?_0xa7ecc9[_0xaa5f('0x6d')]:_0xa7ecc9[_0xaa5f('0x3a')],'custom':_0xa7ecc9[_0xaa5f('0x6c')]>0x7?!![]:![],'options':_0xa7ecc9[_0xaa5f('0x6e')]?_0xa7ecc9[_0xaa5f('0x6e')]:_0xa7ecc9['custom_field_options']?_0xa7ecc9[_0xaa5f('0x6f')]:[]};})};}})[_0xaa5f('0x25')](respondWithResult(_0x216399,null))[_0xaa5f('0x3f')](function(_0x5f2484){var _0x55a3f9=_0x464ac7[_0xaa5f('0x2c')][_0xaa5f('0x70')]?0x1f4:_0x5f2484[_0xaa5f('0x71')]||0x1f4;logger[_0xaa5f('0x72')](_0xaa5f('0x73'),_0xaa5f('0x54'),_0x55a3f9,JSON[_0xaa5f('0x74')](_0x5f2484));delete _0x5f2484[_0xaa5f('0x27')];if(_0x55a3f9===0x191){_0x55a3f9=0x190;}_0x216399[_0xaa5f('0x18')](_0x55a3f9)[_0xaa5f('0x28')](_0x464ac7['query'][_0xaa5f('0x70')]?{'message':_0xaa5f('0x75'),'statusCode':_0x5f2484[_0xaa5f('0x71')]}:_0x5f2484);});};