21de15857bb78d2a21d8d4b0be49e2b45c66ddeb
[motion2.git] / server / api / cmContactHasItem / cmContactHasItem.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 _0xd89a=['order','sort','where','pick','filter','merge','options','includeAll','findAll','catch','show','params','include','find','create','user','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','eml-format','zip-dir','fast-json-patch','request-promise','bluebird','mustache','path','sox','to-csv','ejs','fs-extra','lodash','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','json','offset','undefined','limit','count','set','Content-Range','apply','reject','then','destroy','get','CmContactHasItems','UserProfileResource','end','error','stack','name','index','map','CmContactHasItem','rawAttributes','fieldName','type','key','model','filters','query','attributes','intersection','fields','length','hasOwnProperty'];(function(_0x2c59df,_0x2a377e){var _0x3d4e9d=function(_0x19a3b1){while(--_0x19a3b1){_0x2c59df['push'](_0x2c59df['shift']());}};_0x3d4e9d(++_0x2a377e);}(_0xd89a,0x6b));var _0xad89=function(_0x127011,_0x46a223){_0x127011=_0x127011-0x0;var _0x5dfefb=_0xd89a[_0x127011];return _0x5dfefb;};'use strict';var emlformat=require(_0xad89('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xad89('0x1'));var jsonpatch=require(_0xad89('0x2'));var rp=require(_0xad89('0x3'));var moment=require('moment');var BPromise=require(_0xad89('0x4'));var Mustache=require(_0xad89('0x5'));var util=require('util');var path=require(_0xad89('0x6'));var sox=require(_0xad89('0x7'));var csv=require(_0xad89('0x8'));var ejs=require(_0xad89('0x9'));var fs=require('fs');var fs_extra=require(_0xad89('0xa'));var _=require(_0xad89('0xb'));var squel=require('squel');var crypto=require(_0xad89('0xc'));var jsforce=require(_0xad89('0xd'));var deskjs=require(_0xad89('0xe'));var toCsv=require(_0xad89('0x8'));var querystring=require(_0xad89('0xf'));var Papa=require(_0xad89('0x10'));var Redis=require(_0xad89('0x11'));var authService=require(_0xad89('0x12'));var qs=require(_0xad89('0x13'));var as=require(_0xad89('0x14'));var hardwareService=require(_0xad89('0x15'));var logger=require(_0xad89('0x16'))(_0xad89('0x17'));var utils=require(_0xad89('0x18'));var config=require(_0xad89('0x19'));var licenseUtil=require(_0xad89('0x1a'));var db=require(_0xad89('0x1b'))['db'];function respondWithStatusCode(_0x566dd0,_0x2a183f){_0x2a183f=_0x2a183f||0xcc;return function(_0x5ed58e){if(_0x5ed58e){return _0x566dd0[_0xad89('0x1c')](_0x2a183f);}return _0x566dd0['status'](_0x2a183f)['end']();};}function respondWithResult(_0x36877b,_0x3e939f){_0x3e939f=_0x3e939f||0xc8;return function(_0x2d117c){if(_0x2d117c){return _0x36877b[_0xad89('0x1d')](_0x3e939f)[_0xad89('0x1e')](_0x2d117c);}};}function respondWithFilteredResult(_0x48eeb3,_0xf70bc9){return function(_0x5b59e9){if(_0x5b59e9){var _0x40080a=typeof _0xf70bc9[_0xad89('0x1f')]===_0xad89('0x20')&&typeof _0xf70bc9[_0xad89('0x21')]===_0xad89('0x20');var _0x1e8222=_0x5b59e9[_0xad89('0x22')];var _0x2f2d27=_0x40080a?0x0:_0xf70bc9[_0xad89('0x1f')];var _0x443061=_0x40080a?_0x5b59e9['count']:_0xf70bc9[_0xad89('0x1f')]+_0xf70bc9[_0xad89('0x21')];var _0x3db469;if(_0x443061>=_0x1e8222){_0x443061=_0x1e8222;_0x3db469=0xc8;}else{_0x3db469=0xce;}_0x48eeb3[_0xad89('0x1d')](_0x3db469);return _0x48eeb3[_0xad89('0x23')](_0xad89('0x24'),_0x2f2d27+'-'+_0x443061+'/'+_0x1e8222)['json'](_0x5b59e9);}return null;};}function patchUpdates(_0x2196c4){return function(_0x258939){try{jsonpatch[_0xad89('0x25')](_0x258939,_0x2196c4,!![]);}catch(_0x2aa575){return BPromise[_0xad89('0x26')](_0x2aa575);}return _0x258939['save']();};}function saveUpdates(_0x26722c,_0x4d8de1){return function(_0xae35b1){if(_0xae35b1){return _0xae35b1['update'](_0x26722c)[_0xad89('0x27')](function(_0x17bdb7){return _0x17bdb7;});}return null;};}function removeEntity(_0x3ed5fe,_0x5429a6){return function(_0x1718af){if(_0x1718af){return _0x1718af[_0xad89('0x28')]()[_0xad89('0x27')](function(){var _0x38e67b=_0x1718af[_0xad89('0x29')]({'plain':!![]});var _0x32eca3=_0xad89('0x2a');return db[_0xad89('0x2b')][_0xad89('0x28')]({'where':{'type':_0x32eca3,'resourceId':_0x38e67b['id']}})[_0xad89('0x27')](function(){return _0x1718af;});})[_0xad89('0x27')](function(){_0x3ed5fe[_0xad89('0x1d')](0xcc)[_0xad89('0x2c')]();});}};}function handleEntityNotFound(_0x8d6c00,_0xb2cab8){return function(_0x22ce52){if(!_0x22ce52){_0x8d6c00[_0xad89('0x1c')](0x194);}return _0x22ce52;};}function handleError(_0xe18a85,_0xe1df9f){_0xe1df9f=_0xe1df9f||0x1f4;return function(_0x818f57){logger[_0xad89('0x2d')](_0x818f57[_0xad89('0x2e')]);if(_0x818f57[_0xad89('0x2f')]){delete _0x818f57[_0xad89('0x2f')];}_0xe18a85[_0xad89('0x1d')](_0xe1df9f)['send'](_0x818f57);};}exports[_0xad89('0x30')]=function(_0x6e872c,_0x23ed7b){var _0x46fe07={},_0x2b58d8={},_0x9bc9bc={'count':0x0,'rows':[]};var _0x277d42=_[_0xad89('0x31')](db[_0xad89('0x32')][_0xad89('0x33')],function(_0x527776){return{'name':_0x527776[_0xad89('0x34')],'type':_0x527776[_0xad89('0x35')][_0xad89('0x36')]};});_0x2b58d8[_0xad89('0x37')]=_[_0xad89('0x31')](_0x277d42,'name');_0x2b58d8['query']=_['keys'](_0x6e872c['query']);_0x2b58d8[_0xad89('0x38')]=_['intersection'](_0x2b58d8[_0xad89('0x37')],_0x2b58d8[_0xad89('0x39')]);_0x46fe07[_0xad89('0x3a')]=_[_0xad89('0x3b')](_0x2b58d8['model'],qs[_0xad89('0x3c')](_0x6e872c[_0xad89('0x39')]['fields']));_0x46fe07['attributes']=_0x46fe07[_0xad89('0x3a')][_0xad89('0x3d')]?_0x46fe07[_0xad89('0x3a')]:_0x2b58d8[_0xad89('0x37')];if(!_0x6e872c[_0xad89('0x39')][_0xad89('0x3e')]('nolimit')){_0x46fe07[_0xad89('0x21')]=qs[_0xad89('0x21')](_0x6e872c[_0xad89('0x39')]['limit']);_0x46fe07['offset']=qs[_0xad89('0x1f')](_0x6e872c[_0xad89('0x39')]['offset']);}_0x46fe07[_0xad89('0x3f')]=qs[_0xad89('0x40')](_0x6e872c[_0xad89('0x39')][_0xad89('0x40')]);_0x46fe07[_0xad89('0x41')]=qs[_0xad89('0x38')](_[_0xad89('0x42')](_0x6e872c[_0xad89('0x39')],_0x2b58d8[_0xad89('0x38')]),_0x277d42);if(_0x6e872c[_0xad89('0x39')][_0xad89('0x43')]){_0x46fe07['where']=_[_0xad89('0x44')](_0x46fe07[_0xad89('0x41')],{'$or':_[_0xad89('0x31')](_0x277d42,function(_0x1104eb){if(_0x1104eb[_0xad89('0x35')]!=='VIRTUAL'){var _0x9b2fa7={};_0x9b2fa7[_0x1104eb[_0xad89('0x2f')]]={'$like':'%'+_0x6e872c[_0xad89('0x39')][_0xad89('0x43')]+'%'};return _0x9b2fa7;}})});}_0x46fe07=_[_0xad89('0x44')]({},_0x46fe07,_0x6e872c[_0xad89('0x45')]);var _0x4140fd={'where':_0x46fe07[_0xad89('0x41')]};return db[_0xad89('0x32')][_0xad89('0x22')](_0x4140fd)['then'](function(_0x4b2796){_0x9bc9bc[_0xad89('0x22')]=_0x4b2796;if(_0x6e872c[_0xad89('0x39')][_0xad89('0x46')]){_0x46fe07['include']=[{'all':!![]}];}return db[_0xad89('0x32')][_0xad89('0x47')](_0x46fe07);})['then'](function(_0x1a1975){_0x9bc9bc['rows']=_0x1a1975;return _0x9bc9bc;})[_0xad89('0x27')](respondWithFilteredResult(_0x23ed7b,_0x46fe07))[_0xad89('0x48')](handleError(_0x23ed7b,null));};exports[_0xad89('0x49')]=function(_0x71d7b4,_0x34c4b8){var _0x49617f={'raw':!![],'where':{'id':_0x71d7b4[_0xad89('0x4a')]['id']}},_0x140774={};_0x140774[_0xad89('0x37')]=_['keys'](db[_0xad89('0x32')][_0xad89('0x33')]);_0x140774[_0xad89('0x39')]=_['keys'](_0x71d7b4[_0xad89('0x39')]);_0x140774[_0xad89('0x38')]=_[_0xad89('0x3b')](_0x140774[_0xad89('0x37')],_0x140774[_0xad89('0x39')]);_0x49617f['attributes']=_[_0xad89('0x3b')](_0x140774[_0xad89('0x37')],qs[_0xad89('0x3c')](_0x71d7b4[_0xad89('0x39')][_0xad89('0x3c')]));_0x49617f[_0xad89('0x3a')]=_0x49617f[_0xad89('0x3a')]['length']?_0x49617f[_0xad89('0x3a')]:_0x140774[_0xad89('0x37')];if(_0x71d7b4['query'][_0xad89('0x46')]){_0x49617f[_0xad89('0x4b')]=[{'all':!![]}];}_0x49617f=_[_0xad89('0x44')]({},_0x49617f,_0x71d7b4[_0xad89('0x45')]);return db[_0xad89('0x32')][_0xad89('0x4c')](_0x49617f)[_0xad89('0x27')](handleEntityNotFound(_0x34c4b8,null))[_0xad89('0x27')](respondWithResult(_0x34c4b8,null))['catch'](handleError(_0x34c4b8,null));};exports[_0xad89('0x4d')]=function(_0x21423c,_0x5a8906){return db[_0xad89('0x32')][_0xad89('0x4d')](_0x21423c['body'],{})['then'](function(_0x482068){var _0x581523=_0x21423c[_0xad89('0x4e')]['get']({'plain':!![]});if(!_0x581523)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x581523[_0xad89('0x4f')]===_0xad89('0x4e')){var _0x1820c3=_0x482068[_0xad89('0x29')]({'plain':!![]});var _0x59e6a8='CmContactHasItems';return db[_0xad89('0x50')]['find']({'where':{'name':_0x59e6a8,'userProfileId':_0x581523[_0xad89('0x51')]},'raw':!![]})[_0xad89('0x27')](function(_0x2b0096){if(_0x2b0096&&_0x2b0096[_0xad89('0x52')]===0x0){return db['UserProfileResource']['create']({'name':_0x1820c3['name'],'resourceId':_0x1820c3['id'],'type':_0x2b0096[_0xad89('0x2f')],'sectionId':_0x2b0096['id']},{})['then'](function(){return _0x482068;});}else{return _0x482068;}})[_0xad89('0x48')](function(_0x43a87c){logger['error'](_0xad89('0x53'),_0x43a87c);throw _0x43a87c;});}return _0x482068;})[_0xad89('0x27')](respondWithResult(_0x5a8906,0xc9))[_0xad89('0x48')](handleError(_0x5a8906,null));};exports['update']=function(_0x37bc4a,_0x472bcd){if(_0x37bc4a[_0xad89('0x54')]['id']){delete _0x37bc4a[_0xad89('0x54')]['id'];}return db[_0xad89('0x32')][_0xad89('0x4c')]({'where':{'id':_0x37bc4a['params']['id']}})[_0xad89('0x27')](handleEntityNotFound(_0x472bcd,null))[_0xad89('0x27')](saveUpdates(_0x37bc4a['body'],null))[_0xad89('0x27')](respondWithResult(_0x472bcd,null))[_0xad89('0x48')](handleError(_0x472bcd,null));};exports[_0xad89('0x28')]=function(_0x289c67,_0xabf84d){return db['CmContactHasItem']['find']({'where':{'id':_0x289c67[_0xad89('0x4a')]['id']}})[_0xad89('0x27')](handleEntityNotFound(_0xabf84d,null))[_0xad89('0x27')](removeEntity(_0xabf84d,null))[_0xad89('0x48')](handleError(_0xabf84d,null));};