54b3cebea878273cc4bcf53c4b3c06a7e99e0760
[motion2.git] / server / api / contactItemType / contactItemType.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 _0x32f4=['Unable\x20to\x20retrieve\x20the\x20current\x20user','user','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','CmContactHasItem','eml-format','rimraf','zip-dir','fast-json-patch','moment','bluebird','util','path','sox','to-csv','ejs','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','api','../../config/environment','../../config/license/util','sendStatus','status','json','offset','undefined','count','set','Content-Range','apply','reject','save','update','destroy','then','get','ContactItemTypes','UserProfileResource','end','stack','name','send','index','map','ContactItemType','rawAttributes','fieldName','type','query','filters','intersection','model','attributes','fields','nolimit','limit','sort','where','pick','filter','includeAll','include','findAll','rows','catch','show','params','keys','length','merge','options','find','create'];(function(_0x53ccf2,_0x13cb67){var _0x5a2964=function(_0x3b0514){while(--_0x3b0514){_0x53ccf2['push'](_0x53ccf2['shift']());}};_0x5a2964(++_0x13cb67);}(_0x32f4,0x154));var _0x432f=function(_0x78bad,_0x1eef1b){_0x78bad=_0x78bad-0x0;var _0x41476d=_0x32f4[_0x78bad];return _0x41476d;};'use strict';var emlformat=require(_0x432f('0x0'));var rimraf=require(_0x432f('0x1'));var zipdir=require(_0x432f('0x2'));var jsonpatch=require(_0x432f('0x3'));var rp=require('request-promise');var moment=require(_0x432f('0x4'));var BPromise=require(_0x432f('0x5'));var Mustache=require('mustache');var util=require(_0x432f('0x6'));var path=require(_0x432f('0x7'));var sox=require(_0x432f('0x8'));var csv=require(_0x432f('0x9'));var ejs=require(_0x432f('0xa'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require('lodash');var squel=require(_0x432f('0xb'));var crypto=require(_0x432f('0xc'));var jsforce=require('jsforce');var deskjs=require(_0x432f('0xd'));var toCsv=require('to-csv');var querystring=require(_0x432f('0xe'));var Papa=require(_0x432f('0xf'));var Redis=require(_0x432f('0x10'));var authService=require(_0x432f('0x11'));var qs=require(_0x432f('0x12'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x432f('0x13'));var logger=require('../../config/logger')(_0x432f('0x14'));var utils=require('../../config/utils');var config=require(_0x432f('0x15'));var licenseUtil=require(_0x432f('0x16'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x457389,_0x29f7d2){_0x29f7d2=_0x29f7d2||0xcc;return function(_0x52e40f){if(_0x52e40f){return _0x457389[_0x432f('0x17')](_0x29f7d2);}return _0x457389[_0x432f('0x18')](_0x29f7d2)['end']();};}function respondWithResult(_0x48a4db,_0x28d9e9){_0x28d9e9=_0x28d9e9||0xc8;return function(_0x44756d){if(_0x44756d){return _0x48a4db[_0x432f('0x18')](_0x28d9e9)[_0x432f('0x19')](_0x44756d);}};}function respondWithFilteredResult(_0x419bfe,_0x4f91c5){return function(_0x175ba7){if(_0x175ba7){var _0x2f0708=typeof _0x4f91c5[_0x432f('0x1a')]===_0x432f('0x1b')&&typeof _0x4f91c5['limit']===_0x432f('0x1b');var _0x30f231=_0x175ba7[_0x432f('0x1c')];var _0x243cf=_0x2f0708?0x0:_0x4f91c5[_0x432f('0x1a')];var _0x129582=_0x2f0708?_0x175ba7['count']:_0x4f91c5[_0x432f('0x1a')]+_0x4f91c5['limit'];var _0x3d545d;if(_0x129582>=_0x30f231){_0x129582=_0x30f231;_0x3d545d=0xc8;}else{_0x3d545d=0xce;}_0x419bfe[_0x432f('0x18')](_0x3d545d);return _0x419bfe[_0x432f('0x1d')](_0x432f('0x1e'),_0x243cf+'-'+_0x129582+'/'+_0x30f231)[_0x432f('0x19')](_0x175ba7);}return null;};}function patchUpdates(_0xf1f5d){return function(_0x10f39b){try{jsonpatch[_0x432f('0x1f')](_0x10f39b,_0xf1f5d,!![]);}catch(_0x693eb3){return BPromise[_0x432f('0x20')](_0x693eb3);}return _0x10f39b[_0x432f('0x21')]();};}function saveUpdates(_0x3446fd,_0x4dd230){return function(_0x5940bd){if(_0x5940bd){return _0x5940bd[_0x432f('0x22')](_0x3446fd)['then'](function(_0x28968e){return _0x28968e;});}return null;};}function removeEntity(_0x5b9c70,_0x37f136){return function(_0x175d1b){if(_0x175d1b){return _0x175d1b[_0x432f('0x23')]()[_0x432f('0x24')](function(){var _0x2f84a7=_0x175d1b[_0x432f('0x25')]({'plain':!![]});var _0x59f352=_0x432f('0x26');return db[_0x432f('0x27')]['destroy']({'where':{'type':_0x59f352,'resourceId':_0x2f84a7['id']}})[_0x432f('0x24')](function(){return _0x175d1b;});})[_0x432f('0x24')](function(){_0x5b9c70[_0x432f('0x18')](0xcc)[_0x432f('0x28')]();});}};}function handleEntityNotFound(_0x44839,_0x781862){return function(_0x138a6a){if(!_0x138a6a){_0x44839['sendStatus'](0x194);}return _0x138a6a;};}function handleError(_0x294bfe,_0x4eeae1){_0x4eeae1=_0x4eeae1||0x1f4;return function(_0x1f1fdd){logger['error'](_0x1f1fdd[_0x432f('0x29')]);if(_0x1f1fdd['name']){delete _0x1f1fdd[_0x432f('0x2a')];}_0x294bfe['status'](_0x4eeae1)[_0x432f('0x2b')](_0x1f1fdd);};}exports[_0x432f('0x2c')]=function(_0x263cd8,_0x59c1ab){var _0x6a116e={},_0x24406a={},_0x48139b={'count':0x0,'rows':[]};var _0x5ee0fa=_[_0x432f('0x2d')](db[_0x432f('0x2e')][_0x432f('0x2f')],function(_0x3581c2){return{'name':_0x3581c2[_0x432f('0x30')],'type':_0x3581c2[_0x432f('0x31')]['key']};});_0x24406a['model']=_[_0x432f('0x2d')](_0x5ee0fa,_0x432f('0x2a'));_0x24406a[_0x432f('0x32')]=_['keys'](_0x263cd8[_0x432f('0x32')]);_0x24406a[_0x432f('0x33')]=_[_0x432f('0x34')](_0x24406a[_0x432f('0x35')],_0x24406a[_0x432f('0x32')]);_0x6a116e[_0x432f('0x36')]=_[_0x432f('0x34')](_0x24406a[_0x432f('0x35')],qs['fields'](_0x263cd8['query'][_0x432f('0x37')]));_0x6a116e[_0x432f('0x36')]=_0x6a116e['attributes']['length']?_0x6a116e['attributes']:_0x24406a[_0x432f('0x35')];if(!_0x263cd8[_0x432f('0x32')]['hasOwnProperty'](_0x432f('0x38'))){_0x6a116e[_0x432f('0x39')]=qs[_0x432f('0x39')](_0x263cd8['query'][_0x432f('0x39')]);_0x6a116e[_0x432f('0x1a')]=qs['offset'](_0x263cd8[_0x432f('0x32')][_0x432f('0x1a')]);}_0x6a116e['order']=qs[_0x432f('0x3a')](_0x263cd8[_0x432f('0x32')][_0x432f('0x3a')]);_0x6a116e[_0x432f('0x3b')]=qs[_0x432f('0x33')](_[_0x432f('0x3c')](_0x263cd8[_0x432f('0x32')],_0x24406a[_0x432f('0x33')]),_0x5ee0fa);if(_0x263cd8[_0x432f('0x32')][_0x432f('0x3d')]){_0x6a116e[_0x432f('0x3b')]=_['merge'](_0x6a116e[_0x432f('0x3b')],{'$or':_['map'](_0x5ee0fa,function(_0x1550fc){if(_0x1550fc[_0x432f('0x31')]!=='VIRTUAL'){var _0x43d836={};_0x43d836[_0x1550fc['name']]={'$like':'%'+_0x263cd8[_0x432f('0x32')][_0x432f('0x3d')]+'%'};return _0x43d836;}})});}_0x6a116e=_['merge']({},_0x6a116e,_0x263cd8['options']);var _0xbd4240={'where':_0x6a116e[_0x432f('0x3b')]};return db[_0x432f('0x2e')][_0x432f('0x1c')](_0xbd4240)[_0x432f('0x24')](function(_0x513a57){_0x48139b['count']=_0x513a57;if(_0x263cd8['query'][_0x432f('0x3e')]){_0x6a116e[_0x432f('0x3f')]=[{'all':!![]}];}return db['ContactItemType'][_0x432f('0x40')](_0x6a116e);})[_0x432f('0x24')](function(_0x41c90c){_0x48139b[_0x432f('0x41')]=_0x41c90c;return _0x48139b;})[_0x432f('0x24')](respondWithFilteredResult(_0x59c1ab,_0x6a116e))[_0x432f('0x42')](handleError(_0x59c1ab,null));};exports[_0x432f('0x43')]=function(_0x1a2be2,_0x1c5f87){var _0x5c6298={'raw':!![],'where':{'id':_0x1a2be2[_0x432f('0x44')]['id']}},_0x247b63={};_0x247b63['model']=_[_0x432f('0x45')](db[_0x432f('0x2e')][_0x432f('0x2f')]);_0x247b63[_0x432f('0x32')]=_[_0x432f('0x45')](_0x1a2be2[_0x432f('0x32')]);_0x247b63[_0x432f('0x33')]=_[_0x432f('0x34')](_0x247b63[_0x432f('0x35')],_0x247b63[_0x432f('0x32')]);_0x5c6298[_0x432f('0x36')]=_[_0x432f('0x34')](_0x247b63[_0x432f('0x35')],qs[_0x432f('0x37')](_0x1a2be2['query']['fields']));_0x5c6298[_0x432f('0x36')]=_0x5c6298[_0x432f('0x36')][_0x432f('0x46')]?_0x5c6298[_0x432f('0x36')]:_0x247b63[_0x432f('0x35')];if(_0x1a2be2['query'][_0x432f('0x3e')]){_0x5c6298[_0x432f('0x3f')]=[{'all':!![]}];}_0x5c6298=_[_0x432f('0x47')]({},_0x5c6298,_0x1a2be2[_0x432f('0x48')]);return db[_0x432f('0x2e')][_0x432f('0x49')](_0x5c6298)[_0x432f('0x24')](handleEntityNotFound(_0x1c5f87,null))['then'](respondWithResult(_0x1c5f87,null))[_0x432f('0x42')](handleError(_0x1c5f87,null));};exports[_0x432f('0x4a')]=function(_0x359853,_0x547bd0){return db[_0x432f('0x2e')][_0x432f('0x4a')](_0x359853['body'],{})['then'](function(_0x3b2021){var _0x24f244=_0x359853['user']['get']({'plain':!![]});if(!_0x24f244)throw new Error(_0x432f('0x4b'));if(_0x24f244['role']===_0x432f('0x4c')){var _0x66b80e=_0x3b2021['get']({'plain':!![]});var _0x12f2f7='ContactItemTypes';return db[_0x432f('0x4d')][_0x432f('0x49')]({'where':{'name':_0x12f2f7,'userProfileId':_0x24f244[_0x432f('0x4e')]},'raw':!![]})[_0x432f('0x24')](function(_0x3051ea){if(_0x3051ea&&_0x3051ea[_0x432f('0x4f')]===0x0){return db['UserProfileResource'][_0x432f('0x4a')]({'name':_0x66b80e['name'],'resourceId':_0x66b80e['id'],'type':_0x3051ea[_0x432f('0x2a')],'sectionId':_0x3051ea['id']},{})[_0x432f('0x24')](function(){return _0x3b2021;});}else{return _0x3b2021;}})[_0x432f('0x42')](function(_0x21ae8c){logger['error'](_0x432f('0x50'),_0x21ae8c);throw _0x21ae8c;});}return _0x3b2021;})[_0x432f('0x24')](respondWithResult(_0x547bd0,0xc9))[_0x432f('0x42')](handleError(_0x547bd0,null));};exports[_0x432f('0x22')]=function(_0x13aced,_0x2a26bd){if(_0x13aced[_0x432f('0x51')]['id']){delete _0x13aced['body']['id'];}return db['ContactItemType'][_0x432f('0x49')]({'where':{'id':_0x13aced[_0x432f('0x44')]['id']}})[_0x432f('0x24')](handleEntityNotFound(_0x2a26bd,null))[_0x432f('0x24')](saveUpdates(_0x13aced[_0x432f('0x51')],null))[_0x432f('0x24')](respondWithResult(_0x2a26bd,null))['catch'](handleError(_0x2a26bd,null));};exports[_0x432f('0x23')]=function(_0x545035,_0x4e96d8){return db[_0x432f('0x2e')][_0x432f('0x49')]({'where':{'id':_0x545035[_0x432f('0x44')]['id']}})[_0x432f('0x24')](handleEntityNotFound(_0x4e96d8,null))[_0x432f('0x24')](db[_0x432f('0x52')]['update']({'ItemTypeId':null},{'where':{'ItemTypeId':_0x545035[_0x432f('0x44')]['id']}}))[_0x432f('0x24')](removeEntity(_0x4e96d8,null))['catch'](handleError(_0x4e96d8,null));};