Verified Commit 036f4959 authored by dockx thibault's avatar dockx thibault
Browse files

:sparkles: Feat(Extractor) - implement email notification for batch extraction...

:sparkles: Feat(Extractor) - implement email notification for batch extraction results with attachment
parent 27d81139
1 merge request!90Resolve "[Orchestrator] - Task for CSV Extraction Based on Group and Member Criteria"
Pipeline #32994 failed with stages
in 46 seconds
Showing with 59 additions and 7 deletions
+59 -7
...@@ -138,21 +138,73 @@ class Extractor implements EndpointInterface ...@@ -138,21 +138,73 @@ class Extractor implements EndpointInterface
$success = $this->extractToFileBatch($allUserAttributes, $filename, 'csv'); $success = $this->extractToFileBatch($allUserAttributes, $filename, 'csv');
if ($success) { if ($success) {
$finalMessage = "Batch extraction successful to $filename."; // --- EMAIL LOGIC START ---
if (!empty($errors)) { // Retrieve sender and recipients from main task
$finalMessage .= " Some errors encountered: " . implode("; ", $errors); $mainTaskDetails = $this->gateway->getLdapTasks(
'(objectClass=fdExtractorTasks)',
[
'fdExtractorEmailSender',
'fdExtractorListOfRecipientsMails'
],
'',
$mainTaskDn
);
$sender = $mainTaskDetails[0]['fdextractoremailsender'][0] ?? '';
$recipients = $mainTaskDetails[0]['fdextractorlistofrecipientsmails'] ?? [];
$this->gateway->unsetCountKeys($recipients);
// Compose mail subject/body
$subject = "FusionDirectory Extractor - Export file";
$body = "Your requested extract is attached.\n\nFile: $filename";
$signature = null;
$receipt = null;
// Prepare attachment
$attachments = [[
'cn' => basename($filename),
'content' => file_get_contents($filename)
]];
if (empty($sender) || empty($recipients)) {
$finalMessage = "Batch extraction successful to $filename. Email not sent: sender or recipient missing.";
if (!empty($errors)) {
$finalMessage .= " Some errors encountered: " . implode("; ", $errors);
}
$this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], $finalMessage); $this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], $finalMessage);
$result[$task['dn']]['result'] = $finalMessage;
} else { } else {
$this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], '2'); // Send mail using MailLib
$mail_controller = new \FusionDirectory\Mail\MailLib(
$sender,
null,
$recipients,
$body,
$signature,
$subject,
$receipt,
$attachments
);
$mailSentResult = $mail_controller->sendMail();
if ($mailSentResult[0] == "SUCCESS") {
$finalMessage = "Batch extraction successful to $filename. Email sent to recipients.";
if (!empty($errors)) {
$finalMessage .= " Some errors encountered: " . implode("; ", $errors);
}
$this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], '2');
$result[$task['dn']]['result'] = $finalMessage;
} else {
$errorMessage = "Batch extraction successful to $filename, but email failed: " . $mailSentResult[0];
$this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], $errorMessage);
$result[$task['dn']]['result'] = $errorMessage;
}
} }
$result[$task['dn']]['result'] = $finalMessage; // --- EMAIL LOGIC END ---
} else { } else {
$errorMessage = "Failed to write batch data to $filename."; $errorMessage = "Failed to write batch data to $filename.";
// Update the status to error ('1') // Update the status to error ('1')
$this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], $errorMessage); $this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], $errorMessage);
// Add to result but DON'T throw an exception, which would cause the catch block to overwrite our status
$result[$task['dn']]['result'] = $errorMessage; $result[$task['dn']]['result'] = $errorMessage;
// Continue to next task
continue; continue;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment