Skip to content

Commit 18f052f

Browse files
authored
Merge pull request #8036 from bakaphp/hotfix/affiliate-link
refact: fix
2 parents 2f61b51 + 414b649 commit 18f052f

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

src/Domains/Intelligence/Workflows/HandOffActivity.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public function execute(Lead $lead, Apps $app, array $params): array
4141
app: $app,
4242
integration: IntegrationsEnum::INTERNAL,
4343
integrationOperation: function ($lead, $app, $integrationCompany, $additionalParams) use ($params) {
44+
//for now to avoid issue with duplicate handoff notifications
45+
$deduplicationKey = $this->getDeduplicationKey($lead, $params);
46+
if ($this->isDuplicateNotification($lead, $deduplicationKey)) {
47+
return [
48+
'success' => true,
49+
'message' => 'Handoff already processed (duplicate notification prevented)',
50+
'duplicate' => true,
51+
];
52+
}
53+
4454
$leadOwner = $this->getLeadOwner($lead, $app, $params);
4555
$handOffType = strtolower($params['handoff_type'] ?? self::DEFAULT_HANDOFF_TYPE);
4656
$handOffUserRole = $this->getHandOffUserRole($lead, $handOffType);
@@ -77,11 +87,28 @@ public function execute(Lead $lead, Apps $app, array $params): array
7787
//$communicationChannel = $lead->get(EnumsConfigurationEnum::AGENT_COMMUNICATION_CHANNEL->value) ?? 'sms';
7888
$lead->set(ConfigurationEnum::AGENT_HAND_OFF->value, 1);
7989

80-
$handOffNotification = $this->createHandOffNotification($lead, $leadOwner, $handOffType, $params);
90+
$handOffNotification = $this->createHandOffNotification(
91+
$lead,
92+
$leadOwner,
93+
$handOffType,
94+
$params
95+
);
8196
$leadOwner->notify($handOffNotification);
8297

83-
$this->postConversationSummary($lead, $leadOwner, $handOffType, $params);
84-
$managersNotified = $this->notifyManagers($lead, $leadOwner, $handOffNotification, $handOffUserRole);
98+
$this->postConversationSummary(
99+
$lead,
100+
$leadOwner,
101+
$handOffType,
102+
$params
103+
);
104+
$managersNotified = $this->notifyManagers(
105+
$lead,
106+
$leadOwner,
107+
$handOffNotification,
108+
$handOffUserRole
109+
);
110+
111+
$this->markNotificationAsProcessed($lead, $deduplicationKey);
85112

86113
return [
87114
'success' => true,
@@ -224,4 +251,26 @@ private function getOrCreateServiceLeadType(Lead $lead): LeadType
224251

225252
return new CreateLeadTypeAction($leadTypeDto)->execute();
226253
}
254+
255+
private function getDeduplicationKey(Lead $lead, array $params): string
256+
{
257+
$dataToHash = [
258+
'lead_id' => $lead->getId(),
259+
'params' => $params,
260+
];
261+
262+
return md5((string) json_encode($dataToHash));
263+
}
264+
265+
private function isDuplicateNotification(Lead $lead, string $deduplicationKey): bool
266+
{
267+
$processedKey = $lead->get('handoff_dedup_' . $deduplicationKey);
268+
269+
return $processedKey !== null;
270+
}
271+
272+
private function markNotificationAsProcessed(Lead $lead, string $deduplicationKey): void
273+
{
274+
$lead->set('handoff_dedup_' . $deduplicationKey, time());
275+
}
227276
}

0 commit comments

Comments
 (0)