Built motion from commit 9b156c94.|2.6.22
[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 _0x31f5=['length','query','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','map','options','includeAll','findAll','rows','catch','show','params','rawAttributes','include','find','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','body','destroy','describe','addContacts','ids','omit','findOne','CmContact','getContacts','eml-format','rimraf','zip-dir','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','save','then','get','Companies','UserProfileResource','error','name','index','CmCompany','model','keys','filters','intersection','attributes','fields'];(function(_0x192e56,_0x523539){var _0x3d594c=function(_0x11ad1b){while(--_0x11ad1b){_0x192e56['push'](_0x192e56['shift']());}};_0x3d594c(++_0x523539);}(_0x31f5,0x80));var _0x531f=function(_0x279095,_0x15f37e){_0x279095=_0x279095-0x0;var _0x5a5cdb=_0x31f5[_0x279095];return _0x5a5cdb;};'use strict';var emlformat=require(_0x531f('0x0'));var rimraf=require(_0x531f('0x1'));var zipdir=require(_0x531f('0x2'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0x531f('0x3'));var Mustache=require(_0x531f('0x4'));var util=require(_0x531f('0x5'));var path=require(_0x531f('0x6'));var sox=require(_0x531f('0x7'));var csv=require(_0x531f('0x8'));var ejs=require(_0x531f('0x9'));var fs=require('fs');var fs_extra=require(_0x531f('0xa'));var _=require('lodash');var squel=require(_0x531f('0xb'));var crypto=require(_0x531f('0xc'));var jsforce=require(_0x531f('0xd'));var deskjs=require(_0x531f('0xe'));var toCsv=require(_0x531f('0x8'));var querystring=require(_0x531f('0xf'));var Papa=require('papaparse');var Redis=require(_0x531f('0x10'));var authService=require(_0x531f('0x11'));var qs=require('../../components/parsers/qs');var as=require(_0x531f('0x12'));var hardwareService=require(_0x531f('0x13'));var logger=require(_0x531f('0x14'))('api');var utils=require(_0x531f('0x15'));var config=require(_0x531f('0x16'));var licenseUtil=require(_0x531f('0x17'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x548800,_0x5f145c){_0x5f145c=_0x5f145c||0xcc;return function(_0x384574){if(_0x384574){return _0x548800[_0x531f('0x18')](_0x5f145c);}return _0x548800[_0x531f('0x19')](_0x5f145c)[_0x531f('0x1a')]();};}function respondWithResult(_0xd1b334,_0x1ded0c){_0x1ded0c=_0x1ded0c||0xc8;return function(_0x514570){if(_0x514570){return _0xd1b334['status'](_0x1ded0c)[_0x531f('0x1b')](_0x514570);}};}function respondWithFilteredResult(_0x3c0bf3,_0x45cf14){return function(_0xab0e4d){if(_0xab0e4d){var _0x507657=typeof _0x45cf14[_0x531f('0x1c')]===_0x531f('0x1d')&&typeof _0x45cf14[_0x531f('0x1e')]==='undefined';var _0x3b273f=_0xab0e4d[_0x531f('0x1f')];var _0x320e65=_0x507657?0x0:_0x45cf14[_0x531f('0x1c')];var _0x25e711=_0x507657?_0xab0e4d[_0x531f('0x1f')]:_0x45cf14[_0x531f('0x1c')]+_0x45cf14[_0x531f('0x1e')];var _0xf38d7f;if(_0x25e711>=_0x3b273f){_0x25e711=_0x3b273f;_0xf38d7f=0xc8;}else{_0xf38d7f=0xce;}_0x3c0bf3[_0x531f('0x19')](_0xf38d7f);return _0x3c0bf3[_0x531f('0x20')](_0x531f('0x21'),_0x320e65+'-'+_0x25e711+'/'+_0x3b273f)[_0x531f('0x1b')](_0xab0e4d);}return null;};}function patchUpdates(_0x1e2bac){return function(_0x3b3df){try{jsonpatch[_0x531f('0x22')](_0x3b3df,_0x1e2bac,!![]);}catch(_0x4ebd41){return BPromise['reject'](_0x4ebd41);}return _0x3b3df[_0x531f('0x23')]();};}function saveUpdates(_0x1243bc,_0x3c0342){return function(_0x2c09a0){if(_0x2c09a0){return _0x2c09a0['update'](_0x1243bc)[_0x531f('0x24')](function(_0x5cc702){return _0x5cc702;});}return null;};}function removeEntity(_0x1de01e,_0xdad4ff){return function(_0x449699){if(_0x449699){return _0x449699['destroy']()[_0x531f('0x24')](function(){var _0x1ad90a=_0x449699[_0x531f('0x25')]({'plain':!![]});var _0x2c32ea=_0x531f('0x26');return db[_0x531f('0x27')]['destroy']({'where':{'type':_0x2c32ea,'resourceId':_0x1ad90a['id']}})[_0x531f('0x24')](function(){return _0x449699;});})[_0x531f('0x24')](function(){_0x1de01e[_0x531f('0x19')](0xcc)['end']();});}};}function handleEntityNotFound(_0x31a411,_0x4780bb){return function(_0x4d68b1){if(!_0x4d68b1){_0x31a411['sendStatus'](0x194);}return _0x4d68b1;};}function handleError(_0x427ea7,_0x582878){_0x582878=_0x582878||0x1f4;return function(_0x14bc18){logger[_0x531f('0x28')](_0x14bc18['stack']);if(_0x14bc18[_0x531f('0x29')]){delete _0x14bc18[_0x531f('0x29')];}_0x427ea7['status'](_0x582878)['send'](_0x14bc18);};}exports[_0x531f('0x2a')]=function(_0x52bca2,_0xa32540){var _0x3b4c7a={},_0x37ddfd={},_0x12bc63={'count':0x0,'rows':[]};return db[_0x531f('0x2b')]['describe']()[_0x531f('0x24')](function(_0x4c9880){_0x37ddfd[_0x531f('0x2c')]=_[_0x531f('0x2d')](_0x4c9880);_0x37ddfd['query']=_[_0x531f('0x2d')](_0x52bca2['query']);_0x37ddfd[_0x531f('0x2e')]=_[_0x531f('0x2f')](_0x37ddfd[_0x531f('0x2c')],_0x37ddfd['query']);_0x3b4c7a[_0x531f('0x30')]=_['intersection'](_0x37ddfd[_0x531f('0x2c')],qs['fields'](_0x52bca2['query'][_0x531f('0x31')]));_0x3b4c7a[_0x531f('0x30')]=_0x3b4c7a[_0x531f('0x30')][_0x531f('0x32')]?_0x3b4c7a['attributes']:_0x37ddfd[_0x531f('0x2c')];if(!_0x52bca2[_0x531f('0x33')][_0x531f('0x34')](_0x531f('0x35'))){_0x3b4c7a[_0x531f('0x1e')]=qs[_0x531f('0x1e')](_0x52bca2[_0x531f('0x33')]['limit']);_0x3b4c7a['offset']=qs[_0x531f('0x1c')](_0x52bca2[_0x531f('0x33')][_0x531f('0x1c')]);}_0x3b4c7a[_0x531f('0x36')]=qs[_0x531f('0x37')](_0x52bca2[_0x531f('0x33')][_0x531f('0x37')]);_0x3b4c7a[_0x531f('0x38')]=qs[_0x531f('0x2e')](_[_0x531f('0x39')](_0x52bca2[_0x531f('0x33')],_0x37ddfd[_0x531f('0x2e')]),_0x4c9880);if(_0x52bca2[_0x531f('0x33')][_0x531f('0x3a')]){_0x3b4c7a[_0x531f('0x38')]=_[_0x531f('0x3b')](_0x3b4c7a['where'],{'$or':_[_0x531f('0x3c')](_0x3b4c7a['attributes'],function(_0x3da6c3){var _0x30cebe={};_0x30cebe[_0x3da6c3]={'$like':'%'+_0x52bca2[_0x531f('0x33')][_0x531f('0x3a')]+'%'};return _0x30cebe;})});}_0x3b4c7a=_[_0x531f('0x3b')]({},_0x3b4c7a,_0x52bca2[_0x531f('0x3d')]);var _0x35e497={'where':_0x3b4c7a[_0x531f('0x38')]};return db[_0x531f('0x2b')]['count'](_0x35e497)[_0x531f('0x24')](function(_0x32c867){_0x12bc63[_0x531f('0x1f')]=_0x32c867;if(_0x52bca2[_0x531f('0x33')][_0x531f('0x3e')]){_0x3b4c7a['include']=[{'all':!![]}];}return db[_0x531f('0x2b')][_0x531f('0x3f')](_0x3b4c7a);})[_0x531f('0x24')](function(_0x636310){_0x12bc63[_0x531f('0x40')]=_0x636310;return _0x12bc63;})[_0x531f('0x24')](respondWithFilteredResult(_0xa32540,_0x3b4c7a))[_0x531f('0x41')](handleError(_0xa32540,null));})[_0x531f('0x41')](handleError(_0xa32540,null));};exports[_0x531f('0x42')]=function(_0x578d82,_0x40e1e6){var _0x5cbc38={'raw':![],'where':{'id':_0x578d82[_0x531f('0x43')]['id']}},_0x4e8c20={};_0x4e8c20[_0x531f('0x2c')]=_['keys'](db[_0x531f('0x2b')][_0x531f('0x44')]);_0x4e8c20['query']=_['keys'](_0x578d82[_0x531f('0x33')]);_0x4e8c20[_0x531f('0x2e')]=_[_0x531f('0x2f')](_0x4e8c20[_0x531f('0x2c')],_0x4e8c20[_0x531f('0x33')]);_0x5cbc38['attributes']=_[_0x531f('0x2f')](_0x4e8c20[_0x531f('0x2c')],qs[_0x531f('0x31')](_0x578d82[_0x531f('0x33')][_0x531f('0x31')]));_0x5cbc38['attributes']=_0x5cbc38[_0x531f('0x30')][_0x531f('0x32')]?_0x5cbc38[_0x531f('0x30')]:_0x4e8c20[_0x531f('0x2c')];if(_0x578d82[_0x531f('0x33')][_0x531f('0x3e')]){_0x5cbc38[_0x531f('0x45')]=[{'all':!![]}];}_0x5cbc38=_[_0x531f('0x3b')]({},_0x5cbc38,_0x578d82[_0x531f('0x3d')]);return db[_0x531f('0x2b')][_0x531f('0x46')](_0x5cbc38)[_0x531f('0x24')](handleEntityNotFound(_0x40e1e6,null))['then'](respondWithResult(_0x40e1e6,null))[_0x531f('0x41')](handleError(_0x40e1e6,null));};exports['create']=function(_0x479640,_0x312558){return db[_0x531f('0x2b')][_0x531f('0x47')](_0x479640['body'],{})[_0x531f('0x24')](function(_0xa906a7){var _0x14a601=_0x479640[_0x531f('0x48')]['get']({'plain':!![]});if(!_0x14a601)throw new Error(_0x531f('0x49'));if(_0x14a601[_0x531f('0x4a')]===_0x531f('0x48')){var _0x29c3f3=_0xa906a7[_0x531f('0x25')]({'plain':!![]});var _0x5cf887=_0x531f('0x26');return db[_0x531f('0x4b')][_0x531f('0x46')]({'where':{'name':_0x5cf887,'userProfileId':_0x14a601[_0x531f('0x4c')]},'raw':!![]})[_0x531f('0x24')](function(_0x11a55e){if(_0x11a55e&&_0x11a55e[_0x531f('0x4d')]===0x0){return db[_0x531f('0x27')][_0x531f('0x47')]({'name':_0x29c3f3['name'],'resourceId':_0x29c3f3['id'],'type':_0x11a55e['name'],'sectionId':_0x11a55e['id']},{})[_0x531f('0x24')](function(){return _0xa906a7;});}else{return _0xa906a7;}})[_0x531f('0x41')](function(_0x395666){logger[_0x531f('0x28')](_0x531f('0x4e'),_0x395666);throw _0x395666;});}return _0xa906a7;})[_0x531f('0x24')](respondWithResult(_0x312558,0xc9))[_0x531f('0x41')](handleError(_0x312558,null));};exports[_0x531f('0x4f')]=function(_0x4f7902,_0x387282){if(_0x4f7902['body']['id']){delete _0x4f7902[_0x531f('0x50')]['id'];}return db[_0x531f('0x2b')][_0x531f('0x46')]({'where':{'id':_0x4f7902[_0x531f('0x43')]['id']}})[_0x531f('0x24')](handleEntityNotFound(_0x387282,null))[_0x531f('0x24')](saveUpdates(_0x4f7902['body'],null))[_0x531f('0x24')](respondWithResult(_0x387282,null))[_0x531f('0x41')](handleError(_0x387282,null));};exports[_0x531f('0x51')]=function(_0x395813,_0x1e64cc){return db[_0x531f('0x2b')][_0x531f('0x46')]({'where':{'id':_0x395813['params']['id']}})[_0x531f('0x24')](handleEntityNotFound(_0x1e64cc,null))[_0x531f('0x24')](removeEntity(_0x1e64cc,null))[_0x531f('0x41')](handleError(_0x1e64cc,null));};exports[_0x531f('0x52')]=function(_0x45d912,_0x53e0c2){return db[_0x531f('0x2b')][_0x531f('0x52')]()['then'](respondWithResult(_0x53e0c2,null))[_0x531f('0x41')](handleError(_0x53e0c2,null));};exports[_0x531f('0x53')]=function(_0x1217e8,_0x1575f9,_0x4bb033){return db['CmContact'][_0x531f('0x46')]({'where':{'id':_0x1217e8[_0x531f('0x43')]['id']}})[_0x531f('0x24')](handleEntityNotFound(_0x1575f9,null))['then'](function(_0x176c6b){if(_0x176c6b){return _0x176c6b[_0x531f('0x53')](_0x1217e8['body'][_0x531f('0x54')],_[_0x531f('0x55')](_0x1217e8['body'],['ids','id'])||{});}})['then'](respondWithResult(_0x1575f9,null))[_0x531f('0x41')](handleError(_0x1575f9,null));};exports['getContacts']=function(_0x7413c6,_0x2563a4,_0x942b97){var _0x3812d1={};var _0x2282db={};var _0x55a537;var _0x1f317c;return db['CmCompany'][_0x531f('0x56')]({'where':{'id':_0x7413c6[_0x531f('0x43')]['id']}})['then'](handleEntityNotFound(_0x2563a4,null))[_0x531f('0x24')](function(_0xe9bbea){if(_0xe9bbea){_0x55a537=_0xe9bbea;_0x2282db[_0x531f('0x2c')]=_[_0x531f('0x2d')](db[_0x531f('0x57')][_0x531f('0x44')]);_0x2282db[_0x531f('0x33')]=_[_0x531f('0x2d')](_0x7413c6['query']);_0x2282db['filters']=_[_0x531f('0x2f')](_0x2282db[_0x531f('0x2c')],_0x2282db[_0x531f('0x33')]);_0x3812d1[_0x531f('0x30')]=_['intersection'](_0x2282db[_0x531f('0x2c')],qs[_0x531f('0x31')](_0x7413c6[_0x531f('0x33')][_0x531f('0x31')]));_0x3812d1['attributes']=_0x3812d1[_0x531f('0x30')][_0x531f('0x32')]?_0x3812d1[_0x531f('0x30')]:_0x2282db[_0x531f('0x2c')];_0x3812d1['order']=qs[_0x531f('0x37')](_0x7413c6[_0x531f('0x33')][_0x531f('0x37')]);_0x3812d1[_0x531f('0x38')]=qs[_0x531f('0x2e')](_[_0x531f('0x39')](_0x7413c6[_0x531f('0x33')],_0x2282db['filters']));if(_0x7413c6[_0x531f('0x33')][_0x531f('0x3a')]){_0x3812d1[_0x531f('0x38')]=_[_0x531f('0x3b')](_0x3812d1[_0x531f('0x38')],{'$or':_[_0x531f('0x3c')](_0x3812d1[_0x531f('0x30')],function(_0x5e8f9b){var _0x5bbc51={};_0x5bbc51[_0x5e8f9b]={'$like':'%'+_0x7413c6['query']['filter']+'%'};return _0x5bbc51;})});}_0x3812d1=_[_0x531f('0x3b')]({},_0x3812d1,_0x7413c6[_0x531f('0x3d')]);return _0x55a537[_0x531f('0x58')](_0x3812d1);}})[_0x531f('0x24')](function(_0x52f283){if(_0x52f283){_0x1f317c=_0x52f283[_0x531f('0x32')];if(!_0x7413c6[_0x531f('0x33')]['hasOwnProperty']('nolimit')){_0x3812d1['limit']=qs[_0x531f('0x1e')](_0x7413c6[_0x531f('0x33')][_0x531f('0x1e')]);_0x3812d1[_0x531f('0x1c')]=qs[_0x531f('0x1c')](_0x7413c6[_0x531f('0x33')]['offset']);}return _0x55a537[_0x531f('0x58')](_0x3812d1);}})[_0x531f('0x24')](function(_0x568c30){if(_0x568c30){return _0x568c30?{'count':_0x1f317c,'rows':_0x568c30}:null;}})[_0x531f('0x24')](respondWithResult(_0x2563a4,null))[_0x531f('0x41')](handleError(_0x2563a4,null));};