Built motion from commit ddca4d0d.|2.5.47
[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 _0xc9cd=['catch','show','params','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','UserProfileResource','name','describe','CmContact','addContacts','ids','omit','findOne','rawAttributes','getContacts','eml-format','rimraf','fast-json-patch','request-promise','moment','bluebird','mustache','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/advancedSearch','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','Content-Range','apply','save','update','then','destroy','get','Companies','error','stack','index','CmCompany','model','keys','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','sort','where','pick','filter','map','merge','options','includeAll','include','findAll'];(function(_0x4b8937,_0x1fc99f){var _0x108eeb=function(_0xd0a258){while(--_0xd0a258){_0x4b8937['push'](_0x4b8937['shift']());}};_0x108eeb(++_0x1fc99f);}(_0xc9cd,0x1c2));var _0xdc9c=function(_0xe323e3,_0x110473){_0xe323e3=_0xe323e3-0x0;var _0x208a43=_0xc9cd[_0xe323e3];return _0x208a43;};'use strict';var emlformat=require(_0xdc9c('0x0'));var rimraf=require(_0xdc9c('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0xdc9c('0x2'));var rp=require(_0xdc9c('0x3'));var moment=require(_0xdc9c('0x4'));var BPromise=require(_0xdc9c('0x5'));var Mustache=require(_0xdc9c('0x6'));var util=require('util');var path=require(_0xdc9c('0x7'));var sox=require(_0xdc9c('0x8'));var csv=require(_0xdc9c('0x9'));var ejs=require(_0xdc9c('0xa'));var fs=require('fs');var fs_extra=require(_0xdc9c('0xb'));var _=require(_0xdc9c('0xc'));var squel=require(_0xdc9c('0xd'));var crypto=require(_0xdc9c('0xe'));var jsforce=require(_0xdc9c('0xf'));var deskjs=require(_0xdc9c('0x10'));var toCsv=require(_0xdc9c('0x9'));var querystring=require(_0xdc9c('0x11'));var Papa=require(_0xdc9c('0x12'));var Redis=require('ioredis');var authService=require(_0xdc9c('0x13'));var qs=require('../../components/parsers/qs');var as=require(_0xdc9c('0x14'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xdc9c('0x15'))('api');var utils=require(_0xdc9c('0x16'));var config=require(_0xdc9c('0x17'));var licenseUtil=require(_0xdc9c('0x18'));var db=require(_0xdc9c('0x19'))['db'];function respondWithStatusCode(_0x1823ef,_0x403010){_0x403010=_0x403010||0xcc;return function(_0x639194){if(_0x639194){return _0x1823ef[_0xdc9c('0x1a')](_0x403010);}return _0x1823ef[_0xdc9c('0x1b')](_0x403010)[_0xdc9c('0x1c')]();};}function respondWithResult(_0x459c93,_0x25a3ea){_0x25a3ea=_0x25a3ea||0xc8;return function(_0x2a89a3){if(_0x2a89a3){return _0x459c93[_0xdc9c('0x1b')](_0x25a3ea)[_0xdc9c('0x1d')](_0x2a89a3);}};}function respondWithFilteredResult(_0x3df4de,_0x5fba18){return function(_0x27c5b4){if(_0x27c5b4){var _0x5b89a4=typeof _0x5fba18[_0xdc9c('0x1e')]===_0xdc9c('0x1f')&&typeof _0x5fba18[_0xdc9c('0x20')]===_0xdc9c('0x1f');var _0x15c97a=_0x27c5b4[_0xdc9c('0x21')];var _0x5e04d3=_0x5b89a4?0x0:_0x5fba18[_0xdc9c('0x1e')];var _0x201cd9=_0x5b89a4?_0x27c5b4['count']:_0x5fba18[_0xdc9c('0x1e')]+_0x5fba18[_0xdc9c('0x20')];var _0x2261a7;if(_0x201cd9>=_0x15c97a){_0x201cd9=_0x15c97a;_0x2261a7=0xc8;}else{_0x2261a7=0xce;}_0x3df4de['status'](_0x2261a7);return _0x3df4de['set'](_0xdc9c('0x22'),_0x5e04d3+'-'+_0x201cd9+'/'+_0x15c97a)[_0xdc9c('0x1d')](_0x27c5b4);}return null;};}function patchUpdates(_0x25fd5f){return function(_0x250456){try{jsonpatch[_0xdc9c('0x23')](_0x250456,_0x25fd5f,!![]);}catch(_0x54e2dd){return BPromise['reject'](_0x54e2dd);}return _0x250456[_0xdc9c('0x24')]();};}function saveUpdates(_0x58df93,_0x32d7ee){return function(_0x1b3464){if(_0x1b3464){return _0x1b3464[_0xdc9c('0x25')](_0x58df93)[_0xdc9c('0x26')](function(_0x5d4e52){return _0x5d4e52;});}return null;};}function removeEntity(_0xc3a6c1,_0x4e6e94){return function(_0x516492){if(_0x516492){return _0x516492[_0xdc9c('0x27')]()[_0xdc9c('0x26')](function(){var _0x41212a=_0x516492[_0xdc9c('0x28')]({'plain':!![]});var _0xf61f5f=_0xdc9c('0x29');return db['UserProfileResource']['destroy']({'where':{'type':_0xf61f5f,'resourceId':_0x41212a['id']}})['then'](function(){return _0x516492;});})[_0xdc9c('0x26')](function(){_0xc3a6c1[_0xdc9c('0x1b')](0xcc)[_0xdc9c('0x1c')]();});}};}function handleEntityNotFound(_0x23cc75,_0x4288d9){return function(_0xac6cda){if(!_0xac6cda){_0x23cc75[_0xdc9c('0x1a')](0x194);}return _0xac6cda;};}function handleError(_0x1d9190,_0x917170){_0x917170=_0x917170||0x1f4;return function(_0x2a0d3a){logger[_0xdc9c('0x2a')](_0x2a0d3a[_0xdc9c('0x2b')]);if(_0x2a0d3a['name']){delete _0x2a0d3a['name'];}_0x1d9190[_0xdc9c('0x1b')](_0x917170)['send'](_0x2a0d3a);};}exports[_0xdc9c('0x2c')]=function(_0x1d37b7,_0x5942a0){var _0x445576={},_0x447a50={},_0x227106={'count':0x0,'rows':[]};return db[_0xdc9c('0x2d')]['describe']()[_0xdc9c('0x26')](function(_0x20bb6b){_0x447a50[_0xdc9c('0x2e')]=_[_0xdc9c('0x2f')](_0x20bb6b);_0x447a50[_0xdc9c('0x30')]=_[_0xdc9c('0x2f')](_0x1d37b7[_0xdc9c('0x30')]);_0x447a50[_0xdc9c('0x31')]=_[_0xdc9c('0x32')](_0x447a50[_0xdc9c('0x2e')],_0x447a50[_0xdc9c('0x30')]);_0x445576[_0xdc9c('0x33')]=_['intersection'](_0x447a50[_0xdc9c('0x2e')],qs[_0xdc9c('0x34')](_0x1d37b7[_0xdc9c('0x30')][_0xdc9c('0x34')]));_0x445576['attributes']=_0x445576[_0xdc9c('0x33')][_0xdc9c('0x35')]?_0x445576[_0xdc9c('0x33')]:_0x447a50[_0xdc9c('0x2e')];if(!_0x1d37b7[_0xdc9c('0x30')][_0xdc9c('0x36')](_0xdc9c('0x37'))){_0x445576[_0xdc9c('0x20')]=qs[_0xdc9c('0x20')](_0x1d37b7[_0xdc9c('0x30')][_0xdc9c('0x20')]);_0x445576[_0xdc9c('0x1e')]=qs[_0xdc9c('0x1e')](_0x1d37b7['query'][_0xdc9c('0x1e')]);}_0x445576['order']=qs[_0xdc9c('0x38')](_0x1d37b7['query'][_0xdc9c('0x38')]);_0x445576[_0xdc9c('0x39')]=qs[_0xdc9c('0x31')](_[_0xdc9c('0x3a')](_0x1d37b7[_0xdc9c('0x30')],_0x447a50[_0xdc9c('0x31')]),_0x20bb6b);if(_0x1d37b7[_0xdc9c('0x30')][_0xdc9c('0x3b')]){_0x445576[_0xdc9c('0x39')]=_['merge'](_0x445576[_0xdc9c('0x39')],{'$or':_[_0xdc9c('0x3c')](_0x445576[_0xdc9c('0x33')],function(_0x24432d){var _0x1c951e={};_0x1c951e[_0x24432d]={'$like':'%'+_0x1d37b7[_0xdc9c('0x30')][_0xdc9c('0x3b')]+'%'};return _0x1c951e;})});}_0x445576=_[_0xdc9c('0x3d')]({},_0x445576,_0x1d37b7[_0xdc9c('0x3e')]);var _0x1e88fc={'where':_0x445576[_0xdc9c('0x39')]};return db['CmCompany'][_0xdc9c('0x21')](_0x1e88fc)[_0xdc9c('0x26')](function(_0x1e6546){_0x227106[_0xdc9c('0x21')]=_0x1e6546;if(_0x1d37b7[_0xdc9c('0x30')][_0xdc9c('0x3f')]){_0x445576[_0xdc9c('0x40')]=[{'all':!![]}];}return db[_0xdc9c('0x2d')][_0xdc9c('0x41')](_0x445576);})[_0xdc9c('0x26')](function(_0x33e79e){_0x227106['rows']=_0x33e79e;return _0x227106;})[_0xdc9c('0x26')](respondWithFilteredResult(_0x5942a0,_0x445576))[_0xdc9c('0x42')](handleError(_0x5942a0,null));})[_0xdc9c('0x42')](handleError(_0x5942a0,null));};exports[_0xdc9c('0x43')]=function(_0x1e0e44,_0x117030){var _0x3aef0b={'raw':![],'where':{'id':_0x1e0e44[_0xdc9c('0x44')]['id']}},_0x210074={};_0x210074['model']=_[_0xdc9c('0x2f')](db['CmCompany']['rawAttributes']);_0x210074[_0xdc9c('0x30')]=_['keys'](_0x1e0e44[_0xdc9c('0x30')]);_0x210074[_0xdc9c('0x31')]=_[_0xdc9c('0x32')](_0x210074[_0xdc9c('0x2e')],_0x210074[_0xdc9c('0x30')]);_0x3aef0b[_0xdc9c('0x33')]=_[_0xdc9c('0x32')](_0x210074[_0xdc9c('0x2e')],qs[_0xdc9c('0x34')](_0x1e0e44[_0xdc9c('0x30')]['fields']));_0x3aef0b[_0xdc9c('0x33')]=_0x3aef0b[_0xdc9c('0x33')]['length']?_0x3aef0b[_0xdc9c('0x33')]:_0x210074[_0xdc9c('0x2e')];if(_0x1e0e44[_0xdc9c('0x30')][_0xdc9c('0x3f')]){_0x3aef0b['include']=[{'all':!![]}];}_0x3aef0b=_['merge']({},_0x3aef0b,_0x1e0e44[_0xdc9c('0x3e')]);return db[_0xdc9c('0x2d')][_0xdc9c('0x45')](_0x3aef0b)['then'](handleEntityNotFound(_0x117030,null))[_0xdc9c('0x26')](respondWithResult(_0x117030,null))[_0xdc9c('0x42')](handleError(_0x117030,null));};exports['create']=function(_0xcbe8cc,_0x2845e3){return db[_0xdc9c('0x2d')][_0xdc9c('0x46')](_0xcbe8cc[_0xdc9c('0x47')],{})[_0xdc9c('0x26')](function(_0x38b421){var _0x1a8722=_0xcbe8cc[_0xdc9c('0x48')][_0xdc9c('0x28')]({'plain':!![]});if(!_0x1a8722)throw new Error(_0xdc9c('0x49'));if(_0x1a8722[_0xdc9c('0x4a')]===_0xdc9c('0x48')){var _0x30a45c=_0x38b421[_0xdc9c('0x28')]({'plain':!![]});var _0x19f1fd=_0xdc9c('0x29');return db[_0xdc9c('0x4b')][_0xdc9c('0x45')]({'where':{'name':_0x19f1fd,'userProfileId':_0x1a8722['userProfileId']},'raw':!![]})[_0xdc9c('0x26')](function(_0x55ff2c){if(_0x55ff2c&&_0x55ff2c['autoAssociation']===0x0){return db[_0xdc9c('0x4c')][_0xdc9c('0x46')]({'name':_0x30a45c[_0xdc9c('0x4d')],'resourceId':_0x30a45c['id'],'type':_0x55ff2c[_0xdc9c('0x4d')],'sectionId':_0x55ff2c['id']},{})[_0xdc9c('0x26')](function(){return _0x38b421;});}else{return _0x38b421;}})[_0xdc9c('0x42')](function(_0x1b8fd7){logger[_0xdc9c('0x2a')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x1b8fd7);throw _0x1b8fd7;});}return _0x38b421;})['then'](respondWithResult(_0x2845e3,0xc9))['catch'](handleError(_0x2845e3,null));};exports[_0xdc9c('0x25')]=function(_0x3fcea3,_0xc6004b){if(_0x3fcea3[_0xdc9c('0x47')]['id']){delete _0x3fcea3[_0xdc9c('0x47')]['id'];}return db[_0xdc9c('0x2d')][_0xdc9c('0x45')]({'where':{'id':_0x3fcea3[_0xdc9c('0x44')]['id']}})[_0xdc9c('0x26')](handleEntityNotFound(_0xc6004b,null))[_0xdc9c('0x26')](saveUpdates(_0x3fcea3[_0xdc9c('0x47')],null))[_0xdc9c('0x26')](respondWithResult(_0xc6004b,null))[_0xdc9c('0x42')](handleError(_0xc6004b,null));};exports['destroy']=function(_0x3fcd6d,_0x4abf72){return db[_0xdc9c('0x2d')][_0xdc9c('0x45')]({'where':{'id':_0x3fcd6d[_0xdc9c('0x44')]['id']}})['then'](handleEntityNotFound(_0x4abf72,null))[_0xdc9c('0x26')](removeEntity(_0x4abf72,null))['catch'](handleError(_0x4abf72,null));};exports[_0xdc9c('0x4e')]=function(_0x2800fe,_0x208064){return db[_0xdc9c('0x2d')][_0xdc9c('0x4e')]()[_0xdc9c('0x26')](respondWithResult(_0x208064,null))[_0xdc9c('0x42')](handleError(_0x208064,null));};exports['addContacts']=function(_0x4c7cfb,_0x4cda3f,_0x1bc8c8){return db[_0xdc9c('0x4f')][_0xdc9c('0x45')]({'where':{'id':_0x4c7cfb[_0xdc9c('0x44')]['id']}})[_0xdc9c('0x26')](handleEntityNotFound(_0x4cda3f,null))[_0xdc9c('0x26')](function(_0x2a0372){if(_0x2a0372){return _0x2a0372[_0xdc9c('0x50')](_0x4c7cfb[_0xdc9c('0x47')][_0xdc9c('0x51')],_[_0xdc9c('0x52')](_0x4c7cfb[_0xdc9c('0x47')],['ids','id'])||{});}})['then'](respondWithResult(_0x4cda3f,null))[_0xdc9c('0x42')](handleError(_0x4cda3f,null));};exports['getContacts']=function(_0x277a44,_0x4d45b0,_0x3d5ac2){var _0x9e1aaa={};var _0x195ac2={};var _0x3eb6d0;var _0x2384ba;return db[_0xdc9c('0x2d')][_0xdc9c('0x53')]({'where':{'id':_0x277a44[_0xdc9c('0x44')]['id']}})[_0xdc9c('0x26')](handleEntityNotFound(_0x4d45b0,null))[_0xdc9c('0x26')](function(_0x1f8f5f){if(_0x1f8f5f){_0x3eb6d0=_0x1f8f5f;_0x195ac2[_0xdc9c('0x2e')]=_[_0xdc9c('0x2f')](db[_0xdc9c('0x4f')][_0xdc9c('0x54')]);_0x195ac2[_0xdc9c('0x30')]=_[_0xdc9c('0x2f')](_0x277a44[_0xdc9c('0x30')]);_0x195ac2[_0xdc9c('0x31')]=_[_0xdc9c('0x32')](_0x195ac2[_0xdc9c('0x2e')],_0x195ac2['query']);_0x9e1aaa[_0xdc9c('0x33')]=_[_0xdc9c('0x32')](_0x195ac2['model'],qs[_0xdc9c('0x34')](_0x277a44[_0xdc9c('0x30')][_0xdc9c('0x34')]));_0x9e1aaa[_0xdc9c('0x33')]=_0x9e1aaa[_0xdc9c('0x33')][_0xdc9c('0x35')]?_0x9e1aaa['attributes']:_0x195ac2[_0xdc9c('0x2e')];_0x9e1aaa['order']=qs[_0xdc9c('0x38')](_0x277a44[_0xdc9c('0x30')][_0xdc9c('0x38')]);_0x9e1aaa[_0xdc9c('0x39')]=qs[_0xdc9c('0x31')](_['pick'](_0x277a44[_0xdc9c('0x30')],_0x195ac2[_0xdc9c('0x31')]));if(_0x277a44[_0xdc9c('0x30')][_0xdc9c('0x3b')]){_0x9e1aaa[_0xdc9c('0x39')]=_['merge'](_0x9e1aaa['where'],{'$or':_[_0xdc9c('0x3c')](_0x9e1aaa['attributes'],function(_0x1b7f14){var _0x26f4ed={};_0x26f4ed[_0x1b7f14]={'$like':'%'+_0x277a44[_0xdc9c('0x30')][_0xdc9c('0x3b')]+'%'};return _0x26f4ed;})});}_0x9e1aaa=_[_0xdc9c('0x3d')]({},_0x9e1aaa,_0x277a44['options']);return _0x3eb6d0['getContacts'](_0x9e1aaa);}})[_0xdc9c('0x26')](function(_0xd50dc){if(_0xd50dc){_0x2384ba=_0xd50dc[_0xdc9c('0x35')];if(!_0x277a44[_0xdc9c('0x30')][_0xdc9c('0x36')]('nolimit')){_0x9e1aaa[_0xdc9c('0x20')]=qs['limit'](_0x277a44[_0xdc9c('0x30')][_0xdc9c('0x20')]);_0x9e1aaa[_0xdc9c('0x1e')]=qs['offset'](_0x277a44[_0xdc9c('0x30')][_0xdc9c('0x1e')]);}return _0x3eb6d0[_0xdc9c('0x55')](_0x9e1aaa);}})[_0xdc9c('0x26')](function(_0x1bbd13){if(_0x1bbd13){return _0x1bbd13?{'count':_0x2384ba,'rows':_0x1bbd13}:null;}})[_0xdc9c('0x26')](respondWithResult(_0x4d45b0,null))[_0xdc9c('0x42')](handleError(_0x4d45b0,null));};