29260b86b3cd8e6f7ae9ad412f4c534855037be1
[motion2.git] / server / api / cmCompany / cmCompany.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 _0x60d5=['params','intersection','fields','length','include','find','create','body','user','UserProfileSection','autoAssociation','UserProfileResource','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','CmContact','ids','rawAttributes','getContacts','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','end','json','undefined','limit','offset','status','set','Content-Range','reject','then','destroy','get','Companies','stack','name','index','CmCompany','describe','model','keys','query','filters','attributes','hasOwnProperty','nolimit','order','sort','pick','filter','where','merge','options','count','includeAll','findAll','rows','catch','show'];(function(_0x2ec438,_0x4c25e2){var _0x3d1baa=function(_0x59f81c){while(--_0x59f81c){_0x2ec438['push'](_0x2ec438['shift']());}};_0x3d1baa(++_0x4c25e2);}(_0x60d5,0x16f));var _0x560d=function(_0x55c747,_0xc8e95a){_0x55c747=_0x55c747-0x0;var _0xb7c5c=_0x60d5[_0x55c747];return _0xb7c5c;};'use strict';var emlformat=require(_0x560d('0x0'));var rimraf=require(_0x560d('0x1'));var zipdir=require(_0x560d('0x2'));var jsonpatch=require(_0x560d('0x3'));var rp=require(_0x560d('0x4'));var moment=require(_0x560d('0x5'));var BPromise=require(_0x560d('0x6'));var Mustache=require(_0x560d('0x7'));var util=require(_0x560d('0x8'));var path=require(_0x560d('0x9'));var sox=require(_0x560d('0xa'));var csv=require(_0x560d('0xb'));var ejs=require(_0x560d('0xc'));var fs=require('fs');var fs_extra=require(_0x560d('0xd'));var _=require(_0x560d('0xe'));var squel=require(_0x560d('0xf'));var crypto=require(_0x560d('0x10'));var jsforce=require(_0x560d('0x11'));var deskjs=require('desk.js');var toCsv=require(_0x560d('0xb'));var querystring=require(_0x560d('0x12'));var Papa=require(_0x560d('0x13'));var Redis=require(_0x560d('0x14'));var authService=require(_0x560d('0x15'));var qs=require(_0x560d('0x16'));var as=require(_0x560d('0x17'));var hardwareService=require(_0x560d('0x18'));var logger=require(_0x560d('0x19'))('api');var utils=require(_0x560d('0x1a'));var config=require(_0x560d('0x1b'));var licenseUtil=require(_0x560d('0x1c'));var db=require(_0x560d('0x1d'))['db'];function respondWithStatusCode(_0x558361,_0xf29b00){_0xf29b00=_0xf29b00||0xcc;return function(_0x47952b){if(_0x47952b){return _0x558361['sendStatus'](_0xf29b00);}return _0x558361['status'](_0xf29b00)[_0x560d('0x1e')]();};}function respondWithResult(_0x3ce734,_0x2a8a9c){_0x2a8a9c=_0x2a8a9c||0xc8;return function(_0x74280d){if(_0x74280d){return _0x3ce734['status'](_0x2a8a9c)[_0x560d('0x1f')](_0x74280d);}};}function respondWithFilteredResult(_0xc31961,_0x18ad09){return function(_0x5e462d){if(_0x5e462d){var _0x396136=typeof _0x18ad09['offset']===_0x560d('0x20')&&typeof _0x18ad09[_0x560d('0x21')]===_0x560d('0x20');var _0x4210bf=_0x5e462d['count'];var _0x1d9236=_0x396136?0x0:_0x18ad09[_0x560d('0x22')];var _0x22df66=_0x396136?_0x5e462d['count']:_0x18ad09[_0x560d('0x22')]+_0x18ad09[_0x560d('0x21')];var _0x3d6f13;if(_0x22df66>=_0x4210bf){_0x22df66=_0x4210bf;_0x3d6f13=0xc8;}else{_0x3d6f13=0xce;}_0xc31961[_0x560d('0x23')](_0x3d6f13);return _0xc31961[_0x560d('0x24')](_0x560d('0x25'),_0x1d9236+'-'+_0x22df66+'/'+_0x4210bf)[_0x560d('0x1f')](_0x5e462d);}return null;};}function patchUpdates(_0x155dab){return function(_0x2d6763){try{jsonpatch['apply'](_0x2d6763,_0x155dab,!![]);}catch(_0x4624f6){return BPromise[_0x560d('0x26')](_0x4624f6);}return _0x2d6763['save']();};}function saveUpdates(_0x47db16,_0x228b62){return function(_0x3810eb){if(_0x3810eb){return _0x3810eb['update'](_0x47db16)[_0x560d('0x27')](function(_0x591c0d){return _0x591c0d;});}return null;};}function removeEntity(_0x508f59,_0xe42cce){return function(_0xf873fd){if(_0xf873fd){return _0xf873fd[_0x560d('0x28')]()[_0x560d('0x27')](function(){var _0x27877e=_0xf873fd[_0x560d('0x29')]({'plain':!![]});var _0x4f7779=_0x560d('0x2a');return db['UserProfileResource'][_0x560d('0x28')]({'where':{'type':_0x4f7779,'resourceId':_0x27877e['id']}})[_0x560d('0x27')](function(){return _0xf873fd;});})[_0x560d('0x27')](function(){_0x508f59[_0x560d('0x23')](0xcc)[_0x560d('0x1e')]();});}};}function handleEntityNotFound(_0x4b49ba,_0x5e32d2){return function(_0x5769e3){if(!_0x5769e3){_0x4b49ba['sendStatus'](0x194);}return _0x5769e3;};}function handleError(_0x406bd9,_0x455b86){_0x455b86=_0x455b86||0x1f4;return function(_0x13206c){logger['error'](_0x13206c[_0x560d('0x2b')]);if(_0x13206c[_0x560d('0x2c')]){delete _0x13206c['name'];}_0x406bd9[_0x560d('0x23')](_0x455b86)['send'](_0x13206c);};}exports[_0x560d('0x2d')]=function(_0x383e10,_0x4f940d){var _0xcc988={},_0x560114={},_0x263ec7={'count':0x0,'rows':[]};return db[_0x560d('0x2e')][_0x560d('0x2f')]()['then'](function(_0x47acb4){_0x560114[_0x560d('0x30')]=_[_0x560d('0x31')](_0x47acb4);_0x560114[_0x560d('0x32')]=_[_0x560d('0x31')](_0x383e10[_0x560d('0x32')]);_0x560114[_0x560d('0x33')]=_['intersection'](_0x560114[_0x560d('0x30')],_0x560114[_0x560d('0x32')]);_0xcc988[_0x560d('0x34')]=_['intersection'](_0x560114[_0x560d('0x30')],qs['fields'](_0x383e10[_0x560d('0x32')]['fields']));_0xcc988[_0x560d('0x34')]=_0xcc988[_0x560d('0x34')]['length']?_0xcc988[_0x560d('0x34')]:_0x560114['model'];if(!_0x383e10[_0x560d('0x32')][_0x560d('0x35')](_0x560d('0x36'))){_0xcc988[_0x560d('0x21')]=qs['limit'](_0x383e10['query'][_0x560d('0x21')]);_0xcc988[_0x560d('0x22')]=qs[_0x560d('0x22')](_0x383e10[_0x560d('0x32')]['offset']);}_0xcc988[_0x560d('0x37')]=qs[_0x560d('0x38')](_0x383e10[_0x560d('0x32')][_0x560d('0x38')]);_0xcc988['where']=qs[_0x560d('0x33')](_[_0x560d('0x39')](_0x383e10[_0x560d('0x32')],_0x560114['filters']),_0x47acb4);if(_0x383e10[_0x560d('0x32')][_0x560d('0x3a')]){_0xcc988[_0x560d('0x3b')]=_[_0x560d('0x3c')](_0xcc988[_0x560d('0x3b')],{'$or':_['map'](_0xcc988[_0x560d('0x34')],function(_0x3e2d91){var _0x545386={};_0x545386[_0x3e2d91]={'$like':'%'+_0x383e10[_0x560d('0x32')][_0x560d('0x3a')]+'%'};return _0x545386;})});}_0xcc988=_[_0x560d('0x3c')]({},_0xcc988,_0x383e10[_0x560d('0x3d')]);var _0x162b70={'where':_0xcc988['where']};return db[_0x560d('0x2e')][_0x560d('0x3e')](_0x162b70)[_0x560d('0x27')](function(_0x23d617){_0x263ec7[_0x560d('0x3e')]=_0x23d617;if(_0x383e10[_0x560d('0x32')][_0x560d('0x3f')]){_0xcc988['include']=[{'all':!![]}];}return db[_0x560d('0x2e')][_0x560d('0x40')](_0xcc988);})['then'](function(_0x352460){_0x263ec7[_0x560d('0x41')]=_0x352460;return _0x263ec7;})[_0x560d('0x27')](respondWithFilteredResult(_0x4f940d,_0xcc988))[_0x560d('0x42')](handleError(_0x4f940d,null));})[_0x560d('0x42')](handleError(_0x4f940d,null));};exports[_0x560d('0x43')]=function(_0xda70ff,_0x129e66){var _0x4c1f2a={'raw':![],'where':{'id':_0xda70ff[_0x560d('0x44')]['id']}},_0x2bee73={};_0x2bee73[_0x560d('0x30')]=_[_0x560d('0x31')](db['CmCompany']['rawAttributes']);_0x2bee73[_0x560d('0x32')]=_[_0x560d('0x31')](_0xda70ff[_0x560d('0x32')]);_0x2bee73[_0x560d('0x33')]=_[_0x560d('0x45')](_0x2bee73[_0x560d('0x30')],_0x2bee73['query']);_0x4c1f2a[_0x560d('0x34')]=_[_0x560d('0x45')](_0x2bee73[_0x560d('0x30')],qs[_0x560d('0x46')](_0xda70ff[_0x560d('0x32')]['fields']));_0x4c1f2a[_0x560d('0x34')]=_0x4c1f2a['attributes'][_0x560d('0x47')]?_0x4c1f2a['attributes']:_0x2bee73['model'];if(_0xda70ff[_0x560d('0x32')]['includeAll']){_0x4c1f2a[_0x560d('0x48')]=[{'all':!![]}];}_0x4c1f2a=_[_0x560d('0x3c')]({},_0x4c1f2a,_0xda70ff[_0x560d('0x3d')]);return db[_0x560d('0x2e')][_0x560d('0x49')](_0x4c1f2a)[_0x560d('0x27')](handleEntityNotFound(_0x129e66,null))['then'](respondWithResult(_0x129e66,null))[_0x560d('0x42')](handleError(_0x129e66,null));};exports['create']=function(_0x59103a,_0x32cc66){return db[_0x560d('0x2e')][_0x560d('0x4a')](_0x59103a[_0x560d('0x4b')],{})[_0x560d('0x27')](function(_0x2774a9){var _0x3a4911=_0x59103a['user'][_0x560d('0x29')]({'plain':!![]});if(!_0x3a4911)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x3a4911['role']===_0x560d('0x4c')){var _0x5f53aa=_0x2774a9[_0x560d('0x29')]({'plain':!![]});var _0x465c71=_0x560d('0x2a');return db[_0x560d('0x4d')]['find']({'where':{'name':_0x465c71,'userProfileId':_0x3a4911['userProfileId']},'raw':!![]})[_0x560d('0x27')](function(_0x33d235){if(_0x33d235&&_0x33d235[_0x560d('0x4e')]===0x0){return db[_0x560d('0x4f')][_0x560d('0x4a')]({'name':_0x5f53aa[_0x560d('0x2c')],'resourceId':_0x5f53aa['id'],'type':_0x33d235[_0x560d('0x2c')],'sectionId':_0x33d235['id']},{})[_0x560d('0x27')](function(){return _0x2774a9;});}else{return _0x2774a9;}})['catch'](function(_0x6e0f06){logger[_0x560d('0x50')](_0x560d('0x51'),_0x6e0f06);throw _0x6e0f06;});}return _0x2774a9;})[_0x560d('0x27')](respondWithResult(_0x32cc66,0xc9))['catch'](handleError(_0x32cc66,null));};exports[_0x560d('0x52')]=function(_0x33e78a,_0x513e04){if(_0x33e78a['body']['id']){delete _0x33e78a['body']['id'];}return db[_0x560d('0x2e')][_0x560d('0x49')]({'where':{'id':_0x33e78a['params']['id']}})['then'](handleEntityNotFound(_0x513e04,null))[_0x560d('0x27')](saveUpdates(_0x33e78a[_0x560d('0x4b')],null))['then'](respondWithResult(_0x513e04,null))[_0x560d('0x42')](handleError(_0x513e04,null));};exports[_0x560d('0x28')]=function(_0x2d1a27,_0x1ef5ec){return db[_0x560d('0x2e')][_0x560d('0x49')]({'where':{'id':_0x2d1a27[_0x560d('0x44')]['id']}})[_0x560d('0x27')](handleEntityNotFound(_0x1ef5ec,null))[_0x560d('0x27')](removeEntity(_0x1ef5ec,null))['catch'](handleError(_0x1ef5ec,null));};exports['describe']=function(_0x3e0360,_0x3afc63){return db[_0x560d('0x2e')][_0x560d('0x2f')]()[_0x560d('0x27')](respondWithResult(_0x3afc63,null))[_0x560d('0x42')](handleError(_0x3afc63,null));};exports['addContacts']=function(_0x5997e1,_0x1ab704,_0x412f59){return db[_0x560d('0x53')][_0x560d('0x49')]({'where':{'id':_0x5997e1[_0x560d('0x44')]['id']}})[_0x560d('0x27')](handleEntityNotFound(_0x1ab704,null))['then'](function(_0x378021){if(_0x378021){return _0x378021['addContacts'](_0x5997e1['body'][_0x560d('0x54')],_['omit'](_0x5997e1[_0x560d('0x4b')],[_0x560d('0x54'),'id'])||{});}})['then'](respondWithResult(_0x1ab704,null))[_0x560d('0x42')](handleError(_0x1ab704,null));};exports['getContacts']=function(_0x5b1a2a,_0xf41644,_0x3f629a){var _0x177706={};var _0x311d97={};var _0x10152f;var _0x213173;return db[_0x560d('0x2e')]['findOne']({'where':{'id':_0x5b1a2a[_0x560d('0x44')]['id']}})[_0x560d('0x27')](handleEntityNotFound(_0xf41644,null))[_0x560d('0x27')](function(_0x427b38){if(_0x427b38){_0x10152f=_0x427b38;_0x311d97[_0x560d('0x30')]=_[_0x560d('0x31')](db[_0x560d('0x53')][_0x560d('0x55')]);_0x311d97[_0x560d('0x32')]=_[_0x560d('0x31')](_0x5b1a2a['query']);_0x311d97[_0x560d('0x33')]=_[_0x560d('0x45')](_0x311d97[_0x560d('0x30')],_0x311d97['query']);_0x177706['attributes']=_[_0x560d('0x45')](_0x311d97['model'],qs['fields'](_0x5b1a2a[_0x560d('0x32')][_0x560d('0x46')]));_0x177706[_0x560d('0x34')]=_0x177706[_0x560d('0x34')][_0x560d('0x47')]?_0x177706[_0x560d('0x34')]:_0x311d97[_0x560d('0x30')];_0x177706[_0x560d('0x37')]=qs['sort'](_0x5b1a2a[_0x560d('0x32')][_0x560d('0x38')]);_0x177706[_0x560d('0x3b')]=qs[_0x560d('0x33')](_[_0x560d('0x39')](_0x5b1a2a[_0x560d('0x32')],_0x311d97[_0x560d('0x33')]));if(_0x5b1a2a[_0x560d('0x32')][_0x560d('0x3a')]){_0x177706['where']=_[_0x560d('0x3c')](_0x177706[_0x560d('0x3b')],{'$or':_['map'](_0x177706[_0x560d('0x34')],function(_0x2b4f05){var _0x36f29c={};_0x36f29c[_0x2b4f05]={'$like':'%'+_0x5b1a2a[_0x560d('0x32')][_0x560d('0x3a')]+'%'};return _0x36f29c;})});}_0x177706=_[_0x560d('0x3c')]({},_0x177706,_0x5b1a2a[_0x560d('0x3d')]);return _0x10152f[_0x560d('0x56')](_0x177706);}})[_0x560d('0x27')](function(_0x5b626e){if(_0x5b626e){_0x213173=_0x5b626e['length'];if(!_0x5b1a2a[_0x560d('0x32')][_0x560d('0x35')](_0x560d('0x36'))){_0x177706[_0x560d('0x21')]=qs[_0x560d('0x21')](_0x5b1a2a[_0x560d('0x32')]['limit']);_0x177706[_0x560d('0x22')]=qs[_0x560d('0x22')](_0x5b1a2a[_0x560d('0x32')][_0x560d('0x22')]);}return _0x10152f[_0x560d('0x56')](_0x177706);}})[_0x560d('0x27')](function(_0x4eb089){if(_0x4eb089){return _0x4eb089?{'count':_0x213173,'rows':_0x4eb089}:null;}})[_0x560d('0x27')](respondWithResult(_0xf41644,null))[_0x560d('0x42')](handleError(_0xf41644,null));};