@@ -13,6 +13,9 @@ import {
1313 createVisitor ,
1414 startANewLivechatRoomAndTakeIt ,
1515 closeOmnichannelRoom ,
16+ createVisitorWithCustomData ,
17+ sendAgentMessage ,
18+ sendMessage ,
1619} from '../../../data/livechat/rooms' ;
1720import { getRandomVisitorToken } from '../../../data/livechat/users' ;
1821import { getLivechatVisitorByToken } from '../../../data/livechat/visitor' ;
@@ -1312,6 +1315,7 @@ describe('LIVECHAT - visitors', () => {
13121315 expect ( res . body . visitors [ 0 ] ) . to . have . property ( 'visitorEmails' ) ;
13131316 } ) ;
13141317 } ) ;
1318+
13151319 describe ( 'omnichannel/contact' , ( ) => {
13161320 let contact : ILivechatVisitor ;
13171321 it ( 'should fail if user doesnt have view-l-room permission' , async ( ) => {
@@ -1433,4 +1437,232 @@ describe('LIVECHAT - visitors', () => {
14331437 expect ( contact . livechatData ) . to . have . property ( cfName , 'test' ) ;
14341438 } ) ;
14351439 } ) ;
1440+
1441+ describe ( 'lead capture' , ( ) => {
1442+ let visitor : ILivechatVisitor ;
1443+ let room : IOmnichannelRoom ;
1444+
1445+ before ( async ( ) => {
1446+ visitor = await createVisitorWithCustomData ( {
1447+ ignoreEmail : true ,
1448+ ignorePhone : true ,
1449+ } ) ;
1450+ room = await createLivechatRoom ( visitor . token ) ;
1451+ await createAgent ( ) ;
1452+ } ) ;
1453+ after ( async ( ) => {
1454+ await closeOmnichannelRoom ( room . _id ) ;
1455+ } ) ;
1456+
1457+ it ( 'should capture the data matching the email regex and add it to the visitor' , async ( ) => {
1458+ await sendMessage ( room . _id , 'My email is random@email.com' , visitor . token ) ;
1459+
1460+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1461+ expect ( visitor . visitorEmails ) . to . be . an ( 'array' ) ;
1462+ expect ( visitor . visitorEmails ) . to . have . lengthOf ( 1 ) ;
1463+ expect ( visitor . visitorEmails ?. [ 0 ] . address ) . to . be . equal ( 'random@email.com' ) ;
1464+ } ) ;
1465+
1466+ it ( 'should capture more emails when the visitor sends the message' , async ( ) => {
1467+ await sendMessage ( room . _id , 'Another email is test@teste.com' , visitor . token ) ;
1468+
1469+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1470+ expect ( visitor . visitorEmails ) . to . be . an ( 'array' ) ;
1471+ expect ( visitor . visitorEmails ) . to . have . lengthOf ( 2 ) ;
1472+ const emails = visitor . visitorEmails ?. map ( ( e ) => e . address ) ;
1473+ expect ( emails ) . to . include ( 'test@teste.com' ) ;
1474+ } ) ;
1475+
1476+ it ( 'should capture multiple emails' , async ( ) => {
1477+ await sendMessage ( room . _id , 'My emails are test@123.com notest@1234.com' , visitor . token ) ;
1478+
1479+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1480+ expect ( visitor . visitorEmails ) . to . be . an ( 'array' ) ;
1481+ expect ( visitor . visitorEmails ) . to . have . lengthOf ( 4 ) ;
1482+ const emails = visitor . visitorEmails ?. map ( ( e ) => e . address ) ;
1483+ expect ( emails ) . to . include ( 'test@123.com' ) ;
1484+ expect ( emails ) . to . include ( 'notest@1234.com' ) ;
1485+ } ) ;
1486+
1487+ it ( 'should not add an email thats already registered' , async ( ) => {
1488+ await sendMessage ( room . _id , 'My email is test@123.com' , visitor . token ) ;
1489+
1490+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1491+ expect ( visitor . visitorEmails ) . to . be . an ( 'array' ) ;
1492+ expect ( visitor . visitorEmails ) . to . have . lengthOf ( 4 ) ;
1493+ } ) ;
1494+
1495+ it ( 'should not save emails the agent sends' , async ( ) => {
1496+ await sendAgentMessage ( room . _id , 'Confirming your email is test@12345.com?' , credentials ) ;
1497+
1498+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1499+ expect ( visitor . visitorEmails ) . to . be . an ( 'array' ) ;
1500+ expect ( visitor . visitorEmails ) . to . have . lengthOf ( 4 ) ;
1501+ } ) ;
1502+
1503+ it ( 'should save phone numbers matching the regex' , async ( ) => {
1504+ await sendMessage ( room . _id , 'My phone number is 12345678' , visitor . token ) ;
1505+
1506+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1507+ expect ( visitor . phone ) . to . be . an ( 'array' ) ;
1508+ expect ( visitor . phone ) . to . have . lengthOf ( 1 ) ;
1509+ expect ( visitor . phone ?. [ 0 ] . phoneNumber ) . to . be . equal ( '12345678' ) ;
1510+ } ) ;
1511+
1512+ it ( 'should capture more phone numbers when the visitor sends the message' , async ( ) => {
1513+ await sendMessage ( room . _id , 'Another phone number is 87654321 87654323' , visitor . token ) ;
1514+
1515+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1516+ expect ( visitor . phone ) . to . be . an ( 'array' ) ;
1517+ expect ( visitor . phone ) . to . have . lengthOf ( 3 ) ;
1518+ const phones = visitor . phone ?. map ( ( p ) => p . phoneNumber ) ;
1519+ expect ( phones ) . to . include ( '87654321' ) ;
1520+ expect ( phones ) . to . include ( '87654323' ) ;
1521+ } ) ;
1522+
1523+ it ( 'should not add a phone number thats already registered' , async ( ) => {
1524+ await sendMessage ( room . _id , 'My phone number is 12345678' , visitor . token ) ;
1525+
1526+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1527+ expect ( visitor . phone ) . to . be . an ( 'array' ) ;
1528+ expect ( visitor . phone ) . to . have . lengthOf ( 3 ) ;
1529+ } ) ;
1530+
1531+ it ( 'should not save phone numbers the agent sends' , async ( ) => {
1532+ await sendAgentMessage ( room . _id , 'Confirming your phone number is 99999999?' , credentials ) ;
1533+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1534+ expect ( visitor . phone ) . to . be . an ( 'array' ) ;
1535+ expect ( visitor . phone ) . to . have . lengthOf ( 3 ) ;
1536+ } ) ;
1537+
1538+ it ( 'should capture both phones & emails when sent on the same message' , async ( ) => {
1539+ await sendMessage ( room . _id , 'My email is zardw@asdf.com and my phone is 11223344' , visitor . token ) ;
1540+
1541+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1542+ expect ( visitor . visitorEmails ) . to . be . an ( 'array' ) ;
1543+ expect ( visitor . visitorEmails ) . to . have . lengthOf ( 5 ) ;
1544+ const emails = visitor . visitorEmails ?. map ( ( e ) => e . address ) ;
1545+ expect ( emails ) . to . include ( 'zardw@asdf.com' ) ;
1546+
1547+ expect ( visitor . phone ) . to . be . an ( 'array' ) ;
1548+ expect ( visitor . phone ) . to . have . lengthOf ( 4 ) ;
1549+ const phones = visitor . phone ?. map ( ( p ) => p . phoneNumber ) ;
1550+ expect ( phones ) . to . include ( '11223344' ) ;
1551+ } ) ;
1552+
1553+ describe ( 'when settings are empty' , ( ) => {
1554+ before ( async ( ) => {
1555+ await updateSetting ( 'Livechat_lead_phone_regex' , '' ) ;
1556+ await updateSetting ( 'Livechat_lead_email_regex' , '' ) ;
1557+ } ) ;
1558+
1559+ after ( async ( ) => {
1560+ // reset settings
1561+ await updateSetting ( 'Livechat_lead_email_regex' , '\\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\\.)+[A-Z]{2,4}\\b' ) ;
1562+ await updateSetting (
1563+ 'Livechat_lead_phone_regex' ,
1564+ '((?:\\([0-9]{1,3}\\)|[0-9]{2})[ \\-]*?[0-9]{4,5}(?:[\\-\\s\\_]{1,2})?[0-9]{4}(?:(?=[^0-9])|$)|[0-9]{4,5}(?:[\\-\\s\\_]{1,2})?[0-9]{4}(?:(?=[^0-9])|$))' ,
1565+ ) ;
1566+ } ) ;
1567+
1568+ it ( 'should not capture any email or phone no matter what the visitor sends' , async ( ) => {
1569+ await sendMessage ( room . _id , 'Now my email is this@shalnotpass.com' , visitor . token ) ;
1570+
1571+ await sendMessage ( room . _id , 'And my phone number is 55667788' , visitor . token ) ;
1572+
1573+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1574+ const emails = visitor . visitorEmails ?. map ( ( e ) => e . address ) ;
1575+ expect ( emails || [ ] ) . to . not . include ( 'this@shalnotpass.com' ) ;
1576+
1577+ const phones = visitor . phone ?. map ( ( p ) => p . phoneNumber ) ;
1578+ expect ( phones || [ ] ) . to . not . include ( '55667788' ) ;
1579+ } ) ;
1580+ } ) ;
1581+
1582+ describe ( 'when phone regex is broken' , ( ) => {
1583+ before ( async ( ) => {
1584+ await updateSetting ( 'Livechat_lead_phone_regex' , '(' ) ;
1585+ } ) ;
1586+
1587+ after ( async ( ) => {
1588+ // reset settings
1589+ await updateSetting (
1590+ 'Livechat_lead_phone_regex' ,
1591+ '((?:\\([0-9]{1,3}\\)|[0-9]{2})[ \\-]*?[0-9]{4,5}(?:[\\-\\s\\_]{1,2})?[0-9]{4}(?:(?=[^0-9])|$)|[0-9]{4,5}(?:[\\-\\s\\_]{1,2})?[0-9]{4}(?:(?=[^0-9])|$))' ,
1592+ ) ;
1593+ } ) ;
1594+
1595+ it ( 'should capture email' , async ( ) => {
1596+ await sendMessage ( room . _id , 'Now my email is this@isok.com' , visitor . token ) ;
1597+
1598+ await sendMessage ( room . _id , 'And my phone number is 55667799' , visitor . token ) ;
1599+
1600+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1601+
1602+ expect ( visitor . visitorEmails ) . to . be . an ( 'array' ) ;
1603+ expect ( visitor . visitorEmails ) . to . have . lengthOf ( 6 ) ;
1604+ const emails = visitor . visitorEmails ?. map ( ( e ) => e . address ) ;
1605+ expect ( emails ) . to . include ( 'this@isok.com' ) ;
1606+
1607+ expect ( visitor . phone ) . to . have . lengthOf ( 4 ) ;
1608+ const phones = visitor . phone ?. map ( ( p ) => p . phoneNumber ) ;
1609+ expect ( phones || [ ] ) . to . not . include ( '55667788' ) ;
1610+ } ) ;
1611+ } ) ;
1612+
1613+ describe ( 'when email regex is broken' , ( ) => {
1614+ before ( async ( ) => {
1615+ await updateSetting ( 'Livechat_lead_email_regex' , '(' ) ;
1616+ } ) ;
1617+
1618+ after ( async ( ) => {
1619+ // reset settings
1620+ await updateSetting ( 'Livechat_lead_email_regex' , '\\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\\.)+[A-Z]{2,4}\\b' ) ;
1621+ } ) ;
1622+
1623+ it ( 'should capture email' , async ( ) => {
1624+ await sendMessage ( room . _id , 'Now my email is this@shalnotpass.com' , visitor . token ) ;
1625+
1626+ await sendMessage ( room . _id , 'And my phone number is 98765432' , visitor . token ) ;
1627+
1628+ visitor = await getLivechatVisitorByToken ( visitor . token ) ;
1629+
1630+ expect ( visitor . visitorEmails ) . to . have . lengthOf ( 6 ) ;
1631+ const emails = visitor . visitorEmails ?. map ( ( e ) => e . address ) ;
1632+ expect ( emails || [ ] ) . to . not . include ( 'this@shalnotpass.com' ) ;
1633+
1634+ expect ( visitor . phone ) . to . be . an ( 'array' ) ;
1635+ expect ( visitor . phone ) . to . have . lengthOf ( 5 ) ;
1636+ const phones = visitor . phone ?. map ( ( p ) => p . phoneNumber ) ;
1637+ expect ( phones ) . to . include ( '98765432' ) ;
1638+ } ) ;
1639+ } ) ;
1640+
1641+ describe ( 'when the visitor has emails & phones already' , ( ) => {
1642+ let newVisitor : ILivechatVisitor ;
1643+ let newRoom : IOmnichannelRoom ;
1644+ before ( async ( ) => {
1645+ newVisitor = await createVisitor ( ) ;
1646+ newRoom = await createLivechatRoom ( newVisitor . token ) ;
1647+ } ) ;
1648+ after ( async ( ) => {
1649+ await closeOmnichannelRoom ( newRoom . _id ) ;
1650+ } ) ;
1651+
1652+ it ( 'should capture new emails & phones and add them to the existing ones' , async ( ) => {
1653+ await sendMessage ( newRoom . _id , 'My email is 1234@12344.com and my phone is 11223344' , newVisitor . token ) ;
1654+
1655+ newVisitor = await getLivechatVisitorByToken ( newVisitor . token ) ;
1656+ expect ( newVisitor . visitorEmails ) . to . be . an ( 'array' ) ;
1657+ expect ( newVisitor . visitorEmails ) . to . have . lengthOf ( 2 ) ;
1658+ const emails = newVisitor . visitorEmails ?. map ( ( e ) => e . address ) ;
1659+ expect ( emails ) . to . include ( '1234@12344.com' ) ;
1660+
1661+ expect ( newVisitor . phone ) . to . be . an ( 'array' ) ;
1662+ expect ( newVisitor . phone ) . to . have . lengthOf ( 2 ) ;
1663+ const phones = newVisitor . phone ?. map ( ( p ) => p . phoneNumber ) ;
1664+ expect ( phones ) . to . include ( '11223344' ) ;
1665+ } ) ;
1666+ } ) ;
1667+ } ) ;
14361668} ) ;
0 commit comments