Built motion from commit 639a1a7f.|2.6.19
[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 _0x9f51=['Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','describe','addContacts','CmContact','ids','getContacts','findOne','rawAttributes','map','nolimit','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../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','reject','save','update','then','destroy','get','Companies','UserProfileResource','error','stack','index','CmCompany','keys','query','filters','intersection','model','attributes','fields','length','order','sort','where','pick','filter','merge','options','includeAll','include','rows','catch','show','params','find','body','user','role','UserProfileSection','userProfileId','create','name'];(function(_0x33f685,_0x4e971f){var _0x150be3=function(_0x434e45){while(--_0x434e45){_0x33f685['push'](_0x33f685['shift']());}};_0x150be3(++_0x4e971f);}(_0x9f51,0x1c7));var _0x19f5=function(_0x54fc68,_0x4d6cd3){_0x54fc68=_0x54fc68-0x0;var _0x23d6d8=_0x9f51[_0x54fc68];return _0x23d6d8;};'use strict';var emlformat=require(_0x19f5('0x0'));var rimraf=require(_0x19f5('0x1'));var zipdir=require(_0x19f5('0x2'));var jsonpatch=require(_0x19f5('0x3'));var rp=require(_0x19f5('0x4'));var moment=require('moment');var BPromise=require(_0x19f5('0x5'));var Mustache=require(_0x19f5('0x6'));var util=require(_0x19f5('0x7'));var path=require(_0x19f5('0x8'));var sox=require(_0x19f5('0x9'));var csv=require(_0x19f5('0xa'));var ejs=require(_0x19f5('0xb'));var fs=require('fs');var fs_extra=require(_0x19f5('0xc'));var _=require(_0x19f5('0xd'));var squel=require(_0x19f5('0xe'));var crypto=require(_0x19f5('0xf'));var jsforce=require('jsforce');var deskjs=require(_0x19f5('0x10'));var toCsv=require(_0x19f5('0xa'));var querystring=require(_0x19f5('0x11'));var Papa=require('papaparse');var Redis=require(_0x19f5('0x12'));var authService=require(_0x19f5('0x13'));var qs=require(_0x19f5('0x14'));var as=require(_0x19f5('0x15'));var hardwareService=require(_0x19f5('0x16'));var logger=require(_0x19f5('0x17'))('api');var utils=require(_0x19f5('0x18'));var config=require(_0x19f5('0x19'));var licenseUtil=require(_0x19f5('0x1a'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x3c7d16,_0x4b0d9b){_0x4b0d9b=_0x4b0d9b||0xcc;return function(_0x40c159){if(_0x40c159){return _0x3c7d16[_0x19f5('0x1b')](_0x4b0d9b);}return _0x3c7d16[_0x19f5('0x1c')](_0x4b0d9b)[_0x19f5('0x1d')]();};}function respondWithResult(_0x547f4b,_0x46ab5f){_0x46ab5f=_0x46ab5f||0xc8;return function(_0x50ec26){if(_0x50ec26){return _0x547f4b[_0x19f5('0x1c')](_0x46ab5f)[_0x19f5('0x1e')](_0x50ec26);}};}function respondWithFilteredResult(_0x275056,_0x583342){return function(_0x470562){if(_0x470562){var _0x4ea005=typeof _0x583342[_0x19f5('0x1f')]===_0x19f5('0x20')&&typeof _0x583342[_0x19f5('0x21')]===_0x19f5('0x20');var _0x39e097=_0x470562[_0x19f5('0x22')];var _0x3b69b1=_0x4ea005?0x0:_0x583342[_0x19f5('0x1f')];var _0x37ba30=_0x4ea005?_0x470562['count']:_0x583342['offset']+_0x583342['limit'];var _0x2260b7;if(_0x37ba30>=_0x39e097){_0x37ba30=_0x39e097;_0x2260b7=0xc8;}else{_0x2260b7=0xce;}_0x275056[_0x19f5('0x1c')](_0x2260b7);return _0x275056[_0x19f5('0x23')](_0x19f5('0x24'),_0x3b69b1+'-'+_0x37ba30+'/'+_0x39e097)[_0x19f5('0x1e')](_0x470562);}return null;};}function patchUpdates(_0x525180){return function(_0x4e2ece){try{jsonpatch[_0x19f5('0x25')](_0x4e2ece,_0x525180,!![]);}catch(_0x59ab23){return BPromise[_0x19f5('0x26')](_0x59ab23);}return _0x4e2ece[_0x19f5('0x27')]();};}function saveUpdates(_0x55029f,_0x21b04f){return function(_0x416fda){if(_0x416fda){return _0x416fda[_0x19f5('0x28')](_0x55029f)[_0x19f5('0x29')](function(_0x1bc5ef){return _0x1bc5ef;});}return null;};}function removeEntity(_0x258817,_0x5bd91c){return function(_0x362ae8){if(_0x362ae8){return _0x362ae8[_0x19f5('0x2a')]()[_0x19f5('0x29')](function(){var _0x426dab=_0x362ae8[_0x19f5('0x2b')]({'plain':!![]});var _0x287381=_0x19f5('0x2c');return db[_0x19f5('0x2d')][_0x19f5('0x2a')]({'where':{'type':_0x287381,'resourceId':_0x426dab['id']}})[_0x19f5('0x29')](function(){return _0x362ae8;});})[_0x19f5('0x29')](function(){_0x258817[_0x19f5('0x1c')](0xcc)[_0x19f5('0x1d')]();});}};}function handleEntityNotFound(_0x428f50,_0x2e288){return function(_0x1bf24b){if(!_0x1bf24b){_0x428f50[_0x19f5('0x1b')](0x194);}return _0x1bf24b;};}function handleError(_0x2a4e07,_0x21e689){_0x21e689=_0x21e689||0x1f4;return function(_0x28c8b0){logger[_0x19f5('0x2e')](_0x28c8b0[_0x19f5('0x2f')]);if(_0x28c8b0['name']){delete _0x28c8b0['name'];}_0x2a4e07[_0x19f5('0x1c')](_0x21e689)['send'](_0x28c8b0);};}exports[_0x19f5('0x30')]=function(_0x137aae,_0x4fdd00){var _0xf3c88c={},_0x4003c5={},_0x2da6ea={'count':0x0,'rows':[]};return db[_0x19f5('0x31')]['describe']()[_0x19f5('0x29')](function(_0x29dc91){_0x4003c5['model']=_[_0x19f5('0x32')](_0x29dc91);_0x4003c5[_0x19f5('0x33')]=_[_0x19f5('0x32')](_0x137aae['query']);_0x4003c5[_0x19f5('0x34')]=_[_0x19f5('0x35')](_0x4003c5[_0x19f5('0x36')],_0x4003c5['query']);_0xf3c88c[_0x19f5('0x37')]=_[_0x19f5('0x35')](_0x4003c5[_0x19f5('0x36')],qs[_0x19f5('0x38')](_0x137aae['query'][_0x19f5('0x38')]));_0xf3c88c[_0x19f5('0x37')]=_0xf3c88c[_0x19f5('0x37')][_0x19f5('0x39')]?_0xf3c88c[_0x19f5('0x37')]:_0x4003c5[_0x19f5('0x36')];if(!_0x137aae[_0x19f5('0x33')]['hasOwnProperty']('nolimit')){_0xf3c88c['limit']=qs[_0x19f5('0x21')](_0x137aae['query']['limit']);_0xf3c88c[_0x19f5('0x1f')]=qs[_0x19f5('0x1f')](_0x137aae[_0x19f5('0x33')][_0x19f5('0x1f')]);}_0xf3c88c[_0x19f5('0x3a')]=qs[_0x19f5('0x3b')](_0x137aae[_0x19f5('0x33')][_0x19f5('0x3b')]);_0xf3c88c[_0x19f5('0x3c')]=qs[_0x19f5('0x34')](_[_0x19f5('0x3d')](_0x137aae[_0x19f5('0x33')],_0x4003c5[_0x19f5('0x34')]),_0x29dc91);if(_0x137aae[_0x19f5('0x33')][_0x19f5('0x3e')]){_0xf3c88c[_0x19f5('0x3c')]=_[_0x19f5('0x3f')](_0xf3c88c[_0x19f5('0x3c')],{'$or':_['map'](_0xf3c88c[_0x19f5('0x37')],function(_0x1a3a89){var _0x164df0={};_0x164df0[_0x1a3a89]={'$like':'%'+_0x137aae[_0x19f5('0x33')][_0x19f5('0x3e')]+'%'};return _0x164df0;})});}_0xf3c88c=_[_0x19f5('0x3f')]({},_0xf3c88c,_0x137aae[_0x19f5('0x40')]);var _0x49d31f={'where':_0xf3c88c['where']};return db['CmCompany']['count'](_0x49d31f)['then'](function(_0x444044){_0x2da6ea[_0x19f5('0x22')]=_0x444044;if(_0x137aae['query'][_0x19f5('0x41')]){_0xf3c88c[_0x19f5('0x42')]=[{'all':!![]}];}return db[_0x19f5('0x31')]['findAll'](_0xf3c88c);})[_0x19f5('0x29')](function(_0x4af39a){_0x2da6ea[_0x19f5('0x43')]=_0x4af39a;return _0x2da6ea;})[_0x19f5('0x29')](respondWithFilteredResult(_0x4fdd00,_0xf3c88c))[_0x19f5('0x44')](handleError(_0x4fdd00,null));})['catch'](handleError(_0x4fdd00,null));};exports[_0x19f5('0x45')]=function(_0x3cec0b,_0x4cdf58){var _0x396b1d={'raw':![],'where':{'id':_0x3cec0b[_0x19f5('0x46')]['id']}},_0x27e308={};_0x27e308[_0x19f5('0x36')]=_[_0x19f5('0x32')](db[_0x19f5('0x31')]['rawAttributes']);_0x27e308[_0x19f5('0x33')]=_[_0x19f5('0x32')](_0x3cec0b[_0x19f5('0x33')]);_0x27e308[_0x19f5('0x34')]=_[_0x19f5('0x35')](_0x27e308[_0x19f5('0x36')],_0x27e308[_0x19f5('0x33')]);_0x396b1d['attributes']=_[_0x19f5('0x35')](_0x27e308[_0x19f5('0x36')],qs[_0x19f5('0x38')](_0x3cec0b[_0x19f5('0x33')][_0x19f5('0x38')]));_0x396b1d[_0x19f5('0x37')]=_0x396b1d[_0x19f5('0x37')][_0x19f5('0x39')]?_0x396b1d['attributes']:_0x27e308[_0x19f5('0x36')];if(_0x3cec0b[_0x19f5('0x33')][_0x19f5('0x41')]){_0x396b1d[_0x19f5('0x42')]=[{'all':!![]}];}_0x396b1d=_['merge']({},_0x396b1d,_0x3cec0b[_0x19f5('0x40')]);return db[_0x19f5('0x31')][_0x19f5('0x47')](_0x396b1d)[_0x19f5('0x29')](handleEntityNotFound(_0x4cdf58,null))['then'](respondWithResult(_0x4cdf58,null))[_0x19f5('0x44')](handleError(_0x4cdf58,null));};exports['create']=function(_0x1f878d,_0x778b7){return db[_0x19f5('0x31')]['create'](_0x1f878d[_0x19f5('0x48')],{})[_0x19f5('0x29')](function(_0x20f1d9){var _0x53da1e=_0x1f878d[_0x19f5('0x49')]['get']({'plain':!![]});if(!_0x53da1e)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x53da1e[_0x19f5('0x4a')]==='user'){var _0x49b7d3=_0x20f1d9['get']({'plain':!![]});var _0x59c759='Companies';return db[_0x19f5('0x4b')][_0x19f5('0x47')]({'where':{'name':_0x59c759,'userProfileId':_0x53da1e[_0x19f5('0x4c')]},'raw':!![]})[_0x19f5('0x29')](function(_0xa9cd25){if(_0xa9cd25&&_0xa9cd25['autoAssociation']===0x0){return db[_0x19f5('0x2d')][_0x19f5('0x4d')]({'name':_0x49b7d3[_0x19f5('0x4e')],'resourceId':_0x49b7d3['id'],'type':_0xa9cd25[_0x19f5('0x4e')],'sectionId':_0xa9cd25['id']},{})[_0x19f5('0x29')](function(){return _0x20f1d9;});}else{return _0x20f1d9;}})['catch'](function(_0x131678){logger[_0x19f5('0x2e')](_0x19f5('0x4f'),_0x131678);throw _0x131678;});}return _0x20f1d9;})[_0x19f5('0x29')](respondWithResult(_0x778b7,0xc9))['catch'](handleError(_0x778b7,null));};exports[_0x19f5('0x28')]=function(_0x42d9b4,_0x96e33c){if(_0x42d9b4['body']['id']){delete _0x42d9b4[_0x19f5('0x48')]['id'];}return db[_0x19f5('0x31')][_0x19f5('0x47')]({'where':{'id':_0x42d9b4[_0x19f5('0x46')]['id']}})[_0x19f5('0x29')](handleEntityNotFound(_0x96e33c,null))['then'](saveUpdates(_0x42d9b4[_0x19f5('0x48')],null))[_0x19f5('0x29')](respondWithResult(_0x96e33c,null))[_0x19f5('0x44')](handleError(_0x96e33c,null));};exports[_0x19f5('0x2a')]=function(_0x39fabf,_0x2b4bd5){return db['CmCompany'][_0x19f5('0x47')]({'where':{'id':_0x39fabf[_0x19f5('0x46')]['id']}})['then'](handleEntityNotFound(_0x2b4bd5,null))[_0x19f5('0x29')](removeEntity(_0x2b4bd5,null))[_0x19f5('0x44')](handleError(_0x2b4bd5,null));};exports[_0x19f5('0x50')]=function(_0x12f52b,_0x18eec2){return db['CmCompany'][_0x19f5('0x50')]()[_0x19f5('0x29')](respondWithResult(_0x18eec2,null))[_0x19f5('0x44')](handleError(_0x18eec2,null));};exports[_0x19f5('0x51')]=function(_0x4ccae7,_0x4d8e24,_0x2104ee){return db[_0x19f5('0x52')]['find']({'where':{'id':_0x4ccae7['params']['id']}})[_0x19f5('0x29')](handleEntityNotFound(_0x4d8e24,null))[_0x19f5('0x29')](function(_0x408ac2){if(_0x408ac2){return _0x408ac2['addContacts'](_0x4ccae7[_0x19f5('0x48')][_0x19f5('0x53')],_['omit'](_0x4ccae7[_0x19f5('0x48')],[_0x19f5('0x53'),'id'])||{});}})[_0x19f5('0x29')](respondWithResult(_0x4d8e24,null))[_0x19f5('0x44')](handleError(_0x4d8e24,null));};exports[_0x19f5('0x54')]=function(_0x2edc7a,_0x4c9943,_0x30df9d){var _0x508b6d={};var _0x1860f2={};var _0x31e5ac;var _0x20897d;return db['CmCompany'][_0x19f5('0x55')]({'where':{'id':_0x2edc7a[_0x19f5('0x46')]['id']}})[_0x19f5('0x29')](handleEntityNotFound(_0x4c9943,null))[_0x19f5('0x29')](function(_0x176ca8){if(_0x176ca8){_0x31e5ac=_0x176ca8;_0x1860f2[_0x19f5('0x36')]=_[_0x19f5('0x32')](db[_0x19f5('0x52')][_0x19f5('0x56')]);_0x1860f2[_0x19f5('0x33')]=_[_0x19f5('0x32')](_0x2edc7a[_0x19f5('0x33')]);_0x1860f2[_0x19f5('0x34')]=_[_0x19f5('0x35')](_0x1860f2[_0x19f5('0x36')],_0x1860f2[_0x19f5('0x33')]);_0x508b6d['attributes']=_[_0x19f5('0x35')](_0x1860f2[_0x19f5('0x36')],qs[_0x19f5('0x38')](_0x2edc7a[_0x19f5('0x33')][_0x19f5('0x38')]));_0x508b6d[_0x19f5('0x37')]=_0x508b6d[_0x19f5('0x37')]['length']?_0x508b6d[_0x19f5('0x37')]:_0x1860f2[_0x19f5('0x36')];_0x508b6d['order']=qs['sort'](_0x2edc7a['query'][_0x19f5('0x3b')]);_0x508b6d['where']=qs['filters'](_[_0x19f5('0x3d')](_0x2edc7a[_0x19f5('0x33')],_0x1860f2[_0x19f5('0x34')]));if(_0x2edc7a[_0x19f5('0x33')][_0x19f5('0x3e')]){_0x508b6d[_0x19f5('0x3c')]=_[_0x19f5('0x3f')](_0x508b6d[_0x19f5('0x3c')],{'$or':_[_0x19f5('0x57')](_0x508b6d['attributes'],function(_0x3f5606){var _0x4fdb36={};_0x4fdb36[_0x3f5606]={'$like':'%'+_0x2edc7a['query']['filter']+'%'};return _0x4fdb36;})});}_0x508b6d=_[_0x19f5('0x3f')]({},_0x508b6d,_0x2edc7a['options']);return _0x31e5ac['getContacts'](_0x508b6d);}})['then'](function(_0x326e88){if(_0x326e88){_0x20897d=_0x326e88[_0x19f5('0x39')];if(!_0x2edc7a['query']['hasOwnProperty'](_0x19f5('0x58'))){_0x508b6d[_0x19f5('0x21')]=qs[_0x19f5('0x21')](_0x2edc7a[_0x19f5('0x33')][_0x19f5('0x21')]);_0x508b6d['offset']=qs[_0x19f5('0x1f')](_0x2edc7a['query'][_0x19f5('0x1f')]);}return _0x31e5ac[_0x19f5('0x54')](_0x508b6d);}})['then'](function(_0x3fcbd3){if(_0x3fcbd3){return _0x3fcbd3?{'count':_0x20897d,'rows':_0x3fcbd3}:null;}})['then'](respondWithResult(_0x4c9943,null))['catch'](handleError(_0x4c9943,null));};