Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent contacts from getting prematurely removed from a segment #2716

Merged
merged 1 commit into from
Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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