Built motion from commit (unavailable).|2.5.21
[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 _0xcf5f=['base64','subject','agent','priority','source','ticket_type','includes','default','custom_text','custom_dropdown','label','choices','capitalize','statusCode','getFields,\x20%s,\x20%s,\x20%s','getFields','send','test','html-pdf','eml-format','fast-json-patch','request-promise','moment','mustache','util','path','sox','to-csv','ejs','lodash','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','client','then','request\x20sent','debug','FreshdeskAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','FreshdeskAccount,\x20%s,\x20%s','info','catch','sendStatus','status','end','json','count','offset','limit','Content-Range','reject','save','destroy','stack','name','index','map','FreshdeskAccount','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','pick','filter','VIRTUAL','merge','options','include','findAll','rows','show','params','includeAll','find','create','body','update','getConfigurations','findOne','nolimit','addConfiguration','AccountId','Subjects','getSubjects','channel','Descriptions','FreshdeskConfiguration','FreshdeskField','remoteUri','apiKey','slice','GET','format','%s/%s','api/v2/ticket_fields','Basic\x20%s','%s:X','toString'];(function(_0x22f788,_0x3dea32){var _0x1e06c4=function(_0x2c1a8d){while(--_0x2c1a8d){_0x22f788['push'](_0x22f788['shift']());}};_0x1e06c4(++_0x3dea32);}(_0xcf5f,0x10a));var _0xfcf5=function(_0x25548b,_0x2bc1b9){_0x25548b=_0x25548b-0x0;var _0x5de8b0=_0xcf5f[_0x25548b];return _0x5de8b0;};'use strict';var pdf=require(_0xfcf5('0x0'));var emlformat=require(_0xfcf5('0x1'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0xfcf5('0x2'));var rp=require(_0xfcf5('0x3'));var moment=require(_0xfcf5('0x4'));var BPromise=require('bluebird');var Mustache=require(_0xfcf5('0x5'));var util=require(_0xfcf5('0x6'));var path=require(_0xfcf5('0x7'));var sox=require(_0xfcf5('0x8'));var csv=require(_0xfcf5('0x9'));var ejs=require(_0xfcf5('0xa'));var fs=require('fs');var _=require(_0xfcf5('0xb'));var squel=require('squel');var crypto=require(_0xfcf5('0xc'));var jsforce=require(_0xfcf5('0xd'));var deskjs=require(_0xfcf5('0xe'));var toCsv=require(_0xfcf5('0x9'));var querystring=require(_0xfcf5('0xf'));var Papa=require(_0xfcf5('0x10'));var Redis=require(_0xfcf5('0x11'));var authService=require(_0xfcf5('0x12'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xfcf5('0x13'));var logger=require(_0xfcf5('0x14'))(_0xfcf5('0x15'));var utils=require('../../config/utils');var config=require(_0xfcf5('0x16'));var licenseUtil=require(_0xfcf5('0x17'));var db=require(_0xfcf5('0x18'))['db'];var integrations=require(_0xfcf5('0x19'));var jayson=require('jayson/promise');var client=jayson[_0xfcf5('0x1a')]['http']({'port':0x232a});function respondWithRpcPromise(_0xcc0cb,_0x3fff3d,_0x4bd06a,_0x506eed){return new BPromise(function(_0x19c527,_0x15518c){var _0x206683=_0x506eed||client;return _0x206683['request'](_0xcc0cb,_0x4bd06a)[_0xfcf5('0x1b')](function(_0x174058){logger['info']('FreshdeskAccount,\x20%s,\x20%s',_0x3fff3d,_0xfcf5('0x1c'));logger[_0xfcf5('0x1d')](_0xfcf5('0x1e'),_0x3fff3d,_0xfcf5('0x1c'),JSON[_0xfcf5('0x1f')](_0x174058));if(_0x174058[_0xfcf5('0x20')]){if(_0x174058[_0xfcf5('0x20')][_0xfcf5('0x21')]===0x1f4){logger[_0xfcf5('0x20')]('FreshdeskAccount,\x20%s,\x20%s',_0x3fff3d,_0x174058[_0xfcf5('0x20')]['message']);return _0x15518c(_0x174058['error'][_0xfcf5('0x22')]);}logger[_0xfcf5('0x20')](_0xfcf5('0x23'),_0x3fff3d,_0x174058['error'][_0xfcf5('0x22')]);return _0x19c527(_0x174058[_0xfcf5('0x20')]['message']);}else{logger[_0xfcf5('0x24')](_0xfcf5('0x23'),_0x3fff3d,_0xfcf5('0x1c'));_0x19c527(_0x174058['result'][_0xfcf5('0x22')]);}})[_0xfcf5('0x25')](function(_0x18a5bd){logger['error'](_0xfcf5('0x23'),_0x3fff3d,_0x18a5bd);_0x15518c(_0x18a5bd);});});}function respondWithStatusCode(_0x2f95d7,_0x1d39cc){_0x1d39cc=_0x1d39cc||0xcc;return function(_0x2bbbe6){if(_0x2bbbe6){return _0x2f95d7[_0xfcf5('0x26')](_0x1d39cc);}return _0x2f95d7[_0xfcf5('0x27')](_0x1d39cc)[_0xfcf5('0x28')]();};}function respondWithResult(_0x549d8f,_0x5da571){_0x5da571=_0x5da571||0xc8;return function(_0x15d0e0){if(_0x15d0e0){return _0x549d8f[_0xfcf5('0x27')](_0x5da571)[_0xfcf5('0x29')](_0x15d0e0);}};}function respondWithFilteredResult(_0x22bbc7,_0x4711ca){return function(_0x3a519d){if(_0x3a519d){var _0x287f8f=_0x3a519d[_0xfcf5('0x2a')],_0x164354=_0x4711ca['offset'],_0x39b241=_0x4711ca[_0xfcf5('0x2b')]+_0x4711ca[_0xfcf5('0x2c')],_0x3e5271;if(_0x39b241>=_0x287f8f){_0x39b241=_0x287f8f;_0x3e5271=0xc8;}else{_0x3e5271=0xce;}_0x22bbc7[_0xfcf5('0x27')](_0x3e5271);return _0x22bbc7['set'](_0xfcf5('0x2d'),_0x164354+'-'+_0x39b241+'/'+_0x287f8f)[_0xfcf5('0x29')](_0x3a519d);}return null;};}function patchUpdates(_0x122821){return function(_0x5f43b7){try{jsonpatch['apply'](_0x5f43b7,_0x122821,!![]);}catch(_0x302e50){return BPromise[_0xfcf5('0x2e')](_0x302e50);}return _0x5f43b7[_0xfcf5('0x2f')]();};}function saveUpdates(_0x4b4060,_0x4438ce){return function(_0x30794a){if(_0x30794a){return _0x30794a['update'](_0x4b4060)['then'](function(_0x483a3d){return _0x483a3d;});}return null;};}function removeEntity(_0x1691e5,_0x104aa9){return function(_0x16a643){if(_0x16a643){return _0x16a643[_0xfcf5('0x30')]()[_0xfcf5('0x1b')](function(){_0x1691e5[_0xfcf5('0x27')](0xcc)[_0xfcf5('0x28')]();});}};}function handleEntityNotFound(_0xa7f4b9,_0x584d94){return function(_0x41d850){if(!_0x41d850){_0xa7f4b9[_0xfcf5('0x26')](0x194);}return _0x41d850;};}function handleError(_0x2fc2c2,_0x311235){_0x311235=_0x311235||0x1f4;return function(_0x1b2809){logger[_0xfcf5('0x20')](_0x1b2809[_0xfcf5('0x31')]);if(_0x1b2809[_0xfcf5('0x32')]){delete _0x1b2809[_0xfcf5('0x32')];}_0x2fc2c2[_0xfcf5('0x27')](_0x311235)['send'](_0x1b2809);};}exports[_0xfcf5('0x33')]=function(_0xd11af2,_0x2d257a){var _0x17a088={},_0x3d2f9c={},_0x1b4341={'count':0x0,'rows':[]};var _0x26787a=_[_0xfcf5('0x34')](db[_0xfcf5('0x35')][_0xfcf5('0x36')],function(_0x1fb081){return{'name':_0x1fb081[_0xfcf5('0x37')],'type':_0x1fb081[_0xfcf5('0x38')][_0xfcf5('0x39')]};});_0x3d2f9c[_0xfcf5('0x3a')]=_[_0xfcf5('0x34')](_0x26787a,_0xfcf5('0x32'));_0x3d2f9c[_0xfcf5('0x3b')]=_[_0xfcf5('0x3c')](_0xd11af2['query']);_0x3d2f9c[_0xfcf5('0x3d')]=_[_0xfcf5('0x3e')](_0x3d2f9c[_0xfcf5('0x3a')],_0x3d2f9c[_0xfcf5('0x3b')]);_0x17a088[_0xfcf5('0x3f')]=_[_0xfcf5('0x3e')](_0x3d2f9c[_0xfcf5('0x3a')],qs[_0xfcf5('0x40')](_0xd11af2['query']['fields']));_0x17a088[_0xfcf5('0x3f')]=_0x17a088[_0xfcf5('0x3f')][_0xfcf5('0x41')]?_0x17a088[_0xfcf5('0x3f')]:_0x3d2f9c[_0xfcf5('0x3a')];if(!_0xd11af2[_0xfcf5('0x3b')][_0xfcf5('0x42')]('nolimit')){_0x17a088[_0xfcf5('0x2c')]=qs[_0xfcf5('0x2c')](_0xd11af2[_0xfcf5('0x3b')][_0xfcf5('0x2c')]);_0x17a088[_0xfcf5('0x2b')]=qs[_0xfcf5('0x2b')](_0xd11af2[_0xfcf5('0x3b')][_0xfcf5('0x2b')]);}_0x17a088[_0xfcf5('0x43')]=qs[_0xfcf5('0x44')](_0xd11af2[_0xfcf5('0x3b')][_0xfcf5('0x44')]);_0x17a088[_0xfcf5('0x45')]=qs['filters'](_[_0xfcf5('0x46')](_0xd11af2[_0xfcf5('0x3b')],_0x3d2f9c[_0xfcf5('0x3d')]),_0x26787a);if(_0xd11af2[_0xfcf5('0x3b')][_0xfcf5('0x47')]){_0x17a088[_0xfcf5('0x45')]=_['merge'](_0x17a088['where'],{'$or':_[_0xfcf5('0x34')](_0x26787a,function(_0x58ec4e){if(_0x58ec4e[_0xfcf5('0x38')]!==_0xfcf5('0x48')){var _0x721877={};_0x721877[_0x58ec4e[_0xfcf5('0x32')]]={'$like':'%'+_0xd11af2[_0xfcf5('0x3b')][_0xfcf5('0x47')]+'%'};return _0x721877;}})});}_0x17a088=_[_0xfcf5('0x49')]({},_0x17a088,_0xd11af2[_0xfcf5('0x4a')]);var _0xcb0da3={'where':_0x17a088[_0xfcf5('0x45')]};return db['FreshdeskAccount']['count'](_0xcb0da3)[_0xfcf5('0x1b')](function(_0x1aa7ce){_0x1b4341[_0xfcf5('0x2a')]=_0x1aa7ce;if(_0xd11af2[_0xfcf5('0x3b')]['includeAll']){_0x17a088[_0xfcf5('0x4b')]=[{'all':!![]}];}return db[_0xfcf5('0x35')][_0xfcf5('0x4c')](_0x17a088);})[_0xfcf5('0x1b')](function(_0xf193bd){_0x1b4341[_0xfcf5('0x4d')]=_0xf193bd;return _0x1b4341;})['then'](respondWithFilteredResult(_0x2d257a,_0x17a088))['catch'](handleError(_0x2d257a,null));};exports[_0xfcf5('0x4e')]=function(_0x465c79,_0x94037){var _0x312013={'raw':![],'where':{'id':_0x465c79[_0xfcf5('0x4f')]['id']}},_0x539e01={};_0x539e01['model']=_[_0xfcf5('0x3c')](db[_0xfcf5('0x35')][_0xfcf5('0x36')]);_0x539e01[_0xfcf5('0x3b')]=_[_0xfcf5('0x3c')](_0x465c79[_0xfcf5('0x3b')]);_0x539e01[_0xfcf5('0x3d')]=_[_0xfcf5('0x3e')](_0x539e01['model'],_0x539e01[_0xfcf5('0x3b')]);_0x312013[_0xfcf5('0x3f')]=_['intersection'](_0x539e01[_0xfcf5('0x3a')],qs[_0xfcf5('0x40')](_0x465c79[_0xfcf5('0x3b')][_0xfcf5('0x40')]));_0x312013['attributes']=_0x312013[_0xfcf5('0x3f')]['length']?_0x312013[_0xfcf5('0x3f')]:_0x539e01[_0xfcf5('0x3a')];if(_0x465c79['query'][_0xfcf5('0x50')]){_0x312013[_0xfcf5('0x4b')]=[{'all':!![]}];}_0x312013=_[_0xfcf5('0x49')]({},_0x312013,_0x465c79[_0xfcf5('0x4a')]);return db[_0xfcf5('0x35')][_0xfcf5('0x51')](_0x312013)['then'](handleEntityNotFound(_0x94037,null))[_0xfcf5('0x1b')](respondWithResult(_0x94037,null))['catch'](handleError(_0x94037,null));};exports[_0xfcf5('0x52')]=function(_0x294d65,_0x31ae2c){return db['FreshdeskAccount'][_0xfcf5('0x52')](_0x294d65[_0xfcf5('0x53')],{})[_0xfcf5('0x1b')](respondWithResult(_0x31ae2c,0xc9))[_0xfcf5('0x25')](handleError(_0x31ae2c,null));};exports[_0xfcf5('0x54')]=function(_0x25e5d5,_0x3e9610){if(_0x25e5d5['body']['id']){delete _0x25e5d5[_0xfcf5('0x53')]['id'];}return db[_0xfcf5('0x35')][_0xfcf5('0x51')]({'where':{'id':_0x25e5d5[_0xfcf5('0x4f')]['id']}})[_0xfcf5('0x1b')](handleEntityNotFound(_0x3e9610,null))['then'](saveUpdates(_0x25e5d5[_0xfcf5('0x53')],null))[_0xfcf5('0x1b')](respondWithResult(_0x3e9610,null))[_0xfcf5('0x25')](handleError(_0x3e9610,null));};exports[_0xfcf5('0x30')]=function(_0x24ae5e,_0x209d0a){return db[_0xfcf5('0x35')]['find']({'where':{'id':_0x24ae5e[_0xfcf5('0x4f')]['id']}})['then'](handleEntityNotFound(_0x209d0a,null))[_0xfcf5('0x1b')](removeEntity(_0x209d0a,null))[_0xfcf5('0x25')](handleError(_0x209d0a,null));};exports[_0xfcf5('0x55')]=function(_0x52d454,_0x35b8b9,_0x3df970){var _0x9f32d1={};var _0x2bd8c1={};var _0x347fe9;var _0x1447b6;return db[_0xfcf5('0x35')][_0xfcf5('0x56')]({'where':{'id':_0x52d454[_0xfcf5('0x4f')]['id']}})[_0xfcf5('0x1b')](handleEntityNotFound(_0x35b8b9,null))[_0xfcf5('0x1b')](function(_0x37d6b4){if(_0x37d6b4){_0x347fe9=_0x37d6b4;_0x2bd8c1['model']=_[_0xfcf5('0x3c')](db['FreshdeskConfiguration'][_0xfcf5('0x36')]);_0x2bd8c1['query']=_['keys'](_0x52d454[_0xfcf5('0x3b')]);_0x2bd8c1['filters']=_[_0xfcf5('0x3e')](_0x2bd8c1[_0xfcf5('0x3a')],_0x2bd8c1[_0xfcf5('0x3b')]);_0x9f32d1[_0xfcf5('0x3f')]=_[_0xfcf5('0x3e')](_0x2bd8c1[_0xfcf5('0x3a')],qs[_0xfcf5('0x40')](_0x52d454['query'][_0xfcf5('0x40')]));_0x9f32d1[_0xfcf5('0x3f')]=_0x9f32d1[_0xfcf5('0x3f')][_0xfcf5('0x41')]?_0x9f32d1[_0xfcf5('0x3f')]:_0x2bd8c1[_0xfcf5('0x3a')];_0x9f32d1[_0xfcf5('0x43')]=qs['sort'](_0x52d454[_0xfcf5('0x3b')]['sort']);_0x9f32d1[_0xfcf5('0x45')]=qs[_0xfcf5('0x3d')](_[_0xfcf5('0x46')](_0x52d454[_0xfcf5('0x3b')],_0x2bd8c1[_0xfcf5('0x3d')]));if(_0x52d454[_0xfcf5('0x3b')]['filter']){_0x9f32d1[_0xfcf5('0x45')]=_[_0xfcf5('0x49')](_0x9f32d1[_0xfcf5('0x45')],{'$or':_[_0xfcf5('0x34')](_0x9f32d1[_0xfcf5('0x3f')],function(_0x29f03d){var _0x3a42c8={};_0x3a42c8[_0x29f03d]={'$like':'%'+_0x52d454['query']['filter']+'%'};return _0x3a42c8;})});}_0x9f32d1=_[_0xfcf5('0x49')]({},_0x9f32d1,_0x52d454[_0xfcf5('0x4a')]);return _0x347fe9[_0xfcf5('0x55')](_0x9f32d1);}})[_0xfcf5('0x1b')](function(_0x1b3649){if(_0x1b3649){_0x1447b6=_0x1b3649[_0xfcf5('0x41')];if(!_0x52d454[_0xfcf5('0x3b')][_0xfcf5('0x42')](_0xfcf5('0x57'))){_0x9f32d1['limit']=qs['limit'](_0x52d454[_0xfcf5('0x3b')][_0xfcf5('0x2c')]);_0x9f32d1[_0xfcf5('0x2b')]=qs['offset'](_0x52d454[_0xfcf5('0x3b')][_0xfcf5('0x2b')]);}return _0x347fe9[_0xfcf5('0x55')](_0x9f32d1);}})['then'](function(_0x20afb3){if(_0x20afb3){return _0x20afb3?{'count':_0x1447b6,'rows':_0x20afb3}:null;}})[_0xfcf5('0x1b')](respondWithResult(_0x35b8b9,null))['catch'](handleError(_0x35b8b9,null));};exports[_0xfcf5('0x58')]=function(_0x1b9b41,_0x413995,_0x1b1943){if(_0x1b9b41[_0xfcf5('0x53')]['id']){delete _0x1b9b41[_0xfcf5('0x53')]['id'];}return db[_0xfcf5('0x35')][_0xfcf5('0x56')]({'where':{'id':_0x1b9b41[_0xfcf5('0x4f')]['id']}})[_0xfcf5('0x1b')](handleEntityNotFound(_0x413995,null))[_0xfcf5('0x1b')](function(_0x4fdbae){if(_0x4fdbae){_0x1b9b41[_0xfcf5('0x53')][_0xfcf5('0x59')]=_0x4fdbae['id'];_0x1b9b41[_0xfcf5('0x53')][_0xfcf5('0x5a')]=integrations[_0xfcf5('0x5b')](_0x1b9b41['body'][_0xfcf5('0x5c')],_0x1b9b41['body'][_0xfcf5('0x38')]);_0x1b9b41[_0xfcf5('0x53')][_0xfcf5('0x5d')]=integrations['getDescriptions'](_0x1b9b41['body'][_0xfcf5('0x5c')],_0x1b9b41[_0xfcf5('0x53')][_0xfcf5('0x38')]);return db[_0xfcf5('0x5e')][_0xfcf5('0x52')](_0x1b9b41[_0xfcf5('0x53')],{'include':[{'model':db['FreshdeskField'],'as':_0xfcf5('0x5a')},{'model':db[_0xfcf5('0x5f')],'as':_0xfcf5('0x5d')}]});}return null;})[_0xfcf5('0x1b')](respondWithResult(_0x413995,null))[_0xfcf5('0x25')](handleError(_0x413995,null));};exports['getFields']=function(_0x30f6a4,_0xe87f5a,_0x5d5bfa){var _0x2990e5='';return db[_0xfcf5('0x35')][_0xfcf5('0x56')]({'where':{'id':_0x30f6a4['params']['id']},'attributes':['id',_0xfcf5('0x60'),_0xfcf5('0x61')]})[_0xfcf5('0x1b')](handleEntityNotFound(_0xe87f5a,null))['then'](function(_0x3045da){if(_0x3045da){_0x2990e5=_0x3045da['remoteUri'];var _0x22fd6f=_0x2990e5[_0xfcf5('0x62')](-0x1);if(_0x22fd6f==='/'){_0x2990e5=_0x2990e5['substring'](0x0,_0x2990e5['lastIndexOf'](_0x22fd6f));}return rp({'method':_0xfcf5('0x63'),'uri':util[_0xfcf5('0x64')](_0xfcf5('0x65'),_0x2990e5,_0xfcf5('0x66')),'headers':{'Authorization':util['format'](_0xfcf5('0x67'),new Buffer(util[_0xfcf5('0x64')](_0xfcf5('0x68'),_0x3045da['apiKey']))[_0xfcf5('0x69')](_0xfcf5('0x6a')))},'json':!![]});}})['then'](function(_0x501317){if(_0x501317){var _0x1d1cc1=['requester',_0xfcf5('0x6b'),_0xfcf5('0x6c'),'description',_0xfcf5('0x27'),_0xfcf5('0x6d'),'group',_0xfcf5('0x6e'),_0xfcf5('0x6f'),'group','company'];_['remove'](_0x501317,function(_0x8ea0de){return _0x1d1cc1[_0xfcf5('0x70')](_0x8ea0de[_0xfcf5('0x32')])||!_0x8ea0de[_0xfcf5('0x71')]&&_0x8ea0de[_0xfcf5('0x38')]!==_0xfcf5('0x72')&&_0x8ea0de[_0xfcf5('0x38')]!==_0xfcf5('0x73');});return{'count':_0x501317[_0xfcf5('0x41')],'rows':_[_0xfcf5('0x34')](_0x501317,function(_0x22251c){return{'id':_0x22251c['name'],'name':_0x22251c[_0xfcf5('0x74')],'custom':!_0x22251c[_0xfcf5('0x71')],'options':_0x22251c[_0xfcf5('0x75')]?_[_0xfcf5('0x34')](_0x22251c['choices'],function(_0x2480c5){return{'name':_[_0xfcf5('0x76')](_0x2480c5),'value':_0x2480c5};}):[]};})};}})['then'](respondWithResult(_0xe87f5a,null))[_0xfcf5('0x25')](function(_0x12646b){var _0x5f1199=_0x30f6a4['query']['test']?0x1f4:_0x12646b[_0xfcf5('0x77')]||0x1f4;logger[_0xfcf5('0x20')](_0xfcf5('0x78'),_0xfcf5('0x79'),_0x5f1199,JSON[_0xfcf5('0x1f')](_0x12646b));delete _0x12646b['name'];if(_0x5f1199===0x191){_0x5f1199=0x190;}_0xe87f5a['status'](_0x5f1199)[_0xfcf5('0x7a')](_0x30f6a4['query'][_0xfcf5('0x7b')]?{'message':'Wrong\x20credentials','statusCode':_0x12646b['statusCode']}:_0x12646b);});};