b31efaa528f5e2317561203dd11b57f6a4f29395
[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 _0x3cfa=['fields','length','sort','where','pick','filter','merge','map','includeAll','include','rows','catch','options','create','body','user','role','Companies','UserProfileSection','find','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','params','addContacts','CmContact','omit','ids','getContacts','findOne','order','hasOwnProperty','nolimit','eml-format','rimraf','zip-dir','request-promise','bluebird','mustache','util','path','sox','to-csv','ejs','squel','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','status','end','count','offset','limit','set','Content-Range','json','apply','reject','save','update','destroy','then','get','UserProfileResource','error','stack','name','send','CmCompany','describe','model','keys','query','filters','intersection','attributes'];(function(_0x57dc78,_0x22d9cc){var _0x2ef3a0=function(_0x1965fd){while(--_0x1965fd){_0x57dc78['push'](_0x57dc78['shift']());}};_0x2ef3a0(++_0x22d9cc);}(_0x3cfa,0x1cf));var _0xa3cf=function(_0x2965d6,_0x4f3c54){_0x2965d6=_0x2965d6-0x0;var _0x122ef7=_0x3cfa[_0x2965d6];return _0x122ef7;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0xa3cf('0x0'));var rimraf=require(_0xa3cf('0x1'));var zipdir=require(_0xa3cf('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xa3cf('0x3'));var moment=require('moment');var BPromise=require(_0xa3cf('0x4'));var Mustache=require(_0xa3cf('0x5'));var util=require(_0xa3cf('0x6'));var path=require(_0xa3cf('0x7'));var sox=require(_0xa3cf('0x8'));var csv=require(_0xa3cf('0x9'));var ejs=require(_0xa3cf('0xa'));var fs=require('fs');var _=require('lodash');var squel=require(_0xa3cf('0xb'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0xa3cf('0xc'));var toCsv=require(_0xa3cf('0x9'));var querystring=require(_0xa3cf('0xd'));var Papa=require(_0xa3cf('0xe'));var Redis=require(_0xa3cf('0xf'));var authService=require(_0xa3cf('0x10'));var qs=require(_0xa3cf('0x11'));var as=require(_0xa3cf('0x12'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xa3cf('0x13'))(_0xa3cf('0x14'));var utils=require(_0xa3cf('0x15'));var config=require(_0xa3cf('0x16'));var db=require(_0xa3cf('0x17'))['db'];function respondWithStatusCode(_0x111032,_0x2b1d84){_0x2b1d84=_0x2b1d84||0xcc;return function(_0x309166){if(_0x309166){return _0x111032[_0xa3cf('0x18')](_0x2b1d84);}return _0x111032[_0xa3cf('0x19')](_0x2b1d84)[_0xa3cf('0x1a')]();};}function respondWithResult(_0x561ba7,_0x4e6adf){_0x4e6adf=_0x4e6adf||0xc8;return function(_0x10de7e){if(_0x10de7e){return _0x561ba7[_0xa3cf('0x19')](_0x4e6adf)['json'](_0x10de7e);}};}function respondWithFilteredResult(_0x3f75da,_0x53261a){return function(_0xcd24a0){if(_0xcd24a0){var _0x4744f0=_0xcd24a0[_0xa3cf('0x1b')],_0x5284c3=_0x53261a['offset'],_0xde0078=_0x53261a[_0xa3cf('0x1c')]+_0x53261a[_0xa3cf('0x1d')],_0x645f45;if(_0xde0078>=_0x4744f0){_0xde0078=_0x4744f0;_0x645f45=0xc8;}else{_0x645f45=0xce;}_0x3f75da[_0xa3cf('0x19')](_0x645f45);return _0x3f75da[_0xa3cf('0x1e')](_0xa3cf('0x1f'),_0x5284c3+'-'+_0xde0078+'/'+_0x4744f0)[_0xa3cf('0x20')](_0xcd24a0);}return null;};}function patchUpdates(_0x330ca){return function(_0x32f055){try{jsonpatch[_0xa3cf('0x21')](_0x32f055,_0x330ca,!![]);}catch(_0x47d44){return BPromise[_0xa3cf('0x22')](_0x47d44);}return _0x32f055[_0xa3cf('0x23')]();};}function saveUpdates(_0x4b581e,_0x187b5f){return function(_0x2e0532){if(_0x2e0532){return _0x2e0532[_0xa3cf('0x24')](_0x4b581e)['then'](function(_0xc2df5b){return _0xc2df5b;});}return null;};}function removeEntity(_0x22fb85,_0xb62890){return function(_0x5c8c8c){if(_0x5c8c8c){return _0x5c8c8c[_0xa3cf('0x25')]()[_0xa3cf('0x26')](function(){var _0x106e51=_0x5c8c8c[_0xa3cf('0x27')]({'plain':!![]});var _0x258fc='Companies';return db[_0xa3cf('0x28')][_0xa3cf('0x25')]({'where':{'type':_0x258fc,'resourceId':_0x106e51['id']}})[_0xa3cf('0x26')](function(){return _0x5c8c8c;});})[_0xa3cf('0x26')](function(){_0x22fb85[_0xa3cf('0x19')](0xcc)[_0xa3cf('0x1a')]();});}};}function handleEntityNotFound(_0x18b56e,_0x5c19a5){return function(_0x8f41ae){if(!_0x8f41ae){_0x18b56e[_0xa3cf('0x18')](0x194);}return _0x8f41ae;};}function handleError(_0x30d5e9,_0x3fc92b){_0x3fc92b=_0x3fc92b||0x1f4;return function(_0x4d2f75){logger[_0xa3cf('0x29')](_0x4d2f75[_0xa3cf('0x2a')]);if(_0x4d2f75[_0xa3cf('0x2b')]){delete _0x4d2f75[_0xa3cf('0x2b')];}_0x30d5e9[_0xa3cf('0x19')](_0x3fc92b)[_0xa3cf('0x2c')](_0x4d2f75);};}exports['index']=function(_0x171b68,_0x3509c3){var _0x2c6368={},_0x26af43={},_0x5777f4={'count':0x0,'rows':[]};return db[_0xa3cf('0x2d')][_0xa3cf('0x2e')]()['then'](function(_0x44617b){_0x26af43[_0xa3cf('0x2f')]=_[_0xa3cf('0x30')](_0x44617b);_0x26af43[_0xa3cf('0x31')]=_[_0xa3cf('0x30')](_0x171b68[_0xa3cf('0x31')]);_0x26af43[_0xa3cf('0x32')]=_[_0xa3cf('0x33')](_0x26af43['model'],_0x26af43[_0xa3cf('0x31')]);_0x2c6368[_0xa3cf('0x34')]=_[_0xa3cf('0x33')](_0x26af43[_0xa3cf('0x2f')],qs[_0xa3cf('0x35')](_0x171b68['query']['fields']));_0x2c6368[_0xa3cf('0x34')]=_0x2c6368['attributes'][_0xa3cf('0x36')]?_0x2c6368[_0xa3cf('0x34')]:_0x26af43[_0xa3cf('0x2f')];if(!_0x171b68[_0xa3cf('0x31')]['hasOwnProperty']('nolimit')){_0x2c6368[_0xa3cf('0x1d')]=qs[_0xa3cf('0x1d')](_0x171b68[_0xa3cf('0x31')][_0xa3cf('0x1d')]);_0x2c6368[_0xa3cf('0x1c')]=qs[_0xa3cf('0x1c')](_0x171b68[_0xa3cf('0x31')][_0xa3cf('0x1c')]);}_0x2c6368['order']=qs[_0xa3cf('0x37')](_0x171b68[_0xa3cf('0x31')]['sort']);_0x2c6368[_0xa3cf('0x38')]=qs['filters'](_[_0xa3cf('0x39')](_0x171b68['query'],_0x26af43[_0xa3cf('0x32')]),_0x44617b);if(_0x171b68[_0xa3cf('0x31')][_0xa3cf('0x3a')]){_0x2c6368[_0xa3cf('0x38')]=_[_0xa3cf('0x3b')](_0x2c6368['where'],{'$or':_[_0xa3cf('0x3c')](_0x2c6368[_0xa3cf('0x34')],function(_0x2c8f6f){var _0x3bde57={};_0x3bde57[_0x2c8f6f]={'$like':'%'+_0x171b68['query'][_0xa3cf('0x3a')]+'%'};return _0x3bde57;})});}_0x2c6368=_[_0xa3cf('0x3b')]({},_0x2c6368,_0x171b68['options']);var _0x5dd29e={'where':_0x2c6368[_0xa3cf('0x38')]};return db['CmCompany'][_0xa3cf('0x1b')](_0x5dd29e)[_0xa3cf('0x26')](function(_0x3a1c13){_0x5777f4['count']=_0x3a1c13;if(_0x171b68[_0xa3cf('0x31')][_0xa3cf('0x3d')]){_0x2c6368[_0xa3cf('0x3e')]=[{'all':!![]}];}return db['CmCompany']['findAll'](_0x2c6368);})['then'](function(_0x5f202d){_0x5777f4[_0xa3cf('0x3f')]=_0x5f202d;return _0x5777f4;})[_0xa3cf('0x26')](respondWithFilteredResult(_0x3509c3,_0x2c6368))[_0xa3cf('0x40')](handleError(_0x3509c3,null));})[_0xa3cf('0x40')](handleError(_0x3509c3,null));};exports['show']=function(_0x4f9869,_0x48ecb5){var _0x2b1856={'raw':![],'where':{'id':_0x4f9869['params']['id']}},_0x5bb201={};_0x5bb201['model']=_[_0xa3cf('0x30')](db['CmCompany']['rawAttributes']);_0x5bb201['query']=_[_0xa3cf('0x30')](_0x4f9869[_0xa3cf('0x31')]);_0x5bb201['filters']=_[_0xa3cf('0x33')](_0x5bb201[_0xa3cf('0x2f')],_0x5bb201[_0xa3cf('0x31')]);_0x2b1856[_0xa3cf('0x34')]=_[_0xa3cf('0x33')](_0x5bb201[_0xa3cf('0x2f')],qs['fields'](_0x4f9869[_0xa3cf('0x31')][_0xa3cf('0x35')]));_0x2b1856[_0xa3cf('0x34')]=_0x2b1856[_0xa3cf('0x34')]['length']?_0x2b1856['attributes']:_0x5bb201['model'];if(_0x4f9869[_0xa3cf('0x31')]['includeAll']){_0x2b1856[_0xa3cf('0x3e')]=[{'all':!![]}];}_0x2b1856=_['merge']({},_0x2b1856,_0x4f9869[_0xa3cf('0x41')]);return db['CmCompany']['find'](_0x2b1856)[_0xa3cf('0x26')](handleEntityNotFound(_0x48ecb5,null))[_0xa3cf('0x26')](respondWithResult(_0x48ecb5,null))[_0xa3cf('0x40')](handleError(_0x48ecb5,null));};exports['create']=function(_0x2b40fc,_0x4294b3){return db[_0xa3cf('0x2d')][_0xa3cf('0x42')](_0x2b40fc[_0xa3cf('0x43')],{})['then'](function(_0x167c9a){var _0x5f5804=_0x2b40fc[_0xa3cf('0x44')][_0xa3cf('0x27')]({'plain':!![]});if(!_0x5f5804)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x5f5804[_0xa3cf('0x45')]===_0xa3cf('0x44')){var _0x5691ac=_0x167c9a[_0xa3cf('0x27')]({'plain':!![]});var _0x57d550=_0xa3cf('0x46');return db[_0xa3cf('0x47')][_0xa3cf('0x48')]({'where':{'name':_0x57d550,'userProfileId':_0x5f5804[_0xa3cf('0x49')]},'raw':!![]})[_0xa3cf('0x26')](function(_0x306215){if(_0x306215&&_0x306215[_0xa3cf('0x4a')]===0x0){return db[_0xa3cf('0x28')]['create']({'name':_0x5691ac['name'],'resourceId':_0x5691ac['id'],'type':_0x306215[_0xa3cf('0x2b')],'sectionId':_0x306215['id']},{})[_0xa3cf('0x26')](function(){return _0x167c9a;});}else{return _0x167c9a;}})[_0xa3cf('0x40')](function(_0x22ece8){logger[_0xa3cf('0x29')](_0xa3cf('0x4b'),_0x22ece8);throw _0x22ece8;});}return _0x167c9a;})['then'](respondWithResult(_0x4294b3,0xc9))[_0xa3cf('0x40')](handleError(_0x4294b3,null));};exports['update']=function(_0x3e9802,_0x51c7a6){if(_0x3e9802[_0xa3cf('0x43')]['id']){delete _0x3e9802[_0xa3cf('0x43')]['id'];}return db['CmCompany'][_0xa3cf('0x48')]({'where':{'id':_0x3e9802[_0xa3cf('0x4c')]['id']}})[_0xa3cf('0x26')](handleEntityNotFound(_0x51c7a6,null))[_0xa3cf('0x26')](saveUpdates(_0x3e9802[_0xa3cf('0x43')],null))[_0xa3cf('0x26')](respondWithResult(_0x51c7a6,null))[_0xa3cf('0x40')](handleError(_0x51c7a6,null));};exports[_0xa3cf('0x25')]=function(_0x67a7e0,_0x39cf15){return db[_0xa3cf('0x2d')]['find']({'where':{'id':_0x67a7e0[_0xa3cf('0x4c')]['id']}})['then'](handleEntityNotFound(_0x39cf15,null))[_0xa3cf('0x26')](removeEntity(_0x39cf15,null))[_0xa3cf('0x40')](handleError(_0x39cf15,null));};exports[_0xa3cf('0x2e')]=function(_0x568d69,_0x51d9d0){return db['CmCompany']['describe']()[_0xa3cf('0x26')](respondWithResult(_0x51d9d0,null))['catch'](handleError(_0x51d9d0,null));};exports[_0xa3cf('0x4d')]=function(_0x3de26c,_0x4c3323,_0x4389c8){return db[_0xa3cf('0x4e')][_0xa3cf('0x48')]({'where':{'id':_0x3de26c[_0xa3cf('0x4c')]['id']}})[_0xa3cf('0x26')](handleEntityNotFound(_0x4c3323,null))[_0xa3cf('0x26')](function(_0xe501a7){if(_0xe501a7){return _0xe501a7[_0xa3cf('0x4d')](_0x3de26c[_0xa3cf('0x43')]['ids'],_[_0xa3cf('0x4f')](_0x3de26c[_0xa3cf('0x43')],[_0xa3cf('0x50'),'id'])||{});}})[_0xa3cf('0x26')](respondWithResult(_0x4c3323,null))[_0xa3cf('0x40')](handleError(_0x4c3323,null));};exports[_0xa3cf('0x51')]=function(_0x5bacd4,_0x413e1d,_0x3a7969){var _0x32f78f={};var _0x1c328c={};var _0x51a986;var _0x6c7c7f;return db['CmCompany'][_0xa3cf('0x52')]({'where':{'id':_0x5bacd4[_0xa3cf('0x4c')]['id']}})['then'](handleEntityNotFound(_0x413e1d,null))[_0xa3cf('0x26')](function(_0x1eeef5){if(_0x1eeef5){_0x51a986=_0x1eeef5;_0x1c328c[_0xa3cf('0x2f')]=_['keys'](db[_0xa3cf('0x4e')]['rawAttributes']);_0x1c328c[_0xa3cf('0x31')]=_[_0xa3cf('0x30')](_0x5bacd4[_0xa3cf('0x31')]);_0x1c328c['filters']=_[_0xa3cf('0x33')](_0x1c328c[_0xa3cf('0x2f')],_0x1c328c[_0xa3cf('0x31')]);_0x32f78f[_0xa3cf('0x34')]=_[_0xa3cf('0x33')](_0x1c328c[_0xa3cf('0x2f')],qs[_0xa3cf('0x35')](_0x5bacd4[_0xa3cf('0x31')]['fields']));_0x32f78f[_0xa3cf('0x34')]=_0x32f78f['attributes'][_0xa3cf('0x36')]?_0x32f78f[_0xa3cf('0x34')]:_0x1c328c[_0xa3cf('0x2f')];_0x32f78f[_0xa3cf('0x53')]=qs['sort'](_0x5bacd4[_0xa3cf('0x31')]['sort']);_0x32f78f[_0xa3cf('0x38')]=qs['filters'](_[_0xa3cf('0x39')](_0x5bacd4['query'],_0x1c328c[_0xa3cf('0x32')]));if(_0x5bacd4[_0xa3cf('0x31')]['filter']){_0x32f78f[_0xa3cf('0x38')]=_[_0xa3cf('0x3b')](_0x32f78f[_0xa3cf('0x38')],{'$or':_['map'](_0x32f78f[_0xa3cf('0x34')],function(_0x5e96ee){var _0x313f20={};_0x313f20[_0x5e96ee]={'$like':'%'+_0x5bacd4[_0xa3cf('0x31')][_0xa3cf('0x3a')]+'%'};return _0x313f20;})});}_0x32f78f=_['merge']({},_0x32f78f,_0x5bacd4[_0xa3cf('0x41')]);return _0x51a986[_0xa3cf('0x51')](_0x32f78f);}})[_0xa3cf('0x26')](function(_0x2047e7){if(_0x2047e7){_0x6c7c7f=_0x2047e7[_0xa3cf('0x36')];if(!_0x5bacd4[_0xa3cf('0x31')][_0xa3cf('0x54')](_0xa3cf('0x55'))){_0x32f78f[_0xa3cf('0x1d')]=qs[_0xa3cf('0x1d')](_0x5bacd4[_0xa3cf('0x31')][_0xa3cf('0x1d')]);_0x32f78f[_0xa3cf('0x1c')]=qs['offset'](_0x5bacd4[_0xa3cf('0x31')][_0xa3cf('0x1c')]);}return _0x51a986[_0xa3cf('0x51')](_0x32f78f);}})[_0xa3cf('0x26')](function(_0x5df6cb){if(_0x5df6cb){return _0x5df6cb?{'count':_0x6c7c7f,'rows':_0x5df6cb}:null;}})[_0xa3cf('0x26')](respondWithResult(_0x413e1d,null))[_0xa3cf('0x40')](handleError(_0x413e1d,null));};