Built motion from commit da617fac.|2.6.6
[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 _0x222f=['role','search','autocomplete','true','from','cm_contacts','c.deletedAt\x20IS\x20NULL','subqry','left_join','uc.CmContactId\x20=\x20','.id','cm_contact_has_tags','tools_tags','t.id\x20=\x20ct.TagId','firstName','phone','mobile','fax','email','ListId','CompanyId','createdAt','deletedAt','TagIds','forEach','field','expr','CONCAT(\x27,\x27,\x20subqry.TagIds,\x20\x27,\x27)\x20LIKE\x20?','replace','split','length','MATCH\x20(c.firstName,\x20c.lastName,\x20c.phone,\x20c.mobile,\x20c.fax,\x20c.email)\x20AGAINST\x20(?\x20IN\x20BOOLEAN\x20MODE)','getFullTextValue','isNumeric','c.id\x20LIKE\x20?','c.phone\x20LIKE\x20?','c.mobile\x20LIKE\x20?','c.fax\x20LIKE\x20?','isEmail','c.email\x20LIKE\x20?','c.lastName\x20LIKE\x20?','parseSearch','sqlOperator','conditions','find','name','dateOfBirth','fieldType','datetime','buildExpression','type','default','text','value','end','tags','and','ids','User','clause','uc.UserId\x20=\x20?','user','keys','map','compact','Contact','push','c.ListId\x20IN\x20?','CONCAT_WS(\x27\x20\x27,\x20c.firstName,\x20c.lastName,\x20c.email)\x20LIKE\x20?','resolve','then','fields','filter','agent','c.ListId\x20IS\x20NULL','attributes','select','group','c.id','limit','offset','order','DESC','subqry.','findAll','color','keyBy','Sequelize','QueryTypes','SELECT','CmContact','toString','Tags','clone','COUNT(*)','getSelectedTags','isEmpty','where','t.id\x20IN\x20?','sequelize','query','exports','lodash','squel','../parsers/advancedSearch','../parsers/qs'];(function(_0x59ee6f,_0x450239){var _0x4c0979=function(_0x565e92){while(--_0x565e92){_0x59ee6f['push'](_0x59ee6f['shift']());}};_0x4c0979(++_0x450239);}(_0x222f,0x1a4));var _0xf222=function(_0x303d9d,_0x363884){_0x303d9d=_0x303d9d-0x0;var _0x4ccd15=_0x222f[_0x303d9d];return _0x4ccd15;};'use strict';var _=require(_0xf222('0x0'));var squel=require(_0xf222('0x1'));var as=require(_0xf222('0x2'));var qs=require(_0xf222('0x3'));var utils=require('../interaction/utils');var db=require('../../mysqldb')['db'];function skipContactsQuery(_0x195de1,_0x233e95){var _0x307791=![];if(_0x195de1[_0xf222('0x4')]==='agent'&&!_0x233e95[_0xf222('0x5')]&&!_0x233e95['ListId']&&_0x233e95[_0xf222('0x6')]!==_0xf222('0x7')){_0x307791=!![];}return _0x307791;}function getContactsSubquery(){return squel['select']()[_0xf222('0x8')](_0xf222('0x9'),'c')['where'](_0xf222('0xa'));}function applyJoinClauses(_0x32d6d3,_0x878804){var _0x2eb41f=_0x878804?'c':_0xf222('0xb');_0x32d6d3[_0xf222('0xc')]('cm_user_has_contacts','uc',_0xf222('0xd')+_0x2eb41f+_0xf222('0xe'))[_0xf222('0xc')](_0xf222('0xf'),'ct','ct.CmContactId\x20=\x20'+_0x2eb41f+_0xf222('0xe'))[_0xf222('0xc')](_0xf222('0x10'),'t',_0xf222('0x11'));return _0x32d6d3;}function addFieldsToQuery(_0x12f968,_0x334017,_0x11fca0,_0x17df25,_0x52a003){var _0x5b8f97=_0x52a003?'c':_0xf222('0xb');if(!_0x334017){_0x334017=['id',_0xf222('0x12'),'lastName',_0xf222('0x13'),_0xf222('0x14'),_0xf222('0x15'),_0xf222('0x16'),_0xf222('0x17'),_0xf222('0x18'),_0xf222('0x19'),'updatedAt',_0xf222('0x1a'),_0xf222('0x1b')];}var _0x8783ab=_0x11fca0&&_0x52a003||!_0x11fca0&&!_0x52a003;if(_0x17df25){_0x8783ab=_0x17df25&&_0x52a003||!_0x17df25&&!_0x52a003;}_0x334017[_0xf222('0x1c')](function(_0x12a2e4){if(_0x12a2e4===_0xf222('0x1b')){if(_0x8783ab){_0x12f968[_0xf222('0x1d')]('GROUP_CONCAT(DISTINCT\x20t.id)',_0x12a2e4);}else if(_0x11fca0||_0x17df25){_0x12f968[_0xf222('0x1d')](_0x5b8f97+'.'+_0x12a2e4);}}else{_0x12f968[_0xf222('0x1d')](_0x5b8f97+'.'+_0x12a2e4);}});return _0x12f968;}function getSqlClauses(){var _0x2b0d92={'default':squel[_0xf222('0x1e')](),'fields':squel['expr'](),'filter':squel['expr'](),'tags':{'clause':squel[_0xf222('0x1e')](),'ids':null},'user':{'clause':squel[_0xf222('0x1e')](),'skip':![]}};return _0x2b0d92;}function parseTagParameter(_0x201cf0){var _0x33406d=squel[_0xf222('0x1e')]();if(_0x201cf0){var _0x151a89=utils['getSelectedTags'](_0x201cf0);_0x151a89[_0xf222('0x1c')](function(_0x589e20){_0x33406d['or'](_0xf222('0x1f'),'%,'+_0x589e20+',%');});}return _0x33406d;}function parseSearchFilter(_0x12d443){var _0x2c48ce=squel[_0xf222('0x1e')]();if(_0x12d443){var _0x4616de=_0x12d443[_0xf222('0x20')]('\x5c','\x5c\x5c')['replace'](/'/g,'\x27\x27');if(_0x4616de[_0xf222('0x21')]('\x20')[_0xf222('0x22')]>0x1){_0x2c48ce['or'](_0xf222('0x23'),qs[_0xf222('0x24')](_0x4616de),null);}else{if(qs[_0xf222('0x25')](_0x4616de)){_0x2c48ce['or'](_0xf222('0x26'),_0x4616de+'%');_0x2c48ce['or'](_0xf222('0x27'),_0x4616de+'%');_0x2c48ce['or'](_0xf222('0x28'),_0x4616de+'%');_0x2c48ce['or'](_0xf222('0x29'),_0x4616de+'%');}else if(qs[_0xf222('0x2a')](_0x4616de)){_0x2c48ce['or'](_0xf222('0x2b'),_0x4616de+'%');}else{_0x2c48ce['or']('c.firstName\x20LIKE\x20?','%'+_0x4616de+'%');_0x2c48ce['or'](_0xf222('0x2c'),'%'+_0x4616de+'%');_0x2c48ce['or'](_0xf222('0x2b'),'%'+_0x4616de+'%');}}}return _0x2c48ce;}function parseAdvancedSearchFilter(_0x3d5508,_0x2f04bf){var _0xe41039=getSqlClauses();var _0x40a715=as[_0xf222('0x2d')](_0x3d5508);var _0x1b928b=_0x40a715[_0xf222('0x2e')];for(var _0x59721a=0x0;_0x59721a<_0x40a715[_0xf222('0x2f')][_0xf222('0x22')];_0x59721a++){var _0x3ffc30=_0x40a715['conditions'][_0x59721a];var _0x3ccc9e='c';var _0x5d5963=_[_0xf222('0x30')](_0x2f04bf,[_0xf222('0x31'),_0x3ffc30[_0xf222('0x1d')]]);if(_0x5d5963){if(_0x3ffc30[_0xf222('0x1d')]===_0xf222('0x32')||_0x5d5963[_0xf222('0x33')]===_0xf222('0x34')){_0x3ffc30['castTo']='DATE';}var _0x439c2f=as[_0xf222('0x35')](_0x3ccc9e,_0x5d5963[_0xf222('0x36')],_0x3ffc30);_0xe41039[_0xf222('0x37')][_0x1b928b](_0x439c2f[_0xf222('0x38')],_0x439c2f[_0xf222('0x39')]['start'],_0x439c2f[_0xf222('0x39')][_0xf222('0x3a')]);}else{if(_0x3ffc30[_0xf222('0x1d')]==='Tag'){var _0x2c97cd=_0x3ffc30[_0xf222('0x39')][_0xf222('0x21')](',')['map'](function(_0x226d84){return Number(_0x226d84);});var _0x21e20a=parseTagParameter(_0x2c97cd);_0xe41039[_0xf222('0x3b')]['clause'][_0xf222('0x3c')](_0x21e20a);_0xe41039[_0xf222('0x3b')][_0xf222('0x3d')]=_0x2c97cd;}else if(_0x3ffc30[_0xf222('0x1d')]===_0xf222('0x3e')){_0xe41039['user'][_0xf222('0x3f')]['and'](_0xf222('0x40'),_0x3ffc30[_0xf222('0x39')]);_0xe41039[_0xf222('0x41')]['skip']=!![];}}}return _0xe41039;}function parseModelFields(_0x39784b,_0x3d70b0){var _0x54a10d=squel[_0xf222('0x1e')]();var _0x4b7b15=_(_0x39784b)[_0xf222('0x42')]()[_0xf222('0x43')](function(_0x769510){return _['some'](_0x3d70b0,[_0xf222('0x31'),_0x769510])?_0x769510:undefined;})[_0xf222('0x44')]()[_0xf222('0x39')]();if(_0x39784b[_0xf222('0x45')]){_0x4b7b15[_0xf222('0x46')](_0xf222('0x45'));}if(_0x4b7b15['length']>0x0){_0x4b7b15[_0xf222('0x1c')](function(_0x24e8a5){if(_0x24e8a5==='ListId'){_0x54a10d['and'](_0xf222('0x47'),[]['concat'](_0x39784b[_0x24e8a5]));}else if(_0x24e8a5===_0xf222('0x45')){var _0x281126='%'+_0x39784b[_0x24e8a5]+'%';_0x54a10d[_0xf222('0x3c')](_0xf222('0x48'),_0x281126);}else{_0x54a10d['and']('c.'+_0x24e8a5+'\x20=\x20?',_0x39784b[_0x24e8a5]);}});}return _0x54a10d;}function parseQueryParameters(_0x205895,_0x545464){var _0x522a9e=getSqlClauses();return Promise[_0xf222('0x49')]()[_0xf222('0x4a')](function(){return parseModelFields(_0x205895,_0x545464);})[_0xf222('0x4a')](function(_0x428ee6){_0x522a9e[_0xf222('0x4b')][_0xf222('0x3c')](_0x428ee6);return parseTagParameter(_0x205895['tag']);})[_0xf222('0x4a')](function(_0x1a0eaa){_0x522a9e['tags'][_0xf222('0x3f')][_0xf222('0x3c')](_0x1a0eaa);return parseSearchFilter(_0x205895['filter']);})[_0xf222('0x4a')](function(_0x51ed52){_0x522a9e[_0xf222('0x4c')]=_0x51ed52;return _0x522a9e;});}function checkListsRestrictions(_0x4169a0,_0x136f8b,_0x42d92e){var _0x3a3185=squel[_0xf222('0x1e')]();return Promise[_0xf222('0x49')]()[_0xf222('0x4a')](function(){if(_0x136f8b[_0xf222('0x5')]&&_0x4169a0[_0xf222('0x4')]===_0xf222('0x4d')&&!_0x42d92e){return _0x4169a0['getLists']({'attributes':['id'],'raw':!![]});}})['then'](function(_0x4bc643){if(_0x4bc643){if(_0x4bc643[_0xf222('0x22')]===0x0){_0x3a3185[_0xf222('0x3c')](_0xf222('0x4e'));}else{_0x3a3185[_0xf222('0x3c')]('c.ListId\x20IN\x20?',_[_0xf222('0x43')](_0x4bc643,'id'));}}})[_0xf222('0x4a')](function(){return _0x3a3185;});}function buildContactsQuery(_0x18d9f7,_0x2841a2,_0x59974f,_0x2bd5b1,_0x9c17be,_0x38117a){if(_0x2841a2){_0x2841a2=_0x59974f[_0xf222('0x4f')];}_0x18d9f7=addFieldsToQuery(_0x18d9f7,_0x2841a2,_0x2bd5b1,_0x38117a,!![]);var _0x1d60e5=squel[_0xf222('0x50')]();_0x1d60e5=addFieldsToQuery(_0x1d60e5,_0x2841a2,_0x2bd5b1,_0x38117a,![]);if(_0x2bd5b1||_0x38117a){_0x18d9f7=applyJoinClauses(_0x18d9f7,!![]);_0x18d9f7[_0xf222('0x51')](_0xf222('0x52'));if(_0x59974f[_0xf222('0x53')]){_0x1d60e5[_0xf222('0x53')](_0x59974f['limit']);}if(_0x59974f['offset']){_0x1d60e5[_0xf222('0x54')](_0x59974f['offset']);}}else{_0x1d60e5=applyJoinClauses(_0x1d60e5,![]);_0x1d60e5[_0xf222('0x51')]('subqry.id');if(_0x59974f[_0xf222('0x53')]){_0x18d9f7['limit'](_0x59974f[_0xf222('0x53')]);}if(_0x59974f[_0xf222('0x54')]){_0x18d9f7[_0xf222('0x54')](_0x59974f[_0xf222('0x54')]);}}_0x1d60e5['having'](_0x9c17be);if(_0x59974f[_0xf222('0x55')]){_0x59974f[_0xf222('0x55')][_0xf222('0x1c')](function(_0x2dc8d2){_0x18d9f7[_0xf222('0x55')]('c.'+_0x2dc8d2[0x0],_0x2dc8d2[0x1]===_0xf222('0x56')?![]:!![]);_0x1d60e5[_0xf222('0x55')](_0xf222('0x57')+_0x2dc8d2[0x0],_0x2dc8d2[0x1]===_0xf222('0x56')?![]:!![]);});}_0x1d60e5[_0xf222('0x8')](_0x18d9f7,_0xf222('0xb'));return _0x1d60e5;}function getFilteredContacts(_0x72af0b){var _0x4aa98c=[];return db['Tag'][_0xf222('0x58')]({'attributes':['id',_0xf222('0x31'),_0xf222('0x59')],'raw':!![]})[_0xf222('0x4a')](function(_0x214976){_0x4aa98c=_[_0xf222('0x5a')](_0x214976,'id');var _0x284495={'type':db[_0xf222('0x5b')][_0xf222('0x5c')][_0xf222('0x5d')],'model':db[_0xf222('0x5e')],'mapToModel':!![],'nest':!![],'raw':!![]};return db['sequelize']['query'](_0x72af0b[_0xf222('0x5f')](),_0x284495);})[_0xf222('0x4a')](function(_0x3e2e8d){_0x3e2e8d[_0xf222('0x1c')](function(_0x43dac3){if(_0x43dac3[_0xf222('0x1b')]){_0x43dac3[_0xf222('0x60')]=[];_0x43dac3[_0xf222('0x1b')][_0xf222('0x21')](',')[_0xf222('0x1c')](function(_0x32ec77){_0x43dac3[_0xf222('0x60')][_0xf222('0x46')](_0x4aa98c[_0x32ec77]);});}delete _0x43dac3[_0xf222('0x1b')];});return _0x3e2e8d;});}function countFilteredContacts(_0x564f7f,_0x43f492){var _0x5ee917=_0x564f7f[_0xf222('0x61')]();_0x5ee917[_0xf222('0x1d')](_0xf222('0x62'));_0x5ee917=applyJoinClauses(_0x5ee917,!![]);_0x564f7f[_0xf222('0x51')]('c.id');var _0x51443f=utils[_0xf222('0x63')](_0x43f492);if(!_[_0xf222('0x64')](_0x51443f)){_0x5ee917[_0xf222('0x65')](_0xf222('0x66'),_0x51443f);}var _0x356746={'type':db['Sequelize'][_0xf222('0x5c')]['SELECT'],'raw':!![]};return db[_0xf222('0x67')][_0xf222('0x68')](_0x5ee917['toString'](),_0x356746)[_0xf222('0x4a')](function(_0x5e8349){return _0x5e8349[0x0][_0xf222('0x62')];});}module[_0xf222('0x69')]={'buildContactsQuery':buildContactsQuery,'checkListsRestrictions':checkListsRestrictions,'countFilteredContacts':countFilteredContacts,'getContactsSubquery':getContactsSubquery,'getFilteredContacts':getFilteredContacts,'parseAdvancedSearchFilter':parseAdvancedSearchFilter,'parseQueryParameters':parseQueryParameters,'skipContactsQuery':skipContactsQuery};