Skip to content

Commit

Permalink
Merge pull request #2716 from alanhartless/bug-2461
Browse files Browse the repository at this point in the history
Prevent contacts from getting prematurely removed from a segment
  • Loading branch information
dongilbert committed Oct 11, 2016
2 parents 9f74773 + 9564f8d commit a108b93
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
31 changes: 25 additions & 6 deletions app/bundles/LeadBundle/Entity/LeadListRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,15 @@ public function getLeadsByList($lists, $args = [])
$q->select($select)
->from(MAUTIC_TABLE_PREFIX.'leads', 'l');

$batchExpr = $q->expr()->andX();
// Only leads that existed at the time of count
if ($batchLimiters) {
if (!empty($batchLimiters['minId']) && !empty($batchLimiters['maxId'])) {
$expr->add(
$batchExpr->add(
$q->expr()->comparison('l.id', 'BETWEEN', "{$batchLimiters['minId']} and {$batchLimiters['maxId']}")
);
} elseif (!empty($batchLimiters['maxId'])) {
$expr->add(
$batchExpr->add(
$q->expr()->lte('l.id', $batchLimiters['maxId'])
);
}
Expand All @@ -363,17 +364,30 @@ public function getLeadsByList($lists, $args = [])
// Leads that do not have any record in the lead_lists_leads table for this lead list
// For non null fields - it's apparently better to use left join over not exists due to not using nullable
// fields - https://explainextended.com/2009/09/18/not-in-vs-not-exists-vs-left-join-is-null-mysql/
$listOnExpr = $q->expr()->andX(
$q->expr()->eq('ll.leadlist_id', $id),
$q->expr()->eq('ll.lead_id', 'l.id')
);

if (!empty($batchLimiters['dateTime'])) {
// Only leads in the list at the time of count
$listOnExpr->add(
$q->expr()->lte('ll.date_added', $q->expr()->literal($batchLimiters['dateTime']))
);
}

$q->leftJoin(
'l',
MAUTIC_TABLE_PREFIX.'lead_lists_leads',
'll',
$q->expr()->andX(
$q->expr()->eq('ll.leadlist_id', $id),
$q->expr()->eq('ll.lead_id', 'l.id')
)
$listOnExpr
);
$expr->add($q->expr()->isNull('ll.lead_id'));

if ($batchExpr->count()) {
$expr->add($batchExpr);
}

$q->andWhere($expr);
} elseif ($nonMembersOnly) {
// Only leads that are part of the list that no longer match filters and have not been manually removed
Expand Down Expand Up @@ -412,6 +426,11 @@ public function getLeadsByList($lists, $args = [])
$mainExpr->add(
sprintf('l.id NOT IN (%s)', $sq->getSQL())
);

if ($batchExpr->count()) {
$mainExpr->add($batchExpr);
}

$q->andWhere($mainExpr);
}

Expand Down
6 changes: 6 additions & 0 deletions app/bundles/LeadBundle/Model/ListModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,9 @@ public function rebuildListLeads(LeadList $entity, $limit = 1000, $maxLeads = fa
}
}

// Unset max ID to prevent capping at newly added max ID
unset($batchLimiters['maxId']);

// Get a count of leads to be removed
$removeLeadCount = $this->getLeadsByList(
$list,
Expand All @@ -651,6 +654,9 @@ public function rebuildListLeads(LeadList $entity, $limit = 1000, $maxLeads = fa
]
);

// Ensure the same list is used each batch
$batchLimiters['maxId'] = (int) $removeLeadCount[$id]['maxId'];

// Restart batching
$start = $lastRoundPercentage = 0;
$leadCount = $removeLeadCount[$id]['count'];
Expand Down

0 comments on commit a108b93

Please sign in to comment.