aaa8aba3c20a4cba68f3482303f659c205f17c80
[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 _0x1f97=['ejs','fs-extra','lodash','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/environment','../../mysqldb','defaults','redis','socket.io-emitter','./cmList.socket','sendStatus','status','offset','undefined','limit','count','set','json','reject','save','update','then','destroy','get','Lists','UserProfileResource','end','error','stack','name','send','map','fieldName','key','model','query','filters','intersection','fields','attributes','hasOwnProperty','order','sort','where','pick','filter','merge','type','VIRTUAL','options','includeAll','include','findAll','catch','show','params','keys','find','create','CmList','body','user','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','describe','ListId','Disposition','getDispositions','length','nolimit','rows','removeDispositions','ids','CmContact','tags','isArray','omit','success','push','dataValues','errors','message','Malformed\x20request\x20syntax','CustomFields','concat','cf_%d','raw','getContacts','getContactsCsv','CmCustomField','format','isEmpty','setHeader','Content-disposition','list_%d_%s.csv','Content-type','text/csv','download','/tmp/list_%d_%s.csv','addCustomField','findOne','sequelize','addCustomFields','Sequelize','TEXT','text','INTEGER','switch','BOOLEAN','defaultValue','queryInterface','getCustomFields','rawAttributes','findAndCountAll','addAgents','spread','emit','removeAgents','userCmList:remove','getAgents','User','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv'];(function(_0x45e56c,_0x4235d0){var _0x4826ef=function(_0x18ae28){while(--_0x18ae28){_0x45e56c['push'](_0x45e56c['shift']());}};_0x4826ef(++_0x4235d0);}(_0x1f97,0x82));var _0x71f9=function(_0x253099,_0x4761b5){_0x253099=_0x253099-0x0;var _0x1f0861=_0x1f97[_0x253099];return _0x1f0861;};'use strict';var emlformat=require(_0x71f9('0x0'));var rimraf=require(_0x71f9('0x1'));var zipdir=require(_0x71f9('0x2'));var jsonpatch=require(_0x71f9('0x3'));var rp=require(_0x71f9('0x4'));var moment=require(_0x71f9('0x5'));var BPromise=require(_0x71f9('0x6'));var Mustache=require(_0x71f9('0x7'));var util=require(_0x71f9('0x8'));var path=require(_0x71f9('0x9'));var sox=require(_0x71f9('0xa'));var csv=require(_0x71f9('0xb'));var ejs=require(_0x71f9('0xc'));var fs=require('fs');var fs_extra=require(_0x71f9('0xd'));var _=require(_0x71f9('0xe'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0x71f9('0xf'));var deskjs=require(_0x71f9('0x10'));var toCsv=require(_0x71f9('0xb'));var querystring=require('querystring');var Papa=require(_0x71f9('0x11'));var Redis=require(_0x71f9('0x12'));var authService=require(_0x71f9('0x13'));var qs=require(_0x71f9('0x14'));var as=require(_0x71f9('0x15'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x71f9('0x16'))(_0x71f9('0x17'));var utils=require('../../config/utils');var config=require(_0x71f9('0x18'));var licenseUtil=require('../../config/license/util');var db=require(_0x71f9('0x19'))['db'];config['redis']=_[_0x71f9('0x1a')](config[_0x71f9('0x1b')],{'host':'localhost','port':0x18eb});var socket=require(_0x71f9('0x1c'))(new Redis(config[_0x71f9('0x1b')]));require(_0x71f9('0x1d'))['register'](socket);function respondWithStatusCode(_0x17664d,_0x2c80f5){_0x2c80f5=_0x2c80f5||0xcc;return function(_0xb5dba2){if(_0xb5dba2){return _0x17664d[_0x71f9('0x1e')](_0x2c80f5);}return _0x17664d[_0x71f9('0x1f')](_0x2c80f5)['end']();};}function respondWithResult(_0x4c1219,_0x5a3d6e){_0x5a3d6e=_0x5a3d6e||0xc8;return function(_0x57c530){if(_0x57c530){return _0x4c1219[_0x71f9('0x1f')](_0x5a3d6e)['json'](_0x57c530);}};}function respondWithFilteredResult(_0x59b4bd,_0x2771c3){return function(_0x169c6a){if(_0x169c6a){var _0x2e3c0a=typeof _0x2771c3[_0x71f9('0x20')]===_0x71f9('0x21')&&typeof _0x2771c3[_0x71f9('0x22')]==='undefined';var _0x3fd825=_0x169c6a['count'];var _0x572614=_0x2e3c0a?0x0:_0x2771c3[_0x71f9('0x20')];var _0x543173=_0x2e3c0a?_0x169c6a[_0x71f9('0x23')]:_0x2771c3[_0x71f9('0x20')]+_0x2771c3['limit'];var _0x50fda1;if(_0x543173>=_0x3fd825){_0x543173=_0x3fd825;_0x50fda1=0xc8;}else{_0x50fda1=0xce;}_0x59b4bd['status'](_0x50fda1);return _0x59b4bd[_0x71f9('0x24')]('Content-Range',_0x572614+'-'+_0x543173+'/'+_0x3fd825)[_0x71f9('0x25')](_0x169c6a);}return null;};}function patchUpdates(_0xe92878){return function(_0x3b2297){try{jsonpatch['apply'](_0x3b2297,_0xe92878,!![]);}catch(_0x407dc5){return BPromise[_0x71f9('0x26')](_0x407dc5);}return _0x3b2297[_0x71f9('0x27')]();};}function saveUpdates(_0x335a26,_0xbfb51c){return function(_0x471d66){if(_0x471d66){return _0x471d66[_0x71f9('0x28')](_0x335a26)[_0x71f9('0x29')](function(_0x2ec051){return _0x2ec051;});}return null;};}function removeEntity(_0x4b52b6,_0x136343){return function(_0x51f0fb){if(_0x51f0fb){return _0x51f0fb[_0x71f9('0x2a')]()[_0x71f9('0x29')](function(){var _0x1012fe=_0x51f0fb[_0x71f9('0x2b')]({'plain':!![]});var _0x1fef2b=_0x71f9('0x2c');return db[_0x71f9('0x2d')][_0x71f9('0x2a')]({'where':{'type':_0x1fef2b,'resourceId':_0x1012fe['id']}})['then'](function(){return _0x51f0fb;});})[_0x71f9('0x29')](function(){_0x4b52b6['status'](0xcc)[_0x71f9('0x2e')]();});}};}function handleEntityNotFound(_0x280a16,_0x18efc6){return function(_0x53eb27){if(!_0x53eb27){_0x280a16[_0x71f9('0x1e')](0x194);}return _0x53eb27;};}function handleError(_0x2bf51e,_0x103ab3){_0x103ab3=_0x103ab3||0x1f4;return function(_0x208514){logger[_0x71f9('0x2f')](_0x208514[_0x71f9('0x30')]);if(_0x208514[_0x71f9('0x31')]){delete _0x208514['name'];}_0x2bf51e[_0x71f9('0x1f')](_0x103ab3)[_0x71f9('0x32')](_0x208514);};}exports['index']=function(_0xdb59fb,_0x5506a1){var _0x4c6c97={},_0x350022={},_0x531367={'count':0x0,'rows':[]};var _0x5a697e=_[_0x71f9('0x33')](db['CmList']['rawAttributes'],function(_0x14f0bf){return{'name':_0x14f0bf[_0x71f9('0x34')],'type':_0x14f0bf['type'][_0x71f9('0x35')]};});_0x350022[_0x71f9('0x36')]=_[_0x71f9('0x33')](_0x5a697e,_0x71f9('0x31'));_0x350022['query']=_['keys'](_0xdb59fb[_0x71f9('0x37')]);_0x350022[_0x71f9('0x38')]=_[_0x71f9('0x39')](_0x350022[_0x71f9('0x36')],_0x350022['query']);_0x4c6c97['attributes']=_[_0x71f9('0x39')](_0x350022[_0x71f9('0x36')],qs[_0x71f9('0x3a')](_0xdb59fb['query']['fields']));_0x4c6c97[_0x71f9('0x3b')]=_0x4c6c97[_0x71f9('0x3b')]['length']?_0x4c6c97[_0x71f9('0x3b')]:_0x350022[_0x71f9('0x36')];if(!_0xdb59fb[_0x71f9('0x37')][_0x71f9('0x3c')]('nolimit')){_0x4c6c97['limit']=qs[_0x71f9('0x22')](_0xdb59fb[_0x71f9('0x37')][_0x71f9('0x22')]);_0x4c6c97[_0x71f9('0x20')]=qs[_0x71f9('0x20')](_0xdb59fb[_0x71f9('0x37')][_0x71f9('0x20')]);}_0x4c6c97[_0x71f9('0x3d')]=qs['sort'](_0xdb59fb[_0x71f9('0x37')][_0x71f9('0x3e')]);_0x4c6c97[_0x71f9('0x3f')]=qs[_0x71f9('0x38')](_[_0x71f9('0x40')](_0xdb59fb['query'],_0x350022[_0x71f9('0x38')]),_0x5a697e);if(_0xdb59fb[_0x71f9('0x37')][_0x71f9('0x41')]){_0x4c6c97[_0x71f9('0x3f')]=_[_0x71f9('0x42')](_0x4c6c97[_0x71f9('0x3f')],{'$or':_['map'](_0x5a697e,function(_0x11be69){if(_0x11be69[_0x71f9('0x43')]!==_0x71f9('0x44')){var _0x27de89={};_0x27de89[_0x11be69[_0x71f9('0x31')]]={'$like':'%'+_0xdb59fb['query']['filter']+'%'};return _0x27de89;}})});}_0x4c6c97=_['merge']({},_0x4c6c97,_0xdb59fb[_0x71f9('0x45')]);var _0x2afc3e={'where':_0x4c6c97['where']};return db['CmList'][_0x71f9('0x23')](_0x2afc3e)[_0x71f9('0x29')](function(_0x13bd87){_0x531367[_0x71f9('0x23')]=_0x13bd87;if(_0xdb59fb[_0x71f9('0x37')][_0x71f9('0x46')]){_0x4c6c97[_0x71f9('0x47')]=[{'all':!![]}];}return db['CmList'][_0x71f9('0x48')](_0x4c6c97);})['then'](function(_0x5361a9){_0x531367['rows']=_0x5361a9;return _0x531367;})['then'](respondWithFilteredResult(_0x5506a1,_0x4c6c97))[_0x71f9('0x49')](handleError(_0x5506a1,null));};exports[_0x71f9('0x4a')]=function(_0x3bb4ba,_0x32ab8e){var _0x57d9c9={'raw':![],'where':{'id':_0x3bb4ba[_0x71f9('0x4b')]['id']}},_0x21b746={};_0x21b746[_0x71f9('0x36')]=_[_0x71f9('0x4c')](db['CmList']['rawAttributes']);_0x21b746[_0x71f9('0x37')]=_[_0x71f9('0x4c')](_0x3bb4ba[_0x71f9('0x37')]);_0x21b746[_0x71f9('0x38')]=_[_0x71f9('0x39')](_0x21b746[_0x71f9('0x36')],_0x21b746['query']);_0x57d9c9[_0x71f9('0x3b')]=_[_0x71f9('0x39')](_0x21b746['model'],qs[_0x71f9('0x3a')](_0x3bb4ba[_0x71f9('0x37')][_0x71f9('0x3a')]));_0x57d9c9[_0x71f9('0x3b')]=_0x57d9c9[_0x71f9('0x3b')]['length']?_0x57d9c9['attributes']:_0x21b746[_0x71f9('0x36')];if(_0x3bb4ba[_0x71f9('0x37')][_0x71f9('0x46')]){_0x57d9c9[_0x71f9('0x47')]=[{'all':!![]}];}_0x57d9c9=_[_0x71f9('0x42')]({},_0x57d9c9,_0x3bb4ba['options']);return db['CmList'][_0x71f9('0x4d')](_0x57d9c9)[_0x71f9('0x29')](handleEntityNotFound(_0x32ab8e,null))[_0x71f9('0x29')](respondWithResult(_0x32ab8e,null))[_0x71f9('0x49')](handleError(_0x32ab8e,null));};exports[_0x71f9('0x4e')]=function(_0x42b0ab,_0x134a79){return db[_0x71f9('0x4f')]['create'](_0x42b0ab[_0x71f9('0x50')],{})['then'](function(_0x581aaa){var _0x50ecfb=_0x42b0ab[_0x71f9('0x51')][_0x71f9('0x2b')]({'plain':!![]});if(!_0x50ecfb)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x50ecfb['role']===_0x71f9('0x51')){var _0x86a0d3=_0x581aaa[_0x71f9('0x2b')]({'plain':!![]});var _0x56b579=_0x71f9('0x2c');return db[_0x71f9('0x52')][_0x71f9('0x4d')]({'where':{'name':_0x56b579,'userProfileId':_0x50ecfb['userProfileId']},'raw':!![]})[_0x71f9('0x29')](function(_0x54d82c){if(_0x54d82c&&_0x54d82c[_0x71f9('0x53')]===0x0){return db[_0x71f9('0x2d')][_0x71f9('0x4e')]({'name':_0x86a0d3[_0x71f9('0x31')],'resourceId':_0x86a0d3['id'],'type':_0x54d82c[_0x71f9('0x31')],'sectionId':_0x54d82c['id']},{})['then'](function(){return _0x581aaa;});}else{return _0x581aaa;}})[_0x71f9('0x49')](function(_0x4e849a){logger[_0x71f9('0x2f')](_0x71f9('0x54'),_0x4e849a);throw _0x4e849a;});}return _0x581aaa;})[_0x71f9('0x29')](respondWithResult(_0x134a79,0xc9))[_0x71f9('0x49')](handleError(_0x134a79,null));};exports[_0x71f9('0x28')]=function(_0x43ab37,_0x19c2a0){if(_0x43ab37[_0x71f9('0x50')]['id']){delete _0x43ab37[_0x71f9('0x50')]['id'];}return db[_0x71f9('0x4f')][_0x71f9('0x4d')]({'where':{'id':_0x43ab37[_0x71f9('0x4b')]['id']}})['then'](handleEntityNotFound(_0x19c2a0,null))[_0x71f9('0x29')](saveUpdates(_0x43ab37[_0x71f9('0x50')],null))[_0x71f9('0x29')](respondWithResult(_0x19c2a0,null))['catch'](handleError(_0x19c2a0,null));};exports[_0x71f9('0x2a')]=function(_0x24da84,_0x3baa41){return db[_0x71f9('0x4f')]['find']({'where':{'id':_0x24da84[_0x71f9('0x4b')]['id']}})[_0x71f9('0x29')](handleEntityNotFound(_0x3baa41,null))['then'](removeEntity(_0x3baa41,null))[_0x71f9('0x49')](handleError(_0x3baa41,null));};exports['describe']=function(_0x18889f,_0x339599){return db[_0x71f9('0x4f')][_0x71f9('0x55')]()[_0x71f9('0x29')](respondWithResult(_0x339599,null))[_0x71f9('0x49')](handleError(_0x339599,null));};exports['addDisposition']=function(_0x3a7716,_0x14cf7d,_0x498832){if(_0x3a7716[_0x71f9('0x50')]['id']){delete _0x3a7716[_0x71f9('0x50')]['id'];}return db[_0x71f9('0x4f')][_0x71f9('0x4d')]({'where':{'id':_0x3a7716['params']['id']}})[_0x71f9('0x29')](handleEntityNotFound(_0x14cf7d,null))[_0x71f9('0x29')](function(_0x294b4c){if(_0x294b4c){_0x3a7716['body'][_0x71f9('0x56')]=_0x294b4c['id'];return db[_0x71f9('0x57')][_0x71f9('0x4e')](_0x3a7716[_0x71f9('0x50')]);}})[_0x71f9('0x29')](respondWithResult(_0x14cf7d,null))[_0x71f9('0x49')](handleError(_0x14cf7d,null));};exports[_0x71f9('0x58')]=function(_0x5e1cfc,_0x1e4a6e,_0x386e5d){var _0x387c21={'raw':![],'where':{}};var _0x188d3a={};var _0x5e6a8d={'count':0x0,'rows':[]};return db[_0x71f9('0x4f')]['findOne']({'where':{'id':_0x5e1cfc[_0x71f9('0x4b')]['id']}})[_0x71f9('0x29')](handleEntityNotFound(_0x1e4a6e,null))[_0x71f9('0x29')](function(_0x29e2ad){if(_0x29e2ad){_0x188d3a[_0x71f9('0x36')]=_[_0x71f9('0x4c')](db['Disposition']['rawAttributes']);_0x188d3a[_0x71f9('0x37')]=_[_0x71f9('0x4c')](_0x5e1cfc[_0x71f9('0x37')]);_0x188d3a[_0x71f9('0x38')]=_[_0x71f9('0x39')](_0x188d3a['model'],_0x188d3a[_0x71f9('0x37')]);_0x387c21[_0x71f9('0x3b')]=_['intersection'](_0x188d3a['model'],qs[_0x71f9('0x3a')](_0x5e1cfc['query'][_0x71f9('0x3a')]));_0x387c21[_0x71f9('0x3b')]=_0x387c21[_0x71f9('0x3b')][_0x71f9('0x59')]?_0x387c21[_0x71f9('0x3b')]:_0x188d3a[_0x71f9('0x36')];if(!_0x5e1cfc[_0x71f9('0x37')][_0x71f9('0x3c')](_0x71f9('0x5a'))){_0x387c21[_0x71f9('0x22')]=qs[_0x71f9('0x22')](_0x5e1cfc[_0x71f9('0x37')][_0x71f9('0x22')]);_0x387c21[_0x71f9('0x20')]=qs[_0x71f9('0x20')](_0x5e1cfc[_0x71f9('0x37')][_0x71f9('0x20')]);}_0x387c21['order']=qs[_0x71f9('0x3e')](_0x5e1cfc[_0x71f9('0x37')][_0x71f9('0x3e')]);_0x387c21[_0x71f9('0x3f')]=qs[_0x71f9('0x38')](_[_0x71f9('0x40')](_0x5e1cfc[_0x71f9('0x37')],_0x188d3a['filters']));_0x387c21[_0x71f9('0x3f')][_0x71f9('0x56')]=_0x29e2ad['id'];if(_0x5e1cfc[_0x71f9('0x37')][_0x71f9('0x41')]){_0x387c21[_0x71f9('0x3f')]=_[_0x71f9('0x42')](_0x387c21[_0x71f9('0x3f')],{'$or':_[_0x71f9('0x33')](_0x387c21[_0x71f9('0x3b')],function(_0x521887){var _0x1bfb2a={};_0x1bfb2a[_0x521887]={'$like':'%'+_0x5e1cfc['query'][_0x71f9('0x41')]+'%'};return _0x1bfb2a;})});}_0x387c21=_[_0x71f9('0x42')]({},_0x387c21,_0x5e1cfc[_0x71f9('0x45')]);return db[_0x71f9('0x57')]['count']({'where':_0x387c21[_0x71f9('0x3f')]})[_0x71f9('0x29')](function(_0x30aa40){_0x5e6a8d['count']=_0x30aa40;if(_0x5e1cfc[_0x71f9('0x37')][_0x71f9('0x46')]){_0x387c21[_0x71f9('0x47')]=[{'all':!![]}];}return db[_0x71f9('0x57')]['findAll'](_0x387c21);})[_0x71f9('0x29')](function(_0x47dcd7){_0x5e6a8d[_0x71f9('0x5b')]=_0x47dcd7;return _0x5e6a8d;});}})[_0x71f9('0x29')](respondWithFilteredResult(_0x1e4a6e,_0x387c21))[_0x71f9('0x49')](handleError(_0x1e4a6e,null));};exports[_0x71f9('0x5c')]=function(_0x2da65e,_0x543ec7,_0x55e47c){return db[_0x71f9('0x4f')][_0x71f9('0x4d')]({'where':{'id':_0x2da65e[_0x71f9('0x4b')]['id']}})[_0x71f9('0x29')](handleEntityNotFound(_0x543ec7,null))['then'](function(_0x52a2e7){if(_0x52a2e7){return _0x52a2e7[_0x71f9('0x5c')](_0x2da65e[_0x71f9('0x37')][_0x71f9('0x5d')]);}})[_0x71f9('0x29')](respondWithStatusCode(_0x543ec7,null))[_0x71f9('0x49')](handleError(_0x543ec7,null));};exports['addContacts']=function(_0x10959d,_0x31669c,_0x17f68a){return db['CmList']['find']({'where':{'id':_0x10959d['params']['id']}})['then'](handleEntityNotFound(_0x31669c,null))[_0x71f9('0x29')](function(_0x2118e5){if(_0x2118e5){var _0x50dd56=[];var _0x41d678={'success':[],'errors':[]};return db[_0x71f9('0x5e')][_0x71f9('0x55')]()['then'](function(_0x470069){if(_0x10959d[_0x71f9('0x50')][_0x71f9('0x5f')]&&_[_0x71f9('0x60')](_0x10959d[_0x71f9('0x50')][_0x71f9('0x5f')])){if(_0x10959d[_0x71f9('0x50')][_0x71f9('0x5f')][_0x71f9('0x59')]){_0x10959d['body'][_0x71f9('0x5f')]=_0x10959d[_0x71f9('0x50')][_0x71f9('0x5f')]['join'](';')+';';}else{_0x10959d['body'][_0x71f9('0x5f')]=null;}}else{_0x10959d[_0x71f9('0x50')]['tags']=null;}if(_0x10959d[_0x71f9('0x50')]&&_[_0x71f9('0x60')](_0x10959d[_0x71f9('0x50')])){for(var _0x1419ed=0x0;_0x1419ed<_0x10959d[_0x71f9('0x50')]['length'];_0x1419ed+=0x1){_0x10959d[_0x71f9('0x50')][_0x1419ed]['ListId']=_0x10959d['params']['id'];_0x50dd56['push'](_0x10959d[_0x71f9('0x50')][_0x1419ed]);}return BPromise['each'](_0x50dd56,function(_0x17e6b9){return db[_0x71f9('0x5e')][_0x71f9('0x4e')](_[_0x71f9('0x61')](_0x17e6b9,['ids','id'])||{},{'fields':_[_0x71f9('0x4c')](_0x470069),'raw':!![]})[_0x71f9('0x29')](function(_0x1531eb){_0x41d678[_0x71f9('0x62')][_0x71f9('0x63')](_0x1531eb[_0x71f9('0x64')]);})[_0x71f9('0x49')](function(_0x5a55be){_0x41d678[_0x71f9('0x65')][_0x71f9('0x63')]({'data':_0x17e6b9,'error':_0x5a55be[_0x71f9('0x66')]});});})[_0x71f9('0x29')](function(_0x26cf65){return _0x41d678;})[_0x71f9('0x49')](function(_0x251177){return _0x251177;});}else{_0x31669c[_0x71f9('0x1f')](0x190)[_0x71f9('0x25')]({'message':_0x71f9('0x67')});}});}})[_0x71f9('0x29')](respondWithResult(_0x31669c,null))['catch'](handleError(_0x31669c,null));};exports['getContacts']=function(_0x41c2aa,_0x252ccb,_0x4b55c9){var _0x5d72e8;return db[_0x71f9('0x4f')][_0x71f9('0x4d')]({'where':{'id':_0x41c2aa[_0x71f9('0x4b')]['id']},'include':[{'model':db['CmCustomField'],'as':_0x71f9('0x68')}]})['then'](handleEntityNotFound(_0x252ccb,null))[_0x71f9('0x29')](function(_0x4994ce){if(_0x4994ce){_0x5d72e8=_0x4994ce;return db[_0x71f9('0x5e')]['rawAttributes'];}})[_0x71f9('0x29')](function(_0x1453d9){if(_0x1453d9){var _0x17affb={};var _0x4ee355={};_0x4ee355['model']=_[_0x71f9('0x69')](_['keys'](_0x1453d9),_[_0x71f9('0x33')](_0x5d72e8['CustomFields'],function(_0x36dc3d){return util['format'](_0x71f9('0x6a'),_0x36dc3d['id']);}));_0x4ee355[_0x71f9('0x37')]=_[_0x71f9('0x4c')](_0x41c2aa[_0x71f9('0x37')]);_0x4ee355['filters']=_['intersection'](_0x4ee355[_0x71f9('0x36')],_0x4ee355['query']);_0x17affb[_0x71f9('0x3b')]=_['intersection'](_0x4ee355[_0x71f9('0x36')],qs[_0x71f9('0x3a')](_0x41c2aa[_0x71f9('0x37')]['fields']));_0x17affb[_0x71f9('0x3b')]=_0x17affb[_0x71f9('0x3b')]['length']?_0x17affb[_0x71f9('0x3b')]:_0x4ee355['model'];if(!_0x41c2aa[_0x71f9('0x37')][_0x71f9('0x3c')]('nolimit')){_0x17affb[_0x71f9('0x22')]=qs[_0x71f9('0x22')](_0x41c2aa['query'][_0x71f9('0x22')]);_0x17affb['offset']=qs[_0x71f9('0x20')](_0x41c2aa[_0x71f9('0x37')][_0x71f9('0x20')]);}_0x17affb[_0x71f9('0x3d')]=qs['sort'](_0x41c2aa[_0x71f9('0x37')][_0x71f9('0x3e')]);_0x17affb[_0x71f9('0x3f')]=qs[_0x71f9('0x38')](_['pick'](_0x41c2aa[_0x71f9('0x37')],_0x4ee355[_0x71f9('0x38')]));_0x17affb[_0x71f9('0x6b')]=!![];if(_0x41c2aa[_0x71f9('0x37')]['filter']){_0x17affb[_0x71f9('0x3f')]=_['merge'](_0x17affb[_0x71f9('0x3f')],{'$or':_[_0x71f9('0x33')](_0x17affb['attributes'],function(_0x4baca1){var _0x4704={};_0x4704[_0x4baca1]={'$like':'%'+_0x41c2aa[_0x71f9('0x37')][_0x71f9('0x41')]+'%'};return _0x4704;})});}_0x17affb=_[_0x71f9('0x42')]({},_0x17affb,_0x41c2aa[_0x71f9('0x45')]);return _0x5d72e8[_0x71f9('0x6c')](_0x17affb);}})['then'](respondWithResult(_0x252ccb,null))[_0x71f9('0x49')](handleError(_0x252ccb,null));};exports[_0x71f9('0x6d')]=function(_0x865b99,_0x41f443,_0x56e82f){var _0x355220;return db[_0x71f9('0x4f')][_0x71f9('0x4d')]({'where':{'id':_0x865b99[_0x71f9('0x4b')]['id']},'include':[{'model':db[_0x71f9('0x6e')],'as':_0x71f9('0x68')}]})[_0x71f9('0x29')](handleEntityNotFound(_0x41f443,null))[_0x71f9('0x29')](function(_0x5ebcd2){if(_0x5ebcd2){_0x355220=_0x5ebcd2;return db[_0x71f9('0x5e')]['rawAttributes'];}})[_0x71f9('0x29')](function(_0x35320c){if(_0x35320c){var _0x2b3de3={};var _0x1c7047={};_0x1c7047[_0x71f9('0x36')]=_[_0x71f9('0x69')](_[_0x71f9('0x4c')](_0x35320c),_[_0x71f9('0x33')](_0x355220[_0x71f9('0x68')],function(_0x158aa6){return util[_0x71f9('0x6f')](_0x71f9('0x6a'),_0x158aa6['id']);}));_0x1c7047[_0x71f9('0x37')]=_[_0x71f9('0x4c')](_0x865b99[_0x71f9('0x37')]);_0x1c7047[_0x71f9('0x38')]=_[_0x71f9('0x39')](_0x1c7047[_0x71f9('0x36')],_0x1c7047['query']);_0x2b3de3[_0x71f9('0x3b')]=_[_0x71f9('0x39')](_0x1c7047[_0x71f9('0x36')],qs[_0x71f9('0x3a')](_0x865b99[_0x71f9('0x37')][_0x71f9('0x3a')]));_0x2b3de3[_0x71f9('0x3b')]=_0x2b3de3[_0x71f9('0x3b')][_0x71f9('0x59')]?_0x2b3de3[_0x71f9('0x3b')]:_0x1c7047['model'];if(!_0x865b99[_0x71f9('0x37')]['hasOwnProperty'](_0x71f9('0x5a'))){_0x2b3de3[_0x71f9('0x22')]=qs[_0x71f9('0x22')](_0x865b99[_0x71f9('0x37')][_0x71f9('0x22')]);_0x2b3de3[_0x71f9('0x20')]=qs[_0x71f9('0x20')](_0x865b99['query'][_0x71f9('0x20')]);}_0x2b3de3[_0x71f9('0x3d')]=qs[_0x71f9('0x3e')](_0x865b99[_0x71f9('0x37')][_0x71f9('0x3e')]);_0x2b3de3[_0x71f9('0x3f')]=qs[_0x71f9('0x38')](_[_0x71f9('0x40')](_0x865b99[_0x71f9('0x37')],_0x1c7047[_0x71f9('0x38')]));_0x2b3de3[_0x71f9('0x6b')]=!![];if(_0x865b99[_0x71f9('0x37')][_0x71f9('0x41')]){_0x2b3de3[_0x71f9('0x3f')]=_[_0x71f9('0x42')](_0x2b3de3[_0x71f9('0x3f')],{'$or':_[_0x71f9('0x33')](_0x2b3de3[_0x71f9('0x3b')],function(_0x5ae810){var _0x13107f={};_0x13107f[_0x5ae810]={'$like':'%'+_0x865b99['query'][_0x71f9('0x41')]+'%'};return _0x13107f;})});}_0x2b3de3=_[_0x71f9('0x42')]({},_0x2b3de3,_0x865b99['options']);return _0x355220[_0x71f9('0x6c')](_0x2b3de3);}})[_0x71f9('0x29')](function(_0x9b7a64){if(!_[_0x71f9('0x70')](_0x9b7a64)){var _0x72929d=toCsv(_0x9b7a64);var _0xb88ce=Date['now']();fs['writeFileSync'](util[_0x71f9('0x6f')]('/tmp/list_%d_%s.csv',_0x355220['id'],_0xb88ce),_0x72929d);_0x41f443[_0x71f9('0x71')](_0x71f9('0x72'),'attachment;\x20filename='+util['format'](_0x71f9('0x73'),_0x355220['id'],_0xb88ce));_0x41f443[_0x71f9('0x71')](_0x71f9('0x74'),_0x71f9('0x75'));return _0x41f443[_0x71f9('0x76')](util[_0x71f9('0x6f')](_0x71f9('0x77'),_0x355220['id'],_0xb88ce));}else{return _0x41f443[_0x71f9('0x1e')](0xcc);}})[_0x71f9('0x49')](handleError(_0x41f443,null));};exports[_0x71f9('0x78')]=function(_0x92e15e,_0x4ae22d,_0x3a974f){var _0x22f7a2;delete _0x92e15e[_0x71f9('0x50')]['id'];return db[_0x71f9('0x4f')][_0x71f9('0x79')]({'where':{'id':_0x92e15e[_0x71f9('0x4b')]['id']}})[_0x71f9('0x29')](handleEntityNotFound(_0x4ae22d,null))[_0x71f9('0x29')](function(_0x1f7688){if(_0x1f7688){return db[_0x71f9('0x7a')]['transaction'](function(_0x1c9282){return db['CmCustomField'][_0x71f9('0x4e')](_0x92e15e[_0x71f9('0x50')],{'transaction':_0x1c9282})[_0x71f9('0x29')](function(_0xa882c5){_0x22f7a2=_0xa882c5;return _0x1f7688[_0x71f9('0x7b')](_0xa882c5,{'transaction':_0x1c9282});})['then'](function(){var _0x1dc7f9={'type':db[_0x71f9('0x7c')][_0x71f9('0x7d')],'transaction':_0x1c9282};switch(_0x92e15e[_0x71f9('0x50')]['type']){case _0x71f9('0x7e'):_0x1dc7f9[_0x71f9('0x43')]=db[_0x71f9('0x7c')]['TEXT'];break;case'number':_0x1dc7f9['type']=db[_0x71f9('0x7c')][_0x71f9('0x7f')];break;case _0x71f9('0x80'):_0x1dc7f9[_0x71f9('0x43')]=db[_0x71f9('0x7c')][_0x71f9('0x81')];_0x1dc7f9[_0x71f9('0x82')]=![];break;default:}return db['sequelize'][_0x71f9('0x83')]['addColumn']('cm_contacts',util['format']('cf_%s',_0x22f7a2['id']),_0x1dc7f9);})[_0x71f9('0x29')](function(){return _0x22f7a2;});});}})[_0x71f9('0x29')](respondWithResult(_0x4ae22d,null))[_0x71f9('0x49')](handleError(_0x4ae22d,null));};exports[_0x71f9('0x84')]=function(_0x1b4c82,_0x3ea26b,_0x511a3d){var _0x171701={};var _0x3fc663={};var _0x27572b;var _0x51930d;return db[_0x71f9('0x4f')][_0x71f9('0x79')]({'where':{'id':_0x1b4c82[_0x71f9('0x4b')]['id']}})[_0x71f9('0x29')](handleEntityNotFound(_0x3ea26b,null))['then'](function(_0x5a18a4){if(_0x5a18a4){_0x27572b=_0x5a18a4;_0x3fc663['model']=_[_0x71f9('0x4c')](db[_0x71f9('0x6e')][_0x71f9('0x85')]);_0x3fc663[_0x71f9('0x37')]=_[_0x71f9('0x4c')](_0x1b4c82[_0x71f9('0x37')]);_0x3fc663['filters']=_[_0x71f9('0x39')](_0x3fc663[_0x71f9('0x36')],_0x3fc663[_0x71f9('0x37')]);_0x171701[_0x71f9('0x3b')]=_[_0x71f9('0x39')](_0x3fc663['model'],qs[_0x71f9('0x3a')](_0x1b4c82[_0x71f9('0x37')][_0x71f9('0x3a')]));_0x171701['attributes']=_0x171701['attributes']['length']?_0x171701[_0x71f9('0x3b')]:_0x3fc663[_0x71f9('0x36')];_0x171701['order']=qs[_0x71f9('0x3e')](_0x1b4c82[_0x71f9('0x37')][_0x71f9('0x3e')]);_0x171701[_0x71f9('0x3f')]=qs[_0x71f9('0x38')](_[_0x71f9('0x40')](_0x1b4c82['query'],_0x3fc663['filters']));_0x171701[_0x71f9('0x3f')]=_['merge'](_0x171701[_0x71f9('0x3f')],{'$or':[{'ListId':_0x5a18a4['id']},{'ListId':null}]});if(_0x1b4c82[_0x71f9('0x37')]['filter']){_0x171701[_0x71f9('0x3f')]=_['merge'](_0x171701[_0x71f9('0x3f')],{'$or':_['map'](_0x171701[_0x71f9('0x3b')],function(_0x3eb63a){var _0x346f38={};_0x346f38[_0x3eb63a]={'$like':'%'+_0x1b4c82[_0x71f9('0x37')][_0x71f9('0x41')]+'%'};return _0x346f38;})});}_0x171701=_[_0x71f9('0x42')]({},_0x171701,_0x1b4c82[_0x71f9('0x45')]);return db['CmCustomField'][_0x71f9('0x86')](_0x171701);}})['then'](respondWithResult(_0x3ea26b,null))[_0x71f9('0x49')](handleError(_0x3ea26b,null));};exports[_0x71f9('0x87')]=function(_0x1f65e9,_0x4e1320,_0x82f8ea){return db[_0x71f9('0x4f')][_0x71f9('0x4d')]({'where':{'id':_0x1f65e9['params']['id']}})[_0x71f9('0x29')](handleEntityNotFound(_0x4e1320,null))['then'](function(_0x58788e){if(_0x58788e){return _0x58788e[_0x71f9('0x87')](_0x1f65e9[_0x71f9('0x50')]['ids'],_[_0x71f9('0x61')](_0x1f65e9[_0x71f9('0x50')],['ids','id'])||{})[_0x71f9('0x88')](function(_0x3791f2){for(var _0x4f2628=0x0;_0x4f2628<_0x1f65e9[_0x71f9('0x50')][_0x71f9('0x5d')][_0x71f9('0x59')];_0x4f2628+=0x1){socket[_0x71f9('0x89')]('userCmList:save',{'UserId':Number(_0x1f65e9[_0x71f9('0x50')][_0x71f9('0x5d')][_0x4f2628]),'CmListId':Number(_0x1f65e9[_0x71f9('0x4b')]['id'])});}return _0x3791f2;});}})[_0x71f9('0x29')](respondWithResult(_0x4e1320,null))[_0x71f9('0x49')](handleError(_0x4e1320,null));};exports[_0x71f9('0x8a')]=function(_0x5613f3,_0x15c859,_0x16210c){return db[_0x71f9('0x4f')][_0x71f9('0x4d')]({'where':{'id':_0x5613f3[_0x71f9('0x4b')]['id']}})[_0x71f9('0x29')](handleEntityNotFound(_0x15c859,null))['then'](function(_0x31ad56){if(_0x31ad56){return _0x31ad56[_0x71f9('0x8a')](_0x5613f3['query']['ids'])[_0x71f9('0x29')](function(){if(_['isArray'](_0x5613f3[_0x71f9('0x37')][_0x71f9('0x5d')])){for(var _0x1bb433=0x0;_0x1bb433<_0x5613f3['query']['ids'][_0x71f9('0x59')];_0x1bb433+=0x1){socket[_0x71f9('0x89')](_0x71f9('0x8b'),{'UserId':Number(_0x5613f3[_0x71f9('0x37')][_0x71f9('0x5d')][_0x1bb433]),'CmListId':Number(_0x5613f3[_0x71f9('0x4b')]['id'])});}}else{socket[_0x71f9('0x89')](_0x71f9('0x8b'),{'UserId':Number(_0x5613f3['query']['ids']),'CmListId':Number(_0x5613f3[_0x71f9('0x4b')]['id'])});}});}})['then'](respondWithStatusCode(_0x15c859,null))['catch'](handleError(_0x15c859,null));};exports[_0x71f9('0x8c')]=function(_0x6a686c,_0x5b866f,_0x176f6e){var _0x4162d3={};var _0x465f1d={};var _0x417d3d;var _0x3a4433;return db[_0x71f9('0x4f')][_0x71f9('0x79')]({'where':{'id':_0x6a686c[_0x71f9('0x4b')]['id']}})[_0x71f9('0x29')](handleEntityNotFound(_0x5b866f,null))[_0x71f9('0x29')](function(_0x249219){if(_0x249219){_0x417d3d=_0x249219;_0x465f1d[_0x71f9('0x36')]=_[_0x71f9('0x4c')](db[_0x71f9('0x8d')]['rawAttributes']);_0x465f1d['query']=_[_0x71f9('0x4c')](_0x6a686c[_0x71f9('0x37')]);_0x465f1d[_0x71f9('0x38')]=_[_0x71f9('0x39')](_0x465f1d[_0x71f9('0x36')],_0x465f1d[_0x71f9('0x37')]);_0x4162d3[_0x71f9('0x3b')]=_['intersection'](_0x465f1d[_0x71f9('0x36')],qs[_0x71f9('0x3a')](_0x6a686c[_0x71f9('0x37')][_0x71f9('0x3a')]));_0x4162d3[_0x71f9('0x3b')]=_0x4162d3['attributes'][_0x71f9('0x59')]?_0x4162d3[_0x71f9('0x3b')]:_0x465f1d[_0x71f9('0x36')];_0x4162d3['order']=qs[_0x71f9('0x3e')](_0x6a686c[_0x71f9('0x37')][_0x71f9('0x3e')]);_0x4162d3[_0x71f9('0x3f')]=qs[_0x71f9('0x38')](_[_0x71f9('0x40')](_0x6a686c[_0x71f9('0x37')],_0x465f1d[_0x71f9('0x38')]));if(_0x6a686c[_0x71f9('0x37')][_0x71f9('0x41')]){_0x4162d3[_0x71f9('0x3f')]=_[_0x71f9('0x42')](_0x4162d3['where'],{'$or':_[_0x71f9('0x33')](_0x4162d3[_0x71f9('0x3b')],function(_0x556b00){var _0x3404d5={};_0x3404d5[_0x556b00]={'$like':'%'+_0x6a686c[_0x71f9('0x37')][_0x71f9('0x41')]+'%'};return _0x3404d5;})});}_0x4162d3=_[_0x71f9('0x42')]({},_0x4162d3,_0x6a686c[_0x71f9('0x45')]);return _0x417d3d['getAgents'](_0x4162d3);}})[_0x71f9('0x29')](function(_0x5cf65f){if(_0x5cf65f){_0x3a4433=_0x5cf65f[_0x71f9('0x59')];if(!_0x6a686c[_0x71f9('0x37')][_0x71f9('0x3c')](_0x71f9('0x5a'))){_0x4162d3[_0x71f9('0x22')]=qs[_0x71f9('0x22')](_0x6a686c[_0x71f9('0x37')][_0x71f9('0x22')]);_0x4162d3['offset']=qs[_0x71f9('0x20')](_0x6a686c[_0x71f9('0x37')][_0x71f9('0x20')]);}return _0x417d3d[_0x71f9('0x8c')](_0x4162d3);}})['then'](function(_0x465aa2){if(_0x465aa2){return _0x465aa2?{'count':_0x3a4433,'rows':_0x465aa2}:null;}})['then'](respondWithResult(_0x5b866f,null))[_0x71f9('0x49')](handleError(_0x5b866f,null));};