Built motion from commit 953226e6.|2.6.32
[motion2.git] / server / components / contacts / sql-utils.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 _0xff30=['CONCAT(\x27,\x27,\x20subqry.TagIds,\x20\x27,\x27)\x20LIKE\x20?','replace','length','MATCH\x20(c.firstName,\x20c.lastName,\x20c.phone,\x20c.mobile,\x20c.fax,\x20c.email)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','isNumeric','c.id\x20LIKE\x20?','c.phone\x20LIKE\x20?','c.mobile\x20LIKE\x20?','c.fax\x20LIKE\x20?','c.email\x20LIKE\x20?','parseSearch','sqlOperator','conditions','fieldType','datetime','castTo','DATE','buildExpression','type','default','value','end','Tag','map','tags','clause','and','user','uc.UserId\x20=\x20?','skip','keys','some','compact','Contact','User','c.ListId\x20IN\x20?','resolve','then','tag','filter','search','role','getLists','attributes','group','limit','offset','subqry.id','having','order','DESC','subqry.','from','findAll','name','keyBy','Sequelize','SELECT','CmContact','sequelize','toString','TagIds','split','Tags','clone','COUNT(*)','c.id','isEmpty','t.id\x20IN\x20?','QueryTypes','query','exports','squel','../parsers/advancedSearch','../parsers/qs','../interaction/utils','../../mysqldb','agent','ListId','autocomplete','true','select','cm_contacts','where','c.deletedAt\x20IS\x20NULL','subqry','left_join','cm_user_has_contacts','uc.CmContactId\x20=\x20','.id','ct.CmContactId\x20=\x20','tools_tags','t.id\x20=\x20ct.TagId','lastName','fax','email','CompanyId','createdAt','updatedAt','deletedAt','includes','push','forEach','field','GROUP_CONCAT(DISTINCT\x20t.id)','expr','getSelectedTags'];(function(_0x16ee58,_0x165eb5){var _0x4985cf=function(_0x4806bb){while(--_0x4806bb){_0x16ee58['push'](_0x16ee58['shift']());}};_0x4985cf(++_0x165eb5);}(_0xff30,0x189));var _0x0ff3=function(_0x329e24,_0x35edf8){_0x329e24=_0x329e24-0x0;var _0xa2671b=_0xff30[_0x329e24];return _0xa2671b;};'use strict';var _=require('lodash');var squel=require(_0x0ff3('0x0'));var as=require(_0x0ff3('0x1'));var qs=require(_0x0ff3('0x2'));var utils=require(_0x0ff3('0x3'));var db=require(_0x0ff3('0x4'))['db'];function skipContactsQuery(_0x935648,_0x217e26){var _0x466152=![];if(_0x935648['role']===_0x0ff3('0x5')&&!_0x217e26['search']&&!_0x217e26[_0x0ff3('0x6')]&&_0x217e26[_0x0ff3('0x7')]!==_0x0ff3('0x8')){_0x466152=!![];}return _0x466152;}function getContactsSubquery(){return squel[_0x0ff3('0x9')]()['from'](_0x0ff3('0xa'),'c')[_0x0ff3('0xb')](_0x0ff3('0xc'));}function applyJoinClauses(_0x3a3db9,_0x11c397){var _0x4c0ae2=_0x11c397?'c':_0x0ff3('0xd');_0x3a3db9[_0x0ff3('0xe')](_0x0ff3('0xf'),'uc',_0x0ff3('0x10')+_0x4c0ae2+_0x0ff3('0x11'))[_0x0ff3('0xe')]('cm_contact_has_tags','ct',_0x0ff3('0x12')+_0x4c0ae2+_0x0ff3('0x11'))['left_join'](_0x0ff3('0x13'),'t',_0x0ff3('0x14'));return _0x3a3db9;}function addFieldsToQuery(_0x5895e5,_0x5c1aa4,_0x44ef08,_0x2423ee,_0x482c85){var _0x349ee6=_0x482c85?'c':_0x0ff3('0xd');if(!_0x5c1aa4){_0x5c1aa4=['id','firstName',_0x0ff3('0x15'),'phone','mobile',_0x0ff3('0x16'),_0x0ff3('0x17'),_0x0ff3('0x6'),_0x0ff3('0x18'),_0x0ff3('0x19'),_0x0ff3('0x1a'),_0x0ff3('0x1b'),'TagIds'];}else if(!_0x5c1aa4[_0x0ff3('0x1c')]('id')){_0x5c1aa4[_0x0ff3('0x1d')]('id');}var _0x2b1a35=_0x44ef08&&_0x482c85||!_0x44ef08&&!_0x482c85;if(_0x2423ee){_0x2b1a35=_0x2423ee&&_0x482c85||!_0x2423ee&&!_0x482c85;}_0x5c1aa4[_0x0ff3('0x1e')](function(_0x32281b){if(_0x32281b==='TagIds'){if(_0x2b1a35){_0x5895e5[_0x0ff3('0x1f')](_0x0ff3('0x20'),_0x32281b);}else if(_0x44ef08||_0x2423ee){_0x5895e5[_0x0ff3('0x1f')](_0x349ee6+'.'+_0x32281b);}}else{_0x5895e5[_0x0ff3('0x1f')](_0x349ee6+'.'+_0x32281b);}});return _0x5895e5;}function getSqlClauses(){var _0x3f6316={'default':squel[_0x0ff3('0x21')](),'fields':squel['expr'](),'filter':squel['expr'](),'tags':{'clause':squel[_0x0ff3('0x21')](),'ids':null},'user':{'clause':squel[_0x0ff3('0x21')](),'skip':![]}};return _0x3f6316;}function parseTagParameter(_0x50b8a0){var _0x4712a7=squel['expr']();if(_0x50b8a0){var _0x486816=utils[_0x0ff3('0x22')](_0x50b8a0);_0x486816[_0x0ff3('0x1e')](function(_0x38cad5){_0x4712a7['or'](_0x0ff3('0x23'),'%,'+_0x38cad5+',%');});}return _0x4712a7;}function parseSearchFilter(_0x362db7){var _0x4aade6=squel[_0x0ff3('0x21')]();if(_0x362db7){var _0x2b729b=_0x362db7['replace']('\x5c','\x5c\x5c')[_0x0ff3('0x24')](/'/g,'\x27\x27');var _0x1fc254=qs['escapeLIKESpecialChars'](_0x2b729b);if(_0x2b729b['split']('\x20')[_0x0ff3('0x25')]>0x1){_0x4aade6['or'](_0x0ff3('0x26'),qs['getFullTextValue'](_0x2b729b,!![]),null);}else{if(qs[_0x0ff3('0x27')](_0x2b729b)){_0x4aade6['or'](_0x0ff3('0x28'),_0x1fc254+'%');_0x4aade6['or'](_0x0ff3('0x29'),_0x1fc254+'%');_0x4aade6['or'](_0x0ff3('0x2a'),_0x1fc254+'%');_0x4aade6['or'](_0x0ff3('0x2b'),_0x1fc254+'%');}else if(qs['isEmail'](_0x2b729b)){_0x4aade6['or'](_0x0ff3('0x2c'),_0x1fc254+'%');}else{_0x4aade6['or']('c.firstName\x20LIKE\x20?','%'+_0x1fc254+'%');_0x4aade6['or']('c.lastName\x20LIKE\x20?','%'+_0x1fc254+'%');_0x4aade6['or'](_0x0ff3('0x2c'),'%'+_0x1fc254+'%');}}}return _0x4aade6;}function parseAdvancedSearchFilter(_0x371e38,_0x150fc9){var _0x54623c=getSqlClauses();var _0x5e1af7=as[_0x0ff3('0x2d')](_0x371e38);var _0x74514e=_0x5e1af7[_0x0ff3('0x2e')];for(var _0x250ed5=0x0;_0x250ed5<_0x5e1af7[_0x0ff3('0x2f')][_0x0ff3('0x25')];_0x250ed5++){var _0x4e99b7=_0x5e1af7[_0x0ff3('0x2f')][_0x250ed5];var _0x5588a6='c';var _0x40606f=_['find'](_0x150fc9,['name',_0x4e99b7[_0x0ff3('0x1f')]]);if(_0x40606f){if(_0x4e99b7[_0x0ff3('0x1f')]==='dateOfBirth'||_0x40606f[_0x0ff3('0x30')]===_0x0ff3('0x31')){_0x4e99b7[_0x0ff3('0x32')]=_0x0ff3('0x33');}var _0x43d771=as[_0x0ff3('0x34')](_0x5588a6,_0x40606f[_0x0ff3('0x35')],_0x4e99b7);_0x54623c[_0x0ff3('0x36')][_0x74514e](_0x43d771['text'],_0x43d771[_0x0ff3('0x37')]['start'],_0x43d771[_0x0ff3('0x37')][_0x0ff3('0x38')]);}else{if(_0x4e99b7[_0x0ff3('0x1f')]===_0x0ff3('0x39')){var _0x2014b3=_0x4e99b7[_0x0ff3('0x37')]['split'](',')[_0x0ff3('0x3a')](function(_0x4c1274){return Number(_0x4c1274);});var _0x325e10=parseTagParameter(_0x2014b3);_0x54623c[_0x0ff3('0x3b')][_0x0ff3('0x3c')][_0x0ff3('0x3d')](_0x325e10);_0x54623c['tags']['ids']=_0x2014b3;}else if(_0x4e99b7[_0x0ff3('0x1f')]==='User'){_0x54623c[_0x0ff3('0x3e')][_0x0ff3('0x3c')]['and'](_0x0ff3('0x3f'),_0x4e99b7['value']);_0x54623c['user'][_0x0ff3('0x40')]=!![];}}}return _0x54623c;}function parseModelFields(_0x3625af,_0x2def48){var _0x57db26=squel[_0x0ff3('0x21')]();var _0x27106f=_(_0x3625af)[_0x0ff3('0x41')]()['map'](function(_0x5c005c){return _[_0x0ff3('0x42')](_0x2def48,['name',_0x5c005c])?_0x5c005c:undefined;})[_0x0ff3('0x43')]()[_0x0ff3('0x37')]();if(_0x3625af[_0x0ff3('0x44')]){_0x27106f['push'](_0x0ff3('0x44'));}if(_0x3625af['User']){_0x27106f[_0x0ff3('0x1d')](_0x0ff3('0x45'));}if(_0x27106f['length']>0x0){_0x27106f[_0x0ff3('0x1e')](function(_0x420104){if(_0x420104===_0x0ff3('0x6')){_0x57db26[_0x0ff3('0x3d')](_0x0ff3('0x46'),[]['concat'](_0x3625af[_0x420104]));}else if(_0x420104===_0x0ff3('0x44')){var _0x316808='%'+_0x3625af[_0x420104]+'%';_0x57db26[_0x0ff3('0x3d')]('CONCAT_WS(\x27\x20\x27,\x20c.firstName,\x20c.lastName,\x20c.email)\x20LIKE\x20?',_0x316808);}else if(_0x420104===_0x0ff3('0x45')){_0x57db26[_0x0ff3('0x3d')](_0x0ff3('0x3f'),_0x3625af[_0x420104]);}else{_0x57db26[_0x0ff3('0x3d')]('c.'+_0x420104+'\x20=\x20?',_0x3625af[_0x420104]);}});}return _0x57db26;}function parseQueryParameters(_0x592d7d,_0x453832){var _0xedd084=getSqlClauses();return Promise[_0x0ff3('0x47')]()[_0x0ff3('0x48')](function(){return parseModelFields(_0x592d7d,_0x453832);})['then'](function(_0x59b54d){_0xedd084['fields'][_0x0ff3('0x3d')](_0x59b54d);return parseTagParameter(_0x592d7d[_0x0ff3('0x49')]);})['then'](function(_0x4d5098){_0xedd084[_0x0ff3('0x3b')][_0x0ff3('0x3c')][_0x0ff3('0x3d')](_0x4d5098);return parseSearchFilter(_0x592d7d[_0x0ff3('0x4a')]);})[_0x0ff3('0x48')](function(_0x5b80a5){_0xedd084[_0x0ff3('0x4a')]=_0x5b80a5;return _0xedd084;});}function checkListsRestrictions(_0x1210e2,_0x3a3df8,_0x35d20a){var _0x132015=squel[_0x0ff3('0x21')]();return Promise[_0x0ff3('0x47')]()[_0x0ff3('0x48')](function(){if(_0x3a3df8[_0x0ff3('0x4b')]&&_0x1210e2[_0x0ff3('0x4c')]===_0x0ff3('0x5')&&!_0x35d20a){return _0x1210e2[_0x0ff3('0x4d')]({'attributes':['id'],'raw':!![]});}})['then'](function(_0x5830e6){if(_0x5830e6){if(_0x5830e6[_0x0ff3('0x25')]===0x0){_0x132015[_0x0ff3('0x3d')]('c.ListId\x20IS\x20NULL');}else{_0x132015[_0x0ff3('0x3d')](_0x0ff3('0x46'),_[_0x0ff3('0x3a')](_0x5830e6,'id'));}}})[_0x0ff3('0x48')](function(){return _0x132015;});}function buildContactsQuery(_0x3970e5,_0x58079f,_0x1aae11,_0x2164f2,_0x3ce86a,_0x60dcaf){if(_0x58079f){_0x58079f=_0x1aae11[_0x0ff3('0x4e')];}_0x3970e5=addFieldsToQuery(_0x3970e5,_0x58079f,_0x2164f2,_0x60dcaf,!![]);var _0x1671c7=squel['select']();_0x1671c7=addFieldsToQuery(_0x1671c7,_0x58079f,_0x2164f2,_0x60dcaf,![]);if(_0x2164f2||_0x60dcaf){_0x3970e5=applyJoinClauses(_0x3970e5,!![]);_0x3970e5[_0x0ff3('0x4f')]('c.id');if(_0x1aae11[_0x0ff3('0x50')]){_0x1671c7[_0x0ff3('0x50')](_0x1aae11[_0x0ff3('0x50')]);}if(_0x1aae11['offset']){_0x1671c7[_0x0ff3('0x51')](_0x1aae11[_0x0ff3('0x51')]);}}else{_0x1671c7=applyJoinClauses(_0x1671c7,![]);_0x1671c7[_0x0ff3('0x4f')](_0x0ff3('0x52'));if(_0x1aae11[_0x0ff3('0x50')]){_0x3970e5[_0x0ff3('0x50')](_0x1aae11[_0x0ff3('0x50')]);}if(_0x1aae11[_0x0ff3('0x51')]){_0x3970e5['offset'](_0x1aae11[_0x0ff3('0x51')]);}}_0x1671c7[_0x0ff3('0x53')](_0x3ce86a);if(_0x1aae11[_0x0ff3('0x54')]){_0x1aae11[_0x0ff3('0x54')][_0x0ff3('0x1e')](function(_0x32736c){_0x3970e5['order']('c.'+_0x32736c[0x0],_0x32736c[0x1]===_0x0ff3('0x55')?![]:!![]);_0x1671c7['order'](_0x0ff3('0x56')+_0x32736c[0x0],_0x32736c[0x1]===_0x0ff3('0x55')?![]:!![]);});}_0x1671c7[_0x0ff3('0x57')](_0x3970e5,'subqry');return _0x1671c7;}function getFilteredContacts(_0x52523e){var _0x416ae8=[];return db['Tag'][_0x0ff3('0x58')]({'attributes':['id',_0x0ff3('0x59'),'color'],'raw':!![]})['then'](function(_0x2e0982){_0x416ae8=_[_0x0ff3('0x5a')](_0x2e0982,'id');var _0x3aa264={'type':db[_0x0ff3('0x5b')]['QueryTypes'][_0x0ff3('0x5c')],'model':db[_0x0ff3('0x5d')],'mapToModel':!![],'nest':!![],'raw':!![]};return db[_0x0ff3('0x5e')]['query'](_0x52523e[_0x0ff3('0x5f')](),_0x3aa264);})[_0x0ff3('0x48')](function(_0x1802f1){_0x1802f1[_0x0ff3('0x1e')](function(_0x13f7c9){if(_0x13f7c9[_0x0ff3('0x60')]){_0x13f7c9['Tags']=[];_0x13f7c9['TagIds'][_0x0ff3('0x61')](',')[_0x0ff3('0x1e')](function(_0xb45f54){_0x13f7c9[_0x0ff3('0x62')][_0x0ff3('0x1d')](_0x416ae8[_0xb45f54]);});}delete _0x13f7c9[_0x0ff3('0x60')];});return _0x1802f1;});}function countFilteredContacts(_0x1b4c14,_0x18999d){var _0x3db41f=_0x1b4c14[_0x0ff3('0x63')]();_0x3db41f[_0x0ff3('0x1f')](_0x0ff3('0x64'));_0x3db41f=applyJoinClauses(_0x3db41f,!![]);_0x1b4c14[_0x0ff3('0x4f')](_0x0ff3('0x65'));var _0x4c0741=utils[_0x0ff3('0x22')](_0x18999d);if(!_[_0x0ff3('0x66')](_0x4c0741)){_0x3db41f[_0x0ff3('0xb')](_0x0ff3('0x67'),_0x4c0741);}var _0x65f2ab={'type':db[_0x0ff3('0x5b')][_0x0ff3('0x68')][_0x0ff3('0x5c')],'raw':!![]};return db[_0x0ff3('0x5e')][_0x0ff3('0x69')](_0x3db41f[_0x0ff3('0x5f')](),_0x65f2ab)[_0x0ff3('0x48')](function(_0x4bffae){return _0x4bffae[0x0][_0x0ff3('0x64')];});}module[_0x0ff3('0x6a')]={'buildContactsQuery':buildContactsQuery,'checkListsRestrictions':checkListsRestrictions,'countFilteredContacts':countFilteredContacts,'getContactsSubquery':getContactsSubquery,'getFilteredContacts':getFilteredContacts,'parseAdvancedSearchFilter':parseAdvancedSearchFilter,'parseQueryParameters':parseQueryParameters,'skipContactsQuery':skipContactsQuery};