Built motion from commit bab6f49e.|2.6.17
[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 _0xf5c6=['intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filters','filter','merge','map','includeAll','include','rows','show','params','rawAttributes','options','find','catch','create','body','user','role','get','UserProfileSection','userProfileId','autoAssociation','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','addContacts','CmContact','ids','getContacts','findOne','eml-format','rimraf','zip-dir','fast-json-patch','moment','mustache','path','to-csv','ejs','fs-extra','squel','crypto','jsforce','querystring','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','end','status','json','undefined','limit','count','offset','set','Content-Range','reject','save','update','then','Companies','UserProfileResource','stack','name','send','index','CmCompany','describe','model','keys','query','attributes'];(function(_0x20a8a3,_0x50df97){var _0x20b002=function(_0x510758){while(--_0x510758){_0x20a8a3['push'](_0x20a8a3['shift']());}};_0x20b002(++_0x50df97);}(_0xf5c6,0x7a));var _0x6f5c=function(_0x54b9ac,_0x1d40e8){_0x54b9ac=_0x54b9ac-0x0;var _0x5e0065=_0xf5c6[_0x54b9ac];return _0x5e0065;};'use strict';var emlformat=require(_0x6f5c('0x0'));var rimraf=require(_0x6f5c('0x1'));var zipdir=require(_0x6f5c('0x2'));var jsonpatch=require(_0x6f5c('0x3'));var rp=require('request-promise');var moment=require(_0x6f5c('0x4'));var BPromise=require('bluebird');var Mustache=require(_0x6f5c('0x5'));var util=require('util');var path=require(_0x6f5c('0x6'));var sox=require('sox');var csv=require(_0x6f5c('0x7'));var ejs=require(_0x6f5c('0x8'));var fs=require('fs');var fs_extra=require(_0x6f5c('0x9'));var _=require('lodash');var squel=require(_0x6f5c('0xa'));var crypto=require(_0x6f5c('0xb'));var jsforce=require(_0x6f5c('0xc'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x6f5c('0xd'));var Papa=require('papaparse');var Redis=require(_0x6f5c('0xe'));var authService=require(_0x6f5c('0xf'));var qs=require('../../components/parsers/qs');var as=require(_0x6f5c('0x10'));var hardwareService=require(_0x6f5c('0x11'));var logger=require('../../config/logger')(_0x6f5c('0x12'));var utils=require('../../config/utils');var config=require(_0x6f5c('0x13'));var licenseUtil=require(_0x6f5c('0x14'));var db=require(_0x6f5c('0x15'))['db'];function respondWithStatusCode(_0x47085c,_0x9f664a){_0x9f664a=_0x9f664a||0xcc;return function(_0x3f95c5){if(_0x3f95c5){return _0x47085c[_0x6f5c('0x16')](_0x9f664a);}return _0x47085c['status'](_0x9f664a)[_0x6f5c('0x17')]();};}function respondWithResult(_0x12b469,_0x1141c5){_0x1141c5=_0x1141c5||0xc8;return function(_0x15277e){if(_0x15277e){return _0x12b469[_0x6f5c('0x18')](_0x1141c5)[_0x6f5c('0x19')](_0x15277e);}};}function respondWithFilteredResult(_0xad6ac9,_0x37cc35){return function(_0x46a24a){if(_0x46a24a){var _0x2bcdb2=typeof _0x37cc35['offset']===_0x6f5c('0x1a')&&typeof _0x37cc35[_0x6f5c('0x1b')]===_0x6f5c('0x1a');var _0x5789ca=_0x46a24a['count'];var _0x3ffec5=_0x2bcdb2?0x0:_0x37cc35['offset'];var _0x418edf=_0x2bcdb2?_0x46a24a[_0x6f5c('0x1c')]:_0x37cc35[_0x6f5c('0x1d')]+_0x37cc35[_0x6f5c('0x1b')];var _0x4dc2a7;if(_0x418edf>=_0x5789ca){_0x418edf=_0x5789ca;_0x4dc2a7=0xc8;}else{_0x4dc2a7=0xce;}_0xad6ac9[_0x6f5c('0x18')](_0x4dc2a7);return _0xad6ac9[_0x6f5c('0x1e')](_0x6f5c('0x1f'),_0x3ffec5+'-'+_0x418edf+'/'+_0x5789ca)[_0x6f5c('0x19')](_0x46a24a);}return null;};}function patchUpdates(_0x1c081e){return function(_0x95b67e){try{jsonpatch['apply'](_0x95b67e,_0x1c081e,!![]);}catch(_0x2f5e9e){return BPromise[_0x6f5c('0x20')](_0x2f5e9e);}return _0x95b67e[_0x6f5c('0x21')]();};}function saveUpdates(_0x14c019,_0x399cff){return function(_0x2aadc0){if(_0x2aadc0){return _0x2aadc0[_0x6f5c('0x22')](_0x14c019)[_0x6f5c('0x23')](function(_0x18151f){return _0x18151f;});}return null;};}function removeEntity(_0x3ad8d8,_0x2f6a8b){return function(_0x1935dc){if(_0x1935dc){return _0x1935dc['destroy']()[_0x6f5c('0x23')](function(){var _0x54d767=_0x1935dc['get']({'plain':!![]});var _0x27a066=_0x6f5c('0x24');return db[_0x6f5c('0x25')]['destroy']({'where':{'type':_0x27a066,'resourceId':_0x54d767['id']}})['then'](function(){return _0x1935dc;});})[_0x6f5c('0x23')](function(){_0x3ad8d8[_0x6f5c('0x18')](0xcc)[_0x6f5c('0x17')]();});}};}function handleEntityNotFound(_0xc31a8b,_0x5eeb12){return function(_0x2ba2d4){if(!_0x2ba2d4){_0xc31a8b[_0x6f5c('0x16')](0x194);}return _0x2ba2d4;};}function handleError(_0x5a27a9,_0x14fb20){_0x14fb20=_0x14fb20||0x1f4;return function(_0x12f4a5){logger['error'](_0x12f4a5[_0x6f5c('0x26')]);if(_0x12f4a5[_0x6f5c('0x27')]){delete _0x12f4a5[_0x6f5c('0x27')];}_0x5a27a9['status'](_0x14fb20)[_0x6f5c('0x28')](_0x12f4a5);};}exports[_0x6f5c('0x29')]=function(_0x229d29,_0xf2f902){var _0x58b9c5={},_0x10cfe4={},_0x5a246f={'count':0x0,'rows':[]};return db[_0x6f5c('0x2a')][_0x6f5c('0x2b')]()[_0x6f5c('0x23')](function(_0x5ee76b){_0x10cfe4[_0x6f5c('0x2c')]=_[_0x6f5c('0x2d')](_0x5ee76b);_0x10cfe4[_0x6f5c('0x2e')]=_[_0x6f5c('0x2d')](_0x229d29[_0x6f5c('0x2e')]);_0x10cfe4['filters']=_['intersection'](_0x10cfe4[_0x6f5c('0x2c')],_0x10cfe4[_0x6f5c('0x2e')]);_0x58b9c5[_0x6f5c('0x2f')]=_[_0x6f5c('0x30')](_0x10cfe4[_0x6f5c('0x2c')],qs[_0x6f5c('0x31')](_0x229d29[_0x6f5c('0x2e')]['fields']));_0x58b9c5[_0x6f5c('0x2f')]=_0x58b9c5[_0x6f5c('0x2f')][_0x6f5c('0x32')]?_0x58b9c5[_0x6f5c('0x2f')]:_0x10cfe4[_0x6f5c('0x2c')];if(!_0x229d29['query'][_0x6f5c('0x33')](_0x6f5c('0x34'))){_0x58b9c5['limit']=qs[_0x6f5c('0x1b')](_0x229d29[_0x6f5c('0x2e')][_0x6f5c('0x1b')]);_0x58b9c5[_0x6f5c('0x1d')]=qs[_0x6f5c('0x1d')](_0x229d29[_0x6f5c('0x2e')][_0x6f5c('0x1d')]);}_0x58b9c5[_0x6f5c('0x35')]=qs['sort'](_0x229d29[_0x6f5c('0x2e')][_0x6f5c('0x36')]);_0x58b9c5[_0x6f5c('0x37')]=qs['filters'](_[_0x6f5c('0x38')](_0x229d29[_0x6f5c('0x2e')],_0x10cfe4[_0x6f5c('0x39')]),_0x5ee76b);if(_0x229d29[_0x6f5c('0x2e')][_0x6f5c('0x3a')]){_0x58b9c5[_0x6f5c('0x37')]=_[_0x6f5c('0x3b')](_0x58b9c5['where'],{'$or':_[_0x6f5c('0x3c')](_0x58b9c5[_0x6f5c('0x2f')],function(_0x32aa86){var _0x6d39d7={};_0x6d39d7[_0x32aa86]={'$like':'%'+_0x229d29['query'][_0x6f5c('0x3a')]+'%'};return _0x6d39d7;})});}_0x58b9c5=_['merge']({},_0x58b9c5,_0x229d29['options']);var _0x217f8b={'where':_0x58b9c5[_0x6f5c('0x37')]};return db[_0x6f5c('0x2a')][_0x6f5c('0x1c')](_0x217f8b)[_0x6f5c('0x23')](function(_0x32c9b3){_0x5a246f[_0x6f5c('0x1c')]=_0x32c9b3;if(_0x229d29['query'][_0x6f5c('0x3d')]){_0x58b9c5[_0x6f5c('0x3e')]=[{'all':!![]}];}return db[_0x6f5c('0x2a')]['findAll'](_0x58b9c5);})['then'](function(_0x218199){_0x5a246f[_0x6f5c('0x3f')]=_0x218199;return _0x5a246f;})[_0x6f5c('0x23')](respondWithFilteredResult(_0xf2f902,_0x58b9c5))['catch'](handleError(_0xf2f902,null));})['catch'](handleError(_0xf2f902,null));};exports[_0x6f5c('0x40')]=function(_0x48defe,_0x1c2dda){var _0x40b068={'raw':![],'where':{'id':_0x48defe[_0x6f5c('0x41')]['id']}},_0x1a0a4f={};_0x1a0a4f[_0x6f5c('0x2c')]=_[_0x6f5c('0x2d')](db[_0x6f5c('0x2a')][_0x6f5c('0x42')]);_0x1a0a4f[_0x6f5c('0x2e')]=_['keys'](_0x48defe['query']);_0x1a0a4f[_0x6f5c('0x39')]=_[_0x6f5c('0x30')](_0x1a0a4f['model'],_0x1a0a4f['query']);_0x40b068[_0x6f5c('0x2f')]=_[_0x6f5c('0x30')](_0x1a0a4f['model'],qs[_0x6f5c('0x31')](_0x48defe[_0x6f5c('0x2e')]['fields']));_0x40b068[_0x6f5c('0x2f')]=_0x40b068[_0x6f5c('0x2f')][_0x6f5c('0x32')]?_0x40b068[_0x6f5c('0x2f')]:_0x1a0a4f['model'];if(_0x48defe[_0x6f5c('0x2e')][_0x6f5c('0x3d')]){_0x40b068[_0x6f5c('0x3e')]=[{'all':!![]}];}_0x40b068=_[_0x6f5c('0x3b')]({},_0x40b068,_0x48defe[_0x6f5c('0x43')]);return db[_0x6f5c('0x2a')][_0x6f5c('0x44')](_0x40b068)[_0x6f5c('0x23')](handleEntityNotFound(_0x1c2dda,null))[_0x6f5c('0x23')](respondWithResult(_0x1c2dda,null))[_0x6f5c('0x45')](handleError(_0x1c2dda,null));};exports['create']=function(_0x3e6192,_0x42233c){return db[_0x6f5c('0x2a')][_0x6f5c('0x46')](_0x3e6192[_0x6f5c('0x47')],{})[_0x6f5c('0x23')](function(_0x7ae62c){var _0x3a4e92=_0x3e6192[_0x6f5c('0x48')]['get']({'plain':!![]});if(!_0x3a4e92)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x3a4e92[_0x6f5c('0x49')]==='user'){var _0x174a06=_0x7ae62c[_0x6f5c('0x4a')]({'plain':!![]});var _0x2bbab1=_0x6f5c('0x24');return db[_0x6f5c('0x4b')][_0x6f5c('0x44')]({'where':{'name':_0x2bbab1,'userProfileId':_0x3a4e92[_0x6f5c('0x4c')]},'raw':!![]})[_0x6f5c('0x23')](function(_0x3db40e){if(_0x3db40e&&_0x3db40e[_0x6f5c('0x4d')]===0x0){return db[_0x6f5c('0x25')][_0x6f5c('0x46')]({'name':_0x174a06[_0x6f5c('0x27')],'resourceId':_0x174a06['id'],'type':_0x3db40e['name'],'sectionId':_0x3db40e['id']},{})[_0x6f5c('0x23')](function(){return _0x7ae62c;});}else{return _0x7ae62c;}})[_0x6f5c('0x45')](function(_0x2136d4){logger[_0x6f5c('0x4e')](_0x6f5c('0x4f'),_0x2136d4);throw _0x2136d4;});}return _0x7ae62c;})['then'](respondWithResult(_0x42233c,0xc9))['catch'](handleError(_0x42233c,null));};exports[_0x6f5c('0x22')]=function(_0x133dc5,_0x12ec54){if(_0x133dc5[_0x6f5c('0x47')]['id']){delete _0x133dc5[_0x6f5c('0x47')]['id'];}return db[_0x6f5c('0x2a')][_0x6f5c('0x44')]({'where':{'id':_0x133dc5['params']['id']}})[_0x6f5c('0x23')](handleEntityNotFound(_0x12ec54,null))['then'](saveUpdates(_0x133dc5[_0x6f5c('0x47')],null))[_0x6f5c('0x23')](respondWithResult(_0x12ec54,null))[_0x6f5c('0x45')](handleError(_0x12ec54,null));};exports['destroy']=function(_0x4c285c,_0x1272f6){return db[_0x6f5c('0x2a')][_0x6f5c('0x44')]({'where':{'id':_0x4c285c['params']['id']}})[_0x6f5c('0x23')](handleEntityNotFound(_0x1272f6,null))[_0x6f5c('0x23')](removeEntity(_0x1272f6,null))[_0x6f5c('0x45')](handleError(_0x1272f6,null));};exports[_0x6f5c('0x2b')]=function(_0x4fdb7c,_0x4549ee){return db[_0x6f5c('0x2a')][_0x6f5c('0x2b')]()[_0x6f5c('0x23')](respondWithResult(_0x4549ee,null))[_0x6f5c('0x45')](handleError(_0x4549ee,null));};exports[_0x6f5c('0x50')]=function(_0x5b3cdd,_0x167685,_0x4f5e2a){return db[_0x6f5c('0x51')][_0x6f5c('0x44')]({'where':{'id':_0x5b3cdd[_0x6f5c('0x41')]['id']}})[_0x6f5c('0x23')](handleEntityNotFound(_0x167685,null))['then'](function(_0xcbade7){if(_0xcbade7){return _0xcbade7[_0x6f5c('0x50')](_0x5b3cdd[_0x6f5c('0x47')]['ids'],_['omit'](_0x5b3cdd[_0x6f5c('0x47')],[_0x6f5c('0x52'),'id'])||{});}})[_0x6f5c('0x23')](respondWithResult(_0x167685,null))[_0x6f5c('0x45')](handleError(_0x167685,null));};exports[_0x6f5c('0x53')]=function(_0x3a7efd,_0x1e914b,_0x5c6cc9){var _0x2f914e={};var _0x4955d0={};var _0x4792bd;var _0x5a4191;return db['CmCompany'][_0x6f5c('0x54')]({'where':{'id':_0x3a7efd['params']['id']}})[_0x6f5c('0x23')](handleEntityNotFound(_0x1e914b,null))[_0x6f5c('0x23')](function(_0x5621e4){if(_0x5621e4){_0x4792bd=_0x5621e4;_0x4955d0[_0x6f5c('0x2c')]=_[_0x6f5c('0x2d')](db[_0x6f5c('0x51')][_0x6f5c('0x42')]);_0x4955d0[_0x6f5c('0x2e')]=_[_0x6f5c('0x2d')](_0x3a7efd[_0x6f5c('0x2e')]);_0x4955d0[_0x6f5c('0x39')]=_[_0x6f5c('0x30')](_0x4955d0[_0x6f5c('0x2c')],_0x4955d0[_0x6f5c('0x2e')]);_0x2f914e[_0x6f5c('0x2f')]=_[_0x6f5c('0x30')](_0x4955d0['model'],qs[_0x6f5c('0x31')](_0x3a7efd[_0x6f5c('0x2e')][_0x6f5c('0x31')]));_0x2f914e['attributes']=_0x2f914e[_0x6f5c('0x2f')][_0x6f5c('0x32')]?_0x2f914e[_0x6f5c('0x2f')]:_0x4955d0['model'];_0x2f914e[_0x6f5c('0x35')]=qs[_0x6f5c('0x36')](_0x3a7efd[_0x6f5c('0x2e')][_0x6f5c('0x36')]);_0x2f914e[_0x6f5c('0x37')]=qs[_0x6f5c('0x39')](_[_0x6f5c('0x38')](_0x3a7efd['query'],_0x4955d0['filters']));if(_0x3a7efd[_0x6f5c('0x2e')][_0x6f5c('0x3a')]){_0x2f914e[_0x6f5c('0x37')]=_[_0x6f5c('0x3b')](_0x2f914e[_0x6f5c('0x37')],{'$or':_[_0x6f5c('0x3c')](_0x2f914e['attributes'],function(_0x26aad8){var _0x289c2b={};_0x289c2b[_0x26aad8]={'$like':'%'+_0x3a7efd[_0x6f5c('0x2e')]['filter']+'%'};return _0x289c2b;})});}_0x2f914e=_[_0x6f5c('0x3b')]({},_0x2f914e,_0x3a7efd[_0x6f5c('0x43')]);return _0x4792bd['getContacts'](_0x2f914e);}})[_0x6f5c('0x23')](function(_0x329a66){if(_0x329a66){_0x5a4191=_0x329a66[_0x6f5c('0x32')];if(!_0x3a7efd[_0x6f5c('0x2e')][_0x6f5c('0x33')](_0x6f5c('0x34'))){_0x2f914e[_0x6f5c('0x1b')]=qs[_0x6f5c('0x1b')](_0x3a7efd['query']['limit']);_0x2f914e[_0x6f5c('0x1d')]=qs['offset'](_0x3a7efd['query'][_0x6f5c('0x1d')]);}return _0x4792bd[_0x6f5c('0x53')](_0x2f914e);}})[_0x6f5c('0x23')](function(_0x5e5659){if(_0x5e5659){return _0x5e5659?{'count':_0x5a4191,'rows':_0x5e5659}:null;}})[_0x6f5c('0x23')](respondWithResult(_0x1e914b,null))[_0x6f5c('0x45')](handleError(_0x1e914b,null));};