934bb9cdec40ae4fa1bc8be746fe887ac01465ac
[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 _0x160f=['UserProfileSection','userProfileId','CmContactHasItem','eml-format','rimraf','zip-dir','fast-json-patch','moment','bluebird','mustache','util','path','sox','ejs','fs-extra','crypto','querystring','papaparse','ioredis','../../components/auth/service','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','end','status','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','update','then','destroy','get','UserProfileResource','error','stack','name','map','ContactItemType','key','model','query','keys','intersection','fields','attributes','length','hasOwnProperty','order','sort','where','filters','pick','filter','merge','type','VIRTUAL','options','include','findAll','rows','catch','show','params','rawAttributes','includeAll','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','ContactItemTypes'];(function(_0x652e4a,_0x14b73f){var _0x21b713=function(_0xfb21bb){while(--_0xfb21bb){_0x652e4a['push'](_0x652e4a['shift']());}};_0x21b713(++_0x14b73f);}(_0x160f,0x1ef));var _0xf160=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x160f[_0x3dd15e];return _0x231fd0;};'use strict';var emlformat=require(_0xf160('0x0'));var rimraf=require(_0xf160('0x1'));var zipdir=require(_0xf160('0x2'));var jsonpatch=require(_0xf160('0x3'));var rp=require('request-promise');var moment=require(_0xf160('0x4'));var BPromise=require(_0xf160('0x5'));var Mustache=require(_0xf160('0x6'));var util=require(_0xf160('0x7'));var path=require(_0xf160('0x8'));var sox=require(_0xf160('0x9'));var csv=require('to-csv');var ejs=require(_0xf160('0xa'));var fs=require('fs');var fs_extra=require(_0xf160('0xb'));var _=require('lodash');var squel=require('squel');var crypto=require(_0xf160('0xc'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0xf160('0xd'));var Papa=require(_0xf160('0xe'));var Redis=require(_0xf160('0xf'));var authService=require(_0xf160('0x10'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xf160('0x11'));var logger=require(_0xf160('0x12'))(_0xf160('0x13'));var utils=require('../../config/utils');var config=require(_0xf160('0x14'));var licenseUtil=require(_0xf160('0x15'));var db=require(_0xf160('0x16'))['db'];function respondWithStatusCode(_0x3b09ea,_0x2d8854){_0x2d8854=_0x2d8854||0xcc;return function(_0x523af5){if(_0x523af5){return _0x3b09ea[_0xf160('0x17')](_0x2d8854);}return _0x3b09ea['status'](_0x2d8854)[_0xf160('0x18')]();};}function respondWithResult(_0x26951a,_0x12549a){_0x12549a=_0x12549a||0xc8;return function(_0x2bb6c7){if(_0x2bb6c7){return _0x26951a[_0xf160('0x19')](_0x12549a)[_0xf160('0x1a')](_0x2bb6c7);}};}function respondWithFilteredResult(_0x2643ba,_0x172337){return function(_0x1396ba){if(_0x1396ba){var _0x59ac02=typeof _0x172337[_0xf160('0x1b')]===_0xf160('0x1c')&&typeof _0x172337[_0xf160('0x1d')]===_0xf160('0x1c');var _0x51824b=_0x1396ba[_0xf160('0x1e')];var _0x4b8185=_0x59ac02?0x0:_0x172337[_0xf160('0x1b')];var _0x1cf6ba=_0x59ac02?_0x1396ba[_0xf160('0x1e')]:_0x172337[_0xf160('0x1b')]+_0x172337[_0xf160('0x1d')];var _0x205afa;if(_0x1cf6ba>=_0x51824b){_0x1cf6ba=_0x51824b;_0x205afa=0xc8;}else{_0x205afa=0xce;}_0x2643ba[_0xf160('0x19')](_0x205afa);return _0x2643ba[_0xf160('0x1f')](_0xf160('0x20'),_0x4b8185+'-'+_0x1cf6ba+'/'+_0x51824b)['json'](_0x1396ba);}return null;};}function patchUpdates(_0x40ccf0){return function(_0xa20d5a){try{jsonpatch[_0xf160('0x21')](_0xa20d5a,_0x40ccf0,!![]);}catch(_0x5ad173){return BPromise[_0xf160('0x22')](_0x5ad173);}return _0xa20d5a[_0xf160('0x23')]();};}function saveUpdates(_0x454e63,_0x36d0cb){return function(_0x2e03c8){if(_0x2e03c8){return _0x2e03c8[_0xf160('0x24')](_0x454e63)[_0xf160('0x25')](function(_0x5a72d1){return _0x5a72d1;});}return null;};}function removeEntity(_0x3d1f6c,_0x28bf27){return function(_0x305f6a){if(_0x305f6a){return _0x305f6a[_0xf160('0x26')]()['then'](function(){var _0x42458c=_0x305f6a[_0xf160('0x27')]({'plain':!![]});var _0x4a8003='ContactItemTypes';return db[_0xf160('0x28')][_0xf160('0x26')]({'where':{'type':_0x4a8003,'resourceId':_0x42458c['id']}})[_0xf160('0x25')](function(){return _0x305f6a;});})['then'](function(){_0x3d1f6c[_0xf160('0x19')](0xcc)[_0xf160('0x18')]();});}};}function handleEntityNotFound(_0x2851e8,_0x2e2b00){return function(_0x135e0f){if(!_0x135e0f){_0x2851e8[_0xf160('0x17')](0x194);}return _0x135e0f;};}function handleError(_0x5d0cc8,_0x4d7165){_0x4d7165=_0x4d7165||0x1f4;return function(_0x48145e){logger[_0xf160('0x29')](_0x48145e[_0xf160('0x2a')]);if(_0x48145e[_0xf160('0x2b')]){delete _0x48145e[_0xf160('0x2b')];}_0x5d0cc8[_0xf160('0x19')](_0x4d7165)['send'](_0x48145e);};}exports['index']=function(_0x329d6b,_0x558596){var _0x4ce478={},_0x22c2e3={},_0x3a35b3={'count':0x0,'rows':[]};var _0x5497f6=_[_0xf160('0x2c')](db[_0xf160('0x2d')]['rawAttributes'],function(_0x528dff){return{'name':_0x528dff['fieldName'],'type':_0x528dff['type'][_0xf160('0x2e')]};});_0x22c2e3[_0xf160('0x2f')]=_[_0xf160('0x2c')](_0x5497f6,_0xf160('0x2b'));_0x22c2e3[_0xf160('0x30')]=_[_0xf160('0x31')](_0x329d6b['query']);_0x22c2e3['filters']=_['intersection'](_0x22c2e3[_0xf160('0x2f')],_0x22c2e3['query']);_0x4ce478['attributes']=_[_0xf160('0x32')](_0x22c2e3[_0xf160('0x2f')],qs['fields'](_0x329d6b[_0xf160('0x30')][_0xf160('0x33')]));_0x4ce478[_0xf160('0x34')]=_0x4ce478[_0xf160('0x34')][_0xf160('0x35')]?_0x4ce478['attributes']:_0x22c2e3[_0xf160('0x2f')];if(!_0x329d6b[_0xf160('0x30')][_0xf160('0x36')]('nolimit')){_0x4ce478[_0xf160('0x1d')]=qs[_0xf160('0x1d')](_0x329d6b[_0xf160('0x30')][_0xf160('0x1d')]);_0x4ce478[_0xf160('0x1b')]=qs[_0xf160('0x1b')](_0x329d6b['query']['offset']);}_0x4ce478[_0xf160('0x37')]=qs[_0xf160('0x38')](_0x329d6b[_0xf160('0x30')][_0xf160('0x38')]);_0x4ce478[_0xf160('0x39')]=qs[_0xf160('0x3a')](_[_0xf160('0x3b')](_0x329d6b[_0xf160('0x30')],_0x22c2e3[_0xf160('0x3a')]),_0x5497f6);if(_0x329d6b[_0xf160('0x30')][_0xf160('0x3c')]){_0x4ce478[_0xf160('0x39')]=_[_0xf160('0x3d')](_0x4ce478[_0xf160('0x39')],{'$or':_[_0xf160('0x2c')](_0x5497f6,function(_0x256ce7){if(_0x256ce7[_0xf160('0x3e')]!==_0xf160('0x3f')){var _0x43f07f={};_0x43f07f[_0x256ce7[_0xf160('0x2b')]]={'$like':'%'+_0x329d6b[_0xf160('0x30')][_0xf160('0x3c')]+'%'};return _0x43f07f;}})});}_0x4ce478=_[_0xf160('0x3d')]({},_0x4ce478,_0x329d6b[_0xf160('0x40')]);var _0x494d76={'where':_0x4ce478[_0xf160('0x39')]};return db[_0xf160('0x2d')][_0xf160('0x1e')](_0x494d76)[_0xf160('0x25')](function(_0x3d273d){_0x3a35b3[_0xf160('0x1e')]=_0x3d273d;if(_0x329d6b[_0xf160('0x30')]['includeAll']){_0x4ce478[_0xf160('0x41')]=[{'all':!![]}];}return db['ContactItemType'][_0xf160('0x42')](_0x4ce478);})[_0xf160('0x25')](function(_0x530fc5){_0x3a35b3[_0xf160('0x43')]=_0x530fc5;return _0x3a35b3;})[_0xf160('0x25')](respondWithFilteredResult(_0x558596,_0x4ce478))[_0xf160('0x44')](handleError(_0x558596,null));};exports[_0xf160('0x45')]=function(_0x468600,_0x43d243){var _0x1154ed={'raw':!![],'where':{'id':_0x468600[_0xf160('0x46')]['id']}},_0x4f5eba={};_0x4f5eba[_0xf160('0x2f')]=_[_0xf160('0x31')](db[_0xf160('0x2d')][_0xf160('0x47')]);_0x4f5eba[_0xf160('0x30')]=_[_0xf160('0x31')](_0x468600[_0xf160('0x30')]);_0x4f5eba[_0xf160('0x3a')]=_[_0xf160('0x32')](_0x4f5eba[_0xf160('0x2f')],_0x4f5eba['query']);_0x1154ed['attributes']=_[_0xf160('0x32')](_0x4f5eba[_0xf160('0x2f')],qs[_0xf160('0x33')](_0x468600[_0xf160('0x30')][_0xf160('0x33')]));_0x1154ed[_0xf160('0x34')]=_0x1154ed['attributes'][_0xf160('0x35')]?_0x1154ed['attributes']:_0x4f5eba['model'];if(_0x468600[_0xf160('0x30')][_0xf160('0x48')]){_0x1154ed['include']=[{'all':!![]}];}_0x1154ed=_['merge']({},_0x1154ed,_0x468600[_0xf160('0x40')]);return db[_0xf160('0x2d')][_0xf160('0x49')](_0x1154ed)[_0xf160('0x25')](handleEntityNotFound(_0x43d243,null))['then'](respondWithResult(_0x43d243,null))['catch'](handleError(_0x43d243,null));};exports['create']=function(_0x50a97d,_0x406494){return db['ContactItemType'][_0xf160('0x4a')](_0x50a97d[_0xf160('0x4b')],{})[_0xf160('0x25')](function(_0x3bdfdb){var _0x4f2e23=_0x50a97d[_0xf160('0x4c')][_0xf160('0x27')]({'plain':!![]});if(!_0x4f2e23)throw new Error(_0xf160('0x4d'));if(_0x4f2e23['role']===_0xf160('0x4c')){var _0x40f855=_0x3bdfdb[_0xf160('0x27')]({'plain':!![]});var _0x5c3cee=_0xf160('0x4e');return db[_0xf160('0x4f')][_0xf160('0x49')]({'where':{'name':_0x5c3cee,'userProfileId':_0x4f2e23[_0xf160('0x50')]},'raw':!![]})[_0xf160('0x25')](function(_0x5d0fbb){if(_0x5d0fbb&&_0x5d0fbb['autoAssociation']===0x0){return db[_0xf160('0x28')]['create']({'name':_0x40f855[_0xf160('0x2b')],'resourceId':_0x40f855['id'],'type':_0x5d0fbb[_0xf160('0x2b')],'sectionId':_0x5d0fbb['id']},{})['then'](function(){return _0x3bdfdb;});}else{return _0x3bdfdb;}})['catch'](function(_0x321309){logger[_0xf160('0x29')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x321309);throw _0x321309;});}return _0x3bdfdb;})[_0xf160('0x25')](respondWithResult(_0x406494,0xc9))[_0xf160('0x44')](handleError(_0x406494,null));};exports[_0xf160('0x24')]=function(_0x405d7e,_0x5f3ded){if(_0x405d7e[_0xf160('0x4b')]['id']){delete _0x405d7e['body']['id'];}return db[_0xf160('0x2d')][_0xf160('0x49')]({'where':{'id':_0x405d7e[_0xf160('0x46')]['id']}})[_0xf160('0x25')](handleEntityNotFound(_0x5f3ded,null))[_0xf160('0x25')](saveUpdates(_0x405d7e['body'],null))[_0xf160('0x25')](respondWithResult(_0x5f3ded,null))['catch'](handleError(_0x5f3ded,null));};exports[_0xf160('0x26')]=function(_0x1229cd,_0x39628d){return db[_0xf160('0x2d')][_0xf160('0x49')]({'where':{'id':_0x1229cd[_0xf160('0x46')]['id']}})[_0xf160('0x25')](handleEntityNotFound(_0x39628d,null))[_0xf160('0x25')](db[_0xf160('0x51')][_0xf160('0x24')]({'ItemTypeId':null},{'where':{'ItemTypeId':_0x1229cd['params']['id']}}))['then'](removeEntity(_0x39628d,null))[_0xf160('0x44')](handleError(_0x39628d,null));};