4842556cfc4948626e4b6a51ca1b640098411481
[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 _0x7736=['error','stack','name','CmCompany','model','keys','query','attributes','intersection','fields','length','hasOwnProperty','limit','order','sort','filters','pick','filter','where','merge','map','options','includeAll','include','findAll','rows','catch','show','params','rawAttributes','find','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','body','destroy','describe','addContacts','ids','getContacts','findOne','CmContact','nolimit','eml-format','rimraf','zip-dir','request-promise','bluebird','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','end','status','json','offset','undefined','count','set','Content-Range','apply','reject','save','then','get','Companies','UserProfileResource','sendStatus'];(function(_0x22c6cd,_0x5eb90e){var _0x2da0e9=function(_0x32a2ed){while(--_0x32a2ed){_0x22c6cd['push'](_0x22c6cd['shift']());}};_0x2da0e9(++_0x5eb90e);}(_0x7736,0x13b));var _0x6773=function(_0x99a726,_0x1f9436){_0x99a726=_0x99a726-0x0;var _0x41f6dc=_0x7736[_0x99a726];return _0x41f6dc;};'use strict';var emlformat=require(_0x6773('0x0'));var rimraf=require(_0x6773('0x1'));var zipdir=require(_0x6773('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0x6773('0x3'));var moment=require('moment');var BPromise=require(_0x6773('0x4'));var Mustache=require('mustache');var util=require(_0x6773('0x5'));var path=require(_0x6773('0x6'));var sox=require(_0x6773('0x7'));var csv=require(_0x6773('0x8'));var ejs=require(_0x6773('0x9'));var fs=require('fs');var fs_extra=require(_0x6773('0xa'));var _=require(_0x6773('0xb'));var squel=require(_0x6773('0xc'));var crypto=require(_0x6773('0xd'));var jsforce=require(_0x6773('0xe'));var deskjs=require(_0x6773('0xf'));var toCsv=require('to-csv');var querystring=require(_0x6773('0x10'));var Papa=require(_0x6773('0x11'));var Redis=require(_0x6773('0x12'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x6773('0x13'));var logger=require(_0x6773('0x14'))(_0x6773('0x15'));var utils=require('../../config/utils');var config=require(_0x6773('0x16'));var licenseUtil=require(_0x6773('0x17'));var db=require(_0x6773('0x18'))['db'];function respondWithStatusCode(_0x1e68ae,_0x1b47e2){_0x1b47e2=_0x1b47e2||0xcc;return function(_0x4aabea){if(_0x4aabea){return _0x1e68ae['sendStatus'](_0x1b47e2);}return _0x1e68ae['status'](_0x1b47e2)[_0x6773('0x19')]();};}function respondWithResult(_0x98dfbe,_0x4c2b9c){_0x4c2b9c=_0x4c2b9c||0xc8;return function(_0x4afa77){if(_0x4afa77){return _0x98dfbe[_0x6773('0x1a')](_0x4c2b9c)[_0x6773('0x1b')](_0x4afa77);}};}function respondWithFilteredResult(_0x241de9,_0x9c1850){return function(_0x2b9f8a){if(_0x2b9f8a){var _0x5883f3=typeof _0x9c1850[_0x6773('0x1c')]===_0x6773('0x1d')&&typeof _0x9c1850['limit']==='undefined';var _0x2ca55f=_0x2b9f8a['count'];var _0x3bba78=_0x5883f3?0x0:_0x9c1850[_0x6773('0x1c')];var _0x444e46=_0x5883f3?_0x2b9f8a[_0x6773('0x1e')]:_0x9c1850[_0x6773('0x1c')]+_0x9c1850['limit'];var _0x324f90;if(_0x444e46>=_0x2ca55f){_0x444e46=_0x2ca55f;_0x324f90=0xc8;}else{_0x324f90=0xce;}_0x241de9[_0x6773('0x1a')](_0x324f90);return _0x241de9[_0x6773('0x1f')](_0x6773('0x20'),_0x3bba78+'-'+_0x444e46+'/'+_0x2ca55f)[_0x6773('0x1b')](_0x2b9f8a);}return null;};}function patchUpdates(_0x4a55ef){return function(_0x376f69){try{jsonpatch[_0x6773('0x21')](_0x376f69,_0x4a55ef,!![]);}catch(_0x5131ec){return BPromise[_0x6773('0x22')](_0x5131ec);}return _0x376f69[_0x6773('0x23')]();};}function saveUpdates(_0x16d264,_0x3bcd13){return function(_0x890e55){if(_0x890e55){return _0x890e55['update'](_0x16d264)[_0x6773('0x24')](function(_0x5bc81a){return _0x5bc81a;});}return null;};}function removeEntity(_0x435498,_0x25d99c){return function(_0x212e4c){if(_0x212e4c){return _0x212e4c['destroy']()[_0x6773('0x24')](function(){var _0x21a612=_0x212e4c[_0x6773('0x25')]({'plain':!![]});var _0x2e6d06=_0x6773('0x26');return db[_0x6773('0x27')]['destroy']({'where':{'type':_0x2e6d06,'resourceId':_0x21a612['id']}})[_0x6773('0x24')](function(){return _0x212e4c;});})[_0x6773('0x24')](function(){_0x435498[_0x6773('0x1a')](0xcc)[_0x6773('0x19')]();});}};}function handleEntityNotFound(_0x56f8b5,_0x547d66){return function(_0x41a3a8){if(!_0x41a3a8){_0x56f8b5[_0x6773('0x28')](0x194);}return _0x41a3a8;};}function handleError(_0xfccda1,_0x51735a){_0x51735a=_0x51735a||0x1f4;return function(_0x2113c3){logger[_0x6773('0x29')](_0x2113c3[_0x6773('0x2a')]);if(_0x2113c3[_0x6773('0x2b')]){delete _0x2113c3['name'];}_0xfccda1[_0x6773('0x1a')](_0x51735a)['send'](_0x2113c3);};}exports['index']=function(_0x2406b9,_0x174a38){var _0x44f446={},_0x355349={},_0x5ebab3={'count':0x0,'rows':[]};return db[_0x6773('0x2c')]['describe']()['then'](function(_0x178e6d){_0x355349[_0x6773('0x2d')]=_[_0x6773('0x2e')](_0x178e6d);_0x355349[_0x6773('0x2f')]=_[_0x6773('0x2e')](_0x2406b9[_0x6773('0x2f')]);_0x355349['filters']=_['intersection'](_0x355349['model'],_0x355349[_0x6773('0x2f')]);_0x44f446[_0x6773('0x30')]=_[_0x6773('0x31')](_0x355349[_0x6773('0x2d')],qs[_0x6773('0x32')](_0x2406b9[_0x6773('0x2f')]['fields']));_0x44f446[_0x6773('0x30')]=_0x44f446['attributes'][_0x6773('0x33')]?_0x44f446['attributes']:_0x355349[_0x6773('0x2d')];if(!_0x2406b9[_0x6773('0x2f')][_0x6773('0x34')]('nolimit')){_0x44f446['limit']=qs[_0x6773('0x35')](_0x2406b9[_0x6773('0x2f')][_0x6773('0x35')]);_0x44f446[_0x6773('0x1c')]=qs[_0x6773('0x1c')](_0x2406b9[_0x6773('0x2f')][_0x6773('0x1c')]);}_0x44f446[_0x6773('0x36')]=qs[_0x6773('0x37')](_0x2406b9[_0x6773('0x2f')][_0x6773('0x37')]);_0x44f446['where']=qs[_0x6773('0x38')](_[_0x6773('0x39')](_0x2406b9[_0x6773('0x2f')],_0x355349[_0x6773('0x38')]),_0x178e6d);if(_0x2406b9['query'][_0x6773('0x3a')]){_0x44f446[_0x6773('0x3b')]=_[_0x6773('0x3c')](_0x44f446[_0x6773('0x3b')],{'$or':_[_0x6773('0x3d')](_0x44f446[_0x6773('0x30')],function(_0xac8364){var _0x4e476b={};_0x4e476b[_0xac8364]={'$like':'%'+_0x2406b9['query'][_0x6773('0x3a')]+'%'};return _0x4e476b;})});}_0x44f446=_[_0x6773('0x3c')]({},_0x44f446,_0x2406b9[_0x6773('0x3e')]);var _0xbce8d3={'where':_0x44f446['where']};return db['CmCompany'][_0x6773('0x1e')](_0xbce8d3)[_0x6773('0x24')](function(_0x18c1f5){_0x5ebab3[_0x6773('0x1e')]=_0x18c1f5;if(_0x2406b9['query'][_0x6773('0x3f')]){_0x44f446[_0x6773('0x40')]=[{'all':!![]}];}return db[_0x6773('0x2c')][_0x6773('0x41')](_0x44f446);})[_0x6773('0x24')](function(_0x200f4a){_0x5ebab3[_0x6773('0x42')]=_0x200f4a;return _0x5ebab3;})[_0x6773('0x24')](respondWithFilteredResult(_0x174a38,_0x44f446))['catch'](handleError(_0x174a38,null));})[_0x6773('0x43')](handleError(_0x174a38,null));};exports[_0x6773('0x44')]=function(_0x108f43,_0x3625a7){var _0x4e3a42={'raw':![],'where':{'id':_0x108f43[_0x6773('0x45')]['id']}},_0x5a17a6={};_0x5a17a6[_0x6773('0x2d')]=_[_0x6773('0x2e')](db['CmCompany'][_0x6773('0x46')]);_0x5a17a6[_0x6773('0x2f')]=_['keys'](_0x108f43[_0x6773('0x2f')]);_0x5a17a6[_0x6773('0x38')]=_[_0x6773('0x31')](_0x5a17a6[_0x6773('0x2d')],_0x5a17a6[_0x6773('0x2f')]);_0x4e3a42[_0x6773('0x30')]=_[_0x6773('0x31')](_0x5a17a6[_0x6773('0x2d')],qs[_0x6773('0x32')](_0x108f43[_0x6773('0x2f')][_0x6773('0x32')]));_0x4e3a42['attributes']=_0x4e3a42['attributes'][_0x6773('0x33')]?_0x4e3a42['attributes']:_0x5a17a6['model'];if(_0x108f43[_0x6773('0x2f')][_0x6773('0x3f')]){_0x4e3a42['include']=[{'all':!![]}];}_0x4e3a42=_[_0x6773('0x3c')]({},_0x4e3a42,_0x108f43[_0x6773('0x3e')]);return db['CmCompany'][_0x6773('0x47')](_0x4e3a42)[_0x6773('0x24')](handleEntityNotFound(_0x3625a7,null))['then'](respondWithResult(_0x3625a7,null))['catch'](handleError(_0x3625a7,null));};exports[_0x6773('0x48')]=function(_0x477b22,_0x118950){return db[_0x6773('0x2c')]['create'](_0x477b22['body'],{})['then'](function(_0x47d768){var _0x2549ef=_0x477b22[_0x6773('0x49')]['get']({'plain':!![]});if(!_0x2549ef)throw new Error(_0x6773('0x4a'));if(_0x2549ef[_0x6773('0x4b')]===_0x6773('0x49')){var _0x5aecf0=_0x47d768[_0x6773('0x25')]({'plain':!![]});var _0x5764fc=_0x6773('0x26');return db['UserProfileSection'][_0x6773('0x47')]({'where':{'name':_0x5764fc,'userProfileId':_0x2549ef[_0x6773('0x4c')]},'raw':!![]})[_0x6773('0x24')](function(_0x2dd27f){if(_0x2dd27f&&_0x2dd27f[_0x6773('0x4d')]===0x0){return db['UserProfileResource'][_0x6773('0x48')]({'name':_0x5aecf0['name'],'resourceId':_0x5aecf0['id'],'type':_0x2dd27f['name'],'sectionId':_0x2dd27f['id']},{})['then'](function(){return _0x47d768;});}else{return _0x47d768;}})['catch'](function(_0x229e27){logger[_0x6773('0x29')](_0x6773('0x4e'),_0x229e27);throw _0x229e27;});}return _0x47d768;})['then'](respondWithResult(_0x118950,0xc9))[_0x6773('0x43')](handleError(_0x118950,null));};exports[_0x6773('0x4f')]=function(_0x549227,_0x1abfeb){if(_0x549227[_0x6773('0x50')]['id']){delete _0x549227['body']['id'];}return db['CmCompany']['find']({'where':{'id':_0x549227[_0x6773('0x45')]['id']}})[_0x6773('0x24')](handleEntityNotFound(_0x1abfeb,null))[_0x6773('0x24')](saveUpdates(_0x549227[_0x6773('0x50')],null))[_0x6773('0x24')](respondWithResult(_0x1abfeb,null))['catch'](handleError(_0x1abfeb,null));};exports[_0x6773('0x51')]=function(_0x499e8c,_0x1fe567){return db[_0x6773('0x2c')][_0x6773('0x47')]({'where':{'id':_0x499e8c[_0x6773('0x45')]['id']}})[_0x6773('0x24')](handleEntityNotFound(_0x1fe567,null))[_0x6773('0x24')](removeEntity(_0x1fe567,null))[_0x6773('0x43')](handleError(_0x1fe567,null));};exports[_0x6773('0x52')]=function(_0x4d82f2,_0x505c2f){return db[_0x6773('0x2c')][_0x6773('0x52')]()['then'](respondWithResult(_0x505c2f,null))[_0x6773('0x43')](handleError(_0x505c2f,null));};exports[_0x6773('0x53')]=function(_0x38ff6d,_0x5d67df,_0xba08f4){return db['CmContact']['find']({'where':{'id':_0x38ff6d[_0x6773('0x45')]['id']}})[_0x6773('0x24')](handleEntityNotFound(_0x5d67df,null))[_0x6773('0x24')](function(_0xf71307){if(_0xf71307){return _0xf71307[_0x6773('0x53')](_0x38ff6d[_0x6773('0x50')][_0x6773('0x54')],_['omit'](_0x38ff6d[_0x6773('0x50')],[_0x6773('0x54'),'id'])||{});}})['then'](respondWithResult(_0x5d67df,null))['catch'](handleError(_0x5d67df,null));};exports[_0x6773('0x55')]=function(_0x248aac,_0xa06cb5,_0xfa9ea0){var _0x4d6a8d={};var _0x3c7e7e={};var _0x29aec1;var _0x4297bb;return db['CmCompany'][_0x6773('0x56')]({'where':{'id':_0x248aac[_0x6773('0x45')]['id']}})['then'](handleEntityNotFound(_0xa06cb5,null))[_0x6773('0x24')](function(_0x179617){if(_0x179617){_0x29aec1=_0x179617;_0x3c7e7e[_0x6773('0x2d')]=_['keys'](db[_0x6773('0x57')][_0x6773('0x46')]);_0x3c7e7e[_0x6773('0x2f')]=_['keys'](_0x248aac['query']);_0x3c7e7e[_0x6773('0x38')]=_[_0x6773('0x31')](_0x3c7e7e[_0x6773('0x2d')],_0x3c7e7e[_0x6773('0x2f')]);_0x4d6a8d[_0x6773('0x30')]=_[_0x6773('0x31')](_0x3c7e7e['model'],qs[_0x6773('0x32')](_0x248aac[_0x6773('0x2f')][_0x6773('0x32')]));_0x4d6a8d['attributes']=_0x4d6a8d[_0x6773('0x30')]['length']?_0x4d6a8d[_0x6773('0x30')]:_0x3c7e7e[_0x6773('0x2d')];_0x4d6a8d[_0x6773('0x36')]=qs[_0x6773('0x37')](_0x248aac[_0x6773('0x2f')]['sort']);_0x4d6a8d['where']=qs[_0x6773('0x38')](_[_0x6773('0x39')](_0x248aac[_0x6773('0x2f')],_0x3c7e7e[_0x6773('0x38')]));if(_0x248aac[_0x6773('0x2f')][_0x6773('0x3a')]){_0x4d6a8d[_0x6773('0x3b')]=_[_0x6773('0x3c')](_0x4d6a8d[_0x6773('0x3b')],{'$or':_[_0x6773('0x3d')](_0x4d6a8d['attributes'],function(_0x385acc){var _0x195119={};_0x195119[_0x385acc]={'$like':'%'+_0x248aac[_0x6773('0x2f')][_0x6773('0x3a')]+'%'};return _0x195119;})});}_0x4d6a8d=_['merge']({},_0x4d6a8d,_0x248aac[_0x6773('0x3e')]);return _0x29aec1['getContacts'](_0x4d6a8d);}})[_0x6773('0x24')](function(_0x3a3e24){if(_0x3a3e24){_0x4297bb=_0x3a3e24['length'];if(!_0x248aac[_0x6773('0x2f')][_0x6773('0x34')](_0x6773('0x58'))){_0x4d6a8d[_0x6773('0x35')]=qs[_0x6773('0x35')](_0x248aac[_0x6773('0x2f')][_0x6773('0x35')]);_0x4d6a8d[_0x6773('0x1c')]=qs[_0x6773('0x1c')](_0x248aac[_0x6773('0x2f')][_0x6773('0x1c')]);}return _0x29aec1[_0x6773('0x55')](_0x4d6a8d);}})[_0x6773('0x24')](function(_0x526d46){if(_0x526d46){return _0x526d46?{'count':_0x4297bb,'rows':_0x526d46}:null;}})['then'](respondWithResult(_0xa06cb5,null))['catch'](handleError(_0xa06cb5,null));};