Built motion from commit ffdca78c.|2.6.24
[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 _0x5f66=['fieldName','type','key','model','query','keys','intersection','attributes','fields','hasOwnProperty','sort','where','filters','filter','merge','options','ContactItemType','include','findAll','rows','catch','show','params','rawAttributes','length','find','create','body','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','rimraf','fast-json-patch','request-promise','moment','bluebird','mustache','path','to-csv','ejs','squel','crypto','jsforce','desk.js','querystring','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/license/util','sendStatus','status','end','json','undefined','limit','count','offset','Content-Range','apply','reject','save','destroy','then','ContactItemTypes','UserProfileResource','name','send','index','map'];(function(_0xa3d64b,_0x3ba95c){var _0xdd9ef2=function(_0x3f80b2){while(--_0x3f80b2){_0xa3d64b['push'](_0xa3d64b['shift']());}};_0xdd9ef2(++_0x3ba95c);}(_0x5f66,0x1a0));var _0x65f6=function(_0xefd519,_0xc62635){_0xefd519=_0xefd519-0x0;var _0x4bb882=_0x5f66[_0xefd519];return _0x4bb882;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x65f6('0x0'));var zipdir=require('zip-dir');var jsonpatch=require(_0x65f6('0x1'));var rp=require(_0x65f6('0x2'));var moment=require(_0x65f6('0x3'));var BPromise=require(_0x65f6('0x4'));var Mustache=require(_0x65f6('0x5'));var util=require('util');var path=require(_0x65f6('0x6'));var sox=require('sox');var csv=require(_0x65f6('0x7'));var ejs=require(_0x65f6('0x8'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require('lodash');var squel=require(_0x65f6('0x9'));var crypto=require(_0x65f6('0xa'));var jsforce=require(_0x65f6('0xb'));var deskjs=require(_0x65f6('0xc'));var toCsv=require(_0x65f6('0x7'));var querystring=require(_0x65f6('0xd'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require(_0x65f6('0xe'));var qs=require(_0x65f6('0xf'));var as=require(_0x65f6('0x10'));var hardwareService=require(_0x65f6('0x11'));var logger=require('../../config/logger')(_0x65f6('0x12'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0x65f6('0x13'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x419819,_0x4862a5){_0x4862a5=_0x4862a5||0xcc;return function(_0x2f964a){if(_0x2f964a){return _0x419819[_0x65f6('0x14')](_0x4862a5);}return _0x419819[_0x65f6('0x15')](_0x4862a5)[_0x65f6('0x16')]();};}function respondWithResult(_0x369f96,_0x1d29fd){_0x1d29fd=_0x1d29fd||0xc8;return function(_0x44144b){if(_0x44144b){return _0x369f96[_0x65f6('0x15')](_0x1d29fd)[_0x65f6('0x17')](_0x44144b);}};}function respondWithFilteredResult(_0x3440af,_0x59154e){return function(_0x19b9ff){if(_0x19b9ff){var _0x49a913=typeof _0x59154e['offset']===_0x65f6('0x18')&&typeof _0x59154e[_0x65f6('0x19')]===_0x65f6('0x18');var _0x1c955c=_0x19b9ff[_0x65f6('0x1a')];var _0x1f974c=_0x49a913?0x0:_0x59154e[_0x65f6('0x1b')];var _0x18380e=_0x49a913?_0x19b9ff['count']:_0x59154e['offset']+_0x59154e[_0x65f6('0x19')];var _0x5b4055;if(_0x18380e>=_0x1c955c){_0x18380e=_0x1c955c;_0x5b4055=0xc8;}else{_0x5b4055=0xce;}_0x3440af['status'](_0x5b4055);return _0x3440af['set'](_0x65f6('0x1c'),_0x1f974c+'-'+_0x18380e+'/'+_0x1c955c)[_0x65f6('0x17')](_0x19b9ff);}return null;};}function patchUpdates(_0x5114a4){return function(_0x39e19f){try{jsonpatch[_0x65f6('0x1d')](_0x39e19f,_0x5114a4,!![]);}catch(_0xbee6a4){return BPromise[_0x65f6('0x1e')](_0xbee6a4);}return _0x39e19f[_0x65f6('0x1f')]();};}function saveUpdates(_0xc139c2,_0x581374){return function(_0x197a49){if(_0x197a49){return _0x197a49['update'](_0xc139c2)['then'](function(_0x212247){return _0x212247;});}return null;};}function removeEntity(_0xd173f4,_0x524e42){return function(_0x1eeaaa){if(_0x1eeaaa){return _0x1eeaaa[_0x65f6('0x20')]()[_0x65f6('0x21')](function(){var _0x2774a1=_0x1eeaaa['get']({'plain':!![]});var _0x9d7bb0=_0x65f6('0x22');return db[_0x65f6('0x23')]['destroy']({'where':{'type':_0x9d7bb0,'resourceId':_0x2774a1['id']}})['then'](function(){return _0x1eeaaa;});})['then'](function(){_0xd173f4[_0x65f6('0x15')](0xcc)[_0x65f6('0x16')]();});}};}function handleEntityNotFound(_0x4b2292,_0x296cd9){return function(_0x28f710){if(!_0x28f710){_0x4b2292['sendStatus'](0x194);}return _0x28f710;};}function handleError(_0x325cb4,_0x2bff2b){_0x2bff2b=_0x2bff2b||0x1f4;return function(_0x28d0df){logger['error'](_0x28d0df['stack']);if(_0x28d0df['name']){delete _0x28d0df[_0x65f6('0x24')];}_0x325cb4[_0x65f6('0x15')](_0x2bff2b)[_0x65f6('0x25')](_0x28d0df);};}exports[_0x65f6('0x26')]=function(_0x518cac,_0x14a387){var _0x144e55={},_0x4ace4c={},_0x4ea23d={'count':0x0,'rows':[]};var _0x154cbd=_[_0x65f6('0x27')](db['ContactItemType']['rawAttributes'],function(_0x10b368){return{'name':_0x10b368[_0x65f6('0x28')],'type':_0x10b368[_0x65f6('0x29')][_0x65f6('0x2a')]};});_0x4ace4c[_0x65f6('0x2b')]=_[_0x65f6('0x27')](_0x154cbd,'name');_0x4ace4c[_0x65f6('0x2c')]=_[_0x65f6('0x2d')](_0x518cac[_0x65f6('0x2c')]);_0x4ace4c['filters']=_[_0x65f6('0x2e')](_0x4ace4c[_0x65f6('0x2b')],_0x4ace4c[_0x65f6('0x2c')]);_0x144e55[_0x65f6('0x2f')]=_[_0x65f6('0x2e')](_0x4ace4c[_0x65f6('0x2b')],qs['fields'](_0x518cac[_0x65f6('0x2c')][_0x65f6('0x30')]));_0x144e55['attributes']=_0x144e55[_0x65f6('0x2f')]['length']?_0x144e55[_0x65f6('0x2f')]:_0x4ace4c['model'];if(!_0x518cac[_0x65f6('0x2c')][_0x65f6('0x31')]('nolimit')){_0x144e55[_0x65f6('0x19')]=qs[_0x65f6('0x19')](_0x518cac[_0x65f6('0x2c')]['limit']);_0x144e55['offset']=qs[_0x65f6('0x1b')](_0x518cac['query'][_0x65f6('0x1b')]);}_0x144e55['order']=qs[_0x65f6('0x32')](_0x518cac[_0x65f6('0x2c')]['sort']);_0x144e55[_0x65f6('0x33')]=qs['filters'](_['pick'](_0x518cac[_0x65f6('0x2c')],_0x4ace4c[_0x65f6('0x34')]),_0x154cbd);if(_0x518cac[_0x65f6('0x2c')][_0x65f6('0x35')]){_0x144e55[_0x65f6('0x33')]=_[_0x65f6('0x36')](_0x144e55[_0x65f6('0x33')],{'$or':_[_0x65f6('0x27')](_0x154cbd,function(_0x4f933b){if(_0x4f933b[_0x65f6('0x29')]!=='VIRTUAL'){var _0x12a589={};_0x12a589[_0x4f933b[_0x65f6('0x24')]]={'$like':'%'+_0x518cac[_0x65f6('0x2c')][_0x65f6('0x35')]+'%'};return _0x12a589;}})});}_0x144e55=_['merge']({},_0x144e55,_0x518cac[_0x65f6('0x37')]);var _0x1d6421={'where':_0x144e55[_0x65f6('0x33')]};return db[_0x65f6('0x38')][_0x65f6('0x1a')](_0x1d6421)[_0x65f6('0x21')](function(_0xed4c96){_0x4ea23d[_0x65f6('0x1a')]=_0xed4c96;if(_0x518cac[_0x65f6('0x2c')]['includeAll']){_0x144e55[_0x65f6('0x39')]=[{'all':!![]}];}return db[_0x65f6('0x38')][_0x65f6('0x3a')](_0x144e55);})[_0x65f6('0x21')](function(_0x83e2d9){_0x4ea23d[_0x65f6('0x3b')]=_0x83e2d9;return _0x4ea23d;})[_0x65f6('0x21')](respondWithFilteredResult(_0x14a387,_0x144e55))[_0x65f6('0x3c')](handleError(_0x14a387,null));};exports[_0x65f6('0x3d')]=function(_0x2abbed,_0xcf19da){var _0x2c008a={'raw':!![],'where':{'id':_0x2abbed[_0x65f6('0x3e')]['id']}},_0x6c4d94={};_0x6c4d94[_0x65f6('0x2b')]=_[_0x65f6('0x2d')](db['ContactItemType'][_0x65f6('0x3f')]);_0x6c4d94[_0x65f6('0x2c')]=_[_0x65f6('0x2d')](_0x2abbed[_0x65f6('0x2c')]);_0x6c4d94['filters']=_[_0x65f6('0x2e')](_0x6c4d94[_0x65f6('0x2b')],_0x6c4d94[_0x65f6('0x2c')]);_0x2c008a['attributes']=_[_0x65f6('0x2e')](_0x6c4d94[_0x65f6('0x2b')],qs[_0x65f6('0x30')](_0x2abbed[_0x65f6('0x2c')]['fields']));_0x2c008a[_0x65f6('0x2f')]=_0x2c008a['attributes'][_0x65f6('0x40')]?_0x2c008a[_0x65f6('0x2f')]:_0x6c4d94['model'];if(_0x2abbed[_0x65f6('0x2c')]['includeAll']){_0x2c008a[_0x65f6('0x39')]=[{'all':!![]}];}_0x2c008a=_[_0x65f6('0x36')]({},_0x2c008a,_0x2abbed[_0x65f6('0x37')]);return db[_0x65f6('0x38')][_0x65f6('0x41')](_0x2c008a)[_0x65f6('0x21')](handleEntityNotFound(_0xcf19da,null))['then'](respondWithResult(_0xcf19da,null))['catch'](handleError(_0xcf19da,null));};exports[_0x65f6('0x42')]=function(_0x15d3ba,_0x3b647a){return db[_0x65f6('0x38')][_0x65f6('0x42')](_0x15d3ba[_0x65f6('0x43')],{})[_0x65f6('0x21')](function(_0x41690c){var _0x414f49=_0x15d3ba['user'][_0x65f6('0x44')]({'plain':!![]});if(!_0x414f49)throw new Error(_0x65f6('0x45'));if(_0x414f49[_0x65f6('0x46')]==='user'){var _0x47e749=_0x41690c[_0x65f6('0x44')]({'plain':!![]});var _0x49ba5a=_0x65f6('0x22');return db[_0x65f6('0x47')]['find']({'where':{'name':_0x49ba5a,'userProfileId':_0x414f49[_0x65f6('0x48')]},'raw':!![]})['then'](function(_0x247be3){if(_0x247be3&&_0x247be3[_0x65f6('0x49')]===0x0){return db[_0x65f6('0x23')]['create']({'name':_0x47e749[_0x65f6('0x24')],'resourceId':_0x47e749['id'],'type':_0x247be3[_0x65f6('0x24')],'sectionId':_0x247be3['id']},{})['then'](function(){return _0x41690c;});}else{return _0x41690c;}})[_0x65f6('0x3c')](function(_0x34312c){logger['error'](_0x65f6('0x4a'),_0x34312c);throw _0x34312c;});}return _0x41690c;})[_0x65f6('0x21')](respondWithResult(_0x3b647a,0xc9))[_0x65f6('0x3c')](handleError(_0x3b647a,null));};exports[_0x65f6('0x4b')]=function(_0x234fa7,_0x2350ca){if(_0x234fa7[_0x65f6('0x43')]['id']){delete _0x234fa7[_0x65f6('0x43')]['id'];}return db['ContactItemType']['find']({'where':{'id':_0x234fa7['params']['id']}})[_0x65f6('0x21')](handleEntityNotFound(_0x2350ca,null))[_0x65f6('0x21')](saveUpdates(_0x234fa7[_0x65f6('0x43')],null))[_0x65f6('0x21')](respondWithResult(_0x2350ca,null))['catch'](handleError(_0x2350ca,null));};exports[_0x65f6('0x20')]=function(_0x2032b9,_0x9ff849){return db['ContactItemType'][_0x65f6('0x41')]({'where':{'id':_0x2032b9[_0x65f6('0x3e')]['id']}})[_0x65f6('0x21')](handleEntityNotFound(_0x9ff849,null))['then'](db['CmContactHasItem'][_0x65f6('0x4b')]({'ItemTypeId':null},{'where':{'ItemTypeId':_0x2032b9[_0x65f6('0x3e')]['id']}}))[_0x65f6('0x21')](removeEntity(_0x9ff849,null))[_0x65f6('0x3c')](handleError(_0x9ff849,null));};