Built motion from commit (unavailable).|2.6.0
[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 _0xb10b=['nolimit','order','sort','pick','filter','where','merge','map','options','includeAll','include','findAll','rows','catch','show','rawAttributes','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','Companies','userProfileId','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','params','addContacts','CmContact','ids','omit','getContacts','findOne','eml-format','zip-dir','fast-json-patch','request-promise','bluebird','mustache','util','path','sox','to-csv','fs-extra','lodash','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../config/license/hardware','../../config/logger','api','../../config/environment','../../mysqldb','sendStatus','status','json','offset','limit','undefined','count','set','Content-Range','reject','update','then','destroy','get','end','error','stack','name','send','CmCompany','describe','model','keys','query','filters','intersection','attributes','fields','length','hasOwnProperty'];(function(_0x40a045,_0x516f0e){var _0x1a687c=function(_0x48c9fe){while(--_0x48c9fe){_0x40a045['push'](_0x40a045['shift']());}};_0x1a687c(++_0x516f0e);}(_0xb10b,0x12a));var _0xbb10=function(_0x104e21,_0x2ea86f){_0x104e21=_0x104e21-0x0;var _0x605b37=_0xb10b[_0x104e21];return _0x605b37;};'use strict';var emlformat=require(_0xbb10('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xbb10('0x1'));var jsonpatch=require(_0xbb10('0x2'));var rp=require(_0xbb10('0x3'));var moment=require('moment');var BPromise=require(_0xbb10('0x4'));var Mustache=require(_0xbb10('0x5'));var util=require(_0xbb10('0x6'));var path=require(_0xbb10('0x7'));var sox=require(_0xbb10('0x8'));var csv=require(_0xbb10('0x9'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0xbb10('0xa'));var _=require(_0xbb10('0xb'));var squel=require('squel');var crypto=require(_0xbb10('0xc'));var jsforce=require(_0xbb10('0xd'));var deskjs=require(_0xbb10('0xe'));var toCsv=require(_0xbb10('0x9'));var querystring=require(_0xbb10('0xf'));var Papa=require(_0xbb10('0x10'));var Redis=require(_0xbb10('0x11'));var authService=require(_0xbb10('0x12'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xbb10('0x13'));var logger=require(_0xbb10('0x14'))(_0xbb10('0x15'));var utils=require('../../config/utils');var config=require(_0xbb10('0x16'));var licenseUtil=require('../../config/license/util');var db=require(_0xbb10('0x17'))['db'];function respondWithStatusCode(_0x916a32,_0x285009){_0x285009=_0x285009||0xcc;return function(_0x193a27){if(_0x193a27){return _0x916a32[_0xbb10('0x18')](_0x285009);}return _0x916a32[_0xbb10('0x19')](_0x285009)['end']();};}function respondWithResult(_0x26e021,_0x42d7e4){_0x42d7e4=_0x42d7e4||0xc8;return function(_0x45b9d6){if(_0x45b9d6){return _0x26e021[_0xbb10('0x19')](_0x42d7e4)[_0xbb10('0x1a')](_0x45b9d6);}};}function respondWithFilteredResult(_0x2f2ebf,_0x24a57b){return function(_0x36b5e8){if(_0x36b5e8){var _0x3e26f4=typeof _0x24a57b[_0xbb10('0x1b')]==='undefined'&&typeof _0x24a57b[_0xbb10('0x1c')]===_0xbb10('0x1d');var _0x133ac2=_0x36b5e8[_0xbb10('0x1e')];var _0x378da8=_0x3e26f4?0x0:_0x24a57b[_0xbb10('0x1b')];var _0x575cca=_0x3e26f4?_0x36b5e8[_0xbb10('0x1e')]:_0x24a57b[_0xbb10('0x1b')]+_0x24a57b[_0xbb10('0x1c')];var _0x48b6e9;if(_0x575cca>=_0x133ac2){_0x575cca=_0x133ac2;_0x48b6e9=0xc8;}else{_0x48b6e9=0xce;}_0x2f2ebf[_0xbb10('0x19')](_0x48b6e9);return _0x2f2ebf[_0xbb10('0x1f')](_0xbb10('0x20'),_0x378da8+'-'+_0x575cca+'/'+_0x133ac2)[_0xbb10('0x1a')](_0x36b5e8);}return null;};}function patchUpdates(_0xdf237d){return function(_0x44e563){try{jsonpatch['apply'](_0x44e563,_0xdf237d,!![]);}catch(_0x12f888){return BPromise[_0xbb10('0x21')](_0x12f888);}return _0x44e563['save']();};}function saveUpdates(_0x4c4b0d,_0x8e2b97){return function(_0x311abe){if(_0x311abe){return _0x311abe[_0xbb10('0x22')](_0x4c4b0d)[_0xbb10('0x23')](function(_0x445761){return _0x445761;});}return null;};}function removeEntity(_0x1824b7,_0x407cb4){return function(_0x436104){if(_0x436104){return _0x436104[_0xbb10('0x24')]()[_0xbb10('0x23')](function(){var _0x3ba6cd=_0x436104[_0xbb10('0x25')]({'plain':!![]});var _0x537898='Companies';return db['UserProfileResource'][_0xbb10('0x24')]({'where':{'type':_0x537898,'resourceId':_0x3ba6cd['id']}})[_0xbb10('0x23')](function(){return _0x436104;});})[_0xbb10('0x23')](function(){_0x1824b7['status'](0xcc)[_0xbb10('0x26')]();});}};}function handleEntityNotFound(_0x24d586,_0x6ed43b){return function(_0x1d5e95){if(!_0x1d5e95){_0x24d586[_0xbb10('0x18')](0x194);}return _0x1d5e95;};}function handleError(_0x20d946,_0x2a0e8b){_0x2a0e8b=_0x2a0e8b||0x1f4;return function(_0x4aabb9){logger[_0xbb10('0x27')](_0x4aabb9[_0xbb10('0x28')]);if(_0x4aabb9[_0xbb10('0x29')]){delete _0x4aabb9[_0xbb10('0x29')];}_0x20d946[_0xbb10('0x19')](_0x2a0e8b)[_0xbb10('0x2a')](_0x4aabb9);};}exports['index']=function(_0x221624,_0x4ee02e){var _0x448d5c={},_0x56f2da={},_0x16ad2a={'count':0x0,'rows':[]};return db[_0xbb10('0x2b')][_0xbb10('0x2c')]()['then'](function(_0x518d47){_0x56f2da[_0xbb10('0x2d')]=_[_0xbb10('0x2e')](_0x518d47);_0x56f2da['query']=_[_0xbb10('0x2e')](_0x221624[_0xbb10('0x2f')]);_0x56f2da[_0xbb10('0x30')]=_[_0xbb10('0x31')](_0x56f2da[_0xbb10('0x2d')],_0x56f2da[_0xbb10('0x2f')]);_0x448d5c[_0xbb10('0x32')]=_['intersection'](_0x56f2da[_0xbb10('0x2d')],qs[_0xbb10('0x33')](_0x221624[_0xbb10('0x2f')][_0xbb10('0x33')]));_0x448d5c[_0xbb10('0x32')]=_0x448d5c[_0xbb10('0x32')][_0xbb10('0x34')]?_0x448d5c[_0xbb10('0x32')]:_0x56f2da[_0xbb10('0x2d')];if(!_0x221624['query'][_0xbb10('0x35')](_0xbb10('0x36'))){_0x448d5c[_0xbb10('0x1c')]=qs[_0xbb10('0x1c')](_0x221624['query'][_0xbb10('0x1c')]);_0x448d5c['offset']=qs['offset'](_0x221624[_0xbb10('0x2f')][_0xbb10('0x1b')]);}_0x448d5c[_0xbb10('0x37')]=qs[_0xbb10('0x38')](_0x221624[_0xbb10('0x2f')][_0xbb10('0x38')]);_0x448d5c['where']=qs[_0xbb10('0x30')](_[_0xbb10('0x39')](_0x221624['query'],_0x56f2da['filters']),_0x518d47);if(_0x221624['query'][_0xbb10('0x3a')]){_0x448d5c[_0xbb10('0x3b')]=_[_0xbb10('0x3c')](_0x448d5c['where'],{'$or':_[_0xbb10('0x3d')](_0x448d5c[_0xbb10('0x32')],function(_0x4345f3){var _0x4d1a83={};_0x4d1a83[_0x4345f3]={'$like':'%'+_0x221624[_0xbb10('0x2f')][_0xbb10('0x3a')]+'%'};return _0x4d1a83;})});}_0x448d5c=_[_0xbb10('0x3c')]({},_0x448d5c,_0x221624[_0xbb10('0x3e')]);var _0x4f1cdd={'where':_0x448d5c[_0xbb10('0x3b')]};return db[_0xbb10('0x2b')]['count'](_0x4f1cdd)[_0xbb10('0x23')](function(_0x2e3214){_0x16ad2a[_0xbb10('0x1e')]=_0x2e3214;if(_0x221624[_0xbb10('0x2f')][_0xbb10('0x3f')]){_0x448d5c[_0xbb10('0x40')]=[{'all':!![]}];}return db['CmCompany'][_0xbb10('0x41')](_0x448d5c);})[_0xbb10('0x23')](function(_0x3e7b4c){_0x16ad2a[_0xbb10('0x42')]=_0x3e7b4c;return _0x16ad2a;})['then'](respondWithFilteredResult(_0x4ee02e,_0x448d5c))[_0xbb10('0x43')](handleError(_0x4ee02e,null));})[_0xbb10('0x43')](handleError(_0x4ee02e,null));};exports[_0xbb10('0x44')]=function(_0x403beb,_0x5e2cfa){var _0x351484={'raw':![],'where':{'id':_0x403beb['params']['id']}},_0x202c60={};_0x202c60['model']=_[_0xbb10('0x2e')](db['CmCompany'][_0xbb10('0x45')]);_0x202c60[_0xbb10('0x2f')]=_['keys'](_0x403beb[_0xbb10('0x2f')]);_0x202c60[_0xbb10('0x30')]=_[_0xbb10('0x31')](_0x202c60['model'],_0x202c60[_0xbb10('0x2f')]);_0x351484[_0xbb10('0x32')]=_[_0xbb10('0x31')](_0x202c60[_0xbb10('0x2d')],qs[_0xbb10('0x33')](_0x403beb[_0xbb10('0x2f')][_0xbb10('0x33')]));_0x351484[_0xbb10('0x32')]=_0x351484[_0xbb10('0x32')][_0xbb10('0x34')]?_0x351484[_0xbb10('0x32')]:_0x202c60[_0xbb10('0x2d')];if(_0x403beb['query'][_0xbb10('0x3f')]){_0x351484[_0xbb10('0x40')]=[{'all':!![]}];}_0x351484=_['merge']({},_0x351484,_0x403beb[_0xbb10('0x3e')]);return db[_0xbb10('0x2b')][_0xbb10('0x46')](_0x351484)[_0xbb10('0x23')](handleEntityNotFound(_0x5e2cfa,null))[_0xbb10('0x23')](respondWithResult(_0x5e2cfa,null))[_0xbb10('0x43')](handleError(_0x5e2cfa,null));};exports[_0xbb10('0x47')]=function(_0x5be85e,_0x4a9793){return db[_0xbb10('0x2b')][_0xbb10('0x47')](_0x5be85e[_0xbb10('0x48')],{})['then'](function(_0x5f0c12){var _0x3cd6f3=_0x5be85e[_0xbb10('0x49')][_0xbb10('0x25')]({'plain':!![]});if(!_0x3cd6f3)throw new Error(_0xbb10('0x4a'));if(_0x3cd6f3[_0xbb10('0x4b')]===_0xbb10('0x49')){var _0x2f2552=_0x5f0c12[_0xbb10('0x25')]({'plain':!![]});var _0x2626d8=_0xbb10('0x4c');return db['UserProfileSection']['find']({'where':{'name':_0x2626d8,'userProfileId':_0x3cd6f3[_0xbb10('0x4d')]},'raw':!![]})[_0xbb10('0x23')](function(_0x36ab7d){if(_0x36ab7d&&_0x36ab7d[_0xbb10('0x4e')]===0x0){return db[_0xbb10('0x4f')][_0xbb10('0x47')]({'name':_0x2f2552[_0xbb10('0x29')],'resourceId':_0x2f2552['id'],'type':_0x36ab7d[_0xbb10('0x29')],'sectionId':_0x36ab7d['id']},{})[_0xbb10('0x23')](function(){return _0x5f0c12;});}else{return _0x5f0c12;}})[_0xbb10('0x43')](function(_0x19ca3e){logger[_0xbb10('0x27')](_0xbb10('0x50'),_0x19ca3e);throw _0x19ca3e;});}return _0x5f0c12;})[_0xbb10('0x23')](respondWithResult(_0x4a9793,0xc9))[_0xbb10('0x43')](handleError(_0x4a9793,null));};exports['update']=function(_0x124052,_0x5bd44d){if(_0x124052[_0xbb10('0x48')]['id']){delete _0x124052[_0xbb10('0x48')]['id'];}return db[_0xbb10('0x2b')][_0xbb10('0x46')]({'where':{'id':_0x124052[_0xbb10('0x51')]['id']}})[_0xbb10('0x23')](handleEntityNotFound(_0x5bd44d,null))[_0xbb10('0x23')](saveUpdates(_0x124052[_0xbb10('0x48')],null))[_0xbb10('0x23')](respondWithResult(_0x5bd44d,null))[_0xbb10('0x43')](handleError(_0x5bd44d,null));};exports[_0xbb10('0x24')]=function(_0x1c2dce,_0x1d49e1){return db[_0xbb10('0x2b')][_0xbb10('0x46')]({'where':{'id':_0x1c2dce['params']['id']}})[_0xbb10('0x23')](handleEntityNotFound(_0x1d49e1,null))[_0xbb10('0x23')](removeEntity(_0x1d49e1,null))[_0xbb10('0x43')](handleError(_0x1d49e1,null));};exports[_0xbb10('0x2c')]=function(_0xbffa3c,_0x379d8c){return db['CmCompany'][_0xbb10('0x2c')]()[_0xbb10('0x23')](respondWithResult(_0x379d8c,null))[_0xbb10('0x43')](handleError(_0x379d8c,null));};exports[_0xbb10('0x52')]=function(_0x432728,_0x22de7f,_0x175872){return db[_0xbb10('0x53')][_0xbb10('0x46')]({'where':{'id':_0x432728[_0xbb10('0x51')]['id']}})[_0xbb10('0x23')](handleEntityNotFound(_0x22de7f,null))[_0xbb10('0x23')](function(_0x16932f){if(_0x16932f){return _0x16932f['addContacts'](_0x432728['body'][_0xbb10('0x54')],_[_0xbb10('0x55')](_0x432728[_0xbb10('0x48')],[_0xbb10('0x54'),'id'])||{});}})['then'](respondWithResult(_0x22de7f,null))['catch'](handleError(_0x22de7f,null));};exports[_0xbb10('0x56')]=function(_0xaf2cc3,_0x410bef,_0x2dd150){var _0x412f13={};var _0x3e9753={};var _0x45c0dc;var _0x4a5fb9;return db[_0xbb10('0x2b')][_0xbb10('0x57')]({'where':{'id':_0xaf2cc3['params']['id']}})['then'](handleEntityNotFound(_0x410bef,null))['then'](function(_0x32c1e8){if(_0x32c1e8){_0x45c0dc=_0x32c1e8;_0x3e9753[_0xbb10('0x2d')]=_[_0xbb10('0x2e')](db['CmContact']['rawAttributes']);_0x3e9753['query']=_[_0xbb10('0x2e')](_0xaf2cc3[_0xbb10('0x2f')]);_0x3e9753[_0xbb10('0x30')]=_['intersection'](_0x3e9753[_0xbb10('0x2d')],_0x3e9753[_0xbb10('0x2f')]);_0x412f13['attributes']=_['intersection'](_0x3e9753[_0xbb10('0x2d')],qs[_0xbb10('0x33')](_0xaf2cc3[_0xbb10('0x2f')][_0xbb10('0x33')]));_0x412f13['attributes']=_0x412f13[_0xbb10('0x32')][_0xbb10('0x34')]?_0x412f13[_0xbb10('0x32')]:_0x3e9753['model'];_0x412f13[_0xbb10('0x37')]=qs[_0xbb10('0x38')](_0xaf2cc3[_0xbb10('0x2f')]['sort']);_0x412f13[_0xbb10('0x3b')]=qs[_0xbb10('0x30')](_[_0xbb10('0x39')](_0xaf2cc3[_0xbb10('0x2f')],_0x3e9753['filters']));if(_0xaf2cc3[_0xbb10('0x2f')][_0xbb10('0x3a')]){_0x412f13[_0xbb10('0x3b')]=_[_0xbb10('0x3c')](_0x412f13['where'],{'$or':_[_0xbb10('0x3d')](_0x412f13[_0xbb10('0x32')],function(_0x2a166e){var _0x2ceb3e={};_0x2ceb3e[_0x2a166e]={'$like':'%'+_0xaf2cc3[_0xbb10('0x2f')][_0xbb10('0x3a')]+'%'};return _0x2ceb3e;})});}_0x412f13=_[_0xbb10('0x3c')]({},_0x412f13,_0xaf2cc3['options']);return _0x45c0dc['getContacts'](_0x412f13);}})['then'](function(_0xdfbc0b){if(_0xdfbc0b){_0x4a5fb9=_0xdfbc0b[_0xbb10('0x34')];if(!_0xaf2cc3[_0xbb10('0x2f')]['hasOwnProperty']('nolimit')){_0x412f13[_0xbb10('0x1c')]=qs[_0xbb10('0x1c')](_0xaf2cc3[_0xbb10('0x2f')][_0xbb10('0x1c')]);_0x412f13[_0xbb10('0x1b')]=qs[_0xbb10('0x1b')](_0xaf2cc3['query'][_0xbb10('0x1b')]);}return _0x45c0dc[_0xbb10('0x56')](_0x412f13);}})[_0xbb10('0x23')](function(_0x14f16b){if(_0x14f16b){return _0x14f16b?{'count':_0x4a5fb9,'rows':_0x14f16b}:null;}})[_0xbb10('0x23')](respondWithResult(_0x410bef,null))['catch'](handleError(_0x410bef,null));};