Built motion from commit 1fa89557.|2.6.10
[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 _0xbf84=['key','model','map','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','sort','where','pick','merge','filter','options','count','includeAll','include','findAll','catch','show','params','keys','create','body','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','find','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','describe','addDisposition','ListId','Disposition','getDispositions','findOne','order','rows','removeDispositions','addContacts','CmContact','tags','isArray','join','push','each','ids','success','errors','Malformed\x20request\x20syntax','getContacts','CmCustomField','CustomFields','concat','cf_%d','ContactItemType','select','cm_contacts','c.ListId\x20=\x20?','c.deletedAt\x20is\x20null','distinct','c.cf_','customField_','alias','field','item','cm_contact_has_items','ItemTypeId\x20=\x20?','additionalPhone_','description','from','CmContactId\x20=\x20c.id','description_additionalPhone_','toString','isEmpty','now','writeFileSync','format','/tmp/list_%d_%s.csv','setHeader','Content-disposition','attachment;\x20filename=','Content-type','text/csv','sequelize','transaction','addCustomFields','Sequelize','TEXT','text','INTEGER','switch','BOOLEAN','queryInterface','addColumn','cf_%s','getCustomFields','getGlobalCustomFields','addAgents','omit','spread','emit','userCmList:save','removeAgents','userCmList:remove','getAgents','info','message','eml-format','rimraf','zip-dir','fast-json-patch','moment','bluebird','util','path','sox','ejs','fs-extra','lodash','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../mysqldb','redis','defaults','localhost','socket.io-emitter','sendStatus','status','end','offset','undefined','limit','Content-Range','json','apply','reject','save','update','then','destroy','get','Lists','UserProfileResource','error','stack','name','index','CmList','rawAttributes','fieldName','type'];(function(_0x5aa77c,_0x1d441f){var _0x3ab08d=function(_0x49d796){while(--_0x49d796){_0x5aa77c['push'](_0x5aa77c['shift']());}};_0x3ab08d(++_0x1d441f);}(_0xbf84,0x6f));var _0x4bf8=function(_0x2f79ad,_0x3cd8af){_0x2f79ad=_0x2f79ad-0x0;var _0xb5279e=_0xbf84[_0x2f79ad];return _0xb5279e;};'use strict';var emlformat=require(_0x4bf8('0x0'));var rimraf=require(_0x4bf8('0x1'));var zipdir=require(_0x4bf8('0x2'));var jsonpatch=require(_0x4bf8('0x3'));var rp=require('request-promise');var moment=require(_0x4bf8('0x4'));var BPromise=require(_0x4bf8('0x5'));var Mustache=require('mustache');var util=require(_0x4bf8('0x6'));var path=require(_0x4bf8('0x7'));var sox=require(_0x4bf8('0x8'));var csv=require('to-csv');var ejs=require(_0x4bf8('0x9'));var fs=require('fs');var fs_extra=require(_0x4bf8('0xa'));var _=require(_0x4bf8('0xb'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0x4bf8('0xc'));var deskjs=require(_0x4bf8('0xd'));var toCsv=require('to-csv');var querystring=require(_0x4bf8('0xe'));var Papa=require('papaparse');var Redis=require(_0x4bf8('0xf'));var authService=require(_0x4bf8('0x10'));var qs=require(_0x4bf8('0x11'));var as=require(_0x4bf8('0x12'));var hardwareService=require(_0x4bf8('0x13'));var logger=require(_0x4bf8('0x14'))(_0x4bf8('0x15'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0x4bf8('0x16'))['db'];var csv_results;config[_0x4bf8('0x17')]=_[_0x4bf8('0x18')](config['redis'],{'host':_0x4bf8('0x19'),'port':0x18eb});var socket=require(_0x4bf8('0x1a'))(new Redis(config['redis']));require('./cmList.socket')['register'](socket);function respondWithStatusCode(_0x135fe8,_0x398c99){_0x398c99=_0x398c99||0xcc;return function(_0x5272d2){if(_0x5272d2){return _0x135fe8[_0x4bf8('0x1b')](_0x398c99);}return _0x135fe8[_0x4bf8('0x1c')](_0x398c99)[_0x4bf8('0x1d')]();};}function respondWithResult(_0x22cb73,_0x59c1fd){_0x59c1fd=_0x59c1fd||0xc8;return function(_0x85875c){if(_0x85875c){return _0x22cb73[_0x4bf8('0x1c')](_0x59c1fd)['json'](_0x85875c);}};}function respondWithFilteredResult(_0x1627f7,_0x4f52bc){return function(_0x3aeea3){if(_0x3aeea3){var _0x36b553=typeof _0x4f52bc[_0x4bf8('0x1e')]===_0x4bf8('0x1f')&&typeof _0x4f52bc[_0x4bf8('0x20')]===_0x4bf8('0x1f');var _0x28c431=_0x3aeea3['count'];var _0x21234c=_0x36b553?0x0:_0x4f52bc['offset'];var _0x1ff6ca=_0x36b553?_0x3aeea3['count']:_0x4f52bc[_0x4bf8('0x1e')]+_0x4f52bc[_0x4bf8('0x20')];var _0x3ad29c;if(_0x1ff6ca>=_0x28c431){_0x1ff6ca=_0x28c431;_0x3ad29c=0xc8;}else{_0x3ad29c=0xce;}_0x1627f7['status'](_0x3ad29c);return _0x1627f7['set'](_0x4bf8('0x21'),_0x21234c+'-'+_0x1ff6ca+'/'+_0x28c431)[_0x4bf8('0x22')](_0x3aeea3);}return null;};}function patchUpdates(_0x11afda){return function(_0xb3fcf){try{jsonpatch[_0x4bf8('0x23')](_0xb3fcf,_0x11afda,!![]);}catch(_0x48484f){return BPromise[_0x4bf8('0x24')](_0x48484f);}return _0xb3fcf[_0x4bf8('0x25')]();};}function saveUpdates(_0x35f2c,_0x238ae7){return function(_0x2fd449){if(_0x2fd449){return _0x2fd449[_0x4bf8('0x26')](_0x35f2c)[_0x4bf8('0x27')](function(_0x3120bf){return _0x3120bf;});}return null;};}function removeEntity(_0x3089a5,_0x227087){return function(_0x25212f){if(_0x25212f){return _0x25212f[_0x4bf8('0x28')]()['then'](function(){var _0x8209d6=_0x25212f[_0x4bf8('0x29')]({'plain':!![]});var _0x2fe194=_0x4bf8('0x2a');return db[_0x4bf8('0x2b')]['destroy']({'where':{'type':_0x2fe194,'resourceId':_0x8209d6['id']}})[_0x4bf8('0x27')](function(){return _0x25212f;});})[_0x4bf8('0x27')](function(){_0x3089a5[_0x4bf8('0x1c')](0xcc)[_0x4bf8('0x1d')]();});}};}function handleEntityNotFound(_0xdde05c,_0x3a0dad){return function(_0x4bec4a){if(!_0x4bec4a){_0xdde05c[_0x4bf8('0x1b')](0x194);}return _0x4bec4a;};}function handleError(_0x18a85f,_0x22df78){_0x22df78=_0x22df78||0x1f4;return function(_0x45340c){logger[_0x4bf8('0x2c')](_0x45340c[_0x4bf8('0x2d')]);if(_0x45340c[_0x4bf8('0x2e')]){delete _0x45340c[_0x4bf8('0x2e')];}_0x18a85f[_0x4bf8('0x1c')](_0x22df78)['send'](_0x45340c);};}exports[_0x4bf8('0x2f')]=function(_0x36a029,_0x58d402){var _0x5c7030={},_0xd8a8ba={},_0x446fd0={'count':0x0,'rows':[]};var _0x4cf6fe=_['map'](db[_0x4bf8('0x30')][_0x4bf8('0x31')],function(_0x71453b){return{'name':_0x71453b[_0x4bf8('0x32')],'type':_0x71453b[_0x4bf8('0x33')][_0x4bf8('0x34')]};});_0xd8a8ba[_0x4bf8('0x35')]=_[_0x4bf8('0x36')](_0x4cf6fe,_0x4bf8('0x2e'));_0xd8a8ba[_0x4bf8('0x37')]=_['keys'](_0x36a029[_0x4bf8('0x37')]);_0xd8a8ba[_0x4bf8('0x38')]=_[_0x4bf8('0x39')](_0xd8a8ba[_0x4bf8('0x35')],_0xd8a8ba[_0x4bf8('0x37')]);_0x5c7030[_0x4bf8('0x3a')]=_[_0x4bf8('0x39')](_0xd8a8ba['model'],qs['fields'](_0x36a029[_0x4bf8('0x37')][_0x4bf8('0x3b')]));_0x5c7030[_0x4bf8('0x3a')]=_0x5c7030[_0x4bf8('0x3a')][_0x4bf8('0x3c')]?_0x5c7030[_0x4bf8('0x3a')]:_0xd8a8ba[_0x4bf8('0x35')];if(!_0x36a029[_0x4bf8('0x37')][_0x4bf8('0x3d')](_0x4bf8('0x3e'))){_0x5c7030[_0x4bf8('0x20')]=qs[_0x4bf8('0x20')](_0x36a029[_0x4bf8('0x37')][_0x4bf8('0x20')]);_0x5c7030['offset']=qs[_0x4bf8('0x1e')](_0x36a029[_0x4bf8('0x37')][_0x4bf8('0x1e')]);}_0x5c7030['order']=qs[_0x4bf8('0x3f')](_0x36a029[_0x4bf8('0x37')]['sort']);_0x5c7030[_0x4bf8('0x40')]=qs[_0x4bf8('0x38')](_[_0x4bf8('0x41')](_0x36a029[_0x4bf8('0x37')],_0xd8a8ba[_0x4bf8('0x38')]),_0x4cf6fe);if(_0x36a029['query']['filter']){_0x5c7030[_0x4bf8('0x40')]=_[_0x4bf8('0x42')](_0x5c7030[_0x4bf8('0x40')],{'$or':_[_0x4bf8('0x36')](_0x4cf6fe,function(_0x1e035a){if(_0x1e035a[_0x4bf8('0x33')]!=='VIRTUAL'){var _0x206560={};_0x206560[_0x1e035a[_0x4bf8('0x2e')]]={'$like':'%'+_0x36a029[_0x4bf8('0x37')][_0x4bf8('0x43')]+'%'};return _0x206560;}})});}_0x5c7030=_['merge']({},_0x5c7030,_0x36a029[_0x4bf8('0x44')]);var _0x125e3c={'where':_0x5c7030[_0x4bf8('0x40')]};return db['CmList'][_0x4bf8('0x45')](_0x125e3c)[_0x4bf8('0x27')](function(_0x5aa05a){_0x446fd0[_0x4bf8('0x45')]=_0x5aa05a;if(_0x36a029[_0x4bf8('0x37')][_0x4bf8('0x46')]){_0x5c7030[_0x4bf8('0x47')]=[{'all':!![]}];}return db[_0x4bf8('0x30')][_0x4bf8('0x48')](_0x5c7030);})['then'](function(_0x1a49b9){_0x446fd0['rows']=_0x1a49b9;return _0x446fd0;})['then'](respondWithFilteredResult(_0x58d402,_0x5c7030))[_0x4bf8('0x49')](handleError(_0x58d402,null));};exports[_0x4bf8('0x4a')]=function(_0x384b8b,_0x39308d){var _0x25a8ae={'raw':![],'where':{'id':_0x384b8b[_0x4bf8('0x4b')]['id']}},_0x409c8a={};_0x409c8a[_0x4bf8('0x35')]=_[_0x4bf8('0x4c')](db[_0x4bf8('0x30')]['rawAttributes']);_0x409c8a[_0x4bf8('0x37')]=_[_0x4bf8('0x4c')](_0x384b8b[_0x4bf8('0x37')]);_0x409c8a[_0x4bf8('0x38')]=_[_0x4bf8('0x39')](_0x409c8a[_0x4bf8('0x35')],_0x409c8a[_0x4bf8('0x37')]);_0x25a8ae['attributes']=_['intersection'](_0x409c8a[_0x4bf8('0x35')],qs[_0x4bf8('0x3b')](_0x384b8b['query'][_0x4bf8('0x3b')]));_0x25a8ae[_0x4bf8('0x3a')]=_0x25a8ae[_0x4bf8('0x3a')][_0x4bf8('0x3c')]?_0x25a8ae[_0x4bf8('0x3a')]:_0x409c8a['model'];if(_0x384b8b[_0x4bf8('0x37')][_0x4bf8('0x46')]){_0x25a8ae[_0x4bf8('0x47')]=[{'all':!![]}];}_0x25a8ae=_[_0x4bf8('0x42')]({},_0x25a8ae,_0x384b8b[_0x4bf8('0x44')]);return db['CmList']['find'](_0x25a8ae)[_0x4bf8('0x27')](handleEntityNotFound(_0x39308d,null))[_0x4bf8('0x27')](respondWithResult(_0x39308d,null))[_0x4bf8('0x49')](handleError(_0x39308d,null));};exports[_0x4bf8('0x4d')]=function(_0x59e730,_0x15edf1){return db[_0x4bf8('0x30')][_0x4bf8('0x4d')](_0x59e730[_0x4bf8('0x4e')],{})['then'](function(_0x43d728){var _0x4eb636=_0x59e730['user'][_0x4bf8('0x29')]({'plain':!![]});if(!_0x4eb636)throw new Error(_0x4bf8('0x4f'));if(_0x4eb636['role']==='user'){var _0x380cee=_0x43d728[_0x4bf8('0x29')]({'plain':!![]});var _0x28f029=_0x4bf8('0x2a');return db[_0x4bf8('0x50')][_0x4bf8('0x51')]({'where':{'name':_0x28f029,'userProfileId':_0x4eb636[_0x4bf8('0x52')]},'raw':!![]})[_0x4bf8('0x27')](function(_0xaeb573){if(_0xaeb573&&_0xaeb573[_0x4bf8('0x53')]===0x0){return db[_0x4bf8('0x2b')][_0x4bf8('0x4d')]({'name':_0x380cee[_0x4bf8('0x2e')],'resourceId':_0x380cee['id'],'type':_0xaeb573[_0x4bf8('0x2e')],'sectionId':_0xaeb573['id']},{})[_0x4bf8('0x27')](function(){return _0x43d728;});}else{return _0x43d728;}})[_0x4bf8('0x49')](function(_0x2386a3){logger['error'](_0x4bf8('0x54'),_0x2386a3);throw _0x2386a3;});}return _0x43d728;})[_0x4bf8('0x27')](respondWithResult(_0x15edf1,0xc9))[_0x4bf8('0x49')](handleError(_0x15edf1,null));};exports['update']=function(_0x4cc260,_0x26b857){if(_0x4cc260[_0x4bf8('0x4e')]['id']){delete _0x4cc260[_0x4bf8('0x4e')]['id'];}return db['CmList']['find']({'where':{'id':_0x4cc260[_0x4bf8('0x4b')]['id']}})[_0x4bf8('0x27')](handleEntityNotFound(_0x26b857,null))[_0x4bf8('0x27')](saveUpdates(_0x4cc260['body'],null))[_0x4bf8('0x27')](respondWithResult(_0x26b857,null))[_0x4bf8('0x49')](handleError(_0x26b857,null));};exports[_0x4bf8('0x28')]=function(_0x38d0e2,_0x40eb51){return db[_0x4bf8('0x30')][_0x4bf8('0x51')]({'where':{'id':_0x38d0e2[_0x4bf8('0x4b')]['id']}})[_0x4bf8('0x27')](handleEntityNotFound(_0x40eb51,null))[_0x4bf8('0x27')](removeEntity(_0x40eb51,null))[_0x4bf8('0x49')](handleError(_0x40eb51,null));};exports[_0x4bf8('0x55')]=function(_0xf71a75,_0x56f4c4){return db['CmList'][_0x4bf8('0x55')]()[_0x4bf8('0x27')](respondWithResult(_0x56f4c4,null))[_0x4bf8('0x49')](handleError(_0x56f4c4,null));};exports[_0x4bf8('0x56')]=function(_0x2eb81a,_0x7f7280,_0x5bedd5){if(_0x2eb81a[_0x4bf8('0x4e')]['id']){delete _0x2eb81a[_0x4bf8('0x4e')]['id'];}return db[_0x4bf8('0x30')][_0x4bf8('0x51')]({'where':{'id':_0x2eb81a['params']['id']}})[_0x4bf8('0x27')](handleEntityNotFound(_0x7f7280,null))[_0x4bf8('0x27')](function(_0x45bf48){if(_0x45bf48){_0x2eb81a[_0x4bf8('0x4e')][_0x4bf8('0x57')]=_0x45bf48['id'];return db[_0x4bf8('0x58')][_0x4bf8('0x4d')](_0x2eb81a['body']);}})[_0x4bf8('0x27')](respondWithResult(_0x7f7280,null))['catch'](handleError(_0x7f7280,null));};exports[_0x4bf8('0x59')]=function(_0x1bd655,_0x3d7fdb,_0x281cbc){var _0x4a4ae4={'raw':![],'where':{}};var _0x1f0b8c={};var _0x5c7a9d={'count':0x0,'rows':[]};return db['CmList'][_0x4bf8('0x5a')]({'where':{'id':_0x1bd655[_0x4bf8('0x4b')]['id']}})[_0x4bf8('0x27')](handleEntityNotFound(_0x3d7fdb,null))['then'](function(_0x2a1c3a){if(_0x2a1c3a){_0x1f0b8c[_0x4bf8('0x35')]=_['keys'](db[_0x4bf8('0x58')][_0x4bf8('0x31')]);_0x1f0b8c[_0x4bf8('0x37')]=_[_0x4bf8('0x4c')](_0x1bd655['query']);_0x1f0b8c[_0x4bf8('0x38')]=_['intersection'](_0x1f0b8c[_0x4bf8('0x35')],_0x1f0b8c[_0x4bf8('0x37')]);_0x4a4ae4[_0x4bf8('0x3a')]=_[_0x4bf8('0x39')](_0x1f0b8c[_0x4bf8('0x35')],qs[_0x4bf8('0x3b')](_0x1bd655[_0x4bf8('0x37')][_0x4bf8('0x3b')]));_0x4a4ae4['attributes']=_0x4a4ae4[_0x4bf8('0x3a')][_0x4bf8('0x3c')]?_0x4a4ae4[_0x4bf8('0x3a')]:_0x1f0b8c['model'];if(!_0x1bd655[_0x4bf8('0x37')]['hasOwnProperty']('nolimit')){_0x4a4ae4[_0x4bf8('0x20')]=qs[_0x4bf8('0x20')](_0x1bd655[_0x4bf8('0x37')][_0x4bf8('0x20')]);_0x4a4ae4[_0x4bf8('0x1e')]=qs[_0x4bf8('0x1e')](_0x1bd655[_0x4bf8('0x37')]['offset']);}_0x4a4ae4[_0x4bf8('0x5b')]=qs[_0x4bf8('0x3f')](_0x1bd655[_0x4bf8('0x37')][_0x4bf8('0x3f')]);_0x4a4ae4[_0x4bf8('0x40')]=qs[_0x4bf8('0x38')](_[_0x4bf8('0x41')](_0x1bd655[_0x4bf8('0x37')],_0x1f0b8c[_0x4bf8('0x38')]));_0x4a4ae4[_0x4bf8('0x40')][_0x4bf8('0x57')]=_0x2a1c3a['id'];if(_0x1bd655[_0x4bf8('0x37')][_0x4bf8('0x43')]){_0x4a4ae4[_0x4bf8('0x40')]=_[_0x4bf8('0x42')](_0x4a4ae4[_0x4bf8('0x40')],{'$or':_['map'](_0x4a4ae4[_0x4bf8('0x3a')],function(_0x4a9284){var _0x224bd5={};_0x224bd5[_0x4a9284]={'$like':'%'+_0x1bd655[_0x4bf8('0x37')][_0x4bf8('0x43')]+'%'};return _0x224bd5;})});}_0x4a4ae4=_[_0x4bf8('0x42')]({},_0x4a4ae4,_0x1bd655[_0x4bf8('0x44')]);return db[_0x4bf8('0x58')][_0x4bf8('0x45')]({'where':_0x4a4ae4[_0x4bf8('0x40')]})[_0x4bf8('0x27')](function(_0x1692db){_0x5c7a9d[_0x4bf8('0x45')]=_0x1692db;if(_0x1bd655[_0x4bf8('0x37')]['includeAll']){_0x4a4ae4[_0x4bf8('0x47')]=[{'all':!![]}];}return db[_0x4bf8('0x58')][_0x4bf8('0x48')](_0x4a4ae4);})[_0x4bf8('0x27')](function(_0x3b2fbf){_0x5c7a9d[_0x4bf8('0x5c')]=_0x3b2fbf;return _0x5c7a9d;});}})['then'](respondWithFilteredResult(_0x3d7fdb,_0x4a4ae4))[_0x4bf8('0x49')](handleError(_0x3d7fdb,null));};exports['removeDispositions']=function(_0x46439e,_0xf798f7,_0x2a2719){return db[_0x4bf8('0x30')][_0x4bf8('0x51')]({'where':{'id':_0x46439e[_0x4bf8('0x4b')]['id']}})[_0x4bf8('0x27')](handleEntityNotFound(_0xf798f7,null))[_0x4bf8('0x27')](function(_0x23b3a0){if(_0x23b3a0){return _0x23b3a0[_0x4bf8('0x5d')](_0x46439e[_0x4bf8('0x37')]['ids']);}})[_0x4bf8('0x27')](respondWithStatusCode(_0xf798f7,null))[_0x4bf8('0x49')](handleError(_0xf798f7,null));};exports[_0x4bf8('0x5e')]=function(_0x200a50,_0x4d2a83,_0x5421e5){return db[_0x4bf8('0x30')][_0x4bf8('0x51')]({'where':{'id':_0x200a50[_0x4bf8('0x4b')]['id']}})[_0x4bf8('0x27')](handleEntityNotFound(_0x4d2a83,null))[_0x4bf8('0x27')](function(_0x1f1e82){if(_0x1f1e82){var _0x31e335=[];var _0x13dfb0={'success':[],'errors':[]};return db[_0x4bf8('0x5f')][_0x4bf8('0x55')]()[_0x4bf8('0x27')](function(_0x150aaa){if(_0x200a50[_0x4bf8('0x4e')][_0x4bf8('0x60')]&&_[_0x4bf8('0x61')](_0x200a50['body']['tags'])){if(_0x200a50[_0x4bf8('0x4e')]['tags'][_0x4bf8('0x3c')]){_0x200a50[_0x4bf8('0x4e')][_0x4bf8('0x60')]=_0x200a50[_0x4bf8('0x4e')][_0x4bf8('0x60')][_0x4bf8('0x62')](';')+';';}else{_0x200a50[_0x4bf8('0x4e')][_0x4bf8('0x60')]=null;}}else{_0x200a50['body']['tags']=null;}if(_0x200a50[_0x4bf8('0x4e')]&&_[_0x4bf8('0x61')](_0x200a50['body'])){for(var _0x4dd5c8=0x0;_0x4dd5c8<_0x200a50[_0x4bf8('0x4e')]['length'];_0x4dd5c8+=0x1){_0x200a50[_0x4bf8('0x4e')][_0x4dd5c8][_0x4bf8('0x57')]=_0x200a50['params']['id'];_0x31e335[_0x4bf8('0x63')](_0x200a50[_0x4bf8('0x4e')][_0x4dd5c8]);}return BPromise[_0x4bf8('0x64')](_0x31e335,function(_0xf825f){return db[_0x4bf8('0x5f')][_0x4bf8('0x4d')](_['omit'](_0xf825f,[_0x4bf8('0x65'),'id'])||{},{'fields':_['keys'](_0x150aaa),'raw':!![]})['then'](function(_0xdfc008){_0x13dfb0[_0x4bf8('0x66')]['push'](_0xdfc008['dataValues']);})[_0x4bf8('0x49')](function(_0x31caf2){_0x13dfb0[_0x4bf8('0x67')][_0x4bf8('0x63')]({'data':_0xf825f,'error':_0x31caf2['message']});});})[_0x4bf8('0x27')](function(_0x1cdea4){return _0x13dfb0;})['catch'](function(_0x4c5cd4){return _0x4c5cd4;});}else{_0x4d2a83[_0x4bf8('0x1c')](0x190)[_0x4bf8('0x22')]({'message':_0x4bf8('0x68')});}});}})[_0x4bf8('0x27')](respondWithResult(_0x4d2a83,null))[_0x4bf8('0x49')](handleError(_0x4d2a83,null));};exports[_0x4bf8('0x69')]=function(_0x4981ea,_0x38259c,_0x2eee4d){var _0x29f169;return db[_0x4bf8('0x30')]['find']({'where':{'id':_0x4981ea['params']['id']},'include':[{'model':db[_0x4bf8('0x6a')],'as':_0x4bf8('0x6b')}]})[_0x4bf8('0x27')](handleEntityNotFound(_0x38259c,null))[_0x4bf8('0x27')](function(_0x4af260){if(_0x4af260){_0x29f169=_0x4af260;return db[_0x4bf8('0x5f')][_0x4bf8('0x31')];}})['then'](function(_0x4857e9){if(_0x4857e9){var _0x4ddebc={};var _0x5c41dc={};_0x5c41dc[_0x4bf8('0x35')]=_[_0x4bf8('0x6c')](_[_0x4bf8('0x4c')](_0x4857e9),_[_0x4bf8('0x36')](_0x29f169[_0x4bf8('0x6b')],function(_0x1b9c3c){return util['format'](_0x4bf8('0x6d'),_0x1b9c3c['id']);}));_0x5c41dc[_0x4bf8('0x37')]=_[_0x4bf8('0x4c')](_0x4981ea[_0x4bf8('0x37')]);_0x5c41dc[_0x4bf8('0x38')]=_['intersection'](_0x5c41dc[_0x4bf8('0x35')],_0x5c41dc[_0x4bf8('0x37')]);_0x4ddebc[_0x4bf8('0x3a')]=_[_0x4bf8('0x39')](_0x5c41dc[_0x4bf8('0x35')],qs[_0x4bf8('0x3b')](_0x4981ea[_0x4bf8('0x37')][_0x4bf8('0x3b')]));_0x4ddebc[_0x4bf8('0x3a')]=_0x4ddebc[_0x4bf8('0x3a')][_0x4bf8('0x3c')]?_0x4ddebc['attributes']:_0x5c41dc[_0x4bf8('0x35')];if(!_0x4981ea[_0x4bf8('0x37')][_0x4bf8('0x3d')](_0x4bf8('0x3e'))){_0x4ddebc[_0x4bf8('0x20')]=qs[_0x4bf8('0x20')](_0x4981ea['query'][_0x4bf8('0x20')]);_0x4ddebc[_0x4bf8('0x1e')]=qs[_0x4bf8('0x1e')](_0x4981ea[_0x4bf8('0x37')][_0x4bf8('0x1e')]);}_0x4ddebc[_0x4bf8('0x5b')]=qs[_0x4bf8('0x3f')](_0x4981ea[_0x4bf8('0x37')][_0x4bf8('0x3f')]);_0x4ddebc[_0x4bf8('0x40')]=qs['filters'](_[_0x4bf8('0x41')](_0x4981ea[_0x4bf8('0x37')],_0x5c41dc[_0x4bf8('0x38')]));_0x4ddebc['raw']=!![];if(_0x4981ea[_0x4bf8('0x37')][_0x4bf8('0x43')]){_0x4ddebc[_0x4bf8('0x40')]=_[_0x4bf8('0x42')](_0x4ddebc[_0x4bf8('0x40')],{'$or':_['map'](_0x4ddebc[_0x4bf8('0x3a')],function(_0x3054cf){var _0x529ec0={};_0x529ec0[_0x3054cf]={'$like':'%'+_0x4981ea[_0x4bf8('0x37')][_0x4bf8('0x43')]+'%'};return _0x529ec0;})});}_0x4ddebc=_[_0x4bf8('0x42')]({},_0x4ddebc,_0x4981ea[_0x4bf8('0x44')]);return _0x29f169['getContacts'](_0x4ddebc);}})[_0x4bf8('0x27')](respondWithResult(_0x38259c,null))[_0x4bf8('0x49')](handleError(_0x38259c,null));};exports['getContactsCsv']=function(_0x1dbc75,_0x4eb694,_0x25d01b){var _0x3bafb0;var _0x277b84;var _0x28c5e3;var _0x13d3bd=_0x1dbc75[_0x4bf8('0x4b')]['id'];csv_results=[];return db[_0x4bf8('0x6e')][_0x4bf8('0x48')]({'attributes':['id',_0x4bf8('0x2e')],'raw':!![],'order':['OrderBy']})[_0x4bf8('0x27')](function(_0x1a429f){_0x3bafb0=_0x1a429f;var _0x25aeab=_['keys'](db[_0x4bf8('0x5f')][_0x4bf8('0x31')]);return db[_0x4bf8('0x6a')][_0x4bf8('0x48')]({'raw':!![],'attributes':['id','alias']})['then'](function(_0x545a70){_0x277b84=_0x545a70;var _0x3f34ac=squel[_0x4bf8('0x6f')]()['fields'](_0x25aeab)['from'](_0x4bf8('0x70'),'c')['where'](_0x4bf8('0x71'),_0x13d3bd)[_0x4bf8('0x40')](_0x4bf8('0x72'))[_0x4bf8('0x73')]();for(var _0x3af201=0x0;_0x3af201<_0x277b84[_0x4bf8('0x3c')];_0x3af201++){_0x3f34ac['field'](_0x4bf8('0x74')+_0x277b84[_0x3af201]['id'],_0x4bf8('0x75')+_0x277b84[_0x3af201][_0x4bf8('0x76')]);}for(var _0x3af201=0x0;_0x3af201<_0x3bafb0[_0x4bf8('0x3c')];_0x3af201++){_0x3f34ac[_0x4bf8('0x77')](squel[_0x4bf8('0x6f')]()[_0x4bf8('0x77')](_0x4bf8('0x78'))['from'](_0x4bf8('0x79'))[_0x4bf8('0x40')](_0x4bf8('0x7a'),_0x3bafb0[_0x3af201]['id'])[_0x4bf8('0x40')]('CmContactId\x20=\x20c.id'),_0x4bf8('0x7b')+_0x3bafb0[_0x3af201][_0x4bf8('0x2e')]);_0x3f34ac[_0x4bf8('0x77')](squel[_0x4bf8('0x6f')]()[_0x4bf8('0x77')](_0x4bf8('0x7c'))[_0x4bf8('0x7d')](_0x4bf8('0x79'))[_0x4bf8('0x40')]('ItemTypeId\x20=\x20?',_0x3bafb0[_0x3af201]['id'])[_0x4bf8('0x40')](_0x4bf8('0x7e')),_0x4bf8('0x7f')+_0x3bafb0[_0x3af201][_0x4bf8('0x2e')]);}var _0x1090d0=[];_0x1090d0[_0x4bf8('0x63')](_0x3f34ac[_0x4bf8('0x80')]());BPromise[_0x4bf8('0x64')](_0x1090d0,handleQuery)[_0x4bf8('0x27')](function(_0x57911f){handleQuery(_0x3f34ac[_0x4bf8('0x80')]());var _0x12f9c0=csv_results[0x0];if(!_[_0x4bf8('0x81')](_0x12f9c0)){var _0x46795b=toCsv(_0x12f9c0);var _0x2142e3=Date[_0x4bf8('0x82')]();fs[_0x4bf8('0x83')](util[_0x4bf8('0x84')](_0x4bf8('0x85'),_0x13d3bd,_0x2142e3),_0x46795b);_0x4eb694[_0x4bf8('0x86')](_0x4bf8('0x87'),_0x4bf8('0x88')+util[_0x4bf8('0x84')]('list_%d_%s.csv',_0x13d3bd,_0x2142e3));_0x4eb694['setHeader'](_0x4bf8('0x89'),_0x4bf8('0x8a'));return _0x4eb694['download'](util[_0x4bf8('0x84')](_0x4bf8('0x85'),_0x13d3bd,_0x2142e3));}else{return _0x4eb694[_0x4bf8('0x1b')](0xcc);}});});});};exports['addCustomField']=function(_0x53958b,_0x5ea51d,_0x3270d6){var _0x3b9bae;delete _0x53958b[_0x4bf8('0x4e')]['id'];return db[_0x4bf8('0x30')][_0x4bf8('0x5a')]({'where':{'id':_0x53958b[_0x4bf8('0x4b')]['id']}})[_0x4bf8('0x27')](handleEntityNotFound(_0x5ea51d,null))[_0x4bf8('0x27')](function(_0x5ac9fb){if(_0x5ac9fb){return db[_0x4bf8('0x8b')][_0x4bf8('0x8c')](function(_0x270309){return db[_0x4bf8('0x6a')][_0x4bf8('0x4d')](_0x53958b['body'],{'transaction':_0x270309})[_0x4bf8('0x27')](function(_0x345c39){_0x3b9bae=_0x345c39;return _0x5ac9fb[_0x4bf8('0x8d')](_0x345c39,{'transaction':_0x270309});})[_0x4bf8('0x27')](function(){var _0x5eb608={'type':db[_0x4bf8('0x8e')][_0x4bf8('0x8f')],'transaction':_0x270309};switch(_0x53958b[_0x4bf8('0x4e')]['type']){case _0x4bf8('0x90'):_0x5eb608[_0x4bf8('0x33')]=db[_0x4bf8('0x8e')][_0x4bf8('0x8f')];break;case'number':_0x5eb608[_0x4bf8('0x33')]=db[_0x4bf8('0x8e')][_0x4bf8('0x91')];break;case _0x4bf8('0x92'):_0x5eb608[_0x4bf8('0x33')]=db[_0x4bf8('0x8e')][_0x4bf8('0x93')];_0x5eb608['defaultValue']=![];break;default:}return db[_0x4bf8('0x8b')][_0x4bf8('0x94')][_0x4bf8('0x95')]('cm_contacts',util['format'](_0x4bf8('0x96'),_0x3b9bae['id']),_0x5eb608);})[_0x4bf8('0x27')](function(){return _0x3b9bae;});});}})['then'](respondWithResult(_0x5ea51d,null))[_0x4bf8('0x49')](handleError(_0x5ea51d,null));};exports[_0x4bf8('0x97')]=function(_0x49aca5,_0x1908bc,_0x571789){var _0x5a9e73={};var _0x5bebeb={};var _0x13d504;var _0x4309d8;return db[_0x4bf8('0x30')]['findOne']({'where':{'id':_0x49aca5[_0x4bf8('0x4b')]['id']}})['then'](handleEntityNotFound(_0x1908bc,null))['then'](function(_0x2fd53f){if(_0x2fd53f){_0x13d504=_0x2fd53f;_0x5bebeb[_0x4bf8('0x35')]=_['keys'](db[_0x4bf8('0x6a')][_0x4bf8('0x31')]);_0x5bebeb['query']=_['keys'](_0x49aca5[_0x4bf8('0x37')]);_0x5bebeb[_0x4bf8('0x38')]=_[_0x4bf8('0x39')](_0x5bebeb[_0x4bf8('0x35')],_0x5bebeb['query']);_0x5a9e73[_0x4bf8('0x3a')]=_[_0x4bf8('0x39')](_0x5bebeb[_0x4bf8('0x35')],qs[_0x4bf8('0x3b')](_0x49aca5[_0x4bf8('0x37')][_0x4bf8('0x3b')]));_0x5a9e73['attributes']=_0x5a9e73[_0x4bf8('0x3a')][_0x4bf8('0x3c')]?_0x5a9e73['attributes']:_0x5bebeb[_0x4bf8('0x35')];_0x5a9e73[_0x4bf8('0x5b')]=qs['sort'](_0x49aca5['query'][_0x4bf8('0x3f')]);_0x5a9e73[_0x4bf8('0x40')]=qs[_0x4bf8('0x38')](_[_0x4bf8('0x41')](_0x49aca5[_0x4bf8('0x37')],_0x5bebeb[_0x4bf8('0x38')]));_0x5a9e73[_0x4bf8('0x40')]=_['merge'](_0x5a9e73[_0x4bf8('0x40')],{'$or':[{'ListId':_0x2fd53f['id']},{'ListId':null}]});if(_0x49aca5[_0x4bf8('0x37')][_0x4bf8('0x43')]){_0x5a9e73['where']=_[_0x4bf8('0x42')](_0x5a9e73[_0x4bf8('0x40')],{'$or':_[_0x4bf8('0x36')](_0x5a9e73['attributes'],function(_0x235eee){var _0x2c9d1b={};_0x2c9d1b[_0x235eee]={'$like':'%'+_0x49aca5['query'][_0x4bf8('0x43')]+'%'};return _0x2c9d1b;})});}_0x5a9e73=_[_0x4bf8('0x42')]({},_0x5a9e73,_0x49aca5[_0x4bf8('0x44')]);return db[_0x4bf8('0x6a')]['findAndCountAll'](_0x5a9e73);}})[_0x4bf8('0x27')](respondWithResult(_0x1908bc,null))[_0x4bf8('0x49')](handleError(_0x1908bc,null));};exports[_0x4bf8('0x98')]=function(_0x95e88a,_0x4e4006){return db['CmCustomField']['findAndCountAll']({'where':{'ListId':null}})['then'](handleEntityNotFound(_0x4e4006,null))[_0x4bf8('0x27')](function(_0x3d2346){return _0x3d2346;})[_0x4bf8('0x27')](respondWithResult(_0x4e4006,null))[_0x4bf8('0x49')](handleError(_0x4e4006,null));};exports['addAgents']=function(_0x12a414,_0x3d129c,_0x1e227f){return db[_0x4bf8('0x30')][_0x4bf8('0x51')]({'where':{'id':_0x12a414[_0x4bf8('0x4b')]['id']}})['then'](handleEntityNotFound(_0x3d129c,null))[_0x4bf8('0x27')](function(_0x1894ae){if(_0x1894ae){return _0x1894ae[_0x4bf8('0x99')](_0x12a414['body']['ids'],_[_0x4bf8('0x9a')](_0x12a414[_0x4bf8('0x4e')],[_0x4bf8('0x65'),'id'])||{})[_0x4bf8('0x9b')](function(_0x2530f9){for(var _0x6e200d=0x0;_0x6e200d<_0x12a414[_0x4bf8('0x4e')][_0x4bf8('0x65')][_0x4bf8('0x3c')];_0x6e200d+=0x1){socket[_0x4bf8('0x9c')](_0x4bf8('0x9d'),{'UserId':Number(_0x12a414[_0x4bf8('0x4e')]['ids'][_0x6e200d]),'CmListId':Number(_0x12a414['params']['id'])});}return _0x2530f9;});}})[_0x4bf8('0x27')](respondWithResult(_0x3d129c,null))['catch'](handleError(_0x3d129c,null));};exports[_0x4bf8('0x9e')]=function(_0x5e8fce,_0x2c0887,_0x4d0759){return db['CmList'][_0x4bf8('0x51')]({'where':{'id':_0x5e8fce[_0x4bf8('0x4b')]['id']}})[_0x4bf8('0x27')](handleEntityNotFound(_0x2c0887,null))['then'](function(_0x5d9c78){if(_0x5d9c78){return _0x5d9c78['removeAgents'](_0x5e8fce[_0x4bf8('0x37')][_0x4bf8('0x65')])[_0x4bf8('0x27')](function(){if(_['isArray'](_0x5e8fce[_0x4bf8('0x37')]['ids'])){for(var _0x4e2040=0x0;_0x4e2040<_0x5e8fce[_0x4bf8('0x37')][_0x4bf8('0x65')][_0x4bf8('0x3c')];_0x4e2040+=0x1){socket[_0x4bf8('0x9c')](_0x4bf8('0x9f'),{'UserId':Number(_0x5e8fce[_0x4bf8('0x37')][_0x4bf8('0x65')][_0x4e2040]),'CmListId':Number(_0x5e8fce[_0x4bf8('0x4b')]['id'])});}}else{socket[_0x4bf8('0x9c')](_0x4bf8('0x9f'),{'UserId':Number(_0x5e8fce['query'][_0x4bf8('0x65')]),'CmListId':Number(_0x5e8fce[_0x4bf8('0x4b')]['id'])});}});}})[_0x4bf8('0x27')](respondWithStatusCode(_0x2c0887,null))[_0x4bf8('0x49')](handleError(_0x2c0887,null));};exports['getAgents']=function(_0x29ba4b,_0x5e4ef5,_0x3e3c4f){var _0x26cc3f={};var _0x4774dd={};var _0x37494a;var _0xcd3add;return db[_0x4bf8('0x30')][_0x4bf8('0x5a')]({'where':{'id':_0x29ba4b[_0x4bf8('0x4b')]['id']}})[_0x4bf8('0x27')](handleEntityNotFound(_0x5e4ef5,null))[_0x4bf8('0x27')](function(_0x26df07){if(_0x26df07){_0x37494a=_0x26df07;_0x4774dd[_0x4bf8('0x35')]=_['keys'](db['User'][_0x4bf8('0x31')]);_0x4774dd[_0x4bf8('0x37')]=_['keys'](_0x29ba4b[_0x4bf8('0x37')]);_0x4774dd['filters']=_[_0x4bf8('0x39')](_0x4774dd[_0x4bf8('0x35')],_0x4774dd[_0x4bf8('0x37')]);_0x26cc3f[_0x4bf8('0x3a')]=_[_0x4bf8('0x39')](_0x4774dd[_0x4bf8('0x35')],qs[_0x4bf8('0x3b')](_0x29ba4b[_0x4bf8('0x37')][_0x4bf8('0x3b')]));_0x26cc3f[_0x4bf8('0x3a')]=_0x26cc3f[_0x4bf8('0x3a')]['length']?_0x26cc3f[_0x4bf8('0x3a')]:_0x4774dd[_0x4bf8('0x35')];_0x26cc3f[_0x4bf8('0x5b')]=qs['sort'](_0x29ba4b['query'][_0x4bf8('0x3f')]);_0x26cc3f['where']=qs['filters'](_[_0x4bf8('0x41')](_0x29ba4b[_0x4bf8('0x37')],_0x4774dd['filters']));if(_0x29ba4b['query'][_0x4bf8('0x43')]){_0x26cc3f['where']=_[_0x4bf8('0x42')](_0x26cc3f[_0x4bf8('0x40')],{'$or':_[_0x4bf8('0x36')](_0x26cc3f['attributes'],function(_0xdafbc8){var _0x1c7890={};_0x1c7890[_0xdafbc8]={'$like':'%'+_0x29ba4b[_0x4bf8('0x37')][_0x4bf8('0x43')]+'%'};return _0x1c7890;})});}_0x26cc3f=_['merge']({},_0x26cc3f,_0x29ba4b[_0x4bf8('0x44')]);return _0x37494a[_0x4bf8('0xa0')](_0x26cc3f);}})['then'](function(_0x2264d8){if(_0x2264d8){_0xcd3add=_0x2264d8[_0x4bf8('0x3c')];if(!_0x29ba4b['query'][_0x4bf8('0x3d')](_0x4bf8('0x3e'))){_0x26cc3f[_0x4bf8('0x20')]=qs[_0x4bf8('0x20')](_0x29ba4b[_0x4bf8('0x37')][_0x4bf8('0x20')]);_0x26cc3f[_0x4bf8('0x1e')]=qs[_0x4bf8('0x1e')](_0x29ba4b[_0x4bf8('0x37')][_0x4bf8('0x1e')]);}return _0x37494a[_0x4bf8('0xa0')](_0x26cc3f);}})[_0x4bf8('0x27')](function(_0x21a5a8){if(_0x21a5a8){return _0x21a5a8?{'count':_0xcd3add,'rows':_0x21a5a8}:null;}})[_0x4bf8('0x27')](respondWithResult(_0x5e4ef5,null))[_0x4bf8('0x49')](handleError(_0x5e4ef5,null));};function handleQuery(_0x54b886){logger[_0x4bf8('0xa1')](_0x54b886);return db[_0x4bf8('0x8b')][_0x4bf8('0x37')](_0x54b886)['spread'](function(_0x3d29b5){csv_results[_0x4bf8('0x63')](_0x3d29b5);})['catch'](function(_0x283c4e){logger[_0x4bf8('0x2c')](_0x283c4e[_0x4bf8('0xa2')]);});}