af6886fcbbde4f29f0d93d067667627becd93662
[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 _0x63f5=['params','rawAttributes','options','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','userProfileId','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','describe','addContacts','ids','omit','getContacts','findOne','CmContact','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','to-csv','ejs','fs-extra','lodash','squel','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../mysqldb','sendStatus','end','json','undefined','count','offset','Content-Range','apply','save','update','destroy','then','get','Companies','status','error','name','send','index','CmCompany','model','query','keys','filters','intersection','attributes','fields','length','nolimit','limit','order','sort','where','pick','filter','merge','map','includeAll','include','findAll','catch','show'];(function(_0x2d52a3,_0x43e0b9){var _0x288a4b=function(_0x1bdfda){while(--_0x1bdfda){_0x2d52a3['push'](_0x2d52a3['shift']());}};_0x288a4b(++_0x43e0b9);}(_0x63f5,0x116));var _0x563f=function(_0x28089e,_0x530f8a){_0x28089e=_0x28089e-0x0;var _0x46a73c=_0x63f5[_0x28089e];return _0x46a73c;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0x563f('0x0'));var jsonpatch=require(_0x563f('0x1'));var rp=require(_0x563f('0x2'));var moment=require(_0x563f('0x3'));var BPromise=require(_0x563f('0x4'));var Mustache=require(_0x563f('0x5'));var util=require(_0x563f('0x6'));var path=require('path');var sox=require('sox');var csv=require(_0x563f('0x7'));var ejs=require(_0x563f('0x8'));var fs=require('fs');var fs_extra=require(_0x563f('0x9'));var _=require(_0x563f('0xa'));var squel=require(_0x563f('0xb'));var crypto=require('crypto');var jsforce=require(_0x563f('0xc'));var deskjs=require('desk.js');var toCsv=require(_0x563f('0x7'));var querystring=require(_0x563f('0xd'));var Papa=require(_0x563f('0xe'));var Redis=require(_0x563f('0xf'));var authService=require(_0x563f('0x10'));var qs=require(_0x563f('0x11'));var as=require(_0x563f('0x12'));var hardwareService=require(_0x563f('0x13'));var logger=require(_0x563f('0x14'))(_0x563f('0x15'));var utils=require(_0x563f('0x16'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0x563f('0x17'))['db'];function respondWithStatusCode(_0x338931,_0x963a23){_0x963a23=_0x963a23||0xcc;return function(_0x51f755){if(_0x51f755){return _0x338931[_0x563f('0x18')](_0x963a23);}return _0x338931['status'](_0x963a23)[_0x563f('0x19')]();};}function respondWithResult(_0x4bf8cc,_0x1f46fc){_0x1f46fc=_0x1f46fc||0xc8;return function(_0x1e309b){if(_0x1e309b){return _0x4bf8cc['status'](_0x1f46fc)[_0x563f('0x1a')](_0x1e309b);}};}function respondWithFilteredResult(_0xe68715,_0x1bc955){return function(_0x464dc6){if(_0x464dc6){var _0x4f5dee=typeof _0x1bc955['offset']===_0x563f('0x1b')&&typeof _0x1bc955['limit']===_0x563f('0x1b');var _0x2ffad2=_0x464dc6[_0x563f('0x1c')];var _0x36c1a8=_0x4f5dee?0x0:_0x1bc955[_0x563f('0x1d')];var _0x5a41e6=_0x4f5dee?_0x464dc6[_0x563f('0x1c')]:_0x1bc955[_0x563f('0x1d')]+_0x1bc955['limit'];var _0x39f835;if(_0x5a41e6>=_0x2ffad2){_0x5a41e6=_0x2ffad2;_0x39f835=0xc8;}else{_0x39f835=0xce;}_0xe68715['status'](_0x39f835);return _0xe68715['set'](_0x563f('0x1e'),_0x36c1a8+'-'+_0x5a41e6+'/'+_0x2ffad2)['json'](_0x464dc6);}return null;};}function patchUpdates(_0x3e971d){return function(_0x31edaf){try{jsonpatch[_0x563f('0x1f')](_0x31edaf,_0x3e971d,!![]);}catch(_0x3defdc){return BPromise['reject'](_0x3defdc);}return _0x31edaf[_0x563f('0x20')]();};}function saveUpdates(_0x49638e,_0x50d0fc){return function(_0x397e71){if(_0x397e71){return _0x397e71[_0x563f('0x21')](_0x49638e)['then'](function(_0x4c2fa1){return _0x4c2fa1;});}return null;};}function removeEntity(_0x580e60,_0x231920){return function(_0x4c3a44){if(_0x4c3a44){return _0x4c3a44[_0x563f('0x22')]()[_0x563f('0x23')](function(){var _0x48a63d=_0x4c3a44[_0x563f('0x24')]({'plain':!![]});var _0xac35a6=_0x563f('0x25');return db['UserProfileResource'][_0x563f('0x22')]({'where':{'type':_0xac35a6,'resourceId':_0x48a63d['id']}})[_0x563f('0x23')](function(){return _0x4c3a44;});})[_0x563f('0x23')](function(){_0x580e60[_0x563f('0x26')](0xcc)[_0x563f('0x19')]();});}};}function handleEntityNotFound(_0x3dc7e4,_0x3a5054){return function(_0x2c49d9){if(!_0x2c49d9){_0x3dc7e4['sendStatus'](0x194);}return _0x2c49d9;};}function handleError(_0x4eb772,_0x317a9c){_0x317a9c=_0x317a9c||0x1f4;return function(_0x568510){logger[_0x563f('0x27')](_0x568510['stack']);if(_0x568510[_0x563f('0x28')]){delete _0x568510[_0x563f('0x28')];}_0x4eb772['status'](_0x317a9c)[_0x563f('0x29')](_0x568510);};}exports[_0x563f('0x2a')]=function(_0x23487b,_0x4318c7){var _0xa3bafd={},_0x4b02f8={},_0x543d46={'count':0x0,'rows':[]};return db[_0x563f('0x2b')]['describe']()[_0x563f('0x23')](function(_0x6d2422){_0x4b02f8[_0x563f('0x2c')]=_['keys'](_0x6d2422);_0x4b02f8[_0x563f('0x2d')]=_[_0x563f('0x2e')](_0x23487b[_0x563f('0x2d')]);_0x4b02f8[_0x563f('0x2f')]=_[_0x563f('0x30')](_0x4b02f8[_0x563f('0x2c')],_0x4b02f8[_0x563f('0x2d')]);_0xa3bafd[_0x563f('0x31')]=_[_0x563f('0x30')](_0x4b02f8[_0x563f('0x2c')],qs['fields'](_0x23487b[_0x563f('0x2d')][_0x563f('0x32')]));_0xa3bafd[_0x563f('0x31')]=_0xa3bafd[_0x563f('0x31')][_0x563f('0x33')]?_0xa3bafd[_0x563f('0x31')]:_0x4b02f8[_0x563f('0x2c')];if(!_0x23487b['query']['hasOwnProperty'](_0x563f('0x34'))){_0xa3bafd[_0x563f('0x35')]=qs['limit'](_0x23487b[_0x563f('0x2d')][_0x563f('0x35')]);_0xa3bafd[_0x563f('0x1d')]=qs['offset'](_0x23487b['query'][_0x563f('0x1d')]);}_0xa3bafd[_0x563f('0x36')]=qs[_0x563f('0x37')](_0x23487b['query'][_0x563f('0x37')]);_0xa3bafd[_0x563f('0x38')]=qs[_0x563f('0x2f')](_[_0x563f('0x39')](_0x23487b[_0x563f('0x2d')],_0x4b02f8[_0x563f('0x2f')]),_0x6d2422);if(_0x23487b['query'][_0x563f('0x3a')]){_0xa3bafd[_0x563f('0x38')]=_[_0x563f('0x3b')](_0xa3bafd[_0x563f('0x38')],{'$or':_[_0x563f('0x3c')](_0xa3bafd[_0x563f('0x31')],function(_0x3d2f84){var _0x4638b5={};_0x4638b5[_0x3d2f84]={'$like':'%'+_0x23487b[_0x563f('0x2d')][_0x563f('0x3a')]+'%'};return _0x4638b5;})});}_0xa3bafd=_[_0x563f('0x3b')]({},_0xa3bafd,_0x23487b['options']);var _0x7f12e0={'where':_0xa3bafd[_0x563f('0x38')]};return db[_0x563f('0x2b')][_0x563f('0x1c')](_0x7f12e0)['then'](function(_0x3742c1){_0x543d46[_0x563f('0x1c')]=_0x3742c1;if(_0x23487b[_0x563f('0x2d')][_0x563f('0x3d')]){_0xa3bafd[_0x563f('0x3e')]=[{'all':!![]}];}return db[_0x563f('0x2b')][_0x563f('0x3f')](_0xa3bafd);})[_0x563f('0x23')](function(_0x30d1a8){_0x543d46['rows']=_0x30d1a8;return _0x543d46;})[_0x563f('0x23')](respondWithFilteredResult(_0x4318c7,_0xa3bafd))[_0x563f('0x40')](handleError(_0x4318c7,null));})['catch'](handleError(_0x4318c7,null));};exports[_0x563f('0x41')]=function(_0x4c45b4,_0x33b835){var _0x2b195e={'raw':![],'where':{'id':_0x4c45b4[_0x563f('0x42')]['id']}},_0x4c4ed6={};_0x4c4ed6[_0x563f('0x2c')]=_[_0x563f('0x2e')](db[_0x563f('0x2b')][_0x563f('0x43')]);_0x4c4ed6[_0x563f('0x2d')]=_['keys'](_0x4c45b4[_0x563f('0x2d')]);_0x4c4ed6[_0x563f('0x2f')]=_[_0x563f('0x30')](_0x4c4ed6[_0x563f('0x2c')],_0x4c4ed6[_0x563f('0x2d')]);_0x2b195e[_0x563f('0x31')]=_[_0x563f('0x30')](_0x4c4ed6[_0x563f('0x2c')],qs[_0x563f('0x32')](_0x4c45b4[_0x563f('0x2d')]['fields']));_0x2b195e[_0x563f('0x31')]=_0x2b195e[_0x563f('0x31')][_0x563f('0x33')]?_0x2b195e['attributes']:_0x4c4ed6[_0x563f('0x2c')];if(_0x4c45b4[_0x563f('0x2d')]['includeAll']){_0x2b195e[_0x563f('0x3e')]=[{'all':!![]}];}_0x2b195e=_[_0x563f('0x3b')]({},_0x2b195e,_0x4c45b4[_0x563f('0x44')]);return db[_0x563f('0x2b')][_0x563f('0x45')](_0x2b195e)[_0x563f('0x23')](handleEntityNotFound(_0x33b835,null))[_0x563f('0x23')](respondWithResult(_0x33b835,null))[_0x563f('0x40')](handleError(_0x33b835,null));};exports['create']=function(_0x2187c2,_0x4e82e0){return db[_0x563f('0x2b')][_0x563f('0x46')](_0x2187c2[_0x563f('0x47')],{})['then'](function(_0x4ca7c1){var _0x5010e5=_0x2187c2[_0x563f('0x48')][_0x563f('0x24')]({'plain':!![]});if(!_0x5010e5)throw new Error(_0x563f('0x49'));if(_0x5010e5[_0x563f('0x4a')]==='user'){var _0x5ce4cd=_0x4ca7c1['get']({'plain':!![]});var _0x2df8ec=_0x563f('0x25');return db['UserProfileSection'][_0x563f('0x45')]({'where':{'name':_0x2df8ec,'userProfileId':_0x5010e5[_0x563f('0x4b')]},'raw':!![]})[_0x563f('0x23')](function(_0xa3e27f){if(_0xa3e27f&&_0xa3e27f[_0x563f('0x4c')]===0x0){return db[_0x563f('0x4d')][_0x563f('0x46')]({'name':_0x5ce4cd[_0x563f('0x28')],'resourceId':_0x5ce4cd['id'],'type':_0xa3e27f[_0x563f('0x28')],'sectionId':_0xa3e27f['id']},{})[_0x563f('0x23')](function(){return _0x4ca7c1;});}else{return _0x4ca7c1;}})[_0x563f('0x40')](function(_0x57136f){logger[_0x563f('0x27')](_0x563f('0x4e'),_0x57136f);throw _0x57136f;});}return _0x4ca7c1;})[_0x563f('0x23')](respondWithResult(_0x4e82e0,0xc9))[_0x563f('0x40')](handleError(_0x4e82e0,null));};exports[_0x563f('0x21')]=function(_0x3a5eab,_0x17ff96){if(_0x3a5eab[_0x563f('0x47')]['id']){delete _0x3a5eab[_0x563f('0x47')]['id'];}return db[_0x563f('0x2b')][_0x563f('0x45')]({'where':{'id':_0x3a5eab[_0x563f('0x42')]['id']}})['then'](handleEntityNotFound(_0x17ff96,null))['then'](saveUpdates(_0x3a5eab[_0x563f('0x47')],null))[_0x563f('0x23')](respondWithResult(_0x17ff96,null))[_0x563f('0x40')](handleError(_0x17ff96,null));};exports[_0x563f('0x22')]=function(_0xb1df01,_0x4f4dd7){return db['CmCompany'][_0x563f('0x45')]({'where':{'id':_0xb1df01[_0x563f('0x42')]['id']}})[_0x563f('0x23')](handleEntityNotFound(_0x4f4dd7,null))['then'](removeEntity(_0x4f4dd7,null))['catch'](handleError(_0x4f4dd7,null));};exports[_0x563f('0x4f')]=function(_0x121ba5,_0xf6e635){return db[_0x563f('0x2b')]['describe']()[_0x563f('0x23')](respondWithResult(_0xf6e635,null))[_0x563f('0x40')](handleError(_0xf6e635,null));};exports[_0x563f('0x50')]=function(_0x4a3d1,_0x5630c3,_0x5aaa84){return db['CmContact'][_0x563f('0x45')]({'where':{'id':_0x4a3d1['params']['id']}})[_0x563f('0x23')](handleEntityNotFound(_0x5630c3,null))[_0x563f('0x23')](function(_0x5b9647){if(_0x5b9647){return _0x5b9647[_0x563f('0x50')](_0x4a3d1[_0x563f('0x47')][_0x563f('0x51')],_[_0x563f('0x52')](_0x4a3d1[_0x563f('0x47')],[_0x563f('0x51'),'id'])||{});}})['then'](respondWithResult(_0x5630c3,null))[_0x563f('0x40')](handleError(_0x5630c3,null));};exports[_0x563f('0x53')]=function(_0x20b41d,_0x1631c0,_0x1a56ee){var _0x2a2320={};var _0x3acb39={};var _0x4d46d5;var _0x481589;return db[_0x563f('0x2b')][_0x563f('0x54')]({'where':{'id':_0x20b41d['params']['id']}})[_0x563f('0x23')](handleEntityNotFound(_0x1631c0,null))[_0x563f('0x23')](function(_0x43d994){if(_0x43d994){_0x4d46d5=_0x43d994;_0x3acb39['model']=_[_0x563f('0x2e')](db[_0x563f('0x55')][_0x563f('0x43')]);_0x3acb39[_0x563f('0x2d')]=_['keys'](_0x20b41d[_0x563f('0x2d')]);_0x3acb39[_0x563f('0x2f')]=_[_0x563f('0x30')](_0x3acb39[_0x563f('0x2c')],_0x3acb39[_0x563f('0x2d')]);_0x2a2320[_0x563f('0x31')]=_[_0x563f('0x30')](_0x3acb39[_0x563f('0x2c')],qs['fields'](_0x20b41d[_0x563f('0x2d')][_0x563f('0x32')]));_0x2a2320[_0x563f('0x31')]=_0x2a2320[_0x563f('0x31')][_0x563f('0x33')]?_0x2a2320[_0x563f('0x31')]:_0x3acb39[_0x563f('0x2c')];_0x2a2320[_0x563f('0x36')]=qs['sort'](_0x20b41d[_0x563f('0x2d')][_0x563f('0x37')]);_0x2a2320['where']=qs['filters'](_[_0x563f('0x39')](_0x20b41d[_0x563f('0x2d')],_0x3acb39['filters']));if(_0x20b41d[_0x563f('0x2d')][_0x563f('0x3a')]){_0x2a2320[_0x563f('0x38')]=_[_0x563f('0x3b')](_0x2a2320[_0x563f('0x38')],{'$or':_[_0x563f('0x3c')](_0x2a2320['attributes'],function(_0xdcd80a){var _0x45293c={};_0x45293c[_0xdcd80a]={'$like':'%'+_0x20b41d[_0x563f('0x2d')][_0x563f('0x3a')]+'%'};return _0x45293c;})});}_0x2a2320=_[_0x563f('0x3b')]({},_0x2a2320,_0x20b41d[_0x563f('0x44')]);return _0x4d46d5[_0x563f('0x53')](_0x2a2320);}})[_0x563f('0x23')](function(_0x100721){if(_0x100721){_0x481589=_0x100721[_0x563f('0x33')];if(!_0x20b41d[_0x563f('0x2d')]['hasOwnProperty'](_0x563f('0x34'))){_0x2a2320[_0x563f('0x35')]=qs[_0x563f('0x35')](_0x20b41d[_0x563f('0x2d')][_0x563f('0x35')]);_0x2a2320['offset']=qs['offset'](_0x20b41d['query'][_0x563f('0x1d')]);}return _0x4d46d5['getContacts'](_0x2a2320);}})[_0x563f('0x23')](function(_0x1addc9){if(_0x1addc9){return _0x1addc9?{'count':_0x481589,'rows':_0x1addc9}:null;}})[_0x563f('0x23')](respondWithResult(_0x1631c0,null))[_0x563f('0x40')](handleError(_0x1631c0,null));};