98a3c73b39193e7b376f4d22fdc80c5be180d711
[motion2.git] / server / api / cmList / cmList.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 _0x03de=['addContacts','CmContact','tags','isArray','each','omit','ids','push','errors','message','json','Malformed\x20request\x20syntax','getContacts','CmCustomField','CustomFields','format','cf_%d','raw','getContactsCsv','ContactItemType','OrderBy','select','cm_contacts','c.ListId\x20=\x20?','distinct','field','c.cf_','customField_','alias','item','cm_contact_has_items','CmContactId\x20=\x20c.id','additionalPhone_','description','from','ItemTypeId\x20=\x20?','description_additionalPhone_','toString','isEmpty','writeFileSync','/tmp/list_%d_%s.csv','Content-disposition','attachment;\x20filename=','list_%d_%s.csv','setHeader','text/csv','addCustomField','sequelize','transaction','addCustomFields','TEXT','text','Sequelize','number','INTEGER','defaultValue','findAndCountAll','getGlobalCustomFields','addAgents','spread','emit','userCmList:save','removeAgents','userCmList:remove','User','getAgents','eml-format','zip-dir','fast-json-patch','moment','bluebird','mustache','util','path','to-csv','ejs','lodash','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','../../mysqldb','redis','defaults','localhost','socket.io-emitter','sendStatus','status','end','limit','count','offset','set','apply','update','then','destroy','Lists','name','index','map','CmList','rawAttributes','fieldName','type','key','query','keys','intersection','model','fields','attributes','hasOwnProperty','nolimit','order','sort','where','filters','pick','filter','VIRTUAL','merge','includeAll','include','rows','catch','show','find','create','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','userProfileId','UserProfileResource','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','params','describe','body','ListId','Disposition','findOne','length','options','findAll','removeDispositions'];(function(_0x1d793b,_0x30138e){var _0x69586a=function(_0x2b439c){while(--_0x2b439c){_0x1d793b['push'](_0x1d793b['shift']());}};_0x69586a(++_0x30138e);}(_0x03de,0xde));var _0xe03d=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x03de[_0x3dd15e];return _0x231fd0;};'use strict';var emlformat=require(_0xe03d('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xe03d('0x1'));var jsonpatch=require(_0xe03d('0x2'));var rp=require('request-promise');var moment=require(_0xe03d('0x3'));var BPromise=require(_0xe03d('0x4'));var Mustache=require(_0xe03d('0x5'));var util=require(_0xe03d('0x6'));var path=require(_0xe03d('0x7'));var sox=require('sox');var csv=require(_0xe03d('0x8'));var ejs=require(_0xe03d('0x9'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xe03d('0xa'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0xe03d('0xb'));var deskjs=require(_0xe03d('0xc'));var toCsv=require('to-csv');var querystring=require(_0xe03d('0xd'));var Papa=require(_0xe03d('0xe'));var Redis=require(_0xe03d('0xf'));var authService=require(_0xe03d('0x10'));var qs=require(_0xe03d('0x11'));var as=require(_0xe03d('0x12'));var hardwareService=require(_0xe03d('0x13'));var logger=require(_0xe03d('0x14'))(_0xe03d('0x15'));var utils=require(_0xe03d('0x16'));var config=require(_0xe03d('0x17'));var licenseUtil=require('../../config/license/util');var db=require(_0xe03d('0x18'))['db'];var csv_results;config[_0xe03d('0x19')]=_[_0xe03d('0x1a')](config[_0xe03d('0x19')],{'host':_0xe03d('0x1b'),'port':0x18eb});var socket=require(_0xe03d('0x1c'))(new Redis(config[_0xe03d('0x19')]));require('./cmList.socket')['register'](socket);function respondWithStatusCode(_0xe9663d,_0x296367){_0x296367=_0x296367||0xcc;return function(_0x2c7e8c){if(_0x2c7e8c){return _0xe9663d[_0xe03d('0x1d')](_0x296367);}return _0xe9663d[_0xe03d('0x1e')](_0x296367)[_0xe03d('0x1f')]();};}function respondWithResult(_0x4e062a,_0x2045d4){_0x2045d4=_0x2045d4||0xc8;return function(_0xe089c){if(_0xe089c){return _0x4e062a[_0xe03d('0x1e')](_0x2045d4)['json'](_0xe089c);}};}function respondWithFilteredResult(_0x213cc1,_0x3deee1){return function(_0x3abdec){if(_0x3abdec){var _0x3c4f08=typeof _0x3deee1['offset']==='undefined'&&typeof _0x3deee1[_0xe03d('0x20')]==='undefined';var _0x3676de=_0x3abdec[_0xe03d('0x21')];var _0x5dac64=_0x3c4f08?0x0:_0x3deee1[_0xe03d('0x22')];var _0x41ac6f=_0x3c4f08?_0x3abdec[_0xe03d('0x21')]:_0x3deee1[_0xe03d('0x22')]+_0x3deee1[_0xe03d('0x20')];var _0x23a3dc;if(_0x41ac6f>=_0x3676de){_0x41ac6f=_0x3676de;_0x23a3dc=0xc8;}else{_0x23a3dc=0xce;}_0x213cc1[_0xe03d('0x1e')](_0x23a3dc);return _0x213cc1[_0xe03d('0x23')]('Content-Range',_0x5dac64+'-'+_0x41ac6f+'/'+_0x3676de)['json'](_0x3abdec);}return null;};}function patchUpdates(_0x5cc28d){return function(_0x5091ac){try{jsonpatch[_0xe03d('0x24')](_0x5091ac,_0x5cc28d,!![]);}catch(_0x399a9c){return BPromise['reject'](_0x399a9c);}return _0x5091ac['save']();};}function saveUpdates(_0x40b043,_0x5bcebe){return function(_0x37867e){if(_0x37867e){return _0x37867e[_0xe03d('0x25')](_0x40b043)[_0xe03d('0x26')](function(_0x30accf){return _0x30accf;});}return null;};}function removeEntity(_0x32a598,_0x5a1af7){return function(_0x2d9e7f){if(_0x2d9e7f){return _0x2d9e7f[_0xe03d('0x27')]()[_0xe03d('0x26')](function(){var _0xfed4e9=_0x2d9e7f['get']({'plain':!![]});var _0x5bbd26=_0xe03d('0x28');return db['UserProfileResource']['destroy']({'where':{'type':_0x5bbd26,'resourceId':_0xfed4e9['id']}})[_0xe03d('0x26')](function(){return _0x2d9e7f;});})['then'](function(){_0x32a598[_0xe03d('0x1e')](0xcc)[_0xe03d('0x1f')]();});}};}function handleEntityNotFound(_0x2d96b1,_0x5e418d){return function(_0x162506){if(!_0x162506){_0x2d96b1[_0xe03d('0x1d')](0x194);}return _0x162506;};}function handleError(_0x22eba2,_0x54079f){_0x54079f=_0x54079f||0x1f4;return function(_0x4bcd7a){logger['error'](_0x4bcd7a['stack']);if(_0x4bcd7a['name']){delete _0x4bcd7a[_0xe03d('0x29')];}_0x22eba2[_0xe03d('0x1e')](_0x54079f)['send'](_0x4bcd7a);};}exports[_0xe03d('0x2a')]=function(_0x5c6cd2,_0x582fe3){var _0x559fa4={},_0x258108={},_0x1b0e29={'count':0x0,'rows':[]};var _0xa153c8=_[_0xe03d('0x2b')](db[_0xe03d('0x2c')][_0xe03d('0x2d')],function(_0x3d8aab){return{'name':_0x3d8aab[_0xe03d('0x2e')],'type':_0x3d8aab[_0xe03d('0x2f')][_0xe03d('0x30')]};});_0x258108['model']=_[_0xe03d('0x2b')](_0xa153c8,_0xe03d('0x29'));_0x258108[_0xe03d('0x31')]=_[_0xe03d('0x32')](_0x5c6cd2[_0xe03d('0x31')]);_0x258108['filters']=_[_0xe03d('0x33')](_0x258108[_0xe03d('0x34')],_0x258108[_0xe03d('0x31')]);_0x559fa4['attributes']=_[_0xe03d('0x33')](_0x258108[_0xe03d('0x34')],qs[_0xe03d('0x35')](_0x5c6cd2['query'][_0xe03d('0x35')]));_0x559fa4[_0xe03d('0x36')]=_0x559fa4['attributes']['length']?_0x559fa4[_0xe03d('0x36')]:_0x258108[_0xe03d('0x34')];if(!_0x5c6cd2['query'][_0xe03d('0x37')](_0xe03d('0x38'))){_0x559fa4[_0xe03d('0x20')]=qs[_0xe03d('0x20')](_0x5c6cd2[_0xe03d('0x31')][_0xe03d('0x20')]);_0x559fa4[_0xe03d('0x22')]=qs[_0xe03d('0x22')](_0x5c6cd2[_0xe03d('0x31')][_0xe03d('0x22')]);}_0x559fa4[_0xe03d('0x39')]=qs[_0xe03d('0x3a')](_0x5c6cd2[_0xe03d('0x31')][_0xe03d('0x3a')]);_0x559fa4[_0xe03d('0x3b')]=qs[_0xe03d('0x3c')](_[_0xe03d('0x3d')](_0x5c6cd2[_0xe03d('0x31')],_0x258108[_0xe03d('0x3c')]),_0xa153c8);if(_0x5c6cd2['query'][_0xe03d('0x3e')]){_0x559fa4[_0xe03d('0x3b')]=_['merge'](_0x559fa4['where'],{'$or':_[_0xe03d('0x2b')](_0xa153c8,function(_0x34357c){if(_0x34357c['type']!==_0xe03d('0x3f')){var _0x4b7f30={};_0x4b7f30[_0x34357c[_0xe03d('0x29')]]={'$like':'%'+_0x5c6cd2['query']['filter']+'%'};return _0x4b7f30;}})});}_0x559fa4=_[_0xe03d('0x40')]({},_0x559fa4,_0x5c6cd2['options']);var _0x4ede1f={'where':_0x559fa4['where']};return db[_0xe03d('0x2c')][_0xe03d('0x21')](_0x4ede1f)[_0xe03d('0x26')](function(_0x7d01bb){_0x1b0e29['count']=_0x7d01bb;if(_0x5c6cd2[_0xe03d('0x31')][_0xe03d('0x41')]){_0x559fa4[_0xe03d('0x42')]=[{'all':!![]}];}return db[_0xe03d('0x2c')]['findAll'](_0x559fa4);})[_0xe03d('0x26')](function(_0x6e3c24){_0x1b0e29[_0xe03d('0x43')]=_0x6e3c24;return _0x1b0e29;})['then'](respondWithFilteredResult(_0x582fe3,_0x559fa4))[_0xe03d('0x44')](handleError(_0x582fe3,null));};exports[_0xe03d('0x45')]=function(_0x131640,_0x158dfb){var _0x3c4292={'raw':![],'where':{'id':_0x131640['params']['id']}},_0x2e1711={};_0x2e1711[_0xe03d('0x34')]=_[_0xe03d('0x32')](db['CmList']['rawAttributes']);_0x2e1711[_0xe03d('0x31')]=_['keys'](_0x131640['query']);_0x2e1711[_0xe03d('0x3c')]=_[_0xe03d('0x33')](_0x2e1711['model'],_0x2e1711[_0xe03d('0x31')]);_0x3c4292[_0xe03d('0x36')]=_[_0xe03d('0x33')](_0x2e1711[_0xe03d('0x34')],qs['fields'](_0x131640[_0xe03d('0x31')][_0xe03d('0x35')]));_0x3c4292[_0xe03d('0x36')]=_0x3c4292['attributes']['length']?_0x3c4292[_0xe03d('0x36')]:_0x2e1711[_0xe03d('0x34')];if(_0x131640[_0xe03d('0x31')][_0xe03d('0x41')]){_0x3c4292['include']=[{'all':!![]}];}_0x3c4292=_['merge']({},_0x3c4292,_0x131640['options']);return db['CmList'][_0xe03d('0x46')](_0x3c4292)[_0xe03d('0x26')](handleEntityNotFound(_0x158dfb,null))['then'](respondWithResult(_0x158dfb,null))['catch'](handleError(_0x158dfb,null));};exports[_0xe03d('0x47')]=function(_0x233bb7,_0x375f26){return db[_0xe03d('0x2c')][_0xe03d('0x47')](_0x233bb7['body'],{})[_0xe03d('0x26')](function(_0x16fef4){var _0x592f3b=_0x233bb7[_0xe03d('0x48')][_0xe03d('0x49')]({'plain':!![]});if(!_0x592f3b)throw new Error(_0xe03d('0x4a'));if(_0x592f3b[_0xe03d('0x4b')]===_0xe03d('0x48')){var _0x1cbfc9=_0x16fef4[_0xe03d('0x49')]({'plain':!![]});var _0x4b3f7d='Lists';return db['UserProfileSection'][_0xe03d('0x46')]({'where':{'name':_0x4b3f7d,'userProfileId':_0x592f3b[_0xe03d('0x4c')]},'raw':!![]})[_0xe03d('0x26')](function(_0x5bf438){if(_0x5bf438&&_0x5bf438['autoAssociation']===0x0){return db[_0xe03d('0x4d')][_0xe03d('0x47')]({'name':_0x1cbfc9[_0xe03d('0x29')],'resourceId':_0x1cbfc9['id'],'type':_0x5bf438[_0xe03d('0x29')],'sectionId':_0x5bf438['id']},{})[_0xe03d('0x26')](function(){return _0x16fef4;});}else{return _0x16fef4;}})[_0xe03d('0x44')](function(_0x402ab3){logger[_0xe03d('0x4e')](_0xe03d('0x4f'),_0x402ab3);throw _0x402ab3;});}return _0x16fef4;})[_0xe03d('0x26')](respondWithResult(_0x375f26,0xc9))['catch'](handleError(_0x375f26,null));};exports['update']=function(_0x719ff0,_0x3d56ce){if(_0x719ff0['body']['id']){delete _0x719ff0['body']['id'];}return db[_0xe03d('0x2c')][_0xe03d('0x46')]({'where':{'id':_0x719ff0[_0xe03d('0x50')]['id']}})[_0xe03d('0x26')](handleEntityNotFound(_0x3d56ce,null))['then'](saveUpdates(_0x719ff0['body'],null))[_0xe03d('0x26')](respondWithResult(_0x3d56ce,null))[_0xe03d('0x44')](handleError(_0x3d56ce,null));};exports['destroy']=function(_0xe6cbb5,_0x4f431a){return db[_0xe03d('0x2c')]['find']({'where':{'id':_0xe6cbb5[_0xe03d('0x50')]['id']}})[_0xe03d('0x26')](handleEntityNotFound(_0x4f431a,null))[_0xe03d('0x26')](removeEntity(_0x4f431a,null))['catch'](handleError(_0x4f431a,null));};exports[_0xe03d('0x51')]=function(_0x810ac,_0x1ba8a2){return db[_0xe03d('0x2c')][_0xe03d('0x51')]()[_0xe03d('0x26')](respondWithResult(_0x1ba8a2,null))[_0xe03d('0x44')](handleError(_0x1ba8a2,null));};exports['addDisposition']=function(_0xf0e622,_0x500896,_0x90f5fc){if(_0xf0e622[_0xe03d('0x52')]['id']){delete _0xf0e622['body']['id'];}return db['CmList'][_0xe03d('0x46')]({'where':{'id':_0xf0e622[_0xe03d('0x50')]['id']}})[_0xe03d('0x26')](handleEntityNotFound(_0x500896,null))[_0xe03d('0x26')](function(_0x547409){if(_0x547409){_0xf0e622[_0xe03d('0x52')][_0xe03d('0x53')]=_0x547409['id'];return db[_0xe03d('0x54')][_0xe03d('0x47')](_0xf0e622[_0xe03d('0x52')]);}})[_0xe03d('0x26')](respondWithResult(_0x500896,null))[_0xe03d('0x44')](handleError(_0x500896,null));};exports['getDispositions']=function(_0x52a78a,_0x28595f,_0x9de0fa){var _0x5f5026={'raw':![],'where':{}};var _0x3f3927={};var _0x54339c={'count':0x0,'rows':[]};return db[_0xe03d('0x2c')][_0xe03d('0x55')]({'where':{'id':_0x52a78a[_0xe03d('0x50')]['id']}})[_0xe03d('0x26')](handleEntityNotFound(_0x28595f,null))[_0xe03d('0x26')](function(_0x4288d1){if(_0x4288d1){_0x3f3927[_0xe03d('0x34')]=_[_0xe03d('0x32')](db['Disposition'][_0xe03d('0x2d')]);_0x3f3927[_0xe03d('0x31')]=_['keys'](_0x52a78a[_0xe03d('0x31')]);_0x3f3927['filters']=_[_0xe03d('0x33')](_0x3f3927[_0xe03d('0x34')],_0x3f3927['query']);_0x5f5026[_0xe03d('0x36')]=_[_0xe03d('0x33')](_0x3f3927['model'],qs[_0xe03d('0x35')](_0x52a78a[_0xe03d('0x31')]['fields']));_0x5f5026['attributes']=_0x5f5026['attributes'][_0xe03d('0x56')]?_0x5f5026['attributes']:_0x3f3927[_0xe03d('0x34')];if(!_0x52a78a[_0xe03d('0x31')][_0xe03d('0x37')]('nolimit')){_0x5f5026[_0xe03d('0x20')]=qs['limit'](_0x52a78a[_0xe03d('0x31')][_0xe03d('0x20')]);_0x5f5026[_0xe03d('0x22')]=qs[_0xe03d('0x22')](_0x52a78a[_0xe03d('0x31')]['offset']);}_0x5f5026[_0xe03d('0x39')]=qs[_0xe03d('0x3a')](_0x52a78a[_0xe03d('0x31')][_0xe03d('0x3a')]);_0x5f5026['where']=qs['filters'](_[_0xe03d('0x3d')](_0x52a78a[_0xe03d('0x31')],_0x3f3927[_0xe03d('0x3c')]));_0x5f5026[_0xe03d('0x3b')][_0xe03d('0x53')]=_0x4288d1['id'];if(_0x52a78a[_0xe03d('0x31')][_0xe03d('0x3e')]){_0x5f5026['where']=_[_0xe03d('0x40')](_0x5f5026['where'],{'$or':_[_0xe03d('0x2b')](_0x5f5026[_0xe03d('0x36')],function(_0x4c226f){var _0x4d1280={};_0x4d1280[_0x4c226f]={'$like':'%'+_0x52a78a[_0xe03d('0x31')][_0xe03d('0x3e')]+'%'};return _0x4d1280;})});}_0x5f5026=_[_0xe03d('0x40')]({},_0x5f5026,_0x52a78a[_0xe03d('0x57')]);return db['Disposition']['count']({'where':_0x5f5026['where']})[_0xe03d('0x26')](function(_0x184da6){_0x54339c[_0xe03d('0x21')]=_0x184da6;if(_0x52a78a['query'][_0xe03d('0x41')]){_0x5f5026['include']=[{'all':!![]}];}return db[_0xe03d('0x54')][_0xe03d('0x58')](_0x5f5026);})[_0xe03d('0x26')](function(_0x2fe98e){_0x54339c[_0xe03d('0x43')]=_0x2fe98e;return _0x54339c;});}})[_0xe03d('0x26')](respondWithFilteredResult(_0x28595f,_0x5f5026))['catch'](handleError(_0x28595f,null));};exports[_0xe03d('0x59')]=function(_0x1ec7a7,_0x7d029f,_0x478a8a){return db['CmList'][_0xe03d('0x46')]({'where':{'id':_0x1ec7a7[_0xe03d('0x50')]['id']}})[_0xe03d('0x26')](handleEntityNotFound(_0x7d029f,null))[_0xe03d('0x26')](function(_0x3b5f1d){if(_0x3b5f1d){return _0x3b5f1d[_0xe03d('0x59')](_0x1ec7a7['query']['ids']);}})[_0xe03d('0x26')](respondWithStatusCode(_0x7d029f,null))['catch'](handleError(_0x7d029f,null));};exports[_0xe03d('0x5a')]=function(_0x50798a,_0x391e66,_0x49b983){return db[_0xe03d('0x2c')][_0xe03d('0x46')]({'where':{'id':_0x50798a[_0xe03d('0x50')]['id']}})[_0xe03d('0x26')](handleEntityNotFound(_0x391e66,null))[_0xe03d('0x26')](function(_0x4c45b9){if(_0x4c45b9){var _0x8f213f=[];var _0x12023c={'success':[],'errors':[]};return db[_0xe03d('0x5b')][_0xe03d('0x51')]()[_0xe03d('0x26')](function(_0xa9c77f){if(_0x50798a[_0xe03d('0x52')]['tags']&&_['isArray'](_0x50798a[_0xe03d('0x52')][_0xe03d('0x5c')])){if(_0x50798a[_0xe03d('0x52')][_0xe03d('0x5c')][_0xe03d('0x56')]){_0x50798a[_0xe03d('0x52')][_0xe03d('0x5c')]=_0x50798a[_0xe03d('0x52')][_0xe03d('0x5c')]['join'](';')+';';}else{_0x50798a[_0xe03d('0x52')][_0xe03d('0x5c')]=null;}}else{_0x50798a['body'][_0xe03d('0x5c')]=null;}if(_0x50798a[_0xe03d('0x52')]&&_[_0xe03d('0x5d')](_0x50798a['body'])){for(var _0x3e995c=0x0;_0x3e995c<_0x50798a[_0xe03d('0x52')][_0xe03d('0x56')];_0x3e995c+=0x1){_0x50798a['body'][_0x3e995c][_0xe03d('0x53')]=_0x50798a[_0xe03d('0x50')]['id'];_0x8f213f['push'](_0x50798a[_0xe03d('0x52')][_0x3e995c]);}return BPromise[_0xe03d('0x5e')](_0x8f213f,function(_0x5d3fdd){return db[_0xe03d('0x5b')]['create'](_[_0xe03d('0x5f')](_0x5d3fdd,[_0xe03d('0x60'),'id'])||{},{'fields':_['keys'](_0xa9c77f),'raw':!![]})['then'](function(_0x3d4482){_0x12023c['success'][_0xe03d('0x61')](_0x3d4482['dataValues']);})[_0xe03d('0x44')](function(_0x5b407c){_0x12023c[_0xe03d('0x62')][_0xe03d('0x61')]({'data':_0x5d3fdd,'error':_0x5b407c[_0xe03d('0x63')]});});})[_0xe03d('0x26')](function(_0x26a944){return _0x12023c;})[_0xe03d('0x44')](function(_0x237113){return _0x237113;});}else{_0x391e66[_0xe03d('0x1e')](0x190)[_0xe03d('0x64')]({'message':_0xe03d('0x65')});}});}})[_0xe03d('0x26')](respondWithResult(_0x391e66,null))['catch'](handleError(_0x391e66,null));};exports[_0xe03d('0x66')]=function(_0x22d6ad,_0x2bbcbe,_0x7cd9f){var _0x30894b;return db[_0xe03d('0x2c')][_0xe03d('0x46')]({'where':{'id':_0x22d6ad[_0xe03d('0x50')]['id']},'include':[{'model':db[_0xe03d('0x67')],'as':_0xe03d('0x68')}]})[_0xe03d('0x26')](handleEntityNotFound(_0x2bbcbe,null))[_0xe03d('0x26')](function(_0x149255){if(_0x149255){_0x30894b=_0x149255;return db[_0xe03d('0x5b')][_0xe03d('0x2d')];}})[_0xe03d('0x26')](function(_0x4b6f01){if(_0x4b6f01){var _0x244245={};var _0x2ded2f={};_0x2ded2f[_0xe03d('0x34')]=_['concat'](_[_0xe03d('0x32')](_0x4b6f01),_[_0xe03d('0x2b')](_0x30894b['CustomFields'],function(_0x435d0e){return util[_0xe03d('0x69')](_0xe03d('0x6a'),_0x435d0e['id']);}));_0x2ded2f[_0xe03d('0x31')]=_[_0xe03d('0x32')](_0x22d6ad[_0xe03d('0x31')]);_0x2ded2f[_0xe03d('0x3c')]=_['intersection'](_0x2ded2f[_0xe03d('0x34')],_0x2ded2f['query']);_0x244245[_0xe03d('0x36')]=_[_0xe03d('0x33')](_0x2ded2f[_0xe03d('0x34')],qs[_0xe03d('0x35')](_0x22d6ad[_0xe03d('0x31')]['fields']));_0x244245[_0xe03d('0x36')]=_0x244245[_0xe03d('0x36')][_0xe03d('0x56')]?_0x244245['attributes']:_0x2ded2f[_0xe03d('0x34')];if(!_0x22d6ad[_0xe03d('0x31')][_0xe03d('0x37')](_0xe03d('0x38'))){_0x244245[_0xe03d('0x20')]=qs['limit'](_0x22d6ad['query'][_0xe03d('0x20')]);_0x244245['offset']=qs['offset'](_0x22d6ad['query'][_0xe03d('0x22')]);}_0x244245[_0xe03d('0x39')]=qs['sort'](_0x22d6ad[_0xe03d('0x31')]['sort']);_0x244245[_0xe03d('0x3b')]=qs['filters'](_['pick'](_0x22d6ad[_0xe03d('0x31')],_0x2ded2f[_0xe03d('0x3c')]));_0x244245[_0xe03d('0x6b')]=!![];if(_0x22d6ad[_0xe03d('0x31')]['filter']){_0x244245[_0xe03d('0x3b')]=_[_0xe03d('0x40')](_0x244245[_0xe03d('0x3b')],{'$or':_[_0xe03d('0x2b')](_0x244245[_0xe03d('0x36')],function(_0x2c64e5){var _0x4bf15b={};_0x4bf15b[_0x2c64e5]={'$like':'%'+_0x22d6ad['query'][_0xe03d('0x3e')]+'%'};return _0x4bf15b;})});}_0x244245=_[_0xe03d('0x40')]({},_0x244245,_0x22d6ad[_0xe03d('0x57')]);return _0x30894b[_0xe03d('0x66')](_0x244245);}})[_0xe03d('0x26')](respondWithResult(_0x2bbcbe,null))[_0xe03d('0x44')](handleError(_0x2bbcbe,null));};exports[_0xe03d('0x6c')]=function(_0x46196f,_0x431bff,_0x448e92){var _0x287e24;var _0x3ae1b6;var _0x2eaeb5;var _0x465494=_0x46196f[_0xe03d('0x50')]['id'];csv_results=[];return db[_0xe03d('0x6d')][_0xe03d('0x58')]({'attributes':['id',_0xe03d('0x29')],'raw':!![],'order':[_0xe03d('0x6e')]})[_0xe03d('0x26')](function(_0x3a7d42){_0x287e24=_0x3a7d42;var _0x574880=_['keys'](db[_0xe03d('0x5b')][_0xe03d('0x2d')]);return db[_0xe03d('0x67')][_0xe03d('0x58')]({'raw':!![],'attributes':['id','alias']})[_0xe03d('0x26')](function(_0x4acce6){_0x3ae1b6=_0x4acce6;var _0x2d5d63=squel[_0xe03d('0x6f')]()[_0xe03d('0x35')](_0x574880)['from'](_0xe03d('0x70'),'c')['where'](_0xe03d('0x71'),_0x465494)[_0xe03d('0x3b')]('c.deletedAt\x20is\x20null')[_0xe03d('0x72')]();for(var _0xdb6334=0x0;_0xdb6334<_0x3ae1b6[_0xe03d('0x56')];_0xdb6334++){_0x2d5d63[_0xe03d('0x73')](_0xe03d('0x74')+_0x3ae1b6[_0xdb6334]['id'],_0xe03d('0x75')+_0x3ae1b6[_0xdb6334][_0xe03d('0x76')]);}for(var _0xdb6334=0x0;_0xdb6334<_0x287e24[_0xe03d('0x56')];_0xdb6334++){_0x2d5d63[_0xe03d('0x73')](squel[_0xe03d('0x6f')]()['field'](_0xe03d('0x77'))['from'](_0xe03d('0x78'))[_0xe03d('0x3b')]('ItemTypeId\x20=\x20?',_0x287e24[_0xdb6334]['id'])[_0xe03d('0x3b')](_0xe03d('0x79')),_0xe03d('0x7a')+_0x287e24[_0xdb6334][_0xe03d('0x29')]);_0x2d5d63[_0xe03d('0x73')](squel[_0xe03d('0x6f')]()['field'](_0xe03d('0x7b'))[_0xe03d('0x7c')](_0xe03d('0x78'))[_0xe03d('0x3b')](_0xe03d('0x7d'),_0x287e24[_0xdb6334]['id'])[_0xe03d('0x3b')]('CmContactId\x20=\x20c.id'),_0xe03d('0x7e')+_0x287e24[_0xdb6334]['name']);}var _0x205b69=[];_0x205b69[_0xe03d('0x61')](_0x2d5d63[_0xe03d('0x7f')]());BPromise['each'](_0x205b69,handleQuery)[_0xe03d('0x26')](function(_0x378401){handleQuery(_0x2d5d63[_0xe03d('0x7f')]());var _0x32b800=csv_results[0x0];if(!_[_0xe03d('0x80')](_0x32b800)){var _0x21e873=toCsv(_0x32b800);var _0xd43937=Date['now']();fs[_0xe03d('0x81')](util[_0xe03d('0x69')](_0xe03d('0x82'),_0x465494,_0xd43937),_0x21e873);_0x431bff['setHeader'](_0xe03d('0x83'),_0xe03d('0x84')+util[_0xe03d('0x69')](_0xe03d('0x85'),_0x465494,_0xd43937));_0x431bff[_0xe03d('0x86')]('Content-type',_0xe03d('0x87'));return _0x431bff['download'](util['format'](_0xe03d('0x82'),_0x465494,_0xd43937));}else{return _0x431bff[_0xe03d('0x1d')](0xcc);}});});});};exports[_0xe03d('0x88')]=function(_0x4ac148,_0x48656f,_0x1fac86){var _0x431391;delete _0x4ac148[_0xe03d('0x52')]['id'];return db[_0xe03d('0x2c')]['findOne']({'where':{'id':_0x4ac148[_0xe03d('0x50')]['id']}})[_0xe03d('0x26')](handleEntityNotFound(_0x48656f,null))[_0xe03d('0x26')](function(_0x7ace1d){if(_0x7ace1d){return db[_0xe03d('0x89')][_0xe03d('0x8a')](function(_0x4f0530){return db[_0xe03d('0x67')][_0xe03d('0x47')](_0x4ac148[_0xe03d('0x52')],{'transaction':_0x4f0530})['then'](function(_0x28fa9b){_0x431391=_0x28fa9b;return _0x7ace1d[_0xe03d('0x8b')](_0x28fa9b,{'transaction':_0x4f0530});})[_0xe03d('0x26')](function(){var _0x2187fc={'type':db['Sequelize'][_0xe03d('0x8c')],'transaction':_0x4f0530};switch(_0x4ac148[_0xe03d('0x52')][_0xe03d('0x2f')]){case _0xe03d('0x8d'):_0x2187fc[_0xe03d('0x2f')]=db[_0xe03d('0x8e')]['TEXT'];break;case _0xe03d('0x8f'):_0x2187fc[_0xe03d('0x2f')]=db[_0xe03d('0x8e')][_0xe03d('0x90')];break;case'switch':_0x2187fc[_0xe03d('0x2f')]=db[_0xe03d('0x8e')]['BOOLEAN'];_0x2187fc[_0xe03d('0x91')]=![];break;default:}return db[_0xe03d('0x89')]['queryInterface']['addColumn']('cm_contacts',util[_0xe03d('0x69')]('cf_%s',_0x431391['id']),_0x2187fc);})[_0xe03d('0x26')](function(){return _0x431391;});});}})[_0xe03d('0x26')](respondWithResult(_0x48656f,null))[_0xe03d('0x44')](handleError(_0x48656f,null));};exports['getCustomFields']=function(_0xbad843,_0x1737a4,_0x24d54c){var _0x279872={};var _0x4b4ff7={};var _0x1214d0;var _0x3d06a0;return db[_0xe03d('0x2c')][_0xe03d('0x55')]({'where':{'id':_0xbad843[_0xe03d('0x50')]['id']}})[_0xe03d('0x26')](handleEntityNotFound(_0x1737a4,null))['then'](function(_0x55a115){if(_0x55a115){_0x1214d0=_0x55a115;_0x4b4ff7['model']=_[_0xe03d('0x32')](db[_0xe03d('0x67')][_0xe03d('0x2d')]);_0x4b4ff7[_0xe03d('0x31')]=_[_0xe03d('0x32')](_0xbad843[_0xe03d('0x31')]);_0x4b4ff7[_0xe03d('0x3c')]=_[_0xe03d('0x33')](_0x4b4ff7[_0xe03d('0x34')],_0x4b4ff7['query']);_0x279872[_0xe03d('0x36')]=_[_0xe03d('0x33')](_0x4b4ff7[_0xe03d('0x34')],qs[_0xe03d('0x35')](_0xbad843[_0xe03d('0x31')][_0xe03d('0x35')]));_0x279872[_0xe03d('0x36')]=_0x279872[_0xe03d('0x36')][_0xe03d('0x56')]?_0x279872[_0xe03d('0x36')]:_0x4b4ff7[_0xe03d('0x34')];_0x279872[_0xe03d('0x39')]=qs[_0xe03d('0x3a')](_0xbad843[_0xe03d('0x31')][_0xe03d('0x3a')]);_0x279872[_0xe03d('0x3b')]=qs[_0xe03d('0x3c')](_[_0xe03d('0x3d')](_0xbad843[_0xe03d('0x31')],_0x4b4ff7[_0xe03d('0x3c')]));_0x279872[_0xe03d('0x3b')]=_['merge'](_0x279872[_0xe03d('0x3b')],{'$or':[{'ListId':_0x55a115['id']},{'ListId':null}]});if(_0xbad843[_0xe03d('0x31')][_0xe03d('0x3e')]){_0x279872['where']=_[_0xe03d('0x40')](_0x279872[_0xe03d('0x3b')],{'$or':_[_0xe03d('0x2b')](_0x279872[_0xe03d('0x36')],function(_0x58468e){var _0x470bf3={};_0x470bf3[_0x58468e]={'$like':'%'+_0xbad843['query'][_0xe03d('0x3e')]+'%'};return _0x470bf3;})});}_0x279872=_[_0xe03d('0x40')]({},_0x279872,_0xbad843['options']);return db[_0xe03d('0x67')][_0xe03d('0x92')](_0x279872);}})[_0xe03d('0x26')](respondWithResult(_0x1737a4,null))['catch'](handleError(_0x1737a4,null));};exports[_0xe03d('0x93')]=function(_0x91ce9,_0x51a705){return db[_0xe03d('0x67')][_0xe03d('0x92')]({'where':{'ListId':null}})[_0xe03d('0x26')](handleEntityNotFound(_0x51a705,null))[_0xe03d('0x26')](function(_0x4282d5){return _0x4282d5;})[_0xe03d('0x26')](respondWithResult(_0x51a705,null))[_0xe03d('0x44')](handleError(_0x51a705,null));};exports[_0xe03d('0x94')]=function(_0x2d5fcb,_0x3e3ec8,_0x3c43d5){return db[_0xe03d('0x2c')][_0xe03d('0x46')]({'where':{'id':_0x2d5fcb[_0xe03d('0x50')]['id']}})[_0xe03d('0x26')](handleEntityNotFound(_0x3e3ec8,null))['then'](function(_0x194e9a){if(_0x194e9a){return _0x194e9a[_0xe03d('0x94')](_0x2d5fcb[_0xe03d('0x52')][_0xe03d('0x60')],_[_0xe03d('0x5f')](_0x2d5fcb[_0xe03d('0x52')],[_0xe03d('0x60'),'id'])||{})[_0xe03d('0x95')](function(_0x25c0c7){for(var _0x17cb94=0x0;_0x17cb94<_0x2d5fcb[_0xe03d('0x52')]['ids']['length'];_0x17cb94+=0x1){socket[_0xe03d('0x96')](_0xe03d('0x97'),{'UserId':Number(_0x2d5fcb[_0xe03d('0x52')][_0xe03d('0x60')][_0x17cb94]),'CmListId':Number(_0x2d5fcb[_0xe03d('0x50')]['id'])});}return _0x25c0c7;});}})[_0xe03d('0x26')](respondWithResult(_0x3e3ec8,null))[_0xe03d('0x44')](handleError(_0x3e3ec8,null));};exports[_0xe03d('0x98')]=function(_0x4a1dd1,_0x3075dc,_0x482cac){return db['CmList'][_0xe03d('0x46')]({'where':{'id':_0x4a1dd1[_0xe03d('0x50')]['id']}})[_0xe03d('0x26')](handleEntityNotFound(_0x3075dc,null))['then'](function(_0x568c9b){if(_0x568c9b){return _0x568c9b['removeAgents'](_0x4a1dd1[_0xe03d('0x31')]['ids'])[_0xe03d('0x26')](function(){if(_[_0xe03d('0x5d')](_0x4a1dd1[_0xe03d('0x31')][_0xe03d('0x60')])){for(var _0x107332=0x0;_0x107332<_0x4a1dd1[_0xe03d('0x31')]['ids'][_0xe03d('0x56')];_0x107332+=0x1){socket[_0xe03d('0x96')](_0xe03d('0x99'),{'UserId':Number(_0x4a1dd1['query'][_0xe03d('0x60')][_0x107332]),'CmListId':Number(_0x4a1dd1['params']['id'])});}}else{socket[_0xe03d('0x96')](_0xe03d('0x99'),{'UserId':Number(_0x4a1dd1[_0xe03d('0x31')]['ids']),'CmListId':Number(_0x4a1dd1[_0xe03d('0x50')]['id'])});}});}})['then'](respondWithStatusCode(_0x3075dc,null))['catch'](handleError(_0x3075dc,null));};exports['getAgents']=function(_0x538340,_0x2b289b,_0x4e2fd7){var _0x3dcc34={};var _0x129006={};var _0x3bf415;var _0x475453;return db[_0xe03d('0x2c')][_0xe03d('0x55')]({'where':{'id':_0x538340[_0xe03d('0x50')]['id']}})[_0xe03d('0x26')](handleEntityNotFound(_0x2b289b,null))['then'](function(_0x3a2392){if(_0x3a2392){_0x3bf415=_0x3a2392;_0x129006[_0xe03d('0x34')]=_[_0xe03d('0x32')](db[_0xe03d('0x9a')][_0xe03d('0x2d')]);_0x129006[_0xe03d('0x31')]=_[_0xe03d('0x32')](_0x538340[_0xe03d('0x31')]);_0x129006[_0xe03d('0x3c')]=_['intersection'](_0x129006[_0xe03d('0x34')],_0x129006['query']);_0x3dcc34[_0xe03d('0x36')]=_[_0xe03d('0x33')](_0x129006['model'],qs['fields'](_0x538340[_0xe03d('0x31')][_0xe03d('0x35')]));_0x3dcc34[_0xe03d('0x36')]=_0x3dcc34[_0xe03d('0x36')]['length']?_0x3dcc34[_0xe03d('0x36')]:_0x129006['model'];_0x3dcc34[_0xe03d('0x39')]=qs['sort'](_0x538340[_0xe03d('0x31')][_0xe03d('0x3a')]);_0x3dcc34[_0xe03d('0x3b')]=qs[_0xe03d('0x3c')](_[_0xe03d('0x3d')](_0x538340[_0xe03d('0x31')],_0x129006[_0xe03d('0x3c')]));if(_0x538340['query'][_0xe03d('0x3e')]){_0x3dcc34[_0xe03d('0x3b')]=_[_0xe03d('0x40')](_0x3dcc34[_0xe03d('0x3b')],{'$or':_[_0xe03d('0x2b')](_0x3dcc34[_0xe03d('0x36')],function(_0x440204){var _0x1f1a3c={};_0x1f1a3c[_0x440204]={'$like':'%'+_0x538340[_0xe03d('0x31')][_0xe03d('0x3e')]+'%'};return _0x1f1a3c;})});}_0x3dcc34=_['merge']({},_0x3dcc34,_0x538340[_0xe03d('0x57')]);return _0x3bf415['getAgents'](_0x3dcc34);}})['then'](function(_0x2e4be9){if(_0x2e4be9){_0x475453=_0x2e4be9[_0xe03d('0x56')];if(!_0x538340['query'][_0xe03d('0x37')](_0xe03d('0x38'))){_0x3dcc34[_0xe03d('0x20')]=qs[_0xe03d('0x20')](_0x538340['query'][_0xe03d('0x20')]);_0x3dcc34[_0xe03d('0x22')]=qs['offset'](_0x538340[_0xe03d('0x31')][_0xe03d('0x22')]);}return _0x3bf415[_0xe03d('0x9b')](_0x3dcc34);}})[_0xe03d('0x26')](function(_0x52e167){if(_0x52e167){return _0x52e167?{'count':_0x475453,'rows':_0x52e167}:null;}})[_0xe03d('0x26')](respondWithResult(_0x2b289b,null))['catch'](handleError(_0x2b289b,null));};function handleQuery(_0x246f2a){logger['info'](_0x246f2a);return db['sequelize']['query'](_0x246f2a)[_0xe03d('0x95')](function(_0x3d4144){csv_results[_0xe03d('0x61')](_0x3d4144);})[_0xe03d('0x44')](function(_0x2863fb){logger[_0xe03d('0x4e')](_0x2863fb[_0xe03d('0x63')]);});}