72b2b716402e893aad8856f5f4a128e411e874ad
[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 _0xc0ff=['user','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','addContacts','ids','omit','findOne','CmContact','sort','getContacts','eml-format','rimraf','moment','mustache','util','sox','to-csv','ejs','squel','crypto','jsforce','desk.js','querystring','papaparse','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','offset','limit','set','Content-Range','json','reject','save','update','then','destroy','get','Companies','UserProfileResource','stack','name','send','index','CmCompany','describe','model','keys','query','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','where','filter','merge','map','options','count','includeAll','include','findAll','rows','catch','params','rawAttributes','find','create','body'];(function(_0x321bb2,_0x202794){var _0x9c12ed=function(_0x263dd3){while(--_0x263dd3){_0x321bb2['push'](_0x321bb2['shift']());}};_0x9c12ed(++_0x202794);}(_0xc0ff,0x150));var _0xfc0f=function(_0x2c4d22,_0x4ca120){_0x2c4d22=_0x2c4d22-0x0;var _0x1f0537=_0xc0ff[_0x2c4d22];return _0x1f0537;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0xfc0f('0x0'));var rimraf=require(_0xfc0f('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0xfc0f('0x2'));var BPromise=require('bluebird');var Mustache=require(_0xfc0f('0x3'));var util=require(_0xfc0f('0x4'));var path=require('path');var sox=require(_0xfc0f('0x5'));var csv=require(_0xfc0f('0x6'));var ejs=require(_0xfc0f('0x7'));var fs=require('fs');var _=require('lodash');var squel=require(_0xfc0f('0x8'));var crypto=require(_0xfc0f('0x9'));var jsforce=require(_0xfc0f('0xa'));var deskjs=require(_0xfc0f('0xb'));var toCsv=require('to-csv');var querystring=require(_0xfc0f('0xc'));var Papa=require(_0xfc0f('0xd'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0xfc0f('0xe'));var hardwareService=require(_0xfc0f('0xf'));var logger=require('../../config/logger')(_0xfc0f('0x10'));var utils=require('../../config/utils');var config=require(_0xfc0f('0x11'));var licenseUtil=require(_0xfc0f('0x12'));var db=require(_0xfc0f('0x13'))['db'];function respondWithStatusCode(_0x2753ef,_0xdbb980){_0xdbb980=_0xdbb980||0xcc;return function(_0x26e57a){if(_0x26e57a){return _0x2753ef[_0xfc0f('0x14')](_0xdbb980);}return _0x2753ef[_0xfc0f('0x15')](_0xdbb980)[_0xfc0f('0x16')]();};}function respondWithResult(_0x5eeec0,_0x311cfd){_0x311cfd=_0x311cfd||0xc8;return function(_0x56c8d9){if(_0x56c8d9){return _0x5eeec0[_0xfc0f('0x15')](_0x311cfd)['json'](_0x56c8d9);}};}function respondWithFilteredResult(_0x1c8d5a,_0x45a8bf){return function(_0x5b8f1b){if(_0x5b8f1b){var _0x516dda=_0x5b8f1b['count'],_0x52b859=_0x45a8bf[_0xfc0f('0x17')],_0x202e9b=_0x45a8bf['offset']+_0x45a8bf[_0xfc0f('0x18')],_0x3d7161;if(_0x202e9b>=_0x516dda){_0x202e9b=_0x516dda;_0x3d7161=0xc8;}else{_0x3d7161=0xce;}_0x1c8d5a[_0xfc0f('0x15')](_0x3d7161);return _0x1c8d5a[_0xfc0f('0x19')](_0xfc0f('0x1a'),_0x52b859+'-'+_0x202e9b+'/'+_0x516dda)[_0xfc0f('0x1b')](_0x5b8f1b);}return null;};}function patchUpdates(_0x4093a0){return function(_0x184025){try{jsonpatch['apply'](_0x184025,_0x4093a0,!![]);}catch(_0x27c24e){return BPromise[_0xfc0f('0x1c')](_0x27c24e);}return _0x184025[_0xfc0f('0x1d')]();};}function saveUpdates(_0x9a832c,_0x2d852c){return function(_0x318455){if(_0x318455){return _0x318455[_0xfc0f('0x1e')](_0x9a832c)[_0xfc0f('0x1f')](function(_0x21dc3b){return _0x21dc3b;});}return null;};}function removeEntity(_0x5d441a,_0x41f481){return function(_0x32c7de){if(_0x32c7de){return _0x32c7de[_0xfc0f('0x20')]()[_0xfc0f('0x1f')](function(){var _0xd3f3b9=_0x32c7de[_0xfc0f('0x21')]({'plain':!![]});var _0x5f02b9=_0xfc0f('0x22');return db[_0xfc0f('0x23')][_0xfc0f('0x20')]({'where':{'type':_0x5f02b9,'resourceId':_0xd3f3b9['id']}})['then'](function(){return _0x32c7de;});})[_0xfc0f('0x1f')](function(){_0x5d441a[_0xfc0f('0x15')](0xcc)[_0xfc0f('0x16')]();});}};}function handleEntityNotFound(_0x14301a,_0x5f3e05){return function(_0x20f6c5){if(!_0x20f6c5){_0x14301a['sendStatus'](0x194);}return _0x20f6c5;};}function handleError(_0x50da12,_0x13d007){_0x13d007=_0x13d007||0x1f4;return function(_0x37870f){logger['error'](_0x37870f[_0xfc0f('0x24')]);if(_0x37870f[_0xfc0f('0x25')]){delete _0x37870f[_0xfc0f('0x25')];}_0x50da12[_0xfc0f('0x15')](_0x13d007)[_0xfc0f('0x26')](_0x37870f);};}exports[_0xfc0f('0x27')]=function(_0x39f8c5,_0x187039){var _0x2a3d15={},_0x5c5aad={},_0x453d15={'count':0x0,'rows':[]};return db[_0xfc0f('0x28')][_0xfc0f('0x29')]()[_0xfc0f('0x1f')](function(_0xc211a7){_0x5c5aad[_0xfc0f('0x2a')]=_[_0xfc0f('0x2b')](_0xc211a7);_0x5c5aad['query']=_[_0xfc0f('0x2b')](_0x39f8c5[_0xfc0f('0x2c')]);_0x5c5aad[_0xfc0f('0x2d')]=_[_0xfc0f('0x2e')](_0x5c5aad[_0xfc0f('0x2a')],_0x5c5aad[_0xfc0f('0x2c')]);_0x2a3d15['attributes']=_[_0xfc0f('0x2e')](_0x5c5aad[_0xfc0f('0x2a')],qs[_0xfc0f('0x2f')](_0x39f8c5[_0xfc0f('0x2c')][_0xfc0f('0x2f')]));_0x2a3d15[_0xfc0f('0x30')]=_0x2a3d15[_0xfc0f('0x30')][_0xfc0f('0x31')]?_0x2a3d15[_0xfc0f('0x30')]:_0x5c5aad[_0xfc0f('0x2a')];if(!_0x39f8c5[_0xfc0f('0x2c')][_0xfc0f('0x32')](_0xfc0f('0x33'))){_0x2a3d15[_0xfc0f('0x18')]=qs[_0xfc0f('0x18')](_0x39f8c5[_0xfc0f('0x2c')][_0xfc0f('0x18')]);_0x2a3d15[_0xfc0f('0x17')]=qs['offset'](_0x39f8c5[_0xfc0f('0x2c')][_0xfc0f('0x17')]);}_0x2a3d15[_0xfc0f('0x34')]=qs['sort'](_0x39f8c5['query']['sort']);_0x2a3d15[_0xfc0f('0x35')]=qs[_0xfc0f('0x2d')](_['pick'](_0x39f8c5[_0xfc0f('0x2c')],_0x5c5aad[_0xfc0f('0x2d')]),_0xc211a7);if(_0x39f8c5[_0xfc0f('0x2c')][_0xfc0f('0x36')]){_0x2a3d15[_0xfc0f('0x35')]=_[_0xfc0f('0x37')](_0x2a3d15[_0xfc0f('0x35')],{'$or':_[_0xfc0f('0x38')](_0x2a3d15[_0xfc0f('0x30')],function(_0xecb386){var _0x4dddca={};_0x4dddca[_0xecb386]={'$like':'%'+_0x39f8c5[_0xfc0f('0x2c')]['filter']+'%'};return _0x4dddca;})});}_0x2a3d15=_[_0xfc0f('0x37')]({},_0x2a3d15,_0x39f8c5[_0xfc0f('0x39')]);var _0x5958f8={'where':_0x2a3d15[_0xfc0f('0x35')]};return db[_0xfc0f('0x28')][_0xfc0f('0x3a')](_0x5958f8)['then'](function(_0x46d3a2){_0x453d15['count']=_0x46d3a2;if(_0x39f8c5[_0xfc0f('0x2c')][_0xfc0f('0x3b')]){_0x2a3d15[_0xfc0f('0x3c')]=[{'all':!![]}];}return db[_0xfc0f('0x28')][_0xfc0f('0x3d')](_0x2a3d15);})[_0xfc0f('0x1f')](function(_0x43da98){_0x453d15[_0xfc0f('0x3e')]=_0x43da98;return _0x453d15;})['then'](respondWithFilteredResult(_0x187039,_0x2a3d15))[_0xfc0f('0x3f')](handleError(_0x187039,null));})[_0xfc0f('0x3f')](handleError(_0x187039,null));};exports['show']=function(_0x3ffe4d,_0x48ea47){var _0x2ca4ab={'raw':![],'where':{'id':_0x3ffe4d[_0xfc0f('0x40')]['id']}},_0x2d660c={};_0x2d660c['model']=_[_0xfc0f('0x2b')](db[_0xfc0f('0x28')][_0xfc0f('0x41')]);_0x2d660c['query']=_[_0xfc0f('0x2b')](_0x3ffe4d[_0xfc0f('0x2c')]);_0x2d660c[_0xfc0f('0x2d')]=_[_0xfc0f('0x2e')](_0x2d660c[_0xfc0f('0x2a')],_0x2d660c[_0xfc0f('0x2c')]);_0x2ca4ab[_0xfc0f('0x30')]=_[_0xfc0f('0x2e')](_0x2d660c[_0xfc0f('0x2a')],qs[_0xfc0f('0x2f')](_0x3ffe4d[_0xfc0f('0x2c')][_0xfc0f('0x2f')]));_0x2ca4ab[_0xfc0f('0x30')]=_0x2ca4ab[_0xfc0f('0x30')][_0xfc0f('0x31')]?_0x2ca4ab[_0xfc0f('0x30')]:_0x2d660c[_0xfc0f('0x2a')];if(_0x3ffe4d[_0xfc0f('0x2c')][_0xfc0f('0x3b')]){_0x2ca4ab[_0xfc0f('0x3c')]=[{'all':!![]}];}_0x2ca4ab=_[_0xfc0f('0x37')]({},_0x2ca4ab,_0x3ffe4d['options']);return db[_0xfc0f('0x28')][_0xfc0f('0x42')](_0x2ca4ab)[_0xfc0f('0x1f')](handleEntityNotFound(_0x48ea47,null))[_0xfc0f('0x1f')](respondWithResult(_0x48ea47,null))[_0xfc0f('0x3f')](handleError(_0x48ea47,null));};exports['create']=function(_0x4e7b26,_0x5a478e){return db[_0xfc0f('0x28')][_0xfc0f('0x43')](_0x4e7b26[_0xfc0f('0x44')],{})[_0xfc0f('0x1f')](function(_0x37ceaf){var _0x51d228=_0x4e7b26[_0xfc0f('0x45')]['get']({'plain':!![]});if(!_0x51d228)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x51d228['role']==='user'){var _0x335054=_0x37ceaf[_0xfc0f('0x21')]({'plain':!![]});var _0x42d959='Companies';return db[_0xfc0f('0x46')]['find']({'where':{'name':_0x42d959,'userProfileId':_0x51d228[_0xfc0f('0x47')]},'raw':!![]})[_0xfc0f('0x1f')](function(_0x494ace){if(_0x494ace&&_0x494ace[_0xfc0f('0x48')]===0x0){return db[_0xfc0f('0x23')][_0xfc0f('0x43')]({'name':_0x335054[_0xfc0f('0x25')],'resourceId':_0x335054['id'],'type':_0x494ace['name'],'sectionId':_0x494ace['id']},{})[_0xfc0f('0x1f')](function(){return _0x37ceaf;});}else{return _0x37ceaf;}})[_0xfc0f('0x3f')](function(_0x22a787){logger['error'](_0xfc0f('0x49'),_0x22a787);throw _0x22a787;});}return _0x37ceaf;})[_0xfc0f('0x1f')](respondWithResult(_0x5a478e,0xc9))['catch'](handleError(_0x5a478e,null));};exports[_0xfc0f('0x1e')]=function(_0x379dac,_0x17dfb0){if(_0x379dac['body']['id']){delete _0x379dac['body']['id'];}return db[_0xfc0f('0x28')][_0xfc0f('0x42')]({'where':{'id':_0x379dac[_0xfc0f('0x40')]['id']}})[_0xfc0f('0x1f')](handleEntityNotFound(_0x17dfb0,null))[_0xfc0f('0x1f')](saveUpdates(_0x379dac[_0xfc0f('0x44')],null))['then'](respondWithResult(_0x17dfb0,null))[_0xfc0f('0x3f')](handleError(_0x17dfb0,null));};exports[_0xfc0f('0x20')]=function(_0x50f4d9,_0x13ae73){return db[_0xfc0f('0x28')]['find']({'where':{'id':_0x50f4d9[_0xfc0f('0x40')]['id']}})[_0xfc0f('0x1f')](handleEntityNotFound(_0x13ae73,null))[_0xfc0f('0x1f')](removeEntity(_0x13ae73,null))[_0xfc0f('0x3f')](handleError(_0x13ae73,null));};exports[_0xfc0f('0x29')]=function(_0x3a6ec7,_0x420f78){return db[_0xfc0f('0x28')][_0xfc0f('0x29')]()[_0xfc0f('0x1f')](respondWithResult(_0x420f78,null))[_0xfc0f('0x3f')](handleError(_0x420f78,null));};exports[_0xfc0f('0x4a')]=function(_0x4a443b,_0x2ce7b4,_0x57ad2f){return db['CmContact']['find']({'where':{'id':_0x4a443b[_0xfc0f('0x40')]['id']}})[_0xfc0f('0x1f')](handleEntityNotFound(_0x2ce7b4,null))[_0xfc0f('0x1f')](function(_0x3dd8c7){if(_0x3dd8c7){return _0x3dd8c7[_0xfc0f('0x4a')](_0x4a443b[_0xfc0f('0x44')][_0xfc0f('0x4b')],_[_0xfc0f('0x4c')](_0x4a443b[_0xfc0f('0x44')],[_0xfc0f('0x4b'),'id'])||{});}})[_0xfc0f('0x1f')](respondWithResult(_0x2ce7b4,null))[_0xfc0f('0x3f')](handleError(_0x2ce7b4,null));};exports['getContacts']=function(_0x18d07a,_0x36bdb7,_0x10217c){var _0x1f5e36={};var _0x2ce748={};var _0x33cd29;var _0x624d08;return db[_0xfc0f('0x28')][_0xfc0f('0x4d')]({'where':{'id':_0x18d07a['params']['id']}})['then'](handleEntityNotFound(_0x36bdb7,null))['then'](function(_0x42f5dc){if(_0x42f5dc){_0x33cd29=_0x42f5dc;_0x2ce748['model']=_['keys'](db[_0xfc0f('0x4e')]['rawAttributes']);_0x2ce748['query']=_[_0xfc0f('0x2b')](_0x18d07a['query']);_0x2ce748[_0xfc0f('0x2d')]=_[_0xfc0f('0x2e')](_0x2ce748[_0xfc0f('0x2a')],_0x2ce748[_0xfc0f('0x2c')]);_0x1f5e36['attributes']=_[_0xfc0f('0x2e')](_0x2ce748['model'],qs[_0xfc0f('0x2f')](_0x18d07a[_0xfc0f('0x2c')][_0xfc0f('0x2f')]));_0x1f5e36[_0xfc0f('0x30')]=_0x1f5e36[_0xfc0f('0x30')][_0xfc0f('0x31')]?_0x1f5e36[_0xfc0f('0x30')]:_0x2ce748[_0xfc0f('0x2a')];_0x1f5e36['order']=qs[_0xfc0f('0x4f')](_0x18d07a[_0xfc0f('0x2c')][_0xfc0f('0x4f')]);_0x1f5e36[_0xfc0f('0x35')]=qs[_0xfc0f('0x2d')](_['pick'](_0x18d07a[_0xfc0f('0x2c')],_0x2ce748[_0xfc0f('0x2d')]));if(_0x18d07a[_0xfc0f('0x2c')][_0xfc0f('0x36')]){_0x1f5e36[_0xfc0f('0x35')]=_[_0xfc0f('0x37')](_0x1f5e36['where'],{'$or':_[_0xfc0f('0x38')](_0x1f5e36[_0xfc0f('0x30')],function(_0x3e5d22){var _0x3ce16a={};_0x3ce16a[_0x3e5d22]={'$like':'%'+_0x18d07a[_0xfc0f('0x2c')]['filter']+'%'};return _0x3ce16a;})});}_0x1f5e36=_['merge']({},_0x1f5e36,_0x18d07a[_0xfc0f('0x39')]);return _0x33cd29['getContacts'](_0x1f5e36);}})[_0xfc0f('0x1f')](function(_0x3fa29f){if(_0x3fa29f){_0x624d08=_0x3fa29f[_0xfc0f('0x31')];if(!_0x18d07a[_0xfc0f('0x2c')][_0xfc0f('0x32')](_0xfc0f('0x33'))){_0x1f5e36[_0xfc0f('0x18')]=qs[_0xfc0f('0x18')](_0x18d07a[_0xfc0f('0x2c')][_0xfc0f('0x18')]);_0x1f5e36[_0xfc0f('0x17')]=qs['offset'](_0x18d07a[_0xfc0f('0x2c')][_0xfc0f('0x17')]);}return _0x33cd29[_0xfc0f('0x50')](_0x1f5e36);}})[_0xfc0f('0x1f')](function(_0x3d4a75){if(_0x3d4a75){return _0x3d4a75?{'count':_0x624d08,'rows':_0x3d4a75}:null;}})[_0xfc0f('0x1f')](respondWithResult(_0x36bdb7,null))[_0xfc0f('0x3f')](handleError(_0x36bdb7,null));};