Built motion from commit db94841.|2.5.3
[motion2.git] / server / api / intFreshsalesAccount / intFreshsalesAccount.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 _0xf9f9=['includeAll','findAll','rows','params','find','create','body','findOne','FreshsalesConfiguration','order','getConfigurations','AccountId','Subjects','getSubjects','channel','Descriptions','getDescriptions','type','FreshsalesField','remoteUri','username','slice','substring','lastIndexOf','GET','format','%s%s','Token\x20token=','apiKey','email','getFields,\x20%s,\x20%s,\x20%s','getFields','Wrong\x20credentials','html-pdf','eml-format','rimraf','request-promise','moment','bluebird','mustache','path','sox','to-csv','ejs','crypto','jsforce','desk.js','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','../../components/integrations/configuration','client','http','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~!@#$%^&-_=+[{]}.','request','info','FreshsalesAccount,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','result','catch','sendStatus','json','count','offset','limit','apply','save','update','destroy','then','end','stack','name','send','index','map','FreshsalesAccount','rawAttributes','fieldName','key','model','keys','query','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','sort','where','pick','VIRTUAL','filter','merge','options'];(function(_0x1005d2,_0x230209){var _0x1d2c17=function(_0x22991a){while(--_0x22991a){_0x1005d2['push'](_0x1005d2['shift']());}};_0x1d2c17(++_0x230209);}(_0xf9f9,0x90));var _0x9f9f=function(_0x189530,_0x5758cb){_0x189530=_0x189530-0x0;var _0x361a8d=_0xf9f9[_0x189530];return _0x361a8d;};'use strict';var pdf=require(_0x9f9f('0x0'));var emlformat=require(_0x9f9f('0x1'));var rimraf=require(_0x9f9f('0x2'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x9f9f('0x3'));var moment=require(_0x9f9f('0x4'));var BPromise=require(_0x9f9f('0x5'));var Mustache=require(_0x9f9f('0x6'));var util=require('util');var path=require(_0x9f9f('0x7'));var sox=require(_0x9f9f('0x8'));var csv=require(_0x9f9f('0x9'));var ejs=require(_0x9f9f('0xa'));var fs=require('fs');var _=require('lodash');var squel=require('squel');var crypto=require(_0x9f9f('0xb'));var jsforce=require(_0x9f9f('0xc'));var deskjs=require(_0x9f9f('0xd'));var toCsv=require(_0x9f9f('0x9'));var querystring=require('querystring');var Papa=require(_0x9f9f('0xe'));var Redis=require(_0x9f9f('0xf'));var authService=require('../../components/auth/service');var qs=require(_0x9f9f('0x10'));var as=require(_0x9f9f('0x11'));var hardwareService=require(_0x9f9f('0x12'));var logger=require(_0x9f9f('0x13'))(_0x9f9f('0x14'));var utils=require(_0x9f9f('0x15'));var config=require(_0x9f9f('0x16'));var db=require(_0x9f9f('0x17'))['db'];var integrations=require(_0x9f9f('0x18'));var jayson=require('jayson/promise');var client=jayson[_0x9f9f('0x19')][_0x9f9f('0x1a')]({'port':0x232a});function checkPasswordPattern(_0x2638cf,_0x5c81aa,_0x2cefe6){if(_0x2cefe6){var _0x9d5762=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#\$%\^&\*~\-_=+[{\]\}])(?=.{8,})/);if(_0x9d5762[_0x9f9f('0x1b')](_0x5c81aa)){return _0x2638cf;}else{throw new db[(_0x9f9f('0x1c'))][(_0x9f9f('0x1d'))](_0x9f9f('0x1e'));}}else{return _0x2638cf;}}function respondWithRpcPromise(_0x5b5b1f,_0x1ecbb3,_0x1b36a5,_0x2bd3ec){return new BPromise(function(_0xf341f0,_0x4f2258){var _0x15d14d=_0x2bd3ec||client;return _0x15d14d[_0x9f9f('0x1f')](_0x5b5b1f,_0x1b36a5)['then'](function(_0x45a48d){logger[_0x9f9f('0x20')](_0x9f9f('0x21'),_0x1ecbb3,_0x9f9f('0x22'));logger['debug']('FreshsalesAccount,\x20%s,\x20%s,\x20%s',_0x1ecbb3,_0x9f9f('0x22'),JSON[_0x9f9f('0x23')](_0x45a48d));if(_0x45a48d[_0x9f9f('0x24')]){if(_0x45a48d[_0x9f9f('0x24')][_0x9f9f('0x25')]===0x1f4){logger[_0x9f9f('0x24')]('FreshsalesAccount,\x20%s,\x20%s',_0x1ecbb3,_0x45a48d[_0x9f9f('0x24')]['message']);return _0x4f2258(_0x45a48d[_0x9f9f('0x24')][_0x9f9f('0x26')]);}logger[_0x9f9f('0x24')](_0x9f9f('0x21'),_0x1ecbb3,_0x45a48d[_0x9f9f('0x24')][_0x9f9f('0x26')]);return _0xf341f0(_0x45a48d['error'][_0x9f9f('0x26')]);}else{logger[_0x9f9f('0x20')]('FreshsalesAccount,\x20%s,\x20%s',_0x1ecbb3,_0x9f9f('0x22'));_0xf341f0(_0x45a48d[_0x9f9f('0x27')]['message']);}})[_0x9f9f('0x28')](function(_0x45f056){logger['error']('FreshsalesAccount,\x20%s,\x20%s',_0x1ecbb3,_0x45f056);_0x4f2258(_0x45f056);});});}function respondWithStatusCode(_0x51a2c7,_0xe97bb8){_0xe97bb8=_0xe97bb8||0xcc;return function(_0x521582){if(_0x521582){return _0x51a2c7[_0x9f9f('0x29')](_0xe97bb8);}return _0x51a2c7['status'](_0xe97bb8)['end']();};}function respondWithResult(_0x16d778,_0x234e39){_0x234e39=_0x234e39||0xc8;return function(_0x2cbdf6){if(_0x2cbdf6){return _0x16d778['status'](_0x234e39)[_0x9f9f('0x2a')](_0x2cbdf6);}};}function respondWithFilteredResult(_0x2d6ffa,_0x5ecbed){return function(_0x4aed47){if(_0x4aed47){var _0x1739ab=_0x4aed47[_0x9f9f('0x2b')],_0x8fcb79=_0x5ecbed[_0x9f9f('0x2c')],_0x2954ba=_0x5ecbed[_0x9f9f('0x2c')]+_0x5ecbed[_0x9f9f('0x2d')],_0x393f8a;if(_0x2954ba>=_0x1739ab){_0x2954ba=_0x1739ab;_0x393f8a=0xc8;}else{_0x393f8a=0xce;}_0x2d6ffa['status'](_0x393f8a);return _0x2d6ffa['set']('Content-Range',_0x8fcb79+'-'+_0x2954ba+'/'+_0x1739ab)[_0x9f9f('0x2a')](_0x4aed47);}return null;};}function patchUpdates(_0x11986f){return function(_0x38c57c){try{jsonpatch[_0x9f9f('0x2e')](_0x38c57c,_0x11986f,!![]);}catch(_0x414e6e){return BPromise['reject'](_0x414e6e);}return _0x38c57c[_0x9f9f('0x2f')]();};}function saveUpdates(_0x2750bb,_0x2ca9f4){return function(_0x14ee75){if(_0x14ee75){return _0x14ee75[_0x9f9f('0x30')](_0x2750bb)['then'](function(_0xff5448){return _0xff5448;});}return null;};}function removeEntity(_0x42cd78,_0x289043){return function(_0x315b07){if(_0x315b07){return _0x315b07[_0x9f9f('0x31')]()[_0x9f9f('0x32')](function(){_0x42cd78['status'](0xcc)[_0x9f9f('0x33')]();});}};}function handleEntityNotFound(_0x561129,_0x611867){return function(_0x1535bd){if(!_0x1535bd){_0x561129[_0x9f9f('0x29')](0x194);}return _0x1535bd;};}function handleError(_0x53666b,_0x499764){_0x499764=_0x499764||0x1f4;return function(_0x522f57){logger[_0x9f9f('0x24')](_0x522f57[_0x9f9f('0x34')]);if(_0x522f57['name']){delete _0x522f57[_0x9f9f('0x35')];}_0x53666b['status'](_0x499764)[_0x9f9f('0x36')](_0x522f57);};}exports[_0x9f9f('0x37')]=function(_0x39a7ec,_0x271ce4){var _0x3c51e8={},_0x513f52={},_0x1156d5={'count':0x0,'rows':[]};var _0x3632a2=_[_0x9f9f('0x38')](db[_0x9f9f('0x39')][_0x9f9f('0x3a')],function(_0x155228){return{'name':_0x155228[_0x9f9f('0x3b')],'type':_0x155228['type'][_0x9f9f('0x3c')]};});_0x513f52[_0x9f9f('0x3d')]=_[_0x9f9f('0x38')](_0x3632a2,_0x9f9f('0x35'));_0x513f52['query']=_[_0x9f9f('0x3e')](_0x39a7ec[_0x9f9f('0x3f')]);_0x513f52[_0x9f9f('0x40')]=_[_0x9f9f('0x41')](_0x513f52[_0x9f9f('0x3d')],_0x513f52[_0x9f9f('0x3f')]);_0x3c51e8['attributes']=_[_0x9f9f('0x41')](_0x513f52[_0x9f9f('0x3d')],qs[_0x9f9f('0x42')](_0x39a7ec[_0x9f9f('0x3f')][_0x9f9f('0x42')]));_0x3c51e8[_0x9f9f('0x43')]=_0x3c51e8[_0x9f9f('0x43')][_0x9f9f('0x44')]?_0x3c51e8['attributes']:_0x513f52[_0x9f9f('0x3d')];if(!_0x39a7ec['query'][_0x9f9f('0x45')](_0x9f9f('0x46'))){_0x3c51e8['limit']=qs[_0x9f9f('0x2d')](_0x39a7ec[_0x9f9f('0x3f')][_0x9f9f('0x2d')]);_0x3c51e8['offset']=qs[_0x9f9f('0x2c')](_0x39a7ec['query'][_0x9f9f('0x2c')]);}_0x3c51e8['order']=qs[_0x9f9f('0x47')](_0x39a7ec[_0x9f9f('0x3f')][_0x9f9f('0x47')]);_0x3c51e8[_0x9f9f('0x48')]=qs['filters'](_[_0x9f9f('0x49')](_0x39a7ec[_0x9f9f('0x3f')],_0x513f52['filters']),_0x3632a2);if(_0x39a7ec[_0x9f9f('0x3f')]['filter']){_0x3c51e8[_0x9f9f('0x48')]=_['merge'](_0x3c51e8[_0x9f9f('0x48')],{'$or':_[_0x9f9f('0x38')](_0x3632a2,function(_0x32d844){if(_0x32d844['type']!==_0x9f9f('0x4a')){var _0x35b4c7={};_0x35b4c7[_0x32d844[_0x9f9f('0x35')]]={'$like':'%'+_0x39a7ec[_0x9f9f('0x3f')][_0x9f9f('0x4b')]+'%'};return _0x35b4c7;}})});}_0x3c51e8=_[_0x9f9f('0x4c')]({},_0x3c51e8,_0x39a7ec[_0x9f9f('0x4d')]);var _0x2e7a67={'where':_0x3c51e8[_0x9f9f('0x48')]};return db[_0x9f9f('0x39')][_0x9f9f('0x2b')](_0x2e7a67)['then'](function(_0xb8c9a8){_0x1156d5['count']=_0xb8c9a8;if(_0x39a7ec[_0x9f9f('0x3f')][_0x9f9f('0x4e')]){_0x3c51e8['include']=[{'all':!![]}];}return db[_0x9f9f('0x39')][_0x9f9f('0x4f')](_0x3c51e8);})[_0x9f9f('0x32')](function(_0x2a125c){_0x1156d5[_0x9f9f('0x50')]=_0x2a125c;return _0x1156d5;})[_0x9f9f('0x32')](respondWithFilteredResult(_0x271ce4,_0x3c51e8))[_0x9f9f('0x28')](handleError(_0x271ce4,null));};exports['show']=function(_0x158886,_0x41f272){var _0x161828={'raw':![],'where':{'id':_0x158886[_0x9f9f('0x51')]['id']}},_0x47bda6={};_0x47bda6[_0x9f9f('0x3d')]=_[_0x9f9f('0x3e')](db[_0x9f9f('0x39')][_0x9f9f('0x3a')]);_0x47bda6[_0x9f9f('0x3f')]=_[_0x9f9f('0x3e')](_0x158886[_0x9f9f('0x3f')]);_0x47bda6[_0x9f9f('0x40')]=_['intersection'](_0x47bda6[_0x9f9f('0x3d')],_0x47bda6[_0x9f9f('0x3f')]);_0x161828[_0x9f9f('0x43')]=_[_0x9f9f('0x41')](_0x47bda6[_0x9f9f('0x3d')],qs[_0x9f9f('0x42')](_0x158886[_0x9f9f('0x3f')][_0x9f9f('0x42')]));_0x161828['attributes']=_0x161828[_0x9f9f('0x43')]['length']?_0x161828[_0x9f9f('0x43')]:_0x47bda6[_0x9f9f('0x3d')];if(_0x158886['query'][_0x9f9f('0x4e')]){_0x161828['include']=[{'all':!![]}];}_0x161828=_['merge']({},_0x161828,_0x158886[_0x9f9f('0x4d')]);return db[_0x9f9f('0x39')][_0x9f9f('0x52')](_0x161828)[_0x9f9f('0x32')](handleEntityNotFound(_0x41f272,null))[_0x9f9f('0x32')](respondWithResult(_0x41f272,null))[_0x9f9f('0x28')](handleError(_0x41f272,null));};exports[_0x9f9f('0x53')]=function(_0x8af76f,_0x4bbecb){return db[_0x9f9f('0x39')][_0x9f9f('0x53')](_0x8af76f['body'],{})[_0x9f9f('0x32')](respondWithResult(_0x4bbecb,0xc9))[_0x9f9f('0x28')](handleError(_0x4bbecb,null));};exports['update']=function(_0x58dc9f,_0x571e45){if(_0x58dc9f[_0x9f9f('0x54')]['id']){delete _0x58dc9f[_0x9f9f('0x54')]['id'];}return db[_0x9f9f('0x39')][_0x9f9f('0x52')]({'where':{'id':_0x58dc9f[_0x9f9f('0x51')]['id']}})['then'](handleEntityNotFound(_0x571e45,null))['then'](saveUpdates(_0x58dc9f[_0x9f9f('0x54')],null))[_0x9f9f('0x32')](respondWithResult(_0x571e45,null))[_0x9f9f('0x28')](handleError(_0x571e45,null));};exports[_0x9f9f('0x31')]=function(_0x1e0a04,_0x4e5d98){return db[_0x9f9f('0x39')][_0x9f9f('0x52')]({'where':{'id':_0x1e0a04[_0x9f9f('0x51')]['id']}})[_0x9f9f('0x32')](handleEntityNotFound(_0x4e5d98,null))[_0x9f9f('0x32')](removeEntity(_0x4e5d98,null))[_0x9f9f('0x28')](handleError(_0x4e5d98,null));};exports['getConfigurations']=function(_0x1b168c,_0x498b87,_0x13f222){var _0x3af29c={};var _0x361b7c={};var _0x51ee4e;var _0xf2e7b2;return db[_0x9f9f('0x39')][_0x9f9f('0x55')]({'where':{'id':_0x1b168c['params']['id']}})[_0x9f9f('0x32')](handleEntityNotFound(_0x498b87,null))['then'](function(_0x46892d){if(_0x46892d){_0x51ee4e=_0x46892d;_0x361b7c[_0x9f9f('0x3d')]=_['keys'](db[_0x9f9f('0x56')][_0x9f9f('0x3a')]);_0x361b7c[_0x9f9f('0x3f')]=_[_0x9f9f('0x3e')](_0x1b168c['query']);_0x361b7c[_0x9f9f('0x40')]=_[_0x9f9f('0x41')](_0x361b7c[_0x9f9f('0x3d')],_0x361b7c[_0x9f9f('0x3f')]);_0x3af29c[_0x9f9f('0x43')]=_['intersection'](_0x361b7c[_0x9f9f('0x3d')],qs[_0x9f9f('0x42')](_0x1b168c[_0x9f9f('0x3f')][_0x9f9f('0x42')]));_0x3af29c[_0x9f9f('0x43')]=_0x3af29c[_0x9f9f('0x43')][_0x9f9f('0x44')]?_0x3af29c[_0x9f9f('0x43')]:_0x361b7c[_0x9f9f('0x3d')];_0x3af29c[_0x9f9f('0x57')]=qs[_0x9f9f('0x47')](_0x1b168c[_0x9f9f('0x3f')][_0x9f9f('0x47')]);_0x3af29c[_0x9f9f('0x48')]=qs[_0x9f9f('0x40')](_[_0x9f9f('0x49')](_0x1b168c[_0x9f9f('0x3f')],_0x361b7c[_0x9f9f('0x40')]));if(_0x1b168c[_0x9f9f('0x3f')]['filter']){_0x3af29c['where']=_[_0x9f9f('0x4c')](_0x3af29c[_0x9f9f('0x48')],{'$or':_[_0x9f9f('0x38')](_0x3af29c[_0x9f9f('0x43')],function(_0x3e1d29){var _0x782aff={};_0x782aff[_0x3e1d29]={'$like':'%'+_0x1b168c['query'][_0x9f9f('0x4b')]+'%'};return _0x782aff;})});}_0x3af29c=_[_0x9f9f('0x4c')]({},_0x3af29c,_0x1b168c['options']);return _0x51ee4e[_0x9f9f('0x58')](_0x3af29c);}})[_0x9f9f('0x32')](function(_0x3dc444){if(_0x3dc444){_0xf2e7b2=_0x3dc444[_0x9f9f('0x44')];if(!_0x1b168c['query']['hasOwnProperty']('nolimit')){_0x3af29c[_0x9f9f('0x2d')]=qs[_0x9f9f('0x2d')](_0x1b168c[_0x9f9f('0x3f')]['limit']);_0x3af29c[_0x9f9f('0x2c')]=qs[_0x9f9f('0x2c')](_0x1b168c[_0x9f9f('0x3f')][_0x9f9f('0x2c')]);}return _0x51ee4e[_0x9f9f('0x58')](_0x3af29c);}})[_0x9f9f('0x32')](function(_0x304567){if(_0x304567){return _0x304567?{'count':_0xf2e7b2,'rows':_0x304567}:null;}})[_0x9f9f('0x32')](respondWithResult(_0x498b87,null))[_0x9f9f('0x28')](handleError(_0x498b87,null));};exports['addConfiguration']=function(_0x5c0a87,_0x296078,_0x56aa61){if(_0x5c0a87[_0x9f9f('0x54')]['id']){delete _0x5c0a87['body']['id'];}return db['FreshsalesAccount']['findOne']({'where':{'id':_0x5c0a87['params']['id']}})[_0x9f9f('0x32')](handleEntityNotFound(_0x296078,null))['then'](function(_0x57c47c){if(_0x57c47c){_0x5c0a87['body'][_0x9f9f('0x59')]=_0x57c47c['id'];_0x5c0a87[_0x9f9f('0x54')][_0x9f9f('0x5a')]=integrations[_0x9f9f('0x5b')](_0x5c0a87[_0x9f9f('0x54')][_0x9f9f('0x5c')],_0x5c0a87[_0x9f9f('0x54')]['type']);_0x5c0a87['body'][_0x9f9f('0x5d')]=integrations[_0x9f9f('0x5e')](_0x5c0a87[_0x9f9f('0x54')][_0x9f9f('0x5c')],_0x5c0a87[_0x9f9f('0x54')][_0x9f9f('0x5f')]);return db['FreshsalesConfiguration'][_0x9f9f('0x53')](_0x5c0a87[_0x9f9f('0x54')],{'include':[{'model':db[_0x9f9f('0x60')],'as':_0x9f9f('0x5a')},{'model':db[_0x9f9f('0x60')],'as':_0x9f9f('0x5d')}]});}return null;})[_0x9f9f('0x32')](respondWithResult(_0x296078,null))[_0x9f9f('0x28')](handleError(_0x296078,null));};exports['getFields']=function(_0x95f30b,_0x3abc34,_0x969de4){var _0x37b371='';var _0x280546='';return db[_0x9f9f('0x39')][_0x9f9f('0x55')]({'where':{'id':_0x95f30b[_0x9f9f('0x51')]['id']},'attributes':['id','username',_0x9f9f('0x61'),'apiKey']})['then'](handleEntityNotFound(_0x3abc34,null))[_0x9f9f('0x32')](function(_0x2ba9bc){if(_0x2ba9bc){_0x280546=_0x2ba9bc[_0x9f9f('0x62')];_0x37b371=_0x2ba9bc[_0x9f9f('0x61')];var _0x30e9a1=_0x2ba9bc[_0x9f9f('0x61')][_0x9f9f('0x63')](-0x1);if(_0x30e9a1==='/'){_0x37b371=_0x37b371[_0x9f9f('0x64')](0x0,_0x37b371[_0x9f9f('0x65')](_0x30e9a1));}return rp({'method':_0x9f9f('0x66'),'uri':util[_0x9f9f('0x67')](_0x9f9f('0x68'),_0x37b371,'/api/selector/owners'),'headers':{'Authorization':_0x9f9f('0x69')+_0x2ba9bc[_0x9f9f('0x6a')]},'json':!![]});}})[_0x9f9f('0x32')](function(_0x43b102){if(_0x43b102){var _0x337f5b=_0x43b102['users'];var _0x219f82=![];var _0x578e76=null;for(_0x578e76=0x0;_0x578e76<_0x337f5b[_0x9f9f('0x44')];_0x578e76++){if(_0x337f5b[_0x578e76][_0x9f9f('0x6b')]===_0x280546){_0x219f82=!![];}}if(_0x219f82){return!![];}else{throw new Error({'statusCode':0x194,'message':'Requested\x20Resource\x20not\x20Found'});}}})[_0x9f9f('0x32')](respondWithResult(_0x3abc34,null))['catch'](function(_0x8de87a){var _0x20f6d9=_0x95f30b[_0x9f9f('0x3f')][_0x9f9f('0x1b')]?0x1f4:_0x8de87a['statusCode']||0x1f4;logger[_0x9f9f('0x24')](_0x9f9f('0x6c'),_0x9f9f('0x6d'),_0x20f6d9,JSON[_0x9f9f('0x23')](_0x8de87a));delete _0x8de87a[_0x9f9f('0x35')];if(_0x20f6d9===0x191){_0x20f6d9=0x190;}_0x3abc34['status'](_0x20f6d9)['send'](_0x95f30b['query'][_0x9f9f('0x1b')]?{'message':_0x9f9f('0x6e'),'statusCode':_0x8de87a['statusCode']}:_0x8de87a);});};