830a8931519571ba6ef725b7b61988eb181e3481
[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 _0x550f=['ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../mysqldb','../../components/integrations/configuration','jayson/promise','client','test','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','FreshdeskAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','FreshdeskAccount,\x20%s,\x20%s','message','catch','status','end','json','count','offset','Content-Range','apply','reject','save','update','destroy','sendStatus','stack','name','send','index','FreshdeskAccount','rawAttributes','model','keys','query','filters','intersection','fields','attributes','nolimit','limit','order','sort','filter','where','map','type','key','VIRTUAL','field','merge','options','includeAll','include','findAll','length','find','create','body','params','getConfigurations','findOne','hasOwnProperty','addConfiguration','AccountId','getSubjects','getDescriptions','channel','FreshdeskField','Subjects','Descriptions','getFields','remoteUri','apiKey','lastIndexOf','GET','format','%s/%s','api/v2/ticket_fields','Basic\x20%s','%s:X','toString','base64','requester','subject','agent','description','priority','group','source','ticket_type','company','includes','default','custom_text','custom_dropdown','label','choices','statusCode','getFields,\x20%s,\x20%s,\x20%s','zip-dir','fast-json-patch','request-promise','util','sox','to-csv','lodash','squel','desk.js','querystring','papaparse'];(function(_0x19950b,_0x53d0d7){var _0x3c6b41=function(_0x3924ad){while(--_0x3924ad){_0x19950b['push'](_0x19950b['shift']());}};_0x3c6b41(++_0x53d0d7);}(_0x550f,0x6d));var _0xf550=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x550f[_0x3dd15e];return _0x231fd0;};'use strict';var pdf=require('html-pdf');var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0xf550('0x0'));var jsonpatch=require(_0xf550('0x1'));var rp=require(_0xf550('0x2'));var moment=require('moment');var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0xf550('0x3'));var path=require('path');var sox=require(_0xf550('0x4'));var csv=require(_0xf550('0x5'));var ejs=require('ejs');var fs=require('fs');var _=require(_0xf550('0x6'));var squel=require(_0xf550('0x7'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0xf550('0x8'));var toCsv=require(_0xf550('0x5'));var querystring=require(_0xf550('0x9'));var Papa=require(_0xf550('0xa'));var Redis=require(_0xf550('0xb'));var authService=require(_0xf550('0xc'));var qs=require(_0xf550('0xd'));var hardwareService=require(_0xf550('0xe'));var logger=require(_0xf550('0xf'))(_0xf550('0x10'));var utils=require(_0xf550('0x11'));var config=require('../../config/environment');var db=require(_0xf550('0x12'))['db'];var integrations=require(_0xf550('0x13'));var jayson=require(_0xf550('0x14'));var client=jayson[_0xf550('0x15')]['http']({'port':0x232a});function checkPasswordPattern(_0xd95210,_0x55076d,_0x373e56){if(_0x373e56){var _0x27deb6=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#\$%\^&\*~\-_=+[{\]\}])(?=.{8,})/);if(_0x27deb6[_0xf550('0x16')](_0x55076d)){return _0xd95210;}else{throw new Error(_0xf550('0x17'));}}else{return _0xd95210;}}function respondWithRpcPromise(_0x4f4d58,_0x5242c0,_0x81403e,_0x2b8d53){return new BPromise(function(_0x2bd966,_0x2ae725){var _0x2512f1=_0x2b8d53||client;return _0x2512f1['request'](_0x4f4d58,_0x81403e)[_0xf550('0x18')](function(_0x32ac89){logger[_0xf550('0x19')]('FreshdeskAccount,\x20%s,\x20%s',_0x5242c0,'request\x20sent');logger[_0xf550('0x1a')](_0xf550('0x1b'),_0x5242c0,_0xf550('0x1c'),JSON[_0xf550('0x1d')](_0x32ac89));if(_0x32ac89[_0xf550('0x1e')]){if(_0x32ac89[_0xf550('0x1e')]['code']===0x1f4){logger[_0xf550('0x1e')](_0xf550('0x1f'),_0x5242c0,_0x32ac89[_0xf550('0x1e')][_0xf550('0x20')]);return _0x2ae725(_0x32ac89[_0xf550('0x1e')][_0xf550('0x20')]);}logger[_0xf550('0x1e')](_0xf550('0x1f'),_0x5242c0,_0x32ac89[_0xf550('0x1e')][_0xf550('0x20')]);return _0x2bd966(_0x32ac89[_0xf550('0x1e')]['message']);}else{logger[_0xf550('0x19')](_0xf550('0x1f'),_0x5242c0,_0xf550('0x1c'));_0x2bd966(_0x32ac89['result'][_0xf550('0x20')]);}})[_0xf550('0x21')](function(_0x319239){logger['error'](_0xf550('0x1f'),_0x5242c0,_0x319239);_0x2ae725(_0x319239);});});}function respondWithStatusCode(_0x77fd49,_0x3231fa){_0x3231fa=_0x3231fa||0xcc;return function(_0x5af5c9){if(_0x5af5c9){return _0x77fd49['sendStatus'](_0x3231fa);}return _0x77fd49[_0xf550('0x22')](_0x3231fa)[_0xf550('0x23')]();};}function respondWithResult(_0x25e38a,_0x405148){_0x405148=_0x405148||0xc8;return function(_0x28418d){if(_0x28418d){return _0x25e38a['status'](_0x405148)[_0xf550('0x24')](_0x28418d);}};}function respondWithFilteredResult(_0x2549b5,_0x159d09){return function(_0x5ed6cc){if(_0x5ed6cc){var _0x55cfb9=_0x5ed6cc[_0xf550('0x25')],_0x4632a7=_0x159d09[_0xf550('0x26')],_0x2d6e0d=_0x159d09[_0xf550('0x26')]+_0x159d09['limit'],_0x55d489;if(_0x2d6e0d>=_0x55cfb9){_0x2d6e0d=_0x55cfb9;_0x55d489=0xc8;}else{_0x55d489=0xce;}_0x2549b5[_0xf550('0x22')](_0x55d489);return _0x2549b5['set'](_0xf550('0x27'),_0x4632a7+'-'+_0x2d6e0d+'/'+_0x55cfb9)['json'](_0x5ed6cc);}return null;};}function patchUpdates(_0x1e3ae0){return function(_0x57cf5d){try{jsonpatch[_0xf550('0x28')](_0x57cf5d,_0x1e3ae0,!![]);}catch(_0x4eda31){return BPromise[_0xf550('0x29')](_0x4eda31);}return _0x57cf5d[_0xf550('0x2a')]();};}function saveUpdates(_0x9c7875,_0x3afb2b){return function(_0x36e321){if(_0x36e321){return _0x36e321[_0xf550('0x2b')](_0x9c7875)[_0xf550('0x18')](function(_0x3effa2){return _0x3effa2;});}return null;};}function removeEntity(_0x2df738,_0x3cc4dc){return function(_0x5d6e6f){if(_0x5d6e6f){return _0x5d6e6f[_0xf550('0x2c')]()[_0xf550('0x18')](function(){_0x2df738['status'](0xcc)[_0xf550('0x23')]();});}};}function handleEntityNotFound(_0x4a8a38,_0x2cc85f){return function(_0x2fdf90){if(!_0x2fdf90){_0x4a8a38[_0xf550('0x2d')](0x194);}return _0x2fdf90;};}function handleError(_0x2623a3,_0x14919e){_0x14919e=_0x14919e||0x1f4;return function(_0x1dc651){logger['error'](_0x1dc651[_0xf550('0x2e')]);if(_0x1dc651[_0xf550('0x2f')]){delete _0x1dc651['name'];}_0x2623a3['status'](_0x14919e)[_0xf550('0x30')](_0x1dc651);};}exports[_0xf550('0x31')]=function(_0x26c5fa,_0x169e25){var _0x18d3ff={},_0x5f4032={},_0x4491ee={'count':0x0,'rows':[]};var _0x346f69=db[_0xf550('0x32')][_0xf550('0x33')];_0x5f4032[_0xf550('0x34')]=_[_0xf550('0x35')](_0x346f69);_0x5f4032[_0xf550('0x36')]=_[_0xf550('0x35')](_0x26c5fa[_0xf550('0x36')]);_0x5f4032[_0xf550('0x37')]=_['intersection'](_0x5f4032['model'],_0x5f4032[_0xf550('0x36')]);_0x18d3ff['attributes']=_[_0xf550('0x38')](_0x5f4032[_0xf550('0x34')],qs[_0xf550('0x39')](_0x26c5fa[_0xf550('0x36')][_0xf550('0x39')]));_0x18d3ff['attributes']=_0x18d3ff[_0xf550('0x3a')]['length']?_0x18d3ff['attributes']:_0x5f4032[_0xf550('0x34')];if(!_0x26c5fa[_0xf550('0x36')]['hasOwnProperty'](_0xf550('0x3b'))){_0x18d3ff[_0xf550('0x3c')]=qs[_0xf550('0x3c')](_0x26c5fa[_0xf550('0x36')][_0xf550('0x3c')]);_0x18d3ff['offset']=qs[_0xf550('0x26')](_0x26c5fa['query']['offset']);}_0x18d3ff[_0xf550('0x3d')]=qs[_0xf550('0x3e')](_0x26c5fa[_0xf550('0x36')]['sort']);_0x18d3ff['where']=qs[_0xf550('0x37')](_['pick'](_0x26c5fa['query'],_0x5f4032[_0xf550('0x37')]));if(_0x26c5fa['query'][_0xf550('0x3f')]){_0x18d3ff[_0xf550('0x40')]=_['merge'](_0x18d3ff[_0xf550('0x40')],{'$or':_[_0xf550('0x41')](_0x346f69,function(_0x47acf3){if(_0x47acf3[_0xf550('0x42')][_0xf550('0x43')]!==_0xf550('0x44')){var _0x1aed54={};_0x1aed54[_0x47acf3[_0xf550('0x45')]]={'$like':'%'+_0x26c5fa[_0xf550('0x36')]['filter']+'%'};return _0x1aed54;}})});}_0x18d3ff=_[_0xf550('0x46')]({},_0x18d3ff,_0x26c5fa[_0xf550('0x47')]);var _0x539d9b={'where':_0x18d3ff[_0xf550('0x40')]};return db[_0xf550('0x32')][_0xf550('0x25')](_0x539d9b)[_0xf550('0x18')](function(_0x4cb593){_0x4491ee[_0xf550('0x25')]=_0x4cb593;if(_0x26c5fa[_0xf550('0x36')][_0xf550('0x48')]){_0x18d3ff[_0xf550('0x49')]=[{'all':!![]}];}return db[_0xf550('0x32')][_0xf550('0x4a')](_0x18d3ff);})[_0xf550('0x18')](function(_0x3d9757){_0x4491ee['rows']=_0x3d9757;return _0x4491ee;})[_0xf550('0x18')](respondWithFilteredResult(_0x169e25,_0x18d3ff))['catch'](handleError(_0x169e25,null));};exports['show']=function(_0xe65ca8,_0x4759ca){var _0x362cca={'raw':![],'where':{'id':_0xe65ca8['params']['id']}},_0x259eea={};_0x259eea[_0xf550('0x34')]=_[_0xf550('0x35')](db[_0xf550('0x32')][_0xf550('0x33')]);_0x259eea[_0xf550('0x36')]=_['keys'](_0xe65ca8['query']);_0x259eea[_0xf550('0x37')]=_['intersection'](_0x259eea[_0xf550('0x34')],_0x259eea['query']);_0x362cca[_0xf550('0x3a')]=_[_0xf550('0x38')](_0x259eea['model'],qs[_0xf550('0x39')](_0xe65ca8[_0xf550('0x36')][_0xf550('0x39')]));_0x362cca[_0xf550('0x3a')]=_0x362cca[_0xf550('0x3a')][_0xf550('0x4b')]?_0x362cca[_0xf550('0x3a')]:_0x259eea[_0xf550('0x34')];if(_0xe65ca8[_0xf550('0x36')][_0xf550('0x48')]){_0x362cca[_0xf550('0x49')]=[{'all':!![]}];}_0x362cca=_['merge']({},_0x362cca,_0xe65ca8['options']);return db[_0xf550('0x32')][_0xf550('0x4c')](_0x362cca)[_0xf550('0x18')](handleEntityNotFound(_0x4759ca,null))[_0xf550('0x18')](respondWithResult(_0x4759ca,null))[_0xf550('0x21')](handleError(_0x4759ca,null));};exports[_0xf550('0x4d')]=function(_0x39d523,_0x2e7b5f){return db['FreshdeskAccount']['create'](_0x39d523['body'],{})[_0xf550('0x18')](respondWithResult(_0x2e7b5f,0xc9))[_0xf550('0x21')](handleError(_0x2e7b5f,null));};exports['update']=function(_0x2de579,_0xba728a){if(_0x2de579[_0xf550('0x4e')]['id']){delete _0x2de579[_0xf550('0x4e')]['id'];}return db['FreshdeskAccount'][_0xf550('0x4c')]({'where':{'id':_0x2de579[_0xf550('0x4f')]['id']}})[_0xf550('0x18')](handleEntityNotFound(_0xba728a,null))[_0xf550('0x18')](saveUpdates(_0x2de579[_0xf550('0x4e')],null))[_0xf550('0x18')](respondWithResult(_0xba728a,null))['catch'](handleError(_0xba728a,null));};exports['destroy']=function(_0x356c30,_0x3aa815){return db['FreshdeskAccount'][_0xf550('0x4c')]({'where':{'id':_0x356c30['params']['id']}})[_0xf550('0x18')](handleEntityNotFound(_0x3aa815,null))[_0xf550('0x18')](removeEntity(_0x3aa815,null))[_0xf550('0x21')](handleError(_0x3aa815,null));};exports[_0xf550('0x50')]=function(_0x43db13,_0x1c1233,_0x175cfc){var _0x496d56={};var _0x562a3e={};var _0x10e286;var _0x56a2c8;return db[_0xf550('0x32')][_0xf550('0x51')]({'where':{'id':_0x43db13[_0xf550('0x4f')]['id']}})['then'](handleEntityNotFound(_0x1c1233,null))['then'](function(_0x3c0619){if(_0x3c0619){_0x10e286=_0x3c0619;_0x562a3e[_0xf550('0x34')]=_[_0xf550('0x35')](db['FreshdeskConfiguration']['rawAttributes']);_0x562a3e[_0xf550('0x36')]=_[_0xf550('0x35')](_0x43db13['query']);_0x562a3e['filters']=_[_0xf550('0x38')](_0x562a3e[_0xf550('0x34')],_0x562a3e['query']);_0x496d56[_0xf550('0x3a')]=_[_0xf550('0x38')](_0x562a3e[_0xf550('0x34')],qs[_0xf550('0x39')](_0x43db13[_0xf550('0x36')][_0xf550('0x39')]));_0x496d56[_0xf550('0x3a')]=_0x496d56[_0xf550('0x3a')][_0xf550('0x4b')]?_0x496d56[_0xf550('0x3a')]:_0x562a3e[_0xf550('0x34')];_0x496d56['order']=qs[_0xf550('0x3e')](_0x43db13['query'][_0xf550('0x3e')]);_0x496d56['where']=qs[_0xf550('0x37')](_['pick'](_0x43db13['query'],_0x562a3e['filters']));if(_0x43db13['query']['filter']){_0x496d56[_0xf550('0x40')]=_['merge'](_0x496d56['where'],{'$or':_[_0xf550('0x41')](_0x496d56['attributes'],function(_0x3e6cfc){var _0x43cdd2={};_0x43cdd2[_0x3e6cfc]={'$like':'%'+_0x43db13[_0xf550('0x36')][_0xf550('0x3f')]+'%'};return _0x43cdd2;})});}_0x496d56=_[_0xf550('0x46')]({},_0x496d56,_0x43db13['options']);return _0x10e286['getConfigurations'](_0x496d56);}})[_0xf550('0x18')](function(_0x19d49c){if(_0x19d49c){_0x56a2c8=_0x19d49c['length'];if(!_0x43db13[_0xf550('0x36')][_0xf550('0x52')]('nolimit')){_0x496d56[_0xf550('0x3c')]=qs[_0xf550('0x3c')](_0x43db13[_0xf550('0x36')][_0xf550('0x3c')]);_0x496d56[_0xf550('0x26')]=qs[_0xf550('0x26')](_0x43db13[_0xf550('0x36')][_0xf550('0x26')]);}return _0x10e286[_0xf550('0x50')](_0x496d56);}})[_0xf550('0x18')](function(_0x22a7db){if(_0x22a7db){return _0x22a7db?{'count':_0x56a2c8,'rows':_0x22a7db}:null;}})[_0xf550('0x18')](respondWithResult(_0x1c1233,null))[_0xf550('0x21')](handleError(_0x1c1233,null));};exports[_0xf550('0x53')]=function(_0x3cf29b,_0x30b245,_0x945014){if(_0x3cf29b['body']['id']){delete _0x3cf29b['body']['id'];}return db[_0xf550('0x32')][_0xf550('0x51')]({'where':{'id':_0x3cf29b[_0xf550('0x4f')]['id']}})['then'](handleEntityNotFound(_0x30b245,null))[_0xf550('0x18')](function(_0x1f3680){if(_0x1f3680){_0x3cf29b[_0xf550('0x4e')][_0xf550('0x54')]=_0x1f3680['id'];_0x3cf29b[_0xf550('0x4e')]['Subjects']=integrations[_0xf550('0x55')](_0x3cf29b['body']['channel'],_0x3cf29b[_0xf550('0x4e')][_0xf550('0x42')]);_0x3cf29b['body']['Descriptions']=integrations[_0xf550('0x56')](_0x3cf29b['body'][_0xf550('0x57')],_0x3cf29b[_0xf550('0x4e')][_0xf550('0x42')]);return db['FreshdeskConfiguration'][_0xf550('0x4d')](_0x3cf29b[_0xf550('0x4e')],{'include':[{'model':db[_0xf550('0x58')],'as':_0xf550('0x59')},{'model':db[_0xf550('0x58')],'as':_0xf550('0x5a')}]});}return null;})[_0xf550('0x18')](respondWithResult(_0x30b245,null))['catch'](handleError(_0x30b245,null));};exports[_0xf550('0x5b')]=function(_0x3f3759,_0x135a7a,_0x32b36e){var _0x155593='';return db[_0xf550('0x32')][_0xf550('0x51')]({'where':{'id':_0x3f3759[_0xf550('0x4f')]['id']},'attributes':['id',_0xf550('0x5c'),_0xf550('0x5d')]})[_0xf550('0x18')](handleEntityNotFound(_0x135a7a,null))[_0xf550('0x18')](function(_0xfad4a2){if(_0xfad4a2){_0x155593=_0xfad4a2[_0xf550('0x5c')];var _0x3f2609=_0x155593['slice'](-0x1);if(_0x3f2609==='/'){_0x155593=_0x155593['substring'](0x0,_0x155593[_0xf550('0x5e')](_0x3f2609));}return rp({'method':_0xf550('0x5f'),'uri':util[_0xf550('0x60')](_0xf550('0x61'),_0x155593,_0xf550('0x62')),'headers':{'Authorization':util[_0xf550('0x60')](_0xf550('0x63'),new Buffer(util['format'](_0xf550('0x64'),_0xfad4a2[_0xf550('0x5d')]))[_0xf550('0x65')](_0xf550('0x66')))},'json':!![]});}})[_0xf550('0x18')](function(_0x3c8582){if(_0x3c8582){var _0x57077c=[_0xf550('0x67'),_0xf550('0x68'),_0xf550('0x69'),_0xf550('0x6a'),_0xf550('0x22'),_0xf550('0x6b'),_0xf550('0x6c'),_0xf550('0x6d'),_0xf550('0x6e'),'group',_0xf550('0x6f')];_['remove'](_0x3c8582,function(_0x4a67f5){return _0x57077c[_0xf550('0x70')](_0x4a67f5[_0xf550('0x2f')])||!_0x4a67f5[_0xf550('0x71')]&&_0x4a67f5[_0xf550('0x42')]!==_0xf550('0x72')&&_0x4a67f5[_0xf550('0x42')]!==_0xf550('0x73');});return{'count':_0x3c8582['length'],'rows':_[_0xf550('0x41')](_0x3c8582,function(_0x337b80){return{'id':_0x337b80[_0xf550('0x2f')],'name':_0x337b80[_0xf550('0x74')],'custom':!_0x337b80[_0xf550('0x71')],'options':_0x337b80[_0xf550('0x75')]?_[_0xf550('0x41')](_0x337b80['choices'],function(_0x401c9a){return{'name':_['capitalize'](_0x401c9a),'value':_0x401c9a};}):[]};})};}})[_0xf550('0x18')](respondWithResult(_0x135a7a,null))[_0xf550('0x21')](function(_0x4edfd8){var _0x1aded1=_0x3f3759[_0xf550('0x36')]['test']?0x1f4:_0x4edfd8[_0xf550('0x76')]||0x1f4;logger[_0xf550('0x1e')](_0xf550('0x77'),_0xf550('0x5b'),_0x1aded1,JSON['stringify'](_0x4edfd8));delete _0x4edfd8['name'];if(_0x1aded1===0x191){_0x1aded1=0x190;}_0x135a7a[_0xf550('0x22')](_0x1aded1)[_0xf550('0x30')](_0x3f3759['query']['test']?{'message':'Wrong\x20credentials','statusCode':_0x4edfd8[_0xf550('0x76')]}:_0x4edfd8);});};