6c5dc03759c43e318d7a3dab195c0fb91f674784
[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 _0x6f8f=['json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','update','destroy','then','get','Companies','error','stack','name','send','describe','model','keys','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','map','filter','merge','options','CmCompany','includeAll','include','findAll','rows','catch','show','params','find','create','body','user','UserProfileSection','userProfileId','autoAssociation','UserProfileResource','addContacts','ids','omit','getContacts','findOne','eml-format','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','ejs','fs-extra','lodash','squel','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/environment','status','end'];(function(_0x427352,_0x1bce61){var _0x23e0e6=function(_0x2a42a9){while(--_0x2a42a9){_0x427352['push'](_0x427352['shift']());}};_0x23e0e6(++_0x1bce61);}(_0x6f8f,0x90));var _0xf6f8=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x6f8f[_0x3dd15e];return _0x231fd0;};'use strict';var emlformat=require(_0xf6f8('0x0'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0xf6f8('0x1'));var rp=require(_0xf6f8('0x2'));var moment=require(_0xf6f8('0x3'));var BPromise=require(_0xf6f8('0x4'));var Mustache=require(_0xf6f8('0x5'));var util=require(_0xf6f8('0x6'));var path=require(_0xf6f8('0x7'));var sox=require(_0xf6f8('0x8'));var csv=require('to-csv');var ejs=require(_0xf6f8('0x9'));var fs=require('fs');var fs_extra=require(_0xf6f8('0xa'));var _=require(_0xf6f8('0xb'));var squel=require(_0xf6f8('0xc'));var crypto=require('crypto');var jsforce=require(_0xf6f8('0xd'));var deskjs=require(_0xf6f8('0xe'));var toCsv=require('to-csv');var querystring=require(_0xf6f8('0xf'));var Papa=require(_0xf6f8('0x10'));var Redis=require(_0xf6f8('0x11'));var authService=require(_0xf6f8('0x12'));var qs=require(_0xf6f8('0x13'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xf6f8('0x14'));var logger=require(_0xf6f8('0x15'))(_0xf6f8('0x16'));var utils=require('../../config/utils');var config=require(_0xf6f8('0x17'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x2e6ca0,_0x556b93){_0x556b93=_0x556b93||0xcc;return function(_0x42d711){if(_0x42d711){return _0x2e6ca0['sendStatus'](_0x556b93);}return _0x2e6ca0[_0xf6f8('0x18')](_0x556b93)[_0xf6f8('0x19')]();};}function respondWithResult(_0x213998,_0x2e6ae2){_0x2e6ae2=_0x2e6ae2||0xc8;return function(_0x5a71d3){if(_0x5a71d3){return _0x213998[_0xf6f8('0x18')](_0x2e6ae2)[_0xf6f8('0x1a')](_0x5a71d3);}};}function respondWithFilteredResult(_0x3f9b6b,_0x19b107){return function(_0x2dadf0){if(_0x2dadf0){var _0x19ef31=typeof _0x19b107[_0xf6f8('0x1b')]===_0xf6f8('0x1c')&&typeof _0x19b107[_0xf6f8('0x1d')]===_0xf6f8('0x1c');var _0x16dc05=_0x2dadf0[_0xf6f8('0x1e')];var _0x15cf16=_0x19ef31?0x0:_0x19b107[_0xf6f8('0x1b')];var _0x4310fe=_0x19ef31?_0x2dadf0[_0xf6f8('0x1e')]:_0x19b107[_0xf6f8('0x1b')]+_0x19b107['limit'];var _0x4b9752;if(_0x4310fe>=_0x16dc05){_0x4310fe=_0x16dc05;_0x4b9752=0xc8;}else{_0x4b9752=0xce;}_0x3f9b6b['status'](_0x4b9752);return _0x3f9b6b[_0xf6f8('0x1f')](_0xf6f8('0x20'),_0x15cf16+'-'+_0x4310fe+'/'+_0x16dc05)[_0xf6f8('0x1a')](_0x2dadf0);}return null;};}function patchUpdates(_0x30b27e){return function(_0x4dea46){try{jsonpatch[_0xf6f8('0x21')](_0x4dea46,_0x30b27e,!![]);}catch(_0x145dc4){return BPromise[_0xf6f8('0x22')](_0x145dc4);}return _0x4dea46[_0xf6f8('0x23')]();};}function saveUpdates(_0x17acbe,_0x1aa550){return function(_0x3b6a39){if(_0x3b6a39){return _0x3b6a39[_0xf6f8('0x24')](_0x17acbe)['then'](function(_0x238627){return _0x238627;});}return null;};}function removeEntity(_0x3eea5d,_0x3f6721){return function(_0x5a4c9c){if(_0x5a4c9c){return _0x5a4c9c[_0xf6f8('0x25')]()[_0xf6f8('0x26')](function(){var _0x2de292=_0x5a4c9c[_0xf6f8('0x27')]({'plain':!![]});var _0x2e0144=_0xf6f8('0x28');return db['UserProfileResource']['destroy']({'where':{'type':_0x2e0144,'resourceId':_0x2de292['id']}})[_0xf6f8('0x26')](function(){return _0x5a4c9c;});})[_0xf6f8('0x26')](function(){_0x3eea5d['status'](0xcc)[_0xf6f8('0x19')]();});}};}function handleEntityNotFound(_0x4f1a62,_0x36c8d9){return function(_0x52647a){if(!_0x52647a){_0x4f1a62['sendStatus'](0x194);}return _0x52647a;};}function handleError(_0x3e3fb7,_0x59ca24){_0x59ca24=_0x59ca24||0x1f4;return function(_0x20d7c1){logger[_0xf6f8('0x29')](_0x20d7c1[_0xf6f8('0x2a')]);if(_0x20d7c1['name']){delete _0x20d7c1[_0xf6f8('0x2b')];}_0x3e3fb7['status'](_0x59ca24)[_0xf6f8('0x2c')](_0x20d7c1);};}exports['index']=function(_0x183d35,_0x302fd3){var _0x5e6ae5={},_0x4af85b={},_0x1359da={'count':0x0,'rows':[]};return db['CmCompany'][_0xf6f8('0x2d')]()[_0xf6f8('0x26')](function(_0x125a9e){_0x4af85b[_0xf6f8('0x2e')]=_[_0xf6f8('0x2f')](_0x125a9e);_0x4af85b[_0xf6f8('0x30')]=_[_0xf6f8('0x2f')](_0x183d35[_0xf6f8('0x30')]);_0x4af85b[_0xf6f8('0x31')]=_[_0xf6f8('0x32')](_0x4af85b['model'],_0x4af85b[_0xf6f8('0x30')]);_0x5e6ae5[_0xf6f8('0x33')]=_[_0xf6f8('0x32')](_0x4af85b[_0xf6f8('0x2e')],qs[_0xf6f8('0x34')](_0x183d35[_0xf6f8('0x30')]['fields']));_0x5e6ae5[_0xf6f8('0x33')]=_0x5e6ae5['attributes'][_0xf6f8('0x35')]?_0x5e6ae5[_0xf6f8('0x33')]:_0x4af85b[_0xf6f8('0x2e')];if(!_0x183d35[_0xf6f8('0x30')][_0xf6f8('0x36')](_0xf6f8('0x37'))){_0x5e6ae5['limit']=qs['limit'](_0x183d35[_0xf6f8('0x30')]['limit']);_0x5e6ae5[_0xf6f8('0x1b')]=qs[_0xf6f8('0x1b')](_0x183d35[_0xf6f8('0x30')]['offset']);}_0x5e6ae5[_0xf6f8('0x38')]=qs['sort'](_0x183d35['query'][_0xf6f8('0x39')]);_0x5e6ae5[_0xf6f8('0x3a')]=qs[_0xf6f8('0x31')](_[_0xf6f8('0x3b')](_0x183d35[_0xf6f8('0x30')],_0x4af85b[_0xf6f8('0x31')]),_0x125a9e);if(_0x183d35[_0xf6f8('0x30')]['filter']){_0x5e6ae5[_0xf6f8('0x3a')]=_['merge'](_0x5e6ae5['where'],{'$or':_[_0xf6f8('0x3c')](_0x5e6ae5[_0xf6f8('0x33')],function(_0xa09bcf){var _0x173247={};_0x173247[_0xa09bcf]={'$like':'%'+_0x183d35['query'][_0xf6f8('0x3d')]+'%'};return _0x173247;})});}_0x5e6ae5=_[_0xf6f8('0x3e')]({},_0x5e6ae5,_0x183d35[_0xf6f8('0x3f')]);var _0x497d93={'where':_0x5e6ae5['where']};return db[_0xf6f8('0x40')]['count'](_0x497d93)[_0xf6f8('0x26')](function(_0x164548){_0x1359da[_0xf6f8('0x1e')]=_0x164548;if(_0x183d35[_0xf6f8('0x30')][_0xf6f8('0x41')]){_0x5e6ae5[_0xf6f8('0x42')]=[{'all':!![]}];}return db[_0xf6f8('0x40')][_0xf6f8('0x43')](_0x5e6ae5);})[_0xf6f8('0x26')](function(_0x15fe9d){_0x1359da[_0xf6f8('0x44')]=_0x15fe9d;return _0x1359da;})['then'](respondWithFilteredResult(_0x302fd3,_0x5e6ae5))[_0xf6f8('0x45')](handleError(_0x302fd3,null));})[_0xf6f8('0x45')](handleError(_0x302fd3,null));};exports[_0xf6f8('0x46')]=function(_0x5157d2,_0x234a8c){var _0x2c9b97={'raw':![],'where':{'id':_0x5157d2[_0xf6f8('0x47')]['id']}},_0x42683b={};_0x42683b['model']=_['keys'](db[_0xf6f8('0x40')]['rawAttributes']);_0x42683b[_0xf6f8('0x30')]=_[_0xf6f8('0x2f')](_0x5157d2[_0xf6f8('0x30')]);_0x42683b['filters']=_[_0xf6f8('0x32')](_0x42683b[_0xf6f8('0x2e')],_0x42683b[_0xf6f8('0x30')]);_0x2c9b97[_0xf6f8('0x33')]=_[_0xf6f8('0x32')](_0x42683b[_0xf6f8('0x2e')],qs[_0xf6f8('0x34')](_0x5157d2[_0xf6f8('0x30')][_0xf6f8('0x34')]));_0x2c9b97[_0xf6f8('0x33')]=_0x2c9b97[_0xf6f8('0x33')][_0xf6f8('0x35')]?_0x2c9b97[_0xf6f8('0x33')]:_0x42683b['model'];if(_0x5157d2[_0xf6f8('0x30')][_0xf6f8('0x41')]){_0x2c9b97['include']=[{'all':!![]}];}_0x2c9b97=_['merge']({},_0x2c9b97,_0x5157d2[_0xf6f8('0x3f')]);return db['CmCompany'][_0xf6f8('0x48')](_0x2c9b97)[_0xf6f8('0x26')](handleEntityNotFound(_0x234a8c,null))[_0xf6f8('0x26')](respondWithResult(_0x234a8c,null))[_0xf6f8('0x45')](handleError(_0x234a8c,null));};exports[_0xf6f8('0x49')]=function(_0x457f16,_0x3013bc){return db['CmCompany'][_0xf6f8('0x49')](_0x457f16[_0xf6f8('0x4a')],{})[_0xf6f8('0x26')](function(_0x85bc34){var _0x3decd7=_0x457f16['user'][_0xf6f8('0x27')]({'plain':!![]});if(!_0x3decd7)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x3decd7['role']===_0xf6f8('0x4b')){var _0x12bba8=_0x85bc34[_0xf6f8('0x27')]({'plain':!![]});var _0x3a1056=_0xf6f8('0x28');return db[_0xf6f8('0x4c')][_0xf6f8('0x48')]({'where':{'name':_0x3a1056,'userProfileId':_0x3decd7[_0xf6f8('0x4d')]},'raw':!![]})[_0xf6f8('0x26')](function(_0xa4a051){if(_0xa4a051&&_0xa4a051[_0xf6f8('0x4e')]===0x0){return db[_0xf6f8('0x4f')][_0xf6f8('0x49')]({'name':_0x12bba8[_0xf6f8('0x2b')],'resourceId':_0x12bba8['id'],'type':_0xa4a051['name'],'sectionId':_0xa4a051['id']},{})['then'](function(){return _0x85bc34;});}else{return _0x85bc34;}})[_0xf6f8('0x45')](function(_0x558f24){logger[_0xf6f8('0x29')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x558f24);throw _0x558f24;});}return _0x85bc34;})[_0xf6f8('0x26')](respondWithResult(_0x3013bc,0xc9))[_0xf6f8('0x45')](handleError(_0x3013bc,null));};exports[_0xf6f8('0x24')]=function(_0x89662f,_0x499850){if(_0x89662f[_0xf6f8('0x4a')]['id']){delete _0x89662f[_0xf6f8('0x4a')]['id'];}return db[_0xf6f8('0x40')]['find']({'where':{'id':_0x89662f[_0xf6f8('0x47')]['id']}})[_0xf6f8('0x26')](handleEntityNotFound(_0x499850,null))[_0xf6f8('0x26')](saveUpdates(_0x89662f[_0xf6f8('0x4a')],null))[_0xf6f8('0x26')](respondWithResult(_0x499850,null))['catch'](handleError(_0x499850,null));};exports[_0xf6f8('0x25')]=function(_0x5a4041,_0x1aac88){return db[_0xf6f8('0x40')]['find']({'where':{'id':_0x5a4041['params']['id']}})[_0xf6f8('0x26')](handleEntityNotFound(_0x1aac88,null))['then'](removeEntity(_0x1aac88,null))[_0xf6f8('0x45')](handleError(_0x1aac88,null));};exports[_0xf6f8('0x2d')]=function(_0x3192af,_0x528d73){return db[_0xf6f8('0x40')]['describe']()[_0xf6f8('0x26')](respondWithResult(_0x528d73,null))[_0xf6f8('0x45')](handleError(_0x528d73,null));};exports[_0xf6f8('0x50')]=function(_0x89bbd6,_0x39d0f4,_0x36a31d){return db['CmContact'][_0xf6f8('0x48')]({'where':{'id':_0x89bbd6[_0xf6f8('0x47')]['id']}})[_0xf6f8('0x26')](handleEntityNotFound(_0x39d0f4,null))['then'](function(_0x9ebf7){if(_0x9ebf7){return _0x9ebf7[_0xf6f8('0x50')](_0x89bbd6[_0xf6f8('0x4a')][_0xf6f8('0x51')],_[_0xf6f8('0x52')](_0x89bbd6['body'],[_0xf6f8('0x51'),'id'])||{});}})[_0xf6f8('0x26')](respondWithResult(_0x39d0f4,null))['catch'](handleError(_0x39d0f4,null));};exports[_0xf6f8('0x53')]=function(_0x426432,_0x3d5873,_0x5aed59){var _0x108b60={};var _0x193e04={};var _0x8a591e;var _0x35f355;return db['CmCompany'][_0xf6f8('0x54')]({'where':{'id':_0x426432[_0xf6f8('0x47')]['id']}})[_0xf6f8('0x26')](handleEntityNotFound(_0x3d5873,null))[_0xf6f8('0x26')](function(_0x39faf2){if(_0x39faf2){_0x8a591e=_0x39faf2;_0x193e04['model']=_[_0xf6f8('0x2f')](db['CmContact']['rawAttributes']);_0x193e04[_0xf6f8('0x30')]=_[_0xf6f8('0x2f')](_0x426432[_0xf6f8('0x30')]);_0x193e04[_0xf6f8('0x31')]=_[_0xf6f8('0x32')](_0x193e04[_0xf6f8('0x2e')],_0x193e04[_0xf6f8('0x30')]);_0x108b60['attributes']=_[_0xf6f8('0x32')](_0x193e04[_0xf6f8('0x2e')],qs[_0xf6f8('0x34')](_0x426432[_0xf6f8('0x30')]['fields']));_0x108b60[_0xf6f8('0x33')]=_0x108b60[_0xf6f8('0x33')]['length']?_0x108b60[_0xf6f8('0x33')]:_0x193e04[_0xf6f8('0x2e')];_0x108b60[_0xf6f8('0x38')]=qs[_0xf6f8('0x39')](_0x426432[_0xf6f8('0x30')][_0xf6f8('0x39')]);_0x108b60[_0xf6f8('0x3a')]=qs[_0xf6f8('0x31')](_[_0xf6f8('0x3b')](_0x426432[_0xf6f8('0x30')],_0x193e04[_0xf6f8('0x31')]));if(_0x426432['query']['filter']){_0x108b60['where']=_[_0xf6f8('0x3e')](_0x108b60[_0xf6f8('0x3a')],{'$or':_[_0xf6f8('0x3c')](_0x108b60[_0xf6f8('0x33')],function(_0x5b2d8d){var _0x472dc5={};_0x472dc5[_0x5b2d8d]={'$like':'%'+_0x426432[_0xf6f8('0x30')][_0xf6f8('0x3d')]+'%'};return _0x472dc5;})});}_0x108b60=_['merge']({},_0x108b60,_0x426432[_0xf6f8('0x3f')]);return _0x8a591e[_0xf6f8('0x53')](_0x108b60);}})[_0xf6f8('0x26')](function(_0x7004e7){if(_0x7004e7){_0x35f355=_0x7004e7[_0xf6f8('0x35')];if(!_0x426432[_0xf6f8('0x30')][_0xf6f8('0x36')](_0xf6f8('0x37'))){_0x108b60['limit']=qs[_0xf6f8('0x1d')](_0x426432[_0xf6f8('0x30')]['limit']);_0x108b60[_0xf6f8('0x1b')]=qs[_0xf6f8('0x1b')](_0x426432['query'][_0xf6f8('0x1b')]);}return _0x8a591e[_0xf6f8('0x53')](_0x108b60);}})[_0xf6f8('0x26')](function(_0x17aeb6){if(_0x17aeb6){return _0x17aeb6?{'count':_0x35f355,'rows':_0x17aeb6}:null;}})[_0xf6f8('0x26')](respondWithResult(_0x3d5873,null))[_0xf6f8('0x45')](handleError(_0x3d5873,null));};