1a1df91fcd2fc75eb899abed63f5fb0d549fab3c
[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 _0x57ca=['sort','pick','filter','where','merge','VIRTUAL','rows','params','rawAttributes','fields','length','includeAll','include','options','find','create','user','role','UserProfileSection','autoAssociation','catch','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','papaparse','ioredis','../../components/parsers/qs','../../config/license/hardware','../../config/logger','../../config/utils','../../config/license/util','../../mysqldb','status','json','offset','undefined','count','limit','set','apply','save','update','then','destroy','get','UserProfileResource','end','sendStatus','stack','name','send','index','ContactItemType','fieldName','type','key','model','map','keys','filters','intersection','attributes','query','nolimit','order'];(function(_0x286443,_0x8a8c1b){var _0x3d7a05=function(_0x14b699){while(--_0x14b699){_0x286443['push'](_0x286443['shift']());}};_0x3d7a05(++_0x8a8c1b);}(_0x57ca,0x108));var _0xa57c=function(_0x52f3fa,_0x19b92a){_0x52f3fa=_0x52f3fa-0x0;var _0x11a24c=_0x57ca[_0x52f3fa];return _0x11a24c;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0xa57c('0x0'));var zipdir=require(_0xa57c('0x1'));var jsonpatch=require('fast-json-patch');var rp=require(_0xa57c('0x2'));var moment=require(_0xa57c('0x3'));var BPromise=require(_0xa57c('0x4'));var Mustache=require(_0xa57c('0x5'));var util=require(_0xa57c('0x6'));var path=require('path');var sox=require(_0xa57c('0x7'));var csv=require(_0xa57c('0x8'));var ejs=require(_0xa57c('0x9'));var fs=require('fs');var fs_extra=require(_0xa57c('0xa'));var _=require(_0xa57c('0xb'));var squel=require(_0xa57c('0xc'));var crypto=require(_0xa57c('0xd'));var jsforce=require(_0xa57c('0xe'));var deskjs=require('desk.js');var toCsv=require(_0xa57c('0x8'));var querystring=require('querystring');var Papa=require(_0xa57c('0xf'));var Redis=require(_0xa57c('0x10'));var authService=require('../../components/auth/service');var qs=require(_0xa57c('0x11'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xa57c('0x12'));var logger=require(_0xa57c('0x13'))('api');var utils=require(_0xa57c('0x14'));var config=require('../../config/environment');var licenseUtil=require(_0xa57c('0x15'));var db=require(_0xa57c('0x16'))['db'];function respondWithStatusCode(_0x1f2bb5,_0x9e25bf){_0x9e25bf=_0x9e25bf||0xcc;return function(_0x205e72){if(_0x205e72){return _0x1f2bb5['sendStatus'](_0x9e25bf);}return _0x1f2bb5[_0xa57c('0x17')](_0x9e25bf)['end']();};}function respondWithResult(_0x30d23d,_0x3d5995){_0x3d5995=_0x3d5995||0xc8;return function(_0x30940d){if(_0x30940d){return _0x30d23d[_0xa57c('0x17')](_0x3d5995)[_0xa57c('0x18')](_0x30940d);}};}function respondWithFilteredResult(_0x37a323,_0x3f900a){return function(_0x4b4bdb){if(_0x4b4bdb){var _0x26607a=typeof _0x3f900a[_0xa57c('0x19')]===_0xa57c('0x1a')&&typeof _0x3f900a['limit']===_0xa57c('0x1a');var _0x5f33f1=_0x4b4bdb[_0xa57c('0x1b')];var _0x3d781d=_0x26607a?0x0:_0x3f900a[_0xa57c('0x19')];var _0xe3d505=_0x26607a?_0x4b4bdb[_0xa57c('0x1b')]:_0x3f900a[_0xa57c('0x19')]+_0x3f900a[_0xa57c('0x1c')];var _0x595c7a;if(_0xe3d505>=_0x5f33f1){_0xe3d505=_0x5f33f1;_0x595c7a=0xc8;}else{_0x595c7a=0xce;}_0x37a323['status'](_0x595c7a);return _0x37a323[_0xa57c('0x1d')]('Content-Range',_0x3d781d+'-'+_0xe3d505+'/'+_0x5f33f1)['json'](_0x4b4bdb);}return null;};}function patchUpdates(_0x418415){return function(_0xa5b314){try{jsonpatch[_0xa57c('0x1e')](_0xa5b314,_0x418415,!![]);}catch(_0x23bec0){return BPromise['reject'](_0x23bec0);}return _0xa5b314[_0xa57c('0x1f')]();};}function saveUpdates(_0x25715f,_0x34d1bc){return function(_0x2213b2){if(_0x2213b2){return _0x2213b2[_0xa57c('0x20')](_0x25715f)[_0xa57c('0x21')](function(_0x1e0192){return _0x1e0192;});}return null;};}function removeEntity(_0x42038e,_0x2e99ed){return function(_0xdf65dd){if(_0xdf65dd){return _0xdf65dd[_0xa57c('0x22')]()[_0xa57c('0x21')](function(){var _0x241dcc=_0xdf65dd[_0xa57c('0x23')]({'plain':!![]});var _0x431bec='ContactItemTypes';return db[_0xa57c('0x24')][_0xa57c('0x22')]({'where':{'type':_0x431bec,'resourceId':_0x241dcc['id']}})[_0xa57c('0x21')](function(){return _0xdf65dd;});})[_0xa57c('0x21')](function(){_0x42038e[_0xa57c('0x17')](0xcc)[_0xa57c('0x25')]();});}};}function handleEntityNotFound(_0x8ee1c0,_0xe50fb8){return function(_0x337f97){if(!_0x337f97){_0x8ee1c0[_0xa57c('0x26')](0x194);}return _0x337f97;};}function handleError(_0x57ebe0,_0x3b7364){_0x3b7364=_0x3b7364||0x1f4;return function(_0x4c1478){logger['error'](_0x4c1478[_0xa57c('0x27')]);if(_0x4c1478[_0xa57c('0x28')]){delete _0x4c1478['name'];}_0x57ebe0[_0xa57c('0x17')](_0x3b7364)[_0xa57c('0x29')](_0x4c1478);};}exports[_0xa57c('0x2a')]=function(_0x57565b,_0xd779f1){var _0x58d46f={},_0xf5f70c={},_0x5464f2={'count':0x0,'rows':[]};var _0x2a3bbe=_['map'](db[_0xa57c('0x2b')]['rawAttributes'],function(_0x9e02bb){return{'name':_0x9e02bb[_0xa57c('0x2c')],'type':_0x9e02bb[_0xa57c('0x2d')][_0xa57c('0x2e')]};});_0xf5f70c[_0xa57c('0x2f')]=_[_0xa57c('0x30')](_0x2a3bbe,_0xa57c('0x28'));_0xf5f70c['query']=_[_0xa57c('0x31')](_0x57565b['query']);_0xf5f70c[_0xa57c('0x32')]=_[_0xa57c('0x33')](_0xf5f70c[_0xa57c('0x2f')],_0xf5f70c['query']);_0x58d46f[_0xa57c('0x34')]=_['intersection'](_0xf5f70c[_0xa57c('0x2f')],qs['fields'](_0x57565b[_0xa57c('0x35')]['fields']));_0x58d46f[_0xa57c('0x34')]=_0x58d46f[_0xa57c('0x34')]['length']?_0x58d46f[_0xa57c('0x34')]:_0xf5f70c['model'];if(!_0x57565b[_0xa57c('0x35')]['hasOwnProperty'](_0xa57c('0x36'))){_0x58d46f[_0xa57c('0x1c')]=qs[_0xa57c('0x1c')](_0x57565b[_0xa57c('0x35')][_0xa57c('0x1c')]);_0x58d46f[_0xa57c('0x19')]=qs['offset'](_0x57565b['query'][_0xa57c('0x19')]);}_0x58d46f[_0xa57c('0x37')]=qs[_0xa57c('0x38')](_0x57565b['query'][_0xa57c('0x38')]);_0x58d46f['where']=qs[_0xa57c('0x32')](_[_0xa57c('0x39')](_0x57565b['query'],_0xf5f70c['filters']),_0x2a3bbe);if(_0x57565b['query'][_0xa57c('0x3a')]){_0x58d46f[_0xa57c('0x3b')]=_[_0xa57c('0x3c')](_0x58d46f[_0xa57c('0x3b')],{'$or':_[_0xa57c('0x30')](_0x2a3bbe,function(_0x1b98a0){if(_0x1b98a0['type']!==_0xa57c('0x3d')){var _0x1d4d2e={};_0x1d4d2e[_0x1b98a0['name']]={'$like':'%'+_0x57565b['query'][_0xa57c('0x3a')]+'%'};return _0x1d4d2e;}})});}_0x58d46f=_[_0xa57c('0x3c')]({},_0x58d46f,_0x57565b['options']);var _0x24459a={'where':_0x58d46f[_0xa57c('0x3b')]};return db[_0xa57c('0x2b')][_0xa57c('0x1b')](_0x24459a)[_0xa57c('0x21')](function(_0x43c1d7){_0x5464f2['count']=_0x43c1d7;if(_0x57565b[_0xa57c('0x35')]['includeAll']){_0x58d46f['include']=[{'all':!![]}];}return db['ContactItemType']['findAll'](_0x58d46f);})['then'](function(_0x39b20f){_0x5464f2[_0xa57c('0x3e')]=_0x39b20f;return _0x5464f2;})[_0xa57c('0x21')](respondWithFilteredResult(_0xd779f1,_0x58d46f))['catch'](handleError(_0xd779f1,null));};exports['show']=function(_0x49e072,_0x44f1e8){var _0x2755bc={'raw':!![],'where':{'id':_0x49e072[_0xa57c('0x3f')]['id']}},_0x44e38f={};_0x44e38f[_0xa57c('0x2f')]=_[_0xa57c('0x31')](db['ContactItemType'][_0xa57c('0x40')]);_0x44e38f[_0xa57c('0x35')]=_[_0xa57c('0x31')](_0x49e072[_0xa57c('0x35')]);_0x44e38f[_0xa57c('0x32')]=_[_0xa57c('0x33')](_0x44e38f['model'],_0x44e38f[_0xa57c('0x35')]);_0x2755bc[_0xa57c('0x34')]=_[_0xa57c('0x33')](_0x44e38f[_0xa57c('0x2f')],qs[_0xa57c('0x41')](_0x49e072['query'][_0xa57c('0x41')]));_0x2755bc[_0xa57c('0x34')]=_0x2755bc[_0xa57c('0x34')][_0xa57c('0x42')]?_0x2755bc[_0xa57c('0x34')]:_0x44e38f[_0xa57c('0x2f')];if(_0x49e072['query'][_0xa57c('0x43')]){_0x2755bc[_0xa57c('0x44')]=[{'all':!![]}];}_0x2755bc=_[_0xa57c('0x3c')]({},_0x2755bc,_0x49e072[_0xa57c('0x45')]);return db[_0xa57c('0x2b')][_0xa57c('0x46')](_0x2755bc)['then'](handleEntityNotFound(_0x44f1e8,null))['then'](respondWithResult(_0x44f1e8,null))['catch'](handleError(_0x44f1e8,null));};exports['create']=function(_0x232675,_0x49492f){return db[_0xa57c('0x2b')][_0xa57c('0x47')](_0x232675['body'],{})[_0xa57c('0x21')](function(_0x320a5c){var _0x368e87=_0x232675[_0xa57c('0x48')][_0xa57c('0x23')]({'plain':!![]});if(!_0x368e87)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x368e87[_0xa57c('0x49')]===_0xa57c('0x48')){var _0x4584e8=_0x320a5c[_0xa57c('0x23')]({'plain':!![]});var _0x2e4b84='ContactItemTypes';return db[_0xa57c('0x4a')]['find']({'where':{'name':_0x2e4b84,'userProfileId':_0x368e87['userProfileId']},'raw':!![]})[_0xa57c('0x21')](function(_0x2c706f){if(_0x2c706f&&_0x2c706f[_0xa57c('0x4b')]===0x0){return db['UserProfileResource']['create']({'name':_0x4584e8[_0xa57c('0x28')],'resourceId':_0x4584e8['id'],'type':_0x2c706f[_0xa57c('0x28')],'sectionId':_0x2c706f['id']},{})[_0xa57c('0x21')](function(){return _0x320a5c;});}else{return _0x320a5c;}})[_0xa57c('0x4c')](function(_0x4d2899){logger[_0xa57c('0x4d')](_0xa57c('0x4e'),_0x4d2899);throw _0x4d2899;});}return _0x320a5c;})[_0xa57c('0x21')](respondWithResult(_0x49492f,0xc9))['catch'](handleError(_0x49492f,null));};exports['update']=function(_0xbe70a6,_0x25fc6b){if(_0xbe70a6['body']['id']){delete _0xbe70a6[_0xa57c('0x4f')]['id'];}return db[_0xa57c('0x2b')][_0xa57c('0x46')]({'where':{'id':_0xbe70a6[_0xa57c('0x3f')]['id']}})[_0xa57c('0x21')](handleEntityNotFound(_0x25fc6b,null))[_0xa57c('0x21')](saveUpdates(_0xbe70a6[_0xa57c('0x4f')],null))['then'](respondWithResult(_0x25fc6b,null))['catch'](handleError(_0x25fc6b,null));};exports[_0xa57c('0x22')]=function(_0x14f3d6,_0x2fd759){return db[_0xa57c('0x2b')][_0xa57c('0x46')]({'where':{'id':_0x14f3d6[_0xa57c('0x3f')]['id']}})[_0xa57c('0x21')](handleEntityNotFound(_0x2fd759,null))[_0xa57c('0x21')](db['CmContactHasItem']['update']({'ItemTypeId':null},{'where':{'ItemTypeId':_0x14f3d6[_0xa57c('0x3f')]['id']}}))['then'](removeEntity(_0x2fd759,null))[_0xa57c('0x4c')](handleError(_0x2fd759,null));};