a544fbc417a566f64d3fff21b51d4ffc94eecb4e
[motion2.git] / server / api / cmContact / cmContact.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 _0xf131=['fullname','alias','startCase','Account','closed','closedAt','disposition','secondDisposition','thirdDisposition','Tags','DESC','browserName','customerIp','customerPort','deviceModel','osName','ratingMessage','ratingType','ratingValue','referer','subject','externalUrl','threadId','findAndCountAll','Owner','flatten','sortBy','reverse','value','slice','moment','bluebird','path','lodash','squel','papaparse','../../components/contacts','../../components/parsers/qs','../../config/environment','../../mysqldb','defaults','localhost','socket.io-emitter','redis','register','status','json','offset','undefined','count','limit','set','Content-Range','update','then','destroy','end','sendStatus','error','stack','name','send','index','describe','getOptions','findAll','user','catch','CmContact','params','tags','keys','length','startsWith','cf_','columnName','displayName','query','analytics','custom','type','values','forEach','hasOwnProperty','merge','body','from','get','mergeWith','isArray','info','[CmContact][merge]\x20to\x20%s\x20from\x20%s','[CmContact][merge][destroy][CmHopper],\x20%s','CmHopperBlack','[CmContact][merge][destroy][CmHopperBlack],\x20%s','ChatInteraction','ChatMessage','ChatOfflineMessage','MailInteraction','MailMessage','SmsInteraction','SmsMessage','FaxInteraction','FaxMessage','OpenchannelInteraction','OpenchannelMessage','CmContactHasItem','CmHopperHistory','ReportAgentPreview','VoiceCallReport','MailQueueReport','SmsQueueReport','WhatsappInteraction','WhatsappQueueReport','create','join','isNil','dialTimezone','timezones','utcOffset','additionalPhones','bulkCreate','each','duplicates','where','$or','push','ListId','findOrCreate','contact','show','find','Tag','color','tag','Phones','sequelize','Phones.OrderBy','map','format','cf_%d','$and','CmCustomField','omit','pickBy','isEmpty','cm_contacts','id\x20=\x20?','text','phone','getTags','model','filters','attributes','intersection','fields','order','sort','pick','filter','nolimit','findOne','setTags','ids','setAdditionalPhones','all','ContactItemType','items','item_types','ItemTypeId','description','contact\x20phone\x20exists','PHONE','CmHopperAdditionalPhone','CmHopper','item','OrderBy','literal','`ItemType`.OrderBy','CmHopperId','ItemType.OrderBy','ItemType','duplicateAdditionalPhones','fromContactId','toContactId','rescheduleAdditionalPhones','getHoppers','rawAttributes','ContactId','options','includeAll','include','rows','getHopperHistories','getHopperFinals','CmHopperFinal','getJscriptySessions','JscriptySessionReport','uploadCsv','No\x20ListId\x20specified','binding_keys','binding_values','No\x20association\x20values\x20specified!','socket_timestamp','Unable\x20to\x20synchronize\x20socket,\x20no\x20timestamp\x20passed','split','Incorrect\x20binding\x20parameters\x20passed!','zipObject','firstName','No\x20firstName\x20column\x20specified','root','server/files/tmp','file','filename','chmodSync','0777','fork','../../components/import/cm/csv','CompanyId','File\x20uploaded\x20correctly!','upload','createReadStream','parse','meta','import','User','agent','No\x20phone\x20column\x20specified','binding','../../components/import/cm/csv2','stringify','message','pid','exit','error\x20child\x20process','importUpdates','child_process','../../components/import/cm/csvUpdates','start\x20child\x20process\x20correctly!','mail','chat','openchannel','whatsapp','voice','-createdAt','resolve','channel','includes','toLowerCase','range','Invalid\x20date\x20for\x20range\x20from\x20filter','Invalid\x20date\x20for\x20range\x20to\x20filter','Invalid\x20createdAt\x20filter','uniqueid','destination','callerid','starttime','endtime','duration','note','createdAt','userDisposition','userSecondDisposition','userThirdDisposition','UserId'];(function(_0x47ab99,_0x1b1eb3){var _0x3fa1a5=function(_0x85f150){while(--_0x85f150){_0x47ab99['push'](_0x47ab99['shift']());}};_0x3fa1a5(++_0x1b1eb3);}(_0xf131,0x127));var _0x1f13=function(_0xe19508,_0xafd97e){_0xe19508=_0xe19508-0x0;var _0x6305ff=_0xf131[_0xe19508];return _0x6305ff;};'use strict';var moment=require(_0x1f13('0x0'));var BPromise=require(_0x1f13('0x1'));var util=require('util');var path=require(_0x1f13('0x2'));var fs=require('fs');var _=require(_0x1f13('0x3'));var squel=require(_0x1f13('0x4'));var Papa=require(_0x1f13('0x5'));var Redis=require('ioredis');var contacts=require(_0x1f13('0x6'));var qs=require(_0x1f13('0x7'));var logger=require('../../config/logger')('api');var config=require(_0x1f13('0x8'));var shared=require('../../config/environment/shared');var db=require(_0x1f13('0x9'))['db'];config['redis']=_[_0x1f13('0xa')](config['redis'],{'host':_0x1f13('0xb'),'port':0x18eb});var socket=require(_0x1f13('0xc'))(new Redis(config[_0x1f13('0xd')]));require('./cmContact.socket')[_0x1f13('0xe')](socket);function respondWithResult(_0x38adaa,_0x3d971b){_0x3d971b=_0x3d971b||0xc8;return function(_0xa728ec){if(_0xa728ec){return _0x38adaa[_0x1f13('0xf')](_0x3d971b)[_0x1f13('0x10')](_0xa728ec);}};}function respondWithFilteredResult(_0xd15a7,_0x147633){return function(_0x3b514a){if(_0x3b514a){var _0x19f71a=typeof _0x147633[_0x1f13('0x11')]===_0x1f13('0x12')&&typeof _0x147633['limit']==='undefined';var _0x187fe9=_0x3b514a[_0x1f13('0x13')];var _0x32f7e5=_0x19f71a?0x0:_0x147633[_0x1f13('0x11')];var _0x3e17b0=_0x19f71a?_0x3b514a[_0x1f13('0x13')]:_0x147633[_0x1f13('0x11')]+_0x147633[_0x1f13('0x14')];var _0x2b39ae;if(_0x3e17b0>=_0x187fe9){_0x3e17b0=_0x187fe9;_0x2b39ae=0xc8;}else{_0x2b39ae=0xce;}_0xd15a7['status'](_0x2b39ae);return _0xd15a7[_0x1f13('0x15')](_0x1f13('0x16'),_0x32f7e5+'-'+_0x3e17b0+'/'+_0x187fe9)[_0x1f13('0x10')](_0x3b514a);}return null;};}function saveUpdates(_0x933dba){return function(_0x1cc201){if(_0x1cc201){return _0x1cc201[_0x1f13('0x17')](_0x933dba)[_0x1f13('0x18')](function(_0xf2926){return _0xf2926;});}return null;};}function removeEntity(_0x6ace24){return function(_0x8e35b5){if(_0x8e35b5){return _0x8e35b5[_0x1f13('0x19')]()[_0x1f13('0x18')](function(){_0x6ace24[_0x1f13('0xf')](0xcc)[_0x1f13('0x1a')]();});}};}function handleEntityNotFound(_0x59c7db){return function(_0x5154ba){if(!_0x5154ba){_0x59c7db[_0x1f13('0x1b')](0x194);}return _0x5154ba;};}function handleError(_0x3d427d,_0x413c55){_0x413c55=_0x413c55||0x1f4;return function(_0x45b4be){logger[_0x1f13('0x1c')](_0x45b4be[_0x1f13('0x1d')]);if(_0x45b4be[_0x1f13('0x1e')]){delete _0x45b4be[_0x1f13('0x1e')];}_0x3d427d[_0x1f13('0xf')](_0x413c55)[_0x1f13('0x1f')](_0x45b4be);};}exports[_0x1f13('0x20')]=function(_0x54c98e,_0x2742c3){var _0x286816={};return contacts[_0x1f13('0x21')]()[_0x1f13('0x18')](function(_0x7c2c47){_0x286816=qs[_0x1f13('0x22')](_0x7c2c47,_0x54c98e);return contacts[_0x1f13('0x23')](_0x54c98e['query'],_0x54c98e[_0x1f13('0x24')],_0x286816,_0x7c2c47);})[_0x1f13('0x18')](respondWithFilteredResult(_0x2742c3,_0x286816))[_0x1f13('0x25')](handleError(_0x2742c3,null));};exports[_0x1f13('0x19')]=function(_0x2ec940,_0x56487b){return db[_0x1f13('0x26')]['find']({'where':{'id':_0x2ec940[_0x1f13('0x27')]['id']}})[_0x1f13('0x18')](handleEntityNotFound(_0x56487b,null))[_0x1f13('0x18')](removeEntity(_0x56487b,null))[_0x1f13('0x25')](handleError(_0x56487b,null));};exports[_0x1f13('0x21')]=function(_0x44896d,_0x112497){return db[_0x1f13('0x26')][_0x1f13('0x21')]()[_0x1f13('0x18')](function(_0x5e90b7){delete _0x5e90b7[_0x1f13('0x28')];for(var _0x22808d=0x0;_0x22808d<Object[_0x1f13('0x29')](_0x5e90b7)[_0x1f13('0x2a')];_0x22808d++){var _0x33f421=Object['keys'](_0x5e90b7)[_0x22808d];if(!_[_0x1f13('0x2b')](_0x33f421,_0x1f13('0x2c'))){_0x5e90b7[_0x33f421][_0x1f13('0x2d')]=_0x33f421;_0x5e90b7[_0x33f421][_0x1f13('0x2e')]=_0x33f421;}}return _0x5e90b7;})[_0x1f13('0x18')](function(_0x5da075){var _0x2a0997={'where':Boolean(_0x44896d[_0x1f13('0x2f')][_0x1f13('0x30')])===!![]?undefined:{'$or':[{'ListId':_0x44896d['query']['id']},{'ListId':null}]},'raw':!![]};return db['CmCustomField'][_0x1f13('0x23')](_0x2a0997)[_0x1f13('0x18')](function(_0xbe0070){for(var _0x379464=0x0;_0x379464<_0xbe0070[_0x1f13('0x2a')];_0x379464++){var _0x3ce9bc='cf_'+_0xbe0070[_0x379464]['id'];if(_0x5da075[_0x3ce9bc]){_0x5da075[_0x3ce9bc][_0x1f13('0x2d')]=_0x3ce9bc;_0x5da075[_0x3ce9bc]['displayName']=_0xbe0070[_0x379464]['alias'];_0x5da075[_0x3ce9bc][_0x1f13('0x31')]={'type':_0xbe0070[_0x379464][_0x1f13('0x32')],'values':_0xbe0070[_0x379464][_0x1f13('0x33')]};}else{logger['info']('Custom\x20field\x20%s\x20not\x20found\x20on\x20cm_contacts\x20table',_0x3ce9bc);}}Object['keys'](_0x5da075)[_0x1f13('0x34')](function(_0x594a33){if(!_0x5da075[_0x594a33][_0x1f13('0x35')](_0x1f13('0x2d'))){delete _0x5da075[_0x594a33];}});return _0x5da075;});})[_0x1f13('0x18')](respondWithResult(_0x112497,null))[_0x1f13('0x25')](handleError(_0x112497,null));};exports[_0x1f13('0x36')]=function(_0x2a8630,_0xb6f68a){var _0x485520;var _0x1507ba;var _0x6b2bcf;if(_0x2a8630[_0x1f13('0x37')]['id']){delete _0x2a8630['body']['id'];}return db['CmContact'][_0x1f13('0x23')]({'where':{'id':[_0x2a8630[_0x1f13('0x37')]['to'],_0x2a8630['body'][_0x1f13('0x38')]]}})[_0x1f13('0x18')](function(_0x1daa2e){if(_0x1daa2e){if(_0x1daa2e[_0x1f13('0x2a')]!==0x2){return null;}_0x1daa2e[0x0]=_0x1daa2e[0x0][_0x1f13('0x39')]({'plain':!![]});_0x1daa2e[0x1]=_0x1daa2e[0x1][_0x1f13('0x39')]({'plain':!![]});delete _0x1daa2e[0x0]['id'];delete _0x1daa2e[0x1]['id'];return _[_0x1f13('0x3a')](_0x1daa2e[0x0],_0x1daa2e[0x1],function(_0xa6510d,_0x470e72){if(Array[_0x1f13('0x3b')](_0x470e72)&&!_0x470e72[_0x1f13('0x2a')]){return _0xa6510d;}else if(!_0x470e72){return _0xa6510d;}});}})[_0x1f13('0x18')](handleEntityNotFound(_0xb6f68a,null))['then'](function(_0x2375ab){if(_0x2375ab){if(_0x2375ab[_0x1f13('0x28')]&&_['isArray'](_0x2375ab[_0x1f13('0x28')])){_0x2375ab[_0x1f13('0x28')]=_0x2375ab['tags']['join']();}logger[_0x1f13('0x3c')](_0x1f13('0x3d'),_0x2a8630['body']['to'],_0x2a8630[_0x1f13('0x37')][_0x1f13('0x38')]);return db['CmHopper'][_0x1f13('0x19')]({'where':{'ContactId':[_0x2a8630[_0x1f13('0x37')]['to'],_0x2a8630[_0x1f13('0x37')][_0x1f13('0x38')]]}})[_0x1f13('0x18')](function(_0x4d2408){logger['info'](_0x1f13('0x3e'),_0x4d2408);return db[_0x1f13('0x3f')][_0x1f13('0x19')]({'where':{'ContactId':[_0x2a8630[_0x1f13('0x37')]['to'],_0x2a8630['body'][_0x1f13('0x38')]]}})[_0x1f13('0x18')](function(_0x6c74b2){logger[_0x1f13('0x3c')](_0x1f13('0x40'),_0x6c74b2);return db[_0x1f13('0x26')]['create'](_0x2375ab);});});}})['then'](function(_0x14bfa4){if(_0x14bfa4){_0x485520=_0x14bfa4;_0x1507ba={'ContactId':_0x485520['id']};_0x6b2bcf={'where':{'ContactId':[_0x2a8630[_0x1f13('0x37')]['to'],_0x2a8630[_0x1f13('0x37')][_0x1f13('0x38')]]}};return db[_0x1f13('0x41')]['update'](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x42')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})['then'](function(){if(_0x485520){return db[_0x1f13('0x43')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})['then'](function(){if(_0x485520){return db[_0x1f13('0x44')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x45')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x44')]['update'](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x45')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})['then'](function(){if(_0x485520){return db[_0x1f13('0x46')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x47')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x48')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x49')]['update'](_0x1507ba,_0x6b2bcf);}})['then'](function(){if(_0x485520){return db[_0x1f13('0x4a')]['update'](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x4b')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x4c')][_0x1f13('0x17')]({'CmContactId':_0x485520['id']},{'where':{'CmContactId':[_0x2a8630['body']['to'],_0x2a8630[_0x1f13('0x37')]['from']]}});}})[_0x1f13('0x18')](function(){if(_0x485520){return db['CmHopperFinal']['update'](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x4d')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})['then'](function(){if(_0x485520){return db[_0x1f13('0x4e')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})['then'](function(){if(_0x485520){return db[_0x1f13('0x4f')]['update'](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db['ChatQueueReport'][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db['FaxQueueReport'][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x50')]['update'](_0x1507ba,_0x6b2bcf);}})['then'](function(){if(_0x485520){return db['OpenchannelQueueReport'][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x51')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db['JscriptySessionReport'][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x52')]['update'](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db['WhatsappMessage'][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})[_0x1f13('0x18')](function(){if(_0x485520){return db[_0x1f13('0x53')][_0x1f13('0x17')](_0x1507ba,_0x6b2bcf);}})['then'](function(){if(_0x485520){return db[_0x1f13('0x26')]['destroy']({'where':{'id':[_0x2a8630[_0x1f13('0x37')]['to'],_0x2a8630[_0x1f13('0x37')][_0x1f13('0x38')]]}});}})[_0x1f13('0x18')](function(){if(_0x485520){return _0x485520;}})['then'](respondWithResult(_0xb6f68a,null))['catch'](handleError(_0xb6f68a,null));};exports[_0x1f13('0x54')]=function(_0x457b5b,_0x35f330){return db[_0x1f13('0x26')][_0x1f13('0x21')]()[_0x1f13('0x18')](function(_0x26ce2c){if(_0x457b5b[_0x1f13('0x37')][_0x1f13('0x28')]&&_[_0x1f13('0x3b')](_0x457b5b[_0x1f13('0x37')][_0x1f13('0x28')])){if(_0x457b5b[_0x1f13('0x37')]['tags'][_0x1f13('0x2a')]){_0x457b5b[_0x1f13('0x37')][_0x1f13('0x28')]=_0x457b5b['body']['tags'][_0x1f13('0x55')](';')+';';}else{_0x457b5b[_0x1f13('0x37')][_0x1f13('0x28')]=null;}}else{_0x457b5b['body']['tags']=null;}if(!_[_0x1f13('0x56')](_0x457b5b[_0x1f13('0x37')][_0x1f13('0x57')])){var _0x54a446=_0x457b5b[_0x1f13('0x37')][_0x1f13('0x57')];if(!_[_0x1f13('0x56')](shared[_0x1f13('0x58')][_0x54a446]))_0x457b5b[_0x1f13('0x37')][_0x1f13('0x59')]=shared[_0x1f13('0x58')][_0x54a446][_0x1f13('0x59')];}return db[_0x1f13('0x26')][_0x1f13('0x54')](_0x457b5b[_0x1f13('0x37')],{'fields':_[_0x1f13('0x29')](_0x26ce2c),'raw':!![]});})[_0x1f13('0x18')](function(_0x5be284){if(_0x457b5b['body'][_0x1f13('0x5a')]){return insertAdditionalPhones(_0x5be284,_0x457b5b[_0x1f13('0x37')][_0x1f13('0x5a')]);}else{return _0x5be284;}})[_0x1f13('0x18')](respondWithResult(_0x35f330,0xc9))[_0x1f13('0x25')](handleError(_0x35f330,null));};exports[_0x1f13('0x5b')]=function(_0x5927cb,_0x4fd23b){var _0x312be7=0x0;var _0x2a94b9=0x0;var _0x118860=0x0;var _0x16ecc2=[];return db[_0x1f13('0x26')][_0x1f13('0x21')]()[_0x1f13('0x18')](function(_0x21adca){return BPromise[_0x1f13('0x5c')](_0x5927cb[_0x1f13('0x37')],function(_0x1d312d){var _0x242874={'where':{'$or':[]},'defaults':_0x1d312d,'fields':_[_0x1f13('0x29')](_0x21adca),'raw':!![]};if(_0x1d312d[_0x1f13('0x5d')]){for(var _0xe85d3=0x0;_0xe85d3<_0x1d312d[_0x1f13('0x5d')][_0x1f13('0x2a')];_0xe85d3++){var _0x43d055={};_0x43d055[_0x1d312d[_0x1f13('0x5d')][_0xe85d3]]=_0x1d312d[_0x1d312d['duplicates'][_0xe85d3]];_0x242874[_0x1f13('0x5e')][_0x1f13('0x5f')][_0x1f13('0x60')](_0x43d055);}_0x242874['where']['ListId']=_0x1d312d[_0x1f13('0x61')];}return db[_0x1f13('0x26')][_0x1f13('0x62')](_0x242874)['spread'](function(_0x1f1316,_0x187f26){if(_0x187f26){if(_0x1d312d['hasOwnProperty'](_0x1f13('0x5a'))){_0x16ecc2['push']({'contact':_0x1f1316,'additionalPhones':_0x1d312d[_0x1f13('0x5a')]});}_0x312be7++;}else{_0x118860++;}})[_0x1f13('0x25')](function(){_0x2a94b9++;});});})[_0x1f13('0x18')](function(_0x1c3c4e){if(_0x1c3c4e){if(_0x16ecc2[_0x1f13('0x2a')]>0x0){return new Promise(function(_0x4e1d42){var _0x1f023d=[];for(var _0x56df55=0x0;_0x56df55<_0x16ecc2[_0x1f13('0x2a')];_0x56df55++){_0x1f023d[_0x1f13('0x60')](insertAdditionalPhones(_0x16ecc2[_0x56df55][_0x1f13('0x63')],_0x16ecc2[_0x56df55][_0x1f13('0x5a')]));}Promise['all'](_0x1f023d)[_0x1f13('0x18')](function(){_0x4e1d42(0x1);});})[_0x1f13('0x18')](function(){return{'success':_0x312be7,'failure':_0x2a94b9,'duplicates':_0x118860};});}else{return{'success':_0x312be7,'failure':_0x2a94b9,'duplicates':_0x118860};}}})[_0x1f13('0x18')](respondWithResult(_0x4fd23b,0xc9))[_0x1f13('0x25')](handleError(_0x4fd23b,null));};exports[_0x1f13('0x64')]=function(_0x441620,_0x2b14d1){var _0x3c6c68;return db['CmContact'][_0x1f13('0x21')]()[_0x1f13('0x18')](function(_0x1a2eb6){return db[_0x1f13('0x26')][_0x1f13('0x65')]({'where':{'id':_0x441620[_0x1f13('0x27')]['id']},'include':[{'model':db[_0x1f13('0x66')],'as':'Tags','attributes':['id',_0x1f13('0x1e'),_0x1f13('0x67')],'required':_0x441620[_0x1f13('0x2f')][_0x1f13('0x68')]?!![]:![]},{'model':db['CmContactHasItem'],'as':_0x1f13('0x69'),'where':{'CmContactId':_0x441620['params']['id']},'required':![],'include':[{'model':db['ContactItemType'],'as':'ItemType'}]}],'order':[db[_0x1f13('0x6a')]['literal'](_0x1f13('0x6b'))],'attributes':_[_0x1f13('0x29')](_0x1a2eb6)});})[_0x1f13('0x18')](function(_0x4efb61){if(!_[_0x1f13('0x56')](_0x4efb61)){_0x3c6c68=_0x4efb61[_0x1f13('0x39')]({'plain':!![]});var _0x4bf89b={'where':{'$and':[{'ListId':{'$ne':null}}]}};if(_0x4efb61&&_0x4efb61[_0x1f13('0x61')]){if(_0x4efb61[_0x1f13('0x61')]){_0x4bf89b['where']['$and']['push']({'ListId':{'$ne':_0x4efb61[_0x1f13('0x61')]}});}}return db['CmCustomField']['findAll'](_0x4bf89b);}})[_0x1f13('0x18')](function(_0x49203e){if(!_['isNil'](_0x49203e)){return _['omit'](_0x3c6c68,_[_0x1f13('0x6c')](_0x49203e,function(_0x2730b9){return util[_0x1f13('0x6d')](_0x1f13('0x6e'),_0x2730b9['id']);}));}})[_0x1f13('0x18')](handleEntityNotFound(_0x2b14d1,null))[_0x1f13('0x18')](respondWithResult(_0x2b14d1,null))[_0x1f13('0x25')](handleError(_0x2b14d1,null));};exports['update']=function(_0x4b87c0,_0x1e4de1){var _0x113884;if(_0x4b87c0[_0x1f13('0x37')]['id']){delete _0x4b87c0['body']['id'];}if(!_[_0x1f13('0x56')](_0x4b87c0[_0x1f13('0x37')][_0x1f13('0x57')])){var _0x74e5c0=_0x4b87c0['body']['dialTimezone'];if(!_['isNil'](shared[_0x1f13('0x58')][_0x74e5c0]))_0x4b87c0[_0x1f13('0x37')][_0x1f13('0x59')]=shared[_0x1f13('0x58')][_0x74e5c0][_0x1f13('0x59')];}return db[_0x1f13('0x26')]['find']({'where':{'id':_0x4b87c0[_0x1f13('0x27')]['id']}})[_0x1f13('0x18')](handleEntityNotFound(_0x1e4de1,null))['then'](saveUpdates(_0x4b87c0['body'],null))[_0x1f13('0x18')](function(_0x1bc8a7){_0x113884=_0x1bc8a7;var _0x292abe={'where':{'$and':[{'ListId':{'$ne':null}}]}};if(_0x1bc8a7&&_0x1bc8a7['ListId']){if(_0x1bc8a7[_0x1f13('0x61')]){_0x292abe[_0x1f13('0x5e')][_0x1f13('0x6f')]['push']({'ListId':{'$ne':_0x1bc8a7[_0x1f13('0x61')]}});}}return db[_0x1f13('0x70')][_0x1f13('0x23')](_0x292abe);})['then'](function(_0x5a74b4){if(_0x5a74b4){return _[_0x1f13('0x71')](_[_0x1f13('0x72')](_0x4b87c0[_0x1f13('0x37')],function(_0x5dbd62,_0x5b898c){return _[_0x1f13('0x2b')](_0x5b898c,_0x1f13('0x2c'));}),_['map'](_0x5a74b4,function(_0x56d940){return util[_0x1f13('0x6d')](_0x1f13('0x6e'),_0x56d940['id']);}));}})[_0x1f13('0x18')](function(_0x2a1c35){if(_0x2a1c35&&!_[_0x1f13('0x73')](_0x2a1c35)){var _0x213139=squel[_0x1f13('0x17')]()['table'](_0x1f13('0x74'))['setFields'](_0x2a1c35)[_0x1f13('0x5e')](_0x1f13('0x75'),_0x4b87c0['params']['id'])['toParam']();return db[_0x1f13('0x6a')][_0x1f13('0x2f')](_0x213139[_0x1f13('0x76')],{'replacements':_0x213139['values']});}})[_0x1f13('0x18')](function(){if(_0x4b87c0[_0x1f13('0x37')][_0x1f13('0x77')]&&_0x113884){return db['CmHopper'][_0x1f13('0x17')]({'phone':_0x4b87c0['body'][_0x1f13('0x77')],'OrderBy':0x0},{'where':{'ContactId':_0x113884['id']}});}})[_0x1f13('0x18')](function(){return _0x113884;})[_0x1f13('0x18')](respondWithResult(_0x1e4de1,null))[_0x1f13('0x25')](handleError(_0x1e4de1,null));};exports[_0x1f13('0x78')]=function(_0x4b234c,_0x5f3aa4){var _0x7a9512={};var _0x1056b9={};var _0x5826ea;var _0xbd7e1f;return db[_0x1f13('0x26')]['findOne']({'where':{'id':_0x4b234c[_0x1f13('0x27')]['id']}})[_0x1f13('0x18')](handleEntityNotFound(_0x5f3aa4,null))['then'](function(_0x229cf9){if(_0x229cf9){_0x5826ea=_0x229cf9;_0x1056b9[_0x1f13('0x79')]=_[_0x1f13('0x29')](db['Tag']['rawAttributes']);_0x1056b9[_0x1f13('0x2f')]=_[_0x1f13('0x29')](_0x4b234c[_0x1f13('0x2f')]);_0x1056b9[_0x1f13('0x7a')]=_['intersection'](_0x1056b9[_0x1f13('0x79')],_0x1056b9[_0x1f13('0x2f')]);_0x7a9512[_0x1f13('0x7b')]=_[_0x1f13('0x7c')](_0x1056b9[_0x1f13('0x79')],qs[_0x1f13('0x7d')](_0x4b234c['query'][_0x1f13('0x7d')]));_0x7a9512['attributes']=_0x7a9512[_0x1f13('0x7b')][_0x1f13('0x2a')]?_0x7a9512[_0x1f13('0x7b')]:_0x1056b9['model'];_0x7a9512[_0x1f13('0x7e')]=qs['sort'](_0x4b234c[_0x1f13('0x2f')][_0x1f13('0x7f')]);_0x7a9512[_0x1f13('0x5e')]=qs['filters'](_[_0x1f13('0x80')](_0x4b234c[_0x1f13('0x2f')],_0x1056b9[_0x1f13('0x7a')]));if(_0x4b234c[_0x1f13('0x2f')][_0x1f13('0x81')]){_0x7a9512[_0x1f13('0x5e')]=_['merge'](_0x7a9512['where'],{'$or':_[_0x1f13('0x6c')](_0x7a9512[_0x1f13('0x7b')],function(_0x57f9ca){var _0x39002f={};_0x39002f[_0x57f9ca]={'$like':'%'+_0x4b234c['query'][_0x1f13('0x81')]+'%'};return _0x39002f;})});}_0x7a9512=_[_0x1f13('0x36')]({},_0x7a9512,_0x4b234c['options']);return _0x5826ea[_0x1f13('0x78')](_0x7a9512);}})[_0x1f13('0x18')](function(_0x492038){if(_0x492038){_0xbd7e1f=_0x492038['length'];if(!_0x4b234c[_0x1f13('0x2f')][_0x1f13('0x35')](_0x1f13('0x82'))){_0x7a9512[_0x1f13('0x14')]=qs[_0x1f13('0x14')](_0x4b234c['query']['limit']);_0x7a9512[_0x1f13('0x11')]=qs[_0x1f13('0x11')](_0x4b234c[_0x1f13('0x2f')]['offset']);}return _0x5826ea[_0x1f13('0x78')](_0x7a9512);}})['then'](function(_0x287e1f){if(_0x287e1f){return _0x287e1f?{'count':_0xbd7e1f,'rows':_0x287e1f}:null;}})[_0x1f13('0x18')](respondWithResult(_0x5f3aa4,null))[_0x1f13('0x25')](handleError(_0x5f3aa4,null));};exports['setTags']=function(_0x1c5418,_0x241127){if(_0x1c5418['body']['id']){delete _0x1c5418[_0x1f13('0x37')]['id'];}return db[_0x1f13('0x26')][_0x1f13('0x83')]({'where':{'id':_0x1c5418[_0x1f13('0x27')]['id']}})['then'](handleEntityNotFound(_0x241127,null))['then'](function(_0x33a312){if(_0x33a312){return _0x33a312[_0x1f13('0x84')](_0x1c5418[_0x1f13('0x37')][_0x1f13('0x85')]||[]);}return null;})[_0x1f13('0x18')](respondWithResult(_0x241127,null))[_0x1f13('0x25')](handleError(_0x241127,null));};exports[_0x1f13('0x86')]=function(_0x58ee59,_0x364986){var _0x228f4b=[];_0x228f4b[_0x1f13('0x60')](addAdditionalPhones(_0x58ee59[_0x1f13('0x37')]));Promise['all'](_0x228f4b)[_0x1f13('0x18')](function(){_0x228f4b=[];_0x228f4b[_0x1f13('0x60')](deleteCmHopperPhones(_0x58ee59['body']['id']));_0x228f4b[_0x1f13('0x60')](addCmHopperPhones(_0x58ee59[_0x1f13('0x37')]['id']));Promise[_0x1f13('0x87')](_0x228f4b)[_0x1f13('0x18')](function(){return[0x1];})[_0x1f13('0x18')](respondWithResult(_0x364986,null));});};function addAdditionalPhones(_0x5a0785){var _0x4e6328;return new Promise(function(_0x184dfa){return db[_0x1f13('0x4c')][_0x1f13('0x19')]({'where':{'CmContactId':_0x5a0785['id']}})['then'](function(){db[_0x1f13('0x88')][_0x1f13('0x23')]({'raw':!![],'attributes':['id','OrderBy']})['then'](function(_0x2fd62c){var _0xd0b53e=[];for(var _0x34f4ed=0x0;_0x34f4ed<_0x5a0785[_0x1f13('0x89')][_0x1f13('0x2a')];_0x34f4ed++){var _0x5277c5=_0x5a0785['item_types']?_[_0x1f13('0x56')](_0x5a0785[_0x1f13('0x8a')][_0x34f4ed])?'':_0x5a0785[_0x1f13('0x8a')][_0x34f4ed]:_0x5a0785[_0x1f13('0x89')][_0x34f4ed]['ItemTypeId']?_0x5a0785[_0x1f13('0x89')][_0x34f4ed][_0x1f13('0x8b')]:'';var _0x42b794=_['isNil'](_0x5a0785['items'][_0x34f4ed]['description'])?'':_0x5a0785[_0x1f13('0x89')][_0x34f4ed][_0x1f13('0x8c')];var _0x30ffa4=_0x5277c5+':'+_0x5a0785[_0x1f13('0x89')][_0x34f4ed][_0x1f13('0x77')]+'ยง'+_0x42b794;if(_[_0x1f13('0x56')](_0x4e6328))_0x4e6328=_0x30ffa4;else _0x4e6328+=';'+_0x30ffa4;var _0xc08cb4=null;var _0x2eb25b=null;if(_0x5a0785[_0x1f13('0x8a')]){if(!_[_0x1f13('0x56')](_0x5a0785[_0x1f13('0x8a')][_0x34f4ed])){_0x2eb25b=_[_0x1f13('0x65')](_0x2fd62c,function(_0xcccf85){return _0xcccf85['id']===_0x5a0785[_0x1f13('0x8a')][_0x34f4ed];});if(!_[_0x1f13('0x56')](_0x2eb25b)){_0xc08cb4=_0x2eb25b['OrderBy'];}}}else{if(!_[_0x1f13('0x56')](_0x5a0785[_0x1f13('0x89')][_0x34f4ed])){_0x2eb25b=_['find'](_0x2fd62c,function(_0x4a4c74){return _0x4a4c74['id']===_0x5a0785[_0x1f13('0x89')][_0x34f4ed][_0x1f13('0x8b')];});if(!_['isNil'](_0x2eb25b)){_0xc08cb4=_0x34f4ed;}}}if(_['isNil'](_0xc08cb4)){_0xc08cb4=0x64;}_0xd0b53e[_0x1f13('0x60')](addAdditionalPhone(_0x5a0785['id'],_0x5a0785[_0x1f13('0x89')][_0x34f4ed][_0x1f13('0x77')],_0x5a0785[_0x1f13('0x8a')]?_['isNil'](_0x5a0785[_0x1f13('0x8a')][_0x34f4ed])?null:_0x5a0785[_0x1f13('0x8a')][_0x34f4ed]:_0x5a0785['items'][_0x34f4ed][_0x1f13('0x8b')]?_0x5a0785['items'][_0x34f4ed][_0x1f13('0x8b')]:null,_0xc08cb4,_0x42b794));}Promise[_0x1f13('0x87')](_0xd0b53e)[_0x1f13('0x18')](function(){db[_0x1f13('0x26')][_0x1f13('0x17')]({'AdditionalPhones':_0x4e6328},{'where':{'id':_0x5a0785['id']}})[_0x1f13('0x18')](function(){_0x184dfa(0x1);});});});});});}function addAdditionalPhone(_0x1e39a5,_0x109078,_0xd05a8f,_0x237f90,_0x20876d){return db[_0x1f13('0x4c')]['find']({'where':{'CmContactId':_0x1e39a5,'item':_0x109078}})['then'](function(_0x1860d1){if(_0x1860d1)logger[_0x1f13('0x3c')](_0x1f13('0x8d'),_0x1860d1);else{db[_0x1f13('0x4c')][_0x1f13('0x54')]({'CmContactId':_0x1e39a5,'item':_0x109078,'ItemTypeId':_0xd05a8f,'OrderBy':_0x237f90,'ItemClass':_0x1f13('0x8e'),'description':_0x20876d});}});}function deleteCmHopperPhones(_0x9fac35){return new Promise(function(_0x4a74aa){return db['CmHopper'][_0x1f13('0x23')]({'raw':!![],'attributes':['id'],'where':{'ContactId':_0x9fac35}})[_0x1f13('0x18')](function(_0x2a718c){db[_0x1f13('0x8f')][_0x1f13('0x19')]({'where':{'CmHopperId':{'$in':_['map'](_0x2a718c,'id')}}});})[_0x1f13('0x18')](function(){_0x4a74aa(0x1);});});}function addCmHopperPhones(_0x643795){return new Promise(function(_0x3ac17e){return db[_0x1f13('0x90')][_0x1f13('0x23')]({'raw':!![],'attributes':['id'],'where':{'ContactId':_0x643795}})[_0x1f13('0x18')](function(_0x4c496a){var _0x482d53=[];_[_0x1f13('0x6c')](_0x4c496a,function(_0x55fcff){_0x482d53[_0x1f13('0x60')](createCmHopperPhone(_0x643795,_0x55fcff['id']));});Promise[_0x1f13('0x87')](_0x482d53)[_0x1f13('0x18')](function(){_0x3ac17e(0x1);});});});}function createCmHopperPhone(_0x2a2bdc,_0x558d94){return new Promise(function(_0x59250a){db[_0x1f13('0x4c')][_0x1f13('0x23')]({'raw':!![],'attributes':[_0x1f13('0x91'),'OrderBy'],'where':{'CmContactId':_0x2a2bdc,'ItemClass':_0x1f13('0x8e')},'include':[{'model':db['ContactItemType'],'attributes':[_0x1f13('0x92')],'as':'ItemType'}],'order':[_0x1f13('0x92'),db[_0x1f13('0x6a')][_0x1f13('0x93')](_0x1f13('0x94'))]})[_0x1f13('0x18')](function(_0x5b7cff){var _0x5499b5=0x1;var _0x1f694e=_[_0x1f13('0x6c')](_0x5b7cff,function(_0x23c082){_0x23c082[_0x1f13('0x95')]=_0x558d94;_0x23c082[_0x1f13('0x77')]=_0x23c082[_0x1f13('0x91')];_0x23c082[_0x1f13('0x92')]=_0x5499b5;delete _0x23c082[_0x1f13('0x96')];_0x5499b5=_0x5499b5+0x1;return _0x23c082;});return db[_0x1f13('0x8f')][_0x1f13('0x5b')](_0x1f694e,{});})[_0x1f13('0x18')](function(){_0x59250a(0x1);});});}function insertAdditionalPhones(_0x7e99e2,_0x7d6270){return new Promise(function(_0x58b13c){var _0x147354=[];for(var _0x6469ed=0x0;_0x6469ed<_0x7d6270[_0x1f13('0x2a')];_0x6469ed++){var _0x2a030f=_0x7d6270[_0x6469ed];_0x147354['push'](parseContactItemType(_0x2a030f));}Promise[_0x1f13('0x87')](_0x147354)['then'](function(){var _0x433cd4={'id':_0x7e99e2['id'],'items':_0x7d6270};return addAdditionalPhones(_0x433cd4)[_0x1f13('0x18')](function(){return addCmHopperPhones(_0x7e99e2['id']);});})[_0x1f13('0x18')](function(){_0x58b13c(_0x7e99e2);});});}function parseContactItemType(_0x2f1c21){return new Promise(function(_0x53253a){if(_0x2f1c21[_0x1f13('0x35')]('ItemTypeId')){_0x53253a(_0x2f1c21);}else{return db['ContactItemType'][_0x1f13('0x65')]({'where':{'name':_0x2f1c21[_0x1f13('0x97')]}})[_0x1f13('0x18')](function(_0x6c4f60){if(_[_0x1f13('0x56')](_0x6c4f60))_0x2f1c21['ItemTypeId']=null;else _0x2f1c21[_0x1f13('0x8b')]=_0x6c4f60['id'];_0x53253a(_0x2f1c21);});}});}exports['getAdditionalPhones']=function(_0x38dbe1,_0x51dffd){return db[_0x1f13('0x4c')][_0x1f13('0x23')]({'raw':!![],'where':{'CmContactId':_0x38dbe1[_0x1f13('0x27')]['id'],'ItemClass':'PHONE'},'include':[{'model':db[_0x1f13('0x88')],'as':'ItemType'}],'order':[['OrderBy'],[db[_0x1f13('0x6a')][_0x1f13('0x93')](_0x1f13('0x94'))]]})[_0x1f13('0x18')](function(_0x5e38a6){return _0x5e38a6;})[_0x1f13('0x18')](respondWithResult(_0x51dffd,null))['catch'](handleError(_0x51dffd,null));};exports[_0x1f13('0x98')]=function(_0x56fce5,_0x43427f){return db['CmContactHasItem'][_0x1f13('0x23')]({'raw':!![],'where':{'CmContactId':_0x56fce5[_0x1f13('0x37')][_0x1f13('0x99')]}})[_0x1f13('0x18')](function(_0x57f71a){var _0x518928=_[_0x1f13('0x6c')](_0x57f71a,function(_0x1d57d7){_0x1d57d7['CmContactId']=_0x56fce5['body'][_0x1f13('0x9a')];return _0x1d57d7;});return db['CmContactHasItem'][_0x1f13('0x5b')](_0x518928,{});})[_0x1f13('0x18')](function(){return addCmHopperPhones(_0x56fce5[_0x1f13('0x37')]['toContactId']);})[_0x1f13('0x18')](function(){return['ok'];})[_0x1f13('0x18')](respondWithResult(_0x43427f,null))['catch'](handleError(_0x43427f,null));};exports[_0x1f13('0x9b')]=function(_0x5c62a2,_0x4d1ae0){var _0xb3e1d4=[];_0xb3e1d4[_0x1f13('0x60')](createCmHopperPhone(_0x5c62a2['body']['id'],_0x5c62a2[_0x1f13('0x37')]['hopperId']));Promise[_0x1f13('0x87')](_0xb3e1d4)[_0x1f13('0x18')](function(){return[0x1];})[_0x1f13('0x18')](respondWithResult(_0x4d1ae0,null));};exports[_0x1f13('0x9c')]=function(_0x2a9d84,_0x19cca6){var _0x4a00ad={'raw':![],'where':{}};var _0x338352={};var _0x39cd35={'count':0x0,'rows':[]};return db[_0x1f13('0x26')]['findOne']({'where':{'id':_0x2a9d84[_0x1f13('0x27')]['id']}})['then'](handleEntityNotFound(_0x19cca6,null))[_0x1f13('0x18')](function(_0x2d79c3){if(_0x2d79c3){_0x338352[_0x1f13('0x79')]=_[_0x1f13('0x29')](db['CmHopper'][_0x1f13('0x9d')]);_0x338352[_0x1f13('0x2f')]=_[_0x1f13('0x29')](_0x2a9d84['query']);_0x338352[_0x1f13('0x7a')]=_['intersection'](_0x338352[_0x1f13('0x79')],_0x338352[_0x1f13('0x2f')]);_0x4a00ad[_0x1f13('0x7b')]=_[_0x1f13('0x7c')](_0x338352[_0x1f13('0x79')],qs['fields'](_0x2a9d84[_0x1f13('0x2f')][_0x1f13('0x7d')]));_0x4a00ad[_0x1f13('0x7b')]=_0x4a00ad[_0x1f13('0x7b')][_0x1f13('0x2a')]?_0x4a00ad[_0x1f13('0x7b')]:_0x338352[_0x1f13('0x79')];if(!_0x2a9d84['query'][_0x1f13('0x35')]('nolimit')){_0x4a00ad[_0x1f13('0x14')]=qs['limit'](_0x2a9d84[_0x1f13('0x2f')][_0x1f13('0x14')]);_0x4a00ad[_0x1f13('0x11')]=qs[_0x1f13('0x11')](_0x2a9d84[_0x1f13('0x2f')][_0x1f13('0x11')]);}_0x4a00ad[_0x1f13('0x7e')]=qs[_0x1f13('0x7f')](_0x2a9d84[_0x1f13('0x2f')]['sort']);_0x4a00ad[_0x1f13('0x5e')]=qs[_0x1f13('0x7a')](_[_0x1f13('0x80')](_0x2a9d84['query'],_0x338352['filters']));_0x4a00ad[_0x1f13('0x5e')][_0x1f13('0x9e')]=_0x2d79c3['id'];if(_0x2a9d84[_0x1f13('0x2f')]['filter']){_0x4a00ad['where']=_[_0x1f13('0x36')](_0x4a00ad[_0x1f13('0x5e')],{'$or':_[_0x1f13('0x6c')](_0x4a00ad[_0x1f13('0x7b')],function(_0x129c10){var _0x19d18b={};_0x19d18b[_0x129c10]={'$like':'%'+_0x2a9d84[_0x1f13('0x2f')]['filter']+'%'};return _0x19d18b;})});}_0x4a00ad=_['merge']({},_0x4a00ad,_0x2a9d84[_0x1f13('0x9f')]);return db[_0x1f13('0x90')][_0x1f13('0x13')]({'where':_0x4a00ad[_0x1f13('0x5e')]})[_0x1f13('0x18')](function(_0x20c1ac){_0x39cd35['count']=_0x20c1ac;if(_0x2a9d84[_0x1f13('0x2f')][_0x1f13('0xa0')]){_0x4a00ad[_0x1f13('0xa1')]=[{'all':!![]}];}return db[_0x1f13('0x90')]['findAll'](_0x4a00ad);})[_0x1f13('0x18')](function(_0x46aa33){_0x39cd35[_0x1f13('0xa2')]=_0x46aa33;return _0x39cd35;});}})['then'](respondWithFilteredResult(_0x19cca6,_0x4a00ad))[_0x1f13('0x25')](handleError(_0x19cca6,null));};exports[_0x1f13('0xa3')]=function(_0x252700,_0x2d2eae){var _0x2a4de4={'raw':!![],'where':{}};var _0xe9209f={};var _0x2d8397={'count':0x0,'rows':[]};return db[_0x1f13('0x26')][_0x1f13('0x83')]({'where':{'id':_0x252700['params']['id']}})[_0x1f13('0x18')](handleEntityNotFound(_0x2d2eae,null))['then'](function(_0x2d77a5){if(_0x2d77a5){_0xe9209f[_0x1f13('0x79')]=_[_0x1f13('0x29')](db[_0x1f13('0x4d')]['rawAttributes']);_0xe9209f[_0x1f13('0x2f')]=_['keys'](_0x252700[_0x1f13('0x2f')]);_0xe9209f[_0x1f13('0x7a')]=_['intersection'](_0xe9209f['model'],_0xe9209f[_0x1f13('0x2f')]);_0x2a4de4[_0x1f13('0x7b')]=_[_0x1f13('0x7c')](_0xe9209f[_0x1f13('0x79')],qs[_0x1f13('0x7d')](_0x252700['query'][_0x1f13('0x7d')]));_0x2a4de4[_0x1f13('0x7b')]=_0x2a4de4[_0x1f13('0x7b')][_0x1f13('0x2a')]?_0x2a4de4[_0x1f13('0x7b')]:_0xe9209f['model'];if(!_0x252700['query'][_0x1f13('0x35')]('nolimit')){_0x2a4de4['limit']=qs[_0x1f13('0x14')](_0x252700[_0x1f13('0x2f')]['limit']);_0x2a4de4[_0x1f13('0x11')]=qs[_0x1f13('0x11')](_0x252700['query']['offset']);}_0x2a4de4[_0x1f13('0x7e')]=qs[_0x1f13('0x7f')](_0x252700[_0x1f13('0x2f')][_0x1f13('0x7f')]);_0x2a4de4[_0x1f13('0x5e')]=qs[_0x1f13('0x7a')](_['pick'](_0x252700[_0x1f13('0x2f')],_0xe9209f[_0x1f13('0x7a')]));_0x2a4de4[_0x1f13('0x5e')][_0x1f13('0x9e')]=_0x2d77a5['id'];if(_0x252700[_0x1f13('0x2f')][_0x1f13('0x81')]){_0x2a4de4[_0x1f13('0x5e')]=_['merge'](_0x2a4de4[_0x1f13('0x5e')],{'$or':_[_0x1f13('0x6c')](_0x2a4de4[_0x1f13('0x7b')],function(_0x170e46){var _0x414852={};_0x414852[_0x170e46]={'$like':'%'+_0x252700['query'][_0x1f13('0x81')]+'%'};return _0x414852;})});}_0x2a4de4=_[_0x1f13('0x36')]({},_0x2a4de4,_0x252700[_0x1f13('0x9f')]);return db[_0x1f13('0x4d')]['count']({'where':_0x2a4de4[_0x1f13('0x5e')]})[_0x1f13('0x18')](function(_0x53b825){_0x2d8397['count']=_0x53b825;if(_0x252700['query'][_0x1f13('0xa0')]){_0x2a4de4[_0x1f13('0xa1')]=[{'all':!![]}];}return db['CmHopperHistory'][_0x1f13('0x23')](_0x2a4de4);})[_0x1f13('0x18')](function(_0x256e06){_0x2d8397[_0x1f13('0xa2')]=_0x256e06;return _0x2d8397;});}})['then'](respondWithFilteredResult(_0x2d2eae,_0x2a4de4))['catch'](handleError(_0x2d2eae,null));};exports[_0x1f13('0xa4')]=function(_0x299144,_0x545f5d){var _0x10e942={'raw':!![],'where':{}};var _0x37e15d={};var _0x1b085d={'count':0x0,'rows':[]};return db[_0x1f13('0x26')][_0x1f13('0x83')]({'where':{'id':_0x299144[_0x1f13('0x27')]['id']}})[_0x1f13('0x18')](handleEntityNotFound(_0x545f5d,null))[_0x1f13('0x18')](function(_0x2fd9a2){if(_0x2fd9a2){_0x37e15d['model']=_['keys'](db['CmHopperFinal']['rawAttributes']);_0x37e15d[_0x1f13('0x2f')]=_[_0x1f13('0x29')](_0x299144['query']);_0x37e15d[_0x1f13('0x7a')]=_[_0x1f13('0x7c')](_0x37e15d[_0x1f13('0x79')],_0x37e15d['query']);_0x10e942[_0x1f13('0x7b')]=_[_0x1f13('0x7c')](_0x37e15d[_0x1f13('0x79')],qs[_0x1f13('0x7d')](_0x299144[_0x1f13('0x2f')][_0x1f13('0x7d')]));_0x10e942['attributes']=_0x10e942[_0x1f13('0x7b')][_0x1f13('0x2a')]?_0x10e942['attributes']:_0x37e15d[_0x1f13('0x79')];if(!_0x299144[_0x1f13('0x2f')][_0x1f13('0x35')](_0x1f13('0x82'))){_0x10e942[_0x1f13('0x14')]=qs[_0x1f13('0x14')](_0x299144[_0x1f13('0x2f')][_0x1f13('0x14')]);_0x10e942[_0x1f13('0x11')]=qs[_0x1f13('0x11')](_0x299144[_0x1f13('0x2f')][_0x1f13('0x11')]);}_0x10e942[_0x1f13('0x7e')]=qs[_0x1f13('0x7f')](_0x299144[_0x1f13('0x2f')][_0x1f13('0x7f')]);_0x10e942[_0x1f13('0x5e')]=qs[_0x1f13('0x7a')](_['pick'](_0x299144[_0x1f13('0x2f')],_0x37e15d[_0x1f13('0x7a')]));_0x10e942[_0x1f13('0x5e')][_0x1f13('0x9e')]=_0x2fd9a2['id'];if(_0x299144[_0x1f13('0x2f')][_0x1f13('0x81')]){_0x10e942[_0x1f13('0x5e')]=_[_0x1f13('0x36')](_0x10e942[_0x1f13('0x5e')],{'$or':_['map'](_0x10e942[_0x1f13('0x7b')],function(_0x5a2ca0){var _0xfce451={};_0xfce451[_0x5a2ca0]={'$like':'%'+_0x299144[_0x1f13('0x2f')][_0x1f13('0x81')]+'%'};return _0xfce451;})});}_0x10e942=_[_0x1f13('0x36')]({},_0x10e942,_0x299144[_0x1f13('0x9f')]);return db['CmHopperFinal'][_0x1f13('0x13')]({'where':_0x10e942[_0x1f13('0x5e')]})[_0x1f13('0x18')](function(_0x1b6ee9){_0x1b085d[_0x1f13('0x13')]=_0x1b6ee9;if(_0x299144[_0x1f13('0x2f')][_0x1f13('0xa0')]){_0x10e942['include']=[{'all':!![]}];}return db[_0x1f13('0xa5')]['findAll'](_0x10e942);})[_0x1f13('0x18')](function(_0x3e82f4){_0x1b085d[_0x1f13('0xa2')]=_0x3e82f4;return _0x1b085d;});}})[_0x1f13('0x18')](respondWithFilteredResult(_0x545f5d,_0x10e942))['catch'](handleError(_0x545f5d,null));};exports[_0x1f13('0xa6')]=function(_0x28ea59,_0x323a11){var _0x4e8c00={'raw':!![],'where':{}};var _0x4f0259={};var _0x4d1d65={'count':0x0,'rows':[]};return db['CmContact'][_0x1f13('0x83')]({'where':{'id':_0x28ea59[_0x1f13('0x27')]['id']}})[_0x1f13('0x18')](handleEntityNotFound(_0x323a11,null))[_0x1f13('0x18')](function(_0xe6edf5){if(_0xe6edf5){_0x4f0259[_0x1f13('0x79')]=_[_0x1f13('0x29')](db[_0x1f13('0xa7')]['rawAttributes']);_0x4f0259[_0x1f13('0x2f')]=_[_0x1f13('0x29')](_0x28ea59['query']);_0x4f0259['filters']=_[_0x1f13('0x7c')](_0x4f0259[_0x1f13('0x79')],_0x4f0259[_0x1f13('0x2f')]);_0x4e8c00[_0x1f13('0x7b')]=_[_0x1f13('0x7c')](_0x4f0259[_0x1f13('0x79')],qs[_0x1f13('0x7d')](_0x28ea59[_0x1f13('0x2f')][_0x1f13('0x7d')]));_0x4e8c00[_0x1f13('0x7b')]=_0x4e8c00[_0x1f13('0x7b')][_0x1f13('0x2a')]?_0x4e8c00[_0x1f13('0x7b')]:_0x4f0259[_0x1f13('0x79')];if(!_0x28ea59[_0x1f13('0x2f')]['hasOwnProperty'](_0x1f13('0x82'))){_0x4e8c00[_0x1f13('0x14')]=qs[_0x1f13('0x14')](_0x28ea59[_0x1f13('0x2f')][_0x1f13('0x14')]);_0x4e8c00[_0x1f13('0x11')]=qs[_0x1f13('0x11')](_0x28ea59['query'][_0x1f13('0x11')]);}_0x4e8c00[_0x1f13('0x7e')]=qs[_0x1f13('0x7f')](_0x28ea59[_0x1f13('0x2f')][_0x1f13('0x7f')]);_0x4e8c00['where']=qs['filters'](_[_0x1f13('0x80')](_0x28ea59[_0x1f13('0x2f')],_0x4f0259['filters']));_0x4e8c00['where'][_0x1f13('0x9e')]=_0xe6edf5['id'];if(_0x28ea59[_0x1f13('0x2f')]['filter']){_0x4e8c00[_0x1f13('0x5e')]=_[_0x1f13('0x36')](_0x4e8c00[_0x1f13('0x5e')],{'$or':_[_0x1f13('0x6c')](_0x4e8c00[_0x1f13('0x7b')],function(_0x3e5d12){var _0x3fb56a={};_0x3fb56a[_0x3e5d12]={'$like':'%'+_0x28ea59[_0x1f13('0x2f')]['filter']+'%'};return _0x3fb56a;})});}_0x4e8c00=_['merge']({},_0x4e8c00,_0x28ea59[_0x1f13('0x9f')]);return db[_0x1f13('0xa7')]['count']({'where':_0x4e8c00['where']})[_0x1f13('0x18')](function(_0x46a82a){_0x4d1d65['count']=_0x46a82a;if(_0x28ea59[_0x1f13('0x2f')][_0x1f13('0xa0')]){_0x4e8c00[_0x1f13('0xa1')]=[{'all':!![]}];}return db[_0x1f13('0xa7')]['findAll'](_0x4e8c00);})[_0x1f13('0x18')](function(_0x3986bf){_0x4d1d65[_0x1f13('0xa2')]=_0x3986bf;return _0x4d1d65;});}})['then'](respondWithFilteredResult(_0x323a11,_0x4e8c00))[_0x1f13('0x25')](handleError(_0x323a11,null));};exports[_0x1f13('0xa8')]=function(_0x186e90,_0x1676dd){try{if(!_0x186e90['body'][_0x1f13('0x61')]){throw new Error(_0x1f13('0xa9'));}if(!_0x186e90['body'][_0x1f13('0xaa')]){throw new Error('No\x20association\x20keys\x20specified!');}if(!_0x186e90[_0x1f13('0x37')][_0x1f13('0xab')]){throw new Error(_0x1f13('0xac'));}if(!_0x186e90[_0x1f13('0x37')][_0x1f13('0xad')]){throw new Error(_0x1f13('0xae'));}var _0xc1ee98=_0x186e90[_0x1f13('0x37')]['binding_keys'][_0x1f13('0xaf')](',');var _0x73790d=_0x186e90[_0x1f13('0x37')][_0x1f13('0xab')][_0x1f13('0xaf')](',');if(!_0xc1ee98[_0x1f13('0x2a')]||!_0x73790d[_0x1f13('0x2a')]||_0xc1ee98[_0x1f13('0x2a')]!==_0x73790d[_0x1f13('0x2a')]){throw new Error(_0x1f13('0xb0'));}_0x186e90[_0x1f13('0x37')]['fields']=_[_0x1f13('0xb1')](_0xc1ee98,_0x73790d);if(!_0x186e90[_0x1f13('0x37')][_0x1f13('0x7d')][_0x1f13('0x77')]){throw new Error('No\x20phone\x20column\x20specified');}if(!_0x186e90['body'][_0x1f13('0x7d')][_0x1f13('0xb2')]){throw new Error(_0x1f13('0xb3'));}var _0x1f04ff=path[_0x1f13('0x55')](config[_0x1f13('0xb4')],_0x1f13('0xb5'),_0x186e90[_0x1f13('0xb6')][_0x1f13('0xb7')]);fs[_0x1f13('0xb8')](_0x1f04ff,parseInt(_0x1f13('0xb9'),0x8));var _0x5f1f87=require('child_process')[_0x1f13('0xba')](path[_0x1f13('0x55')](__dirname,_0x1f13('0xbb')),[_0x1f04ff,_0x186e90[_0x1f13('0x37')]['binding_keys'],_0x186e90[_0x1f13('0x37')]['binding_values'],_0x186e90[_0x1f13('0x37')][_0x1f13('0x61')],_0x186e90[_0x1f13('0x37')][_0x1f13('0xbc')],_0x186e90[_0x1f13('0x37')][_0x1f13('0x5d')],_0x186e90[_0x1f13('0x37')][_0x1f13('0xad')]]);return _0x1676dd[_0x1f13('0xf')](0xc8)[_0x1f13('0x10')]({'message':_0x1f13('0xbd'),'pid':_0x5f1f87['pid']});}catch(_0x453c92){return _0x1676dd[_0x1f13('0xf')](0x1f4)[_0x1f13('0x1f')](_0x453c92);}};exports[_0x1f13('0xbe')]=function(_0xd4e325,_0x11bb18){var _0x2e38ca=fs[_0x1f13('0xbf')](_0xd4e325['file'][_0x1f13('0x2')]);Papa[_0x1f13('0xc0')](_0x2e38ca,{'header':!![],'skipEmptyLines':!![],'preview':0x1,'complete':function(_0x423ef0){return _0x11bb18[_0x1f13('0xf')](0xc8)['json']({'file':_0xd4e325[_0x1f13('0xb6')],'fields':_0x423ef0[_0x1f13('0xc1')][_0x1f13('0x7d')]});}});};exports[_0x1f13('0xc2')]=function(_0x346251,_0x57789c){return db[_0x1f13('0xc3')][_0x1f13('0x23')]({'raw':!![],'attributes':['id'],'where':{'role':_0x1f13('0xc4')}})['then'](function(_0x1fcb00){var _0x421c0c=![];if(!_0x346251['body'][_0x1f13('0x61')]){throw new Error(_0x1f13('0xa9'));}if(!_0x346251[_0x1f13('0x37')]['binding']['phone']){throw new Error(_0x1f13('0xc5'));}if(!_0x346251[_0x1f13('0x37')][_0x1f13('0xc6')][_0x1f13('0xb2')]){throw new Error(_0x1f13('0xb3'));}if(!_0x346251[_0x1f13('0x37')][_0x1f13('0xad')]){throw new Error(_0x1f13('0xae'));}var _0x236f1d=require('child_process')[_0x1f13('0xba')](path[_0x1f13('0x55')](__dirname,_0x1f13('0xc7')),[_0x346251[_0x1f13('0x27')]['id'],JSON[_0x1f13('0xc8')](_0x346251['body']),JSON['stringify'](_0x1fcb00)]);_0x236f1d['on'](_0x1f13('0xc9'),function(){_0x421c0c=!![];return _0x57789c[_0x1f13('0xf')](0xc8)['json']({'message':'start\x20child\x20process\x20correctly!','pid':_0x236f1d[_0x1f13('0xca')]});});_0x236f1d['on'](_0x1f13('0xcb'),function(_0x4e9622){if(_0x4e9622&&!_0x421c0c){return _0x57789c[_0x1f13('0xf')](0x1f4)[_0x1f13('0x10')]({'message':_0x1f13('0xcc'),'pid':_0x236f1d[_0x1f13('0xca')]});}});})['catch'](handleError(_0x57789c,null));};exports[_0x1f13('0xcd')]=function(_0x21fe84,_0x204020){return db['User'][_0x1f13('0x23')]({'raw':!![],'attributes':['id'],'where':{'role':_0x1f13('0xc4')}})[_0x1f13('0x18')](function(_0xfb915){var _0x285596=![];if(!_0x21fe84[_0x1f13('0x37')]['ListId']){throw new Error('No\x20ListId\x20specified');}if(!_0x21fe84[_0x1f13('0x37')][_0x1f13('0xc6')][_0x1f13('0x77')]){throw new Error('No\x20phone\x20column\x20specified');}if(!_0x21fe84['body'][_0x1f13('0xc6')][_0x1f13('0xb2')]){throw new Error('No\x20firstName\x20column\x20specified');}if(!_0x21fe84[_0x1f13('0x37')][_0x1f13('0xad')]){throw new Error(_0x1f13('0xae'));}var _0xcff088=require(_0x1f13('0xce'))[_0x1f13('0xba')](path[_0x1f13('0x55')](__dirname,_0x1f13('0xcf')),[_0x21fe84['params']['id'],JSON['stringify'](_0x21fe84[_0x1f13('0x37')]),JSON[_0x1f13('0xc8')](_0xfb915)]);_0xcff088['on']('message',function(){_0x285596=!![];return _0x204020[_0x1f13('0xf')](0xc8)[_0x1f13('0x10')]({'message':_0x1f13('0xd0'),'pid':_0xcff088[_0x1f13('0xca')]});});_0xcff088['on'](_0x1f13('0xcb'),function(_0x29f895){if(_0x29f895&&!_0x285596){return _0x204020[_0x1f13('0xf')](0x1f4)[_0x1f13('0x10')]({'message':_0x1f13('0xcc'),'pid':_0xcff088[_0x1f13('0xca')]});}});})[_0x1f13('0x25')](handleError(_0x204020,null));};exports['getJourney']=function(_0x4ca182,_0x5f5086){var _0x32ea26=[_0x1f13('0xd1'),_0x1f13('0xd2'),'sms',_0x1f13('0xd3'),'fax',_0x1f13('0xd4'),_0x1f13('0xd5')];var _0xe72235={'offset':0x0,'order':qs[_0x1f13('0x7f')](_0x1f13('0xd6'))},_0x2b0ddc={'count':0x0,'rows':[]};Promise[_0x1f13('0xd7')]()[_0x1f13('0x18')](function(){if(_0x4ca182[_0x1f13('0x2f')][_0x1f13('0xd8')]){if(!_[_0x1f13('0xd9')](_0x32ea26,_0x4ca182[_0x1f13('0x2f')]['channel'][_0x1f13('0xda')]())){throw new Error('Invalid\x20channel');}_0x32ea26=[_0x4ca182[_0x1f13('0x2f')][_0x1f13('0xd8')][_0x1f13('0xda')]()];}if(_0x4ca182['query'][_0x1f13('0xdb')]){try{var _0x10761d=JSON[_0x1f13('0xc0')](_0x4ca182[_0x1f13('0x2f')][_0x1f13('0xdb')]);if(!moment(_0x10761d[_0x1f13('0x38')])['isValid']()){throw new Error(_0x1f13('0xdc'));}if(!moment(_0x10761d['to'])['isValid']()){throw new Error(_0x1f13('0xdd'));}}catch(_0x369f0e){throw new Error(_0x1f13('0xde'));}}if(!_0x4ca182[_0x1f13('0x2f')]['hasOwnProperty'](_0x1f13('0x82'))){_0xe72235[_0x1f13('0x14')]=qs[_0x1f13('0x14')](_0x4ca182[_0x1f13('0x2f')][_0x1f13('0x14')]||0xa);_0xe72235['offset']=qs[_0x1f13('0x11')](_0x4ca182[_0x1f13('0x2f')][_0x1f13('0x11')]);}var _0x1bd663=_0x32ea26[_0x1f13('0x6c')](function(_0x1ae23d){if(_0x1ae23d===_0x1f13('0xd5')){var _0x40fac3={'attributes':['id',_0x1f13('0xdf'),'type','source',_0x1f13('0xe0'),_0x1f13('0xe1'),_0x1f13('0xe2'),_0x1f13('0xe3'),_0x1f13('0xe4'),_0x1f13('0xe5'),_0x1f13('0xe6'),_0x1f13('0xe7'),_0x1f13('0xe8'),_0x1f13('0xe9'),_0x1f13('0xea')],'where':{'ContactId':_0x4ca182['params']['id']},'include':[{'model':db[_0x1f13('0xc3')],'as':'User','attributes':['id','name',_0x1f13('0xeb'),_0x1f13('0xec')]}],'order':[[_0x1f13('0xe6'),'DESC']]};if(_0x4ca182[_0x1f13('0x2f')][_0x1f13('0xdb')]){_0x40fac3['where'][_0x1f13('0xe6')]={'$gte':_0x10761d[_0x1f13('0x38')],'$lte':_0x10761d['to']};}return db[_0x1f13('0x4f')]['findAndCountAll'](_0x40fac3)[_0x1f13('0x18')](function(_0x3dde83){_0x2b0ddc[_0x1f13('0x13')]+=_0x3dde83[_0x1f13('0x13')];return _['map'](_0x3dde83['rows'],function(_0x2857d7){var _0x468cc3=_0x2857d7[_0x1f13('0x39')]({'plain':!![]});_0x468cc3[_0x1f13('0xd8')]=_0x1ae23d;if(_0x468cc3[_0x1f13('0xc3')]){_0x468cc3[_0x1f13('0xc3')]={'id':_0x468cc3[_0x1f13('0xc3')]['id'],'name':_0x468cc3['User'][_0x1f13('0x1e')],'fullname':_0x468cc3[_0x1f13('0xc3')][_0x1f13('0xeb')],'alias':_0x468cc3[_0x1f13('0xc3')][_0x1f13('0xec')]};}return _0x468cc3;});});}else{var _0x172b75=_[_0x1f13('0xed')](_0x1ae23d)+(_0x1ae23d===_0x1f13('0xd2')?'Website':_0x1f13('0xee'));var _0x17fde9={'attributes':['id',_0x1f13('0xef'),_0x1f13('0xf0'),_0x1f13('0xe6'),_0x1f13('0xe5'),_0x1f13('0xf1'),_0x1f13('0xf2'),_0x1f13('0xf3'),_0x1f13('0xea'),_0x172b75+'Id'],'where':{'ContactId':_0x4ca182[_0x1f13('0x27')]['id']},'include':[{'model':db[_0x172b75],'as':_0x1f13('0xee'),'attributes':['id',_0x1f13('0x1e'),'key']},{'model':db[_0x1f13('0xc3')],'as':'Owner','attributes':['id',_0x1f13('0x1e'),'fullname']},{'model':db[_0x1f13('0x66')],'as':_0x1f13('0xf4'),'attributes':['id',_0x1f13('0x1e'),'color'],'through':{'attributes':[]}}],'order':[[_0x1f13('0xe6'),_0x1f13('0xf5')]]};if(_0x4ca182[_0x1f13('0x2f')][_0x1f13('0xdb')]){_0x17fde9[_0x1f13('0x5e')][_0x1f13('0xe6')]={'$gte':_0x10761d[_0x1f13('0x38')],'$lte':_0x10761d['to']};}switch(_0x1ae23d){case'chat':_0x17fde9['attributes'][_0x1f13('0x60')](_0x1f13('0xf6'),_0x1f13('0xf7'),_0x1f13('0xf8'),_0x1f13('0xf9'),_0x1f13('0xfa'),_0x1f13('0xfb'),_0x1f13('0xfc'),_0x1f13('0xfd'),_0x1f13('0xfe'));break;case _0x1f13('0xd1'):_0x17fde9[_0x1f13('0x7b')][_0x1f13('0x60')](_0x1f13('0xff'),'substatus');break;case _0x1f13('0xd3'):_0x17fde9[_0x1f13('0x7b')][_0x1f13('0x60')](_0x1f13('0x100'),_0x1f13('0x101'));break;}return db[_['startCase'](_0x1ae23d)+'Interaction'][_0x1f13('0x102')](_0x17fde9)[_0x1f13('0x18')](function(_0x4cc122){_0x2b0ddc['count']+=_0x4cc122[_0x1f13('0x13')];return _['map'](_0x4cc122['rows'],function(_0x28a7af){var _0x3a00fa=_0x28a7af[_0x1f13('0x39')]({'plain':!![]});_0x3a00fa[_0x1f13('0xd8')]=_0x1ae23d;if(_0x3a00fa[_0x1f13('0x103')]){_0x3a00fa[_0x1f13('0x103')]={'id':_0x3a00fa[_0x1f13('0x103')]['id'],'name':_0x3a00fa[_0x1f13('0x103')][_0x1f13('0x1e')],'fullname':_0x3a00fa[_0x1f13('0x103')][_0x1f13('0xeb')]};}return _0x3a00fa;});});}});return Promise['all'](_0x1bd663);})['then'](function(_0x265d75){return _(_0x265d75)[_0x1f13('0x104')]()[_0x1f13('0x105')]('createdAt')[_0x1f13('0x106')]()[_0x1f13('0x107')]();})[_0x1f13('0x18')](function(_0x43a54c){_0x2b0ddc[_0x1f13('0xa2')]=_0xe72235['limit']?_0x43a54c[_0x1f13('0x108')](_0xe72235['offset'],_0xe72235[_0x1f13('0x11')]+_0xe72235[_0x1f13('0x14')]):_0x43a54c;return _0x2b0ddc;})['then'](respondWithFilteredResult(_0x5f5086,_0xe72235))[_0x1f13('0x25')](handleError(_0x5f5086,null));};