Built motion from commit (unavailable).|2.4.18
[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 _0x19a4=['pick','type','VIRTUAL','filter','merge','includeAll','include','findAll','rows','catch','show','options','find','create','body','params','getConfigurations','findOne','ZendeskConfiguration','AccountId','Subjects','getSubjects','channel','Descriptions','getDescriptions','ZendeskField','username','password','token','authType','remoteUri','slice','substring','lastIndexOf','GET','ticket_fields','headers','format','Basic\x20%s','toString','base64','Unknown\x20authentication\x20type','subject','description','assignee','remove','includes','position','title','system_field_options','custom_field_options','getFields,\x20%s,\x20%s,\x20%s','getFields','send','statusCode','html-pdf','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','ejs','lodash','crypto','jsforce','desk.js','querystring','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../mysqldb','../../components/integrations/configuration','client','test','Sequelize','ValidationError','The\x20password\x20must\x20be\x20at\x20least\x208\x20characters\x20long\x20and\x20have\x201\x20lowercase\x20character,\x201\x20uppercase\x20character,\x201\x20number\x20and\x201\x20special\x20character\x20~!@#$%^&-_=+[{]}.','then','info','debug','stringify','error','code','message','request\x20sent','sendStatus','status','end','json','count','offset','limit','set','Content-Range','apply','reject','save','update','destroy','name','index','ZendeskAccount','rawAttributes','key','model','map','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where'];(function(_0x160c73,_0x2ee678){var _0xd9302=function(_0x169454){while(--_0x169454){_0x160c73['push'](_0x160c73['shift']());}};_0xd9302(++_0x2ee678);}(_0x19a4,0xb6));var _0x419a=function(_0x4d1e68,_0x30eda4){_0x4d1e68=_0x4d1e68-0x0;var _0x16be97=_0x19a4[_0x4d1e68];return _0x16be97;};'use strict';var pdf=require(_0x419a('0x0'));var emlformat=require(_0x419a('0x1'));var rimraf=require(_0x419a('0x2'));var zipdir=require(_0x419a('0x3'));var jsonpatch=require(_0x419a('0x4'));var rp=require(_0x419a('0x5'));var moment=require(_0x419a('0x6'));var BPromise=require(_0x419a('0x7'));var Mustache=require(_0x419a('0x8'));var util=require(_0x419a('0x9'));var path=require(_0x419a('0xa'));var sox=require(_0x419a('0xb'));var csv=require('to-csv');var ejs=require(_0x419a('0xc'));var fs=require('fs');var _=require(_0x419a('0xd'));var squel=require('squel');var crypto=require(_0x419a('0xe'));var jsforce=require(_0x419a('0xf'));var deskjs=require(_0x419a('0x10'));var toCsv=require('to-csv');var querystring=require(_0x419a('0x11'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0x419a('0x12'));var as=require(_0x419a('0x13'));var hardwareService=require(_0x419a('0x14'));var logger=require(_0x419a('0x15'))(_0x419a('0x16'));var utils=require('../../config/utils');var config=require(_0x419a('0x17'));var db=require(_0x419a('0x18'))['db'];var integrations=require(_0x419a('0x19'));var jayson=require('jayson/promise');var client=jayson[_0x419a('0x1a')]['http']({'port':0x232a});function checkPasswordPattern(_0x34bf77,_0x19d18e,_0x208e95){if(_0x208e95){var _0x18ed41=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#\$%\^&\*~\-_=+[{\]\}])(?=.{8,})/);if(_0x18ed41[_0x419a('0x1b')](_0x19d18e)){return _0x34bf77;}else{throw new db[(_0x419a('0x1c'))][(_0x419a('0x1d'))](_0x419a('0x1e'));}}else{return _0x34bf77;}}function respondWithRpcPromise(_0x1b63c2,_0x58ed84,_0xe2c007,_0x16e7bb){return new BPromise(function(_0x10db47,_0x1eee89){var _0x3aff44=_0x16e7bb||client;return _0x3aff44['request'](_0x1b63c2,_0xe2c007)[_0x419a('0x1f')](function(_0x5da537){logger[_0x419a('0x20')]('ZendeskAccount,\x20%s,\x20%s',_0x58ed84,'request\x20sent');logger[_0x419a('0x21')]('ZendeskAccount,\x20%s,\x20%s,\x20%s',_0x58ed84,'request\x20sent',JSON[_0x419a('0x22')](_0x5da537));if(_0x5da537[_0x419a('0x23')]){if(_0x5da537[_0x419a('0x23')][_0x419a('0x24')]===0x1f4){logger[_0x419a('0x23')]('ZendeskAccount,\x20%s,\x20%s',_0x58ed84,_0x5da537[_0x419a('0x23')]['message']);return _0x1eee89(_0x5da537[_0x419a('0x23')][_0x419a('0x25')]);}logger[_0x419a('0x23')]('ZendeskAccount,\x20%s,\x20%s',_0x58ed84,_0x5da537['error'][_0x419a('0x25')]);return _0x10db47(_0x5da537['error']['message']);}else{logger[_0x419a('0x20')]('ZendeskAccount,\x20%s,\x20%s',_0x58ed84,_0x419a('0x26'));_0x10db47(_0x5da537['result'][_0x419a('0x25')]);}})['catch'](function(_0x444c6b){logger[_0x419a('0x23')]('ZendeskAccount,\x20%s,\x20%s',_0x58ed84,_0x444c6b);_0x1eee89(_0x444c6b);});});}function respondWithStatusCode(_0x2d7b76,_0x142f8a){_0x142f8a=_0x142f8a||0xcc;return function(_0x19d1d0){if(_0x19d1d0){return _0x2d7b76[_0x419a('0x27')](_0x142f8a);}return _0x2d7b76[_0x419a('0x28')](_0x142f8a)[_0x419a('0x29')]();};}function respondWithResult(_0x5a2f60,_0x56d461){_0x56d461=_0x56d461||0xc8;return function(_0x29e7f8){if(_0x29e7f8){return _0x5a2f60[_0x419a('0x28')](_0x56d461)[_0x419a('0x2a')](_0x29e7f8);}};}function respondWithFilteredResult(_0x1bbc65,_0x1cf052){return function(_0x4616f8){if(_0x4616f8){var _0x1fa68a=_0x4616f8[_0x419a('0x2b')],_0x196a63=_0x1cf052[_0x419a('0x2c')],_0x512c2a=_0x1cf052[_0x419a('0x2c')]+_0x1cf052[_0x419a('0x2d')],_0x26022c;if(_0x512c2a>=_0x1fa68a){_0x512c2a=_0x1fa68a;_0x26022c=0xc8;}else{_0x26022c=0xce;}_0x1bbc65[_0x419a('0x28')](_0x26022c);return _0x1bbc65[_0x419a('0x2e')](_0x419a('0x2f'),_0x196a63+'-'+_0x512c2a+'/'+_0x1fa68a)[_0x419a('0x2a')](_0x4616f8);}return null;};}function patchUpdates(_0x1236a4){return function(_0x2d334a){try{jsonpatch[_0x419a('0x30')](_0x2d334a,_0x1236a4,!![]);}catch(_0x29e23f){return BPromise[_0x419a('0x31')](_0x29e23f);}return _0x2d334a[_0x419a('0x32')]();};}function saveUpdates(_0xbd494f,_0x4771d6){return function(_0xede0d){if(_0xede0d){return _0xede0d[_0x419a('0x33')](_0xbd494f)[_0x419a('0x1f')](function(_0x5acf39){return _0x5acf39;});}return null;};}function removeEntity(_0x4393fb,_0x1bfc99){return function(_0x1c9500){if(_0x1c9500){return _0x1c9500[_0x419a('0x34')]()[_0x419a('0x1f')](function(){_0x4393fb[_0x419a('0x28')](0xcc)[_0x419a('0x29')]();});}};}function handleEntityNotFound(_0x4d8915,_0x2c5cb0){return function(_0x150554){if(!_0x150554){_0x4d8915[_0x419a('0x27')](0x194);}return _0x150554;};}function handleError(_0x5cb3cb,_0xea5443){_0xea5443=_0xea5443||0x1f4;return function(_0x550758){logger['error'](_0x550758['stack']);if(_0x550758['name']){delete _0x550758[_0x419a('0x35')];}_0x5cb3cb['status'](_0xea5443)['send'](_0x550758);};}exports[_0x419a('0x36')]=function(_0xfed8bd,_0x22fedf){var _0x35914d={},_0x486800={},_0x4d0b03={'count':0x0,'rows':[]};var _0x1796c8=_['map'](db[_0x419a('0x37')][_0x419a('0x38')],function(_0x52ee35){return{'name':_0x52ee35['fieldName'],'type':_0x52ee35['type'][_0x419a('0x39')]};});_0x486800[_0x419a('0x3a')]=_[_0x419a('0x3b')](_0x1796c8,_0x419a('0x35'));_0x486800[_0x419a('0x3c')]=_[_0x419a('0x3d')](_0xfed8bd[_0x419a('0x3c')]);_0x486800[_0x419a('0x3e')]=_[_0x419a('0x3f')](_0x486800[_0x419a('0x3a')],_0x486800[_0x419a('0x3c')]);_0x35914d[_0x419a('0x40')]=_[_0x419a('0x3f')](_0x486800['model'],qs['fields'](_0xfed8bd[_0x419a('0x3c')][_0x419a('0x41')]));_0x35914d[_0x419a('0x40')]=_0x35914d[_0x419a('0x40')][_0x419a('0x42')]?_0x35914d['attributes']:_0x486800[_0x419a('0x3a')];if(!_0xfed8bd[_0x419a('0x3c')][_0x419a('0x43')](_0x419a('0x44'))){_0x35914d[_0x419a('0x2d')]=qs[_0x419a('0x2d')](_0xfed8bd[_0x419a('0x3c')][_0x419a('0x2d')]);_0x35914d[_0x419a('0x2c')]=qs[_0x419a('0x2c')](_0xfed8bd[_0x419a('0x3c')]['offset']);}_0x35914d[_0x419a('0x45')]=qs['sort'](_0xfed8bd[_0x419a('0x3c')][_0x419a('0x46')]);_0x35914d[_0x419a('0x47')]=qs[_0x419a('0x3e')](_[_0x419a('0x48')](_0xfed8bd['query'],_0x486800[_0x419a('0x3e')]));if(_0xfed8bd[_0x419a('0x3c')]['filter']){_0x35914d[_0x419a('0x47')]=_['merge'](_0x35914d[_0x419a('0x47')],{'$or':_['map'](_0x1796c8,function(_0x5bfe4a){if(_0x5bfe4a[_0x419a('0x49')]!==_0x419a('0x4a')){var _0x1a9eb5={};_0x1a9eb5[_0x5bfe4a[_0x419a('0x35')]]={'$like':'%'+_0xfed8bd[_0x419a('0x3c')][_0x419a('0x4b')]+'%'};return _0x1a9eb5;}})});}_0x35914d=_[_0x419a('0x4c')]({},_0x35914d,_0xfed8bd['options']);var _0xaf2769={'where':_0x35914d[_0x419a('0x47')]};return db[_0x419a('0x37')]['count'](_0xaf2769)[_0x419a('0x1f')](function(_0x28395f){_0x4d0b03[_0x419a('0x2b')]=_0x28395f;if(_0xfed8bd[_0x419a('0x3c')][_0x419a('0x4d')]){_0x35914d[_0x419a('0x4e')]=[{'all':!![]}];}return db[_0x419a('0x37')][_0x419a('0x4f')](_0x35914d);})['then'](function(_0x54a424){_0x4d0b03[_0x419a('0x50')]=_0x54a424;return _0x4d0b03;})[_0x419a('0x1f')](respondWithFilteredResult(_0x22fedf,_0x35914d))[_0x419a('0x51')](handleError(_0x22fedf,null));};exports[_0x419a('0x52')]=function(_0xb3972a,_0x595c61){var _0xe2ec34={'raw':![],'where':{'id':_0xb3972a['params']['id']}},_0x4411a7={};_0x4411a7[_0x419a('0x3a')]=_[_0x419a('0x3d')](db[_0x419a('0x37')][_0x419a('0x38')]);_0x4411a7[_0x419a('0x3c')]=_[_0x419a('0x3d')](_0xb3972a[_0x419a('0x3c')]);_0x4411a7[_0x419a('0x3e')]=_[_0x419a('0x3f')](_0x4411a7[_0x419a('0x3a')],_0x4411a7[_0x419a('0x3c')]);_0xe2ec34[_0x419a('0x40')]=_[_0x419a('0x3f')](_0x4411a7[_0x419a('0x3a')],qs[_0x419a('0x41')](_0xb3972a[_0x419a('0x3c')][_0x419a('0x41')]));_0xe2ec34[_0x419a('0x40')]=_0xe2ec34['attributes']['length']?_0xe2ec34[_0x419a('0x40')]:_0x4411a7[_0x419a('0x3a')];if(_0xb3972a[_0x419a('0x3c')]['includeAll']){_0xe2ec34['include']=[{'all':!![]}];}_0xe2ec34=_['merge']({},_0xe2ec34,_0xb3972a[_0x419a('0x53')]);return db[_0x419a('0x37')][_0x419a('0x54')](_0xe2ec34)[_0x419a('0x1f')](handleEntityNotFound(_0x595c61,null))[_0x419a('0x1f')](respondWithResult(_0x595c61,null))[_0x419a('0x51')](handleError(_0x595c61,null));};exports[_0x419a('0x55')]=function(_0x41d00a,_0x5e6a5e){return db[_0x419a('0x37')][_0x419a('0x55')](_0x41d00a[_0x419a('0x56')],{})[_0x419a('0x1f')](respondWithResult(_0x5e6a5e,0xc9))['catch'](handleError(_0x5e6a5e,null));};exports[_0x419a('0x33')]=function(_0x54659f,_0x2ef3fa){if(_0x54659f[_0x419a('0x56')]['id']){delete _0x54659f[_0x419a('0x56')]['id'];}return db[_0x419a('0x37')]['find']({'where':{'id':_0x54659f[_0x419a('0x57')]['id']}})[_0x419a('0x1f')](handleEntityNotFound(_0x2ef3fa,null))[_0x419a('0x1f')](saveUpdates(_0x54659f[_0x419a('0x56')],null))[_0x419a('0x1f')](respondWithResult(_0x2ef3fa,null))[_0x419a('0x51')](handleError(_0x2ef3fa,null));};exports[_0x419a('0x34')]=function(_0x26a97b,_0x1d8e67){return db[_0x419a('0x37')]['find']({'where':{'id':_0x26a97b[_0x419a('0x57')]['id']}})['then'](handleEntityNotFound(_0x1d8e67,null))[_0x419a('0x1f')](removeEntity(_0x1d8e67,null))[_0x419a('0x51')](handleError(_0x1d8e67,null));};exports[_0x419a('0x58')]=function(_0x494180,_0x5721d3,_0x161f06){var _0x4b398b={};var _0x132343={};var _0xfe73e5;var _0x294dd7;return db[_0x419a('0x37')][_0x419a('0x59')]({'where':{'id':_0x494180['params']['id']}})[_0x419a('0x1f')](handleEntityNotFound(_0x5721d3,null))[_0x419a('0x1f')](function(_0x20da3a){if(_0x20da3a){_0xfe73e5=_0x20da3a;_0x132343[_0x419a('0x3a')]=_[_0x419a('0x3d')](db[_0x419a('0x5a')][_0x419a('0x38')]);_0x132343['query']=_[_0x419a('0x3d')](_0x494180[_0x419a('0x3c')]);_0x132343['filters']=_[_0x419a('0x3f')](_0x132343['model'],_0x132343[_0x419a('0x3c')]);_0x4b398b['attributes']=_[_0x419a('0x3f')](_0x132343[_0x419a('0x3a')],qs[_0x419a('0x41')](_0x494180[_0x419a('0x3c')]['fields']));_0x4b398b[_0x419a('0x40')]=_0x4b398b[_0x419a('0x40')]['length']?_0x4b398b[_0x419a('0x40')]:_0x132343[_0x419a('0x3a')];_0x4b398b[_0x419a('0x45')]=qs[_0x419a('0x46')](_0x494180[_0x419a('0x3c')][_0x419a('0x46')]);_0x4b398b[_0x419a('0x47')]=qs[_0x419a('0x3e')](_[_0x419a('0x48')](_0x494180[_0x419a('0x3c')],_0x132343[_0x419a('0x3e')]));if(_0x494180[_0x419a('0x3c')][_0x419a('0x4b')]){_0x4b398b[_0x419a('0x47')]=_['merge'](_0x4b398b[_0x419a('0x47')],{'$or':_[_0x419a('0x3b')](_0x4b398b[_0x419a('0x40')],function(_0x2bccaf){var _0x48707c={};_0x48707c[_0x2bccaf]={'$like':'%'+_0x494180[_0x419a('0x3c')][_0x419a('0x4b')]+'%'};return _0x48707c;})});}_0x4b398b=_[_0x419a('0x4c')]({},_0x4b398b,_0x494180[_0x419a('0x53')]);return _0xfe73e5[_0x419a('0x58')](_0x4b398b);}})[_0x419a('0x1f')](function(_0x5b8479){if(_0x5b8479){_0x294dd7=_0x5b8479[_0x419a('0x42')];if(!_0x494180[_0x419a('0x3c')][_0x419a('0x43')](_0x419a('0x44'))){_0x4b398b['limit']=qs[_0x419a('0x2d')](_0x494180[_0x419a('0x3c')][_0x419a('0x2d')]);_0x4b398b[_0x419a('0x2c')]=qs[_0x419a('0x2c')](_0x494180[_0x419a('0x3c')][_0x419a('0x2c')]);}return _0xfe73e5[_0x419a('0x58')](_0x4b398b);}})['then'](function(_0x393650){if(_0x393650){return _0x393650?{'count':_0x294dd7,'rows':_0x393650}:null;}})['then'](respondWithResult(_0x5721d3,null))[_0x419a('0x51')](handleError(_0x5721d3,null));};exports['addConfiguration']=function(_0x224d21,_0x232517,_0x3955db){if(_0x224d21['body']['id']){delete _0x224d21[_0x419a('0x56')]['id'];}return db[_0x419a('0x37')]['findOne']({'where':{'id':_0x224d21[_0x419a('0x57')]['id']}})[_0x419a('0x1f')](handleEntityNotFound(_0x232517,null))[_0x419a('0x1f')](function(_0x3e8b2e){if(_0x3e8b2e){_0x224d21[_0x419a('0x56')][_0x419a('0x5b')]=_0x3e8b2e['id'];_0x224d21[_0x419a('0x56')][_0x419a('0x5c')]=integrations[_0x419a('0x5d')](_0x224d21[_0x419a('0x56')][_0x419a('0x5e')],_0x224d21[_0x419a('0x56')][_0x419a('0x49')]);_0x224d21[_0x419a('0x56')][_0x419a('0x5f')]=integrations[_0x419a('0x60')](_0x224d21[_0x419a('0x56')][_0x419a('0x5e')],_0x224d21[_0x419a('0x56')][_0x419a('0x49')]);return db[_0x419a('0x5a')][_0x419a('0x55')](_0x224d21[_0x419a('0x56')],{'include':[{'model':db[_0x419a('0x61')],'as':_0x419a('0x5c')},{'model':db[_0x419a('0x61')],'as':_0x419a('0x5f')}]});}return null;})[_0x419a('0x1f')](respondWithResult(_0x232517,null))[_0x419a('0x51')](handleError(_0x232517,null));};exports['getFields']=function(_0x11b1cf,_0x2a39f5,_0x28f301){var _0x5df5a4='';return db[_0x419a('0x37')][_0x419a('0x59')]({'where':{'id':_0x11b1cf[_0x419a('0x57')]['id']},'attributes':['id',_0x419a('0x62'),_0x419a('0x63'),_0x419a('0x64'),_0x419a('0x65'),_0x419a('0x66')]})[_0x419a('0x1f')](handleEntityNotFound(_0x2a39f5,null))[_0x419a('0x1f')](function(_0x59e81f){if(_0x59e81f){_0x5df5a4=_0x59e81f[_0x419a('0x66')];var _0x44bc0d=_0x5df5a4[_0x419a('0x67')](-0x1);if(_0x44bc0d==='/'){_0x5df5a4=_0x5df5a4[_0x419a('0x68')](0x0,_0x5df5a4[_0x419a('0x69')](_0x44bc0d));}var _0x58a19e={'method':_0x419a('0x6a'),'uri':util['format']('%s/%s',_0x5df5a4,_0x419a('0x6b')),'json':!![]};switch(_0x59e81f['authType']){case _0x419a('0x63'):_0x58a19e[_0x419a('0x6c')]={'Authorization':util[_0x419a('0x6d')](_0x419a('0x6e'),new Buffer(util[_0x419a('0x6d')]('%s:%s',_0x59e81f[_0x419a('0x62')],_0x59e81f[_0x419a('0x63')]))[_0x419a('0x6f')](_0x419a('0x70')))};break;case _0x419a('0x64'):_0x58a19e[_0x419a('0x6c')]={'Authorization':util[_0x419a('0x6d')](_0x419a('0x6e'),new Buffer(util[_0x419a('0x6d')]('%s/token:%s',_0x59e81f['username'],_0x59e81f[_0x419a('0x64')]))[_0x419a('0x6f')](_0x419a('0x70')))};break;default:throw new db[(_0x419a('0x1c'))][(_0x419a('0x1d'))](_0x419a('0x71'));}return rp(_0x58a19e);}})['then'](function(_0x44ec6f){if(_0x44ec6f){var _0x501155=[_0x419a('0x72'),_0x419a('0x73'),'group',_0x419a('0x74')];_[_0x419a('0x75')](_0x44ec6f[_0x419a('0x6b')],function(_0xd9a945){return _0x501155[_0x419a('0x76')](_0xd9a945[_0x419a('0x49')]);});return{'count':_0x44ec6f[_0x419a('0x6b')][_0x419a('0x42')],'rows':_[_0x419a('0x3b')](_0x44ec6f['ticket_fields'],function(_0x4448a8){return{'id':_0x4448a8['id'],'name':_0x4448a8[_0x419a('0x77')]>0x7?_0x4448a8[_0x419a('0x78')]:_0x4448a8[_0x419a('0x49')],'custom':_0x4448a8[_0x419a('0x77')]>0x7?!![]:![],'options':_0x4448a8[_0x419a('0x79')]?_0x4448a8['system_field_options']:_0x4448a8[_0x419a('0x7a')]?_0x4448a8[_0x419a('0x7a')]:[]};})};}})['then'](respondWithResult(_0x2a39f5,null))[_0x419a('0x51')](function(_0xe3b814){var _0x38934c=_0x11b1cf['query'][_0x419a('0x1b')]?0x1f4:_0xe3b814['statusCode']||0x1f4;logger[_0x419a('0x23')](_0x419a('0x7b'),_0x419a('0x7c'),_0x38934c,JSON['stringify'](_0xe3b814));delete _0xe3b814[_0x419a('0x35')];if(_0x38934c===0x191){_0x38934c=0x190;}_0x2a39f5[_0x419a('0x28')](_0x38934c)[_0x419a('0x7d')](_0x11b1cf[_0x419a('0x3c')][_0x419a('0x1b')]?{'message':'Wrong\x20credentials','statusCode':_0xe3b814[_0x419a('0x7e')]}:_0xe3b814);});};