Built motion from commit (unavailable).|2.5.1
[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 _0xb5e1=['length','hasOwnProperty','nolimit','order','sort','where','merge','map','filter','CmCompany','include','findAll','catch','show','params','options','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','addContacts','CmContact','ids','omit','getContacts','findOne','html-pdf','zip-dir','fast-json-patch','request-promise','bluebird','mustache','util','path','sox','to-csv','ejs','lodash','squel','crypto','jsforce','desk.js','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','sendStatus','end','status','json','count','offset','limit','set','apply','save','then','destroy','get','Companies','UserProfileResource','error','stack','name','send','index','describe','model','keys','query','filters','intersection','attributes','fields'];(function(_0x398294,_0x5ceb9d){var _0x2a872d=function(_0x390883){while(--_0x390883){_0x398294['push'](_0x398294['shift']());}};_0x2a872d(++_0x5ceb9d);}(_0xb5e1,0xc5));var _0x1b5e=function(_0x2a8483,_0x41009a){_0x2a8483=_0x2a8483-0x0;var _0x54880f=_0xb5e1[_0x2a8483];return _0x54880f;};'use strict';var pdf=require(_0x1b5e('0x0'));var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0x1b5e('0x1'));var jsonpatch=require(_0x1b5e('0x2'));var rp=require(_0x1b5e('0x3'));var moment=require('moment');var BPromise=require(_0x1b5e('0x4'));var Mustache=require(_0x1b5e('0x5'));var util=require(_0x1b5e('0x6'));var path=require(_0x1b5e('0x7'));var sox=require(_0x1b5e('0x8'));var csv=require(_0x1b5e('0x9'));var ejs=require(_0x1b5e('0xa'));var fs=require('fs');var _=require(_0x1b5e('0xb'));var squel=require(_0x1b5e('0xc'));var crypto=require(_0x1b5e('0xd'));var jsforce=require(_0x1b5e('0xe'));var deskjs=require(_0x1b5e('0xf'));var toCsv=require('to-csv');var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0x1b5e('0x10'));var authService=require(_0x1b5e('0x11'));var qs=require(_0x1b5e('0x12'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x1b5e('0x13'));var logger=require(_0x1b5e('0x14'))(_0x1b5e('0x15'));var utils=require(_0x1b5e('0x16'));var config=require(_0x1b5e('0x17'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x101857,_0x3b500e){_0x3b500e=_0x3b500e||0xcc;return function(_0x26e4c6){if(_0x26e4c6){return _0x101857[_0x1b5e('0x18')](_0x3b500e);}return _0x101857['status'](_0x3b500e)[_0x1b5e('0x19')]();};}function respondWithResult(_0x3cd7f1,_0x3413b1){_0x3413b1=_0x3413b1||0xc8;return function(_0x57cef5){if(_0x57cef5){return _0x3cd7f1[_0x1b5e('0x1a')](_0x3413b1)[_0x1b5e('0x1b')](_0x57cef5);}};}function respondWithFilteredResult(_0x5a1524,_0x228ce5){return function(_0x558451){if(_0x558451){var _0x105780=_0x558451[_0x1b5e('0x1c')],_0x2000c8=_0x228ce5[_0x1b5e('0x1d')],_0x5c6e61=_0x228ce5[_0x1b5e('0x1d')]+_0x228ce5[_0x1b5e('0x1e')],_0x4887dd;if(_0x5c6e61>=_0x105780){_0x5c6e61=_0x105780;_0x4887dd=0xc8;}else{_0x4887dd=0xce;}_0x5a1524[_0x1b5e('0x1a')](_0x4887dd);return _0x5a1524[_0x1b5e('0x1f')]('Content-Range',_0x2000c8+'-'+_0x5c6e61+'/'+_0x105780)[_0x1b5e('0x1b')](_0x558451);}return null;};}function patchUpdates(_0x538d23){return function(_0x3e8b9c){try{jsonpatch[_0x1b5e('0x20')](_0x3e8b9c,_0x538d23,!![]);}catch(_0x40b07a){return BPromise['reject'](_0x40b07a);}return _0x3e8b9c[_0x1b5e('0x21')]();};}function saveUpdates(_0x173ac8,_0x474e3a){return function(_0x32905e){if(_0x32905e){return _0x32905e['update'](_0x173ac8)[_0x1b5e('0x22')](function(_0x3c64f2){return _0x3c64f2;});}return null;};}function removeEntity(_0x345aca,_0x36973e){return function(_0xf7a9f3){if(_0xf7a9f3){return _0xf7a9f3[_0x1b5e('0x23')]()[_0x1b5e('0x22')](function(){var _0x2ba114=_0xf7a9f3[_0x1b5e('0x24')]({'plain':!![]});var _0x5a19a2=_0x1b5e('0x25');return db[_0x1b5e('0x26')][_0x1b5e('0x23')]({'where':{'type':_0x5a19a2,'resourceId':_0x2ba114['id']}})[_0x1b5e('0x22')](function(){return _0xf7a9f3;});})[_0x1b5e('0x22')](function(){_0x345aca[_0x1b5e('0x1a')](0xcc)['end']();});}};}function handleEntityNotFound(_0x43da9f,_0x57913c){return function(_0x4304cd){if(!_0x4304cd){_0x43da9f['sendStatus'](0x194);}return _0x4304cd;};}function handleError(_0x25d980,_0x45105c){_0x45105c=_0x45105c||0x1f4;return function(_0x1a7670){logger[_0x1b5e('0x27')](_0x1a7670[_0x1b5e('0x28')]);if(_0x1a7670['name']){delete _0x1a7670[_0x1b5e('0x29')];}_0x25d980[_0x1b5e('0x1a')](_0x45105c)[_0x1b5e('0x2a')](_0x1a7670);};}exports[_0x1b5e('0x2b')]=function(_0x1774ca,_0x20f802){var _0x11cae4={},_0x816857={},_0x521e20={'count':0x0,'rows':[]};return db['CmCompany'][_0x1b5e('0x2c')]()['then'](function(_0x462fd0){_0x816857[_0x1b5e('0x2d')]=_[_0x1b5e('0x2e')](_0x462fd0);_0x816857[_0x1b5e('0x2f')]=_[_0x1b5e('0x2e')](_0x1774ca[_0x1b5e('0x2f')]);_0x816857[_0x1b5e('0x30')]=_[_0x1b5e('0x31')](_0x816857[_0x1b5e('0x2d')],_0x816857[_0x1b5e('0x2f')]);_0x11cae4[_0x1b5e('0x32')]=_[_0x1b5e('0x31')](_0x816857['model'],qs[_0x1b5e('0x33')](_0x1774ca[_0x1b5e('0x2f')][_0x1b5e('0x33')]));_0x11cae4['attributes']=_0x11cae4[_0x1b5e('0x32')][_0x1b5e('0x34')]?_0x11cae4[_0x1b5e('0x32')]:_0x816857[_0x1b5e('0x2d')];if(!_0x1774ca[_0x1b5e('0x2f')][_0x1b5e('0x35')](_0x1b5e('0x36'))){_0x11cae4['limit']=qs[_0x1b5e('0x1e')](_0x1774ca['query']['limit']);_0x11cae4[_0x1b5e('0x1d')]=qs[_0x1b5e('0x1d')](_0x1774ca[_0x1b5e('0x2f')][_0x1b5e('0x1d')]);}_0x11cae4[_0x1b5e('0x37')]=qs[_0x1b5e('0x38')](_0x1774ca[_0x1b5e('0x2f')][_0x1b5e('0x38')]);_0x11cae4['where']=qs[_0x1b5e('0x30')](_['pick'](_0x1774ca[_0x1b5e('0x2f')],_0x816857[_0x1b5e('0x30')]),_0x462fd0);if(_0x1774ca[_0x1b5e('0x2f')]['filter']){_0x11cae4[_0x1b5e('0x39')]=_[_0x1b5e('0x3a')](_0x11cae4['where'],{'$or':_[_0x1b5e('0x3b')](_0x11cae4[_0x1b5e('0x32')],function(_0x10975){var _0x570402={};_0x570402[_0x10975]={'$like':'%'+_0x1774ca[_0x1b5e('0x2f')][_0x1b5e('0x3c')]+'%'};return _0x570402;})});}_0x11cae4=_[_0x1b5e('0x3a')]({},_0x11cae4,_0x1774ca['options']);var _0x4113d5={'where':_0x11cae4[_0x1b5e('0x39')]};return db[_0x1b5e('0x3d')]['count'](_0x4113d5)[_0x1b5e('0x22')](function(_0x1dddd0){_0x521e20[_0x1b5e('0x1c')]=_0x1dddd0;if(_0x1774ca[_0x1b5e('0x2f')]['includeAll']){_0x11cae4[_0x1b5e('0x3e')]=[{'all':!![]}];}return db[_0x1b5e('0x3d')][_0x1b5e('0x3f')](_0x11cae4);})['then'](function(_0xabd991){_0x521e20['rows']=_0xabd991;return _0x521e20;})[_0x1b5e('0x22')](respondWithFilteredResult(_0x20f802,_0x11cae4))['catch'](handleError(_0x20f802,null));})[_0x1b5e('0x40')](handleError(_0x20f802,null));};exports[_0x1b5e('0x41')]=function(_0x3b33b6,_0x35fd50){var _0x2db7ec={'raw':![],'where':{'id':_0x3b33b6[_0x1b5e('0x42')]['id']}},_0x18fdb9={};_0x18fdb9[_0x1b5e('0x2d')]=_[_0x1b5e('0x2e')](db[_0x1b5e('0x3d')]['rawAttributes']);_0x18fdb9['query']=_[_0x1b5e('0x2e')](_0x3b33b6[_0x1b5e('0x2f')]);_0x18fdb9['filters']=_[_0x1b5e('0x31')](_0x18fdb9[_0x1b5e('0x2d')],_0x18fdb9[_0x1b5e('0x2f')]);_0x2db7ec['attributes']=_['intersection'](_0x18fdb9[_0x1b5e('0x2d')],qs[_0x1b5e('0x33')](_0x3b33b6[_0x1b5e('0x2f')][_0x1b5e('0x33')]));_0x2db7ec['attributes']=_0x2db7ec[_0x1b5e('0x32')][_0x1b5e('0x34')]?_0x2db7ec[_0x1b5e('0x32')]:_0x18fdb9[_0x1b5e('0x2d')];if(_0x3b33b6[_0x1b5e('0x2f')]['includeAll']){_0x2db7ec[_0x1b5e('0x3e')]=[{'all':!![]}];}_0x2db7ec=_[_0x1b5e('0x3a')]({},_0x2db7ec,_0x3b33b6[_0x1b5e('0x43')]);return db[_0x1b5e('0x3d')][_0x1b5e('0x44')](_0x2db7ec)['then'](handleEntityNotFound(_0x35fd50,null))[_0x1b5e('0x22')](respondWithResult(_0x35fd50,null))['catch'](handleError(_0x35fd50,null));};exports[_0x1b5e('0x45')]=function(_0x24c269,_0x48efc1){return db[_0x1b5e('0x3d')][_0x1b5e('0x45')](_0x24c269[_0x1b5e('0x46')],{})['then'](function(_0x374a9a){var _0x1d14e8=_0x24c269[_0x1b5e('0x47')]['get']({'plain':!![]});if(!_0x1d14e8)throw new Error(_0x1b5e('0x48'));if(_0x1d14e8['role']===_0x1b5e('0x47')){var _0x2e0603=_0x374a9a['get']({'plain':!![]});var _0x39b582=_0x1b5e('0x25');return db['UserProfileSection']['find']({'where':{'name':_0x39b582,'userProfileId':_0x1d14e8[_0x1b5e('0x49')]},'raw':!![]})[_0x1b5e('0x22')](function(_0xa0acae){if(_0xa0acae&&_0xa0acae[_0x1b5e('0x4a')]===0x0){return db[_0x1b5e('0x26')][_0x1b5e('0x45')]({'name':_0x2e0603[_0x1b5e('0x29')],'resourceId':_0x2e0603['id'],'type':_0xa0acae[_0x1b5e('0x29')],'sectionId':_0xa0acae['id']},{})[_0x1b5e('0x22')](function(){return _0x374a9a;});}else{return _0x374a9a;}})[_0x1b5e('0x40')](function(_0x43f387){logger['error'](_0x1b5e('0x4b'),_0x43f387);throw _0x43f387;});}return _0x374a9a;})[_0x1b5e('0x22')](respondWithResult(_0x48efc1,0xc9))[_0x1b5e('0x40')](handleError(_0x48efc1,null));};exports[_0x1b5e('0x4c')]=function(_0x4c4d88,_0x38e2d1){if(_0x4c4d88[_0x1b5e('0x46')]['id']){delete _0x4c4d88[_0x1b5e('0x46')]['id'];}return db['CmCompany'][_0x1b5e('0x44')]({'where':{'id':_0x4c4d88[_0x1b5e('0x42')]['id']}})[_0x1b5e('0x22')](handleEntityNotFound(_0x38e2d1,null))[_0x1b5e('0x22')](saveUpdates(_0x4c4d88[_0x1b5e('0x46')],null))[_0x1b5e('0x22')](respondWithResult(_0x38e2d1,null))[_0x1b5e('0x40')](handleError(_0x38e2d1,null));};exports[_0x1b5e('0x23')]=function(_0x23e5ec,_0x5b193d){return db['CmCompany'][_0x1b5e('0x44')]({'where':{'id':_0x23e5ec['params']['id']}})['then'](handleEntityNotFound(_0x5b193d,null))[_0x1b5e('0x22')](removeEntity(_0x5b193d,null))[_0x1b5e('0x40')](handleError(_0x5b193d,null));};exports[_0x1b5e('0x2c')]=function(_0x5a0154,_0xf26505){return db['CmCompany'][_0x1b5e('0x2c')]()['then'](respondWithResult(_0xf26505,null))['catch'](handleError(_0xf26505,null));};exports[_0x1b5e('0x4d')]=function(_0x3862ac,_0x5a2107,_0x4c20c1){return db[_0x1b5e('0x4e')][_0x1b5e('0x44')]({'where':{'id':_0x3862ac[_0x1b5e('0x42')]['id']}})['then'](handleEntityNotFound(_0x5a2107,null))[_0x1b5e('0x22')](function(_0x2ee17e){if(_0x2ee17e){return _0x2ee17e[_0x1b5e('0x4d')](_0x3862ac[_0x1b5e('0x46')][_0x1b5e('0x4f')],_[_0x1b5e('0x50')](_0x3862ac[_0x1b5e('0x46')],[_0x1b5e('0x4f'),'id'])||{});}})[_0x1b5e('0x22')](respondWithResult(_0x5a2107,null))[_0x1b5e('0x40')](handleError(_0x5a2107,null));};exports[_0x1b5e('0x51')]=function(_0x191248,_0x17dc21,_0x20e44b){var _0x1702de={};var _0x2421b7={};var _0xae63c1;var _0x46512d;return db['CmCompany'][_0x1b5e('0x52')]({'where':{'id':_0x191248[_0x1b5e('0x42')]['id']}})['then'](handleEntityNotFound(_0x17dc21,null))['then'](function(_0x371833){if(_0x371833){_0xae63c1=_0x371833;_0x2421b7[_0x1b5e('0x2d')]=_['keys'](db[_0x1b5e('0x4e')]['rawAttributes']);_0x2421b7['query']=_['keys'](_0x191248['query']);_0x2421b7[_0x1b5e('0x30')]=_['intersection'](_0x2421b7[_0x1b5e('0x2d')],_0x2421b7[_0x1b5e('0x2f')]);_0x1702de['attributes']=_['intersection'](_0x2421b7[_0x1b5e('0x2d')],qs[_0x1b5e('0x33')](_0x191248[_0x1b5e('0x2f')][_0x1b5e('0x33')]));_0x1702de['attributes']=_0x1702de[_0x1b5e('0x32')][_0x1b5e('0x34')]?_0x1702de[_0x1b5e('0x32')]:_0x2421b7[_0x1b5e('0x2d')];_0x1702de[_0x1b5e('0x37')]=qs[_0x1b5e('0x38')](_0x191248['query'][_0x1b5e('0x38')]);_0x1702de[_0x1b5e('0x39')]=qs['filters'](_['pick'](_0x191248[_0x1b5e('0x2f')],_0x2421b7['filters']));if(_0x191248[_0x1b5e('0x2f')][_0x1b5e('0x3c')]){_0x1702de['where']=_['merge'](_0x1702de[_0x1b5e('0x39')],{'$or':_[_0x1b5e('0x3b')](_0x1702de[_0x1b5e('0x32')],function(_0x4d180d){var _0x19abee={};_0x19abee[_0x4d180d]={'$like':'%'+_0x191248[_0x1b5e('0x2f')]['filter']+'%'};return _0x19abee;})});}_0x1702de=_[_0x1b5e('0x3a')]({},_0x1702de,_0x191248['options']);return _0xae63c1[_0x1b5e('0x51')](_0x1702de);}})['then'](function(_0x15ecc5){if(_0x15ecc5){_0x46512d=_0x15ecc5[_0x1b5e('0x34')];if(!_0x191248[_0x1b5e('0x2f')][_0x1b5e('0x35')](_0x1b5e('0x36'))){_0x1702de[_0x1b5e('0x1e')]=qs[_0x1b5e('0x1e')](_0x191248[_0x1b5e('0x2f')]['limit']);_0x1702de['offset']=qs[_0x1b5e('0x1d')](_0x191248[_0x1b5e('0x2f')][_0x1b5e('0x1d')]);}return _0xae63c1[_0x1b5e('0x51')](_0x1702de);}})[_0x1b5e('0x22')](function(_0xec1504){if(_0xec1504){return _0xec1504?{'count':_0x46512d,'rows':_0xec1504}:null;}})[_0x1b5e('0x22')](respondWithResult(_0x17dc21,null))[_0x1b5e('0x40')](handleError(_0x17dc21,null));};