Skip to content

FOUR-20673: Create a new column to know if an email was sent #7887

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

Merged
merged 5 commits into from
Jan 16, 2025
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
1 change: 1 addition & 0 deletions ProcessMaker/Models/ProcessRequestToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class ProcessRequestToken extends ProcessMakerModel implements TokenInterface
'token_properties' => 'array',
'is_priority' => 'boolean',
'is_actionbyemail' => 'boolean',
'is_emailsent' => 'boolean',
];

/**
Expand Down
72 changes: 70 additions & 2 deletions ProcessMaker/Repositories/TokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
use ProcessMaker\Models\FeelExpressionEvaluator;
use ProcessMaker\Models\ProcessAbeRequestToken;
use ProcessMaker\Models\ProcessCollaboration;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequest as Instance;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\ProcessRequestToken as Token;
use ProcessMaker\Models\Screen;
use ProcessMaker\Models\User;
use ProcessMaker\Nayra\Bpmn\Collection;
use ProcessMaker\Nayra\Bpmn\Models\EndEvent;
Expand Down Expand Up @@ -134,7 +136,7 @@ public function persistActivityActivated(ActivityInterface $activity, TokenInter
$dataManager = new DataManager();
$tokenData = $dataManager->getData($token);
$feel = new FeelExpressionEvaluator();
$evaluatedUsers = $selfServiceUsers ? $feel->render($selfServiceUsers, $tokenData) ?? null: [];
$evaluatedUsers = $selfServiceUsers ? $feel->render($selfServiceUsers, $tokenData) ?? null : [];
$evaluatedGroups = $selfServiceGroups ? $feel->render($selfServiceGroups, $tokenData) ?? null : [];

// If we have single values we put it inside an array
Expand Down Expand Up @@ -172,7 +174,10 @@ public function persistActivityActivated(ActivityInterface $activity, TokenInter
CaseUpdate::dispatchSync($request, $token);

if (!is_null($user)) {
// Review if the task has enable the action by email
$this->validateAndSendActionByEmail($activity, $token, $user->email);
// Review if the user has enable the email notification
$this->validateEmailUserNotification($token, $user);
}
$this->instanceRepository->persistInstanceUpdated($token->getInstance());
}
Expand All @@ -183,7 +188,6 @@ public function persistActivityActivated(ActivityInterface $activity, TokenInter
* @param ActivityInterface $activity
* @param TokenInterface $token
* @param string $to
* @param array $data
*
* @return void
*/
Expand Down Expand Up @@ -225,6 +229,70 @@ private function validateAndSendActionByEmail(ActivityInterface $activity, Token
}
}

/**
* Validates the user's email notification settings and sends an email if enabled.
*
* @param TokenInterface $token The token containing task information.
* @param User $user The user to whom the email notification will be sent.
* @return mixed|null Returns the result of the email sending operation or null if not sent.
*/
private function validateEmailUserNotification(TokenInterface $token, User $user)
{
try {
Log::Info('User isEmailTaskEnable: ' . $user->email_task_notification);
// Return if email task notification is not enabled or email is empty
if (!$user->email_task_notification || empty($user->email)) {
return null;
}
// Prepare data for the email
$data = $this->prepareEmailData($token, $user);

// Send Email
return (new TaskActionByEmail())->sendAbeEmail($data['configEmail'], $user->email, $data['emailData']);
} catch (\Exception $e) {
// Catch and log the error
Log::error('Failed to validate and send email task notification', [
'error' => $e->getMessage(),
]);
}
}

/**
* Prepares the email data and configuration for sending an email notification.
*
* @param TokenInterface $token The token containing task information.
* @param User $user The user for whom the email data is being prepared.
* @return array An associative array containing 'emailData' and 'configEmail'.
*/
private function prepareEmailData(TokenInterface $token, User $user)
{
// Get the case
$caseTitle = ProcessRequest::where('id', $token->process_request_id)->value('case_title');
// Prepare the email data
$taskName = $token->element_name ?? '';
$emailData = [
'firstname' => $user->firstname ?? '',
'assigned_by' => Auth::user()->fullname ?? '',
'element_name' => $taskName,
'case_title' => $caseTitle, // Populate this if needed
'due_date' => $token->due_at ?? '',
'imgHeader' => config('app.url') . '/img/processmaker-login.svg',
];
// Get the screen
$screen = Screen::where('title', 'Default Email Task Notification')->first();
// Prepare the email configuration
$configEmail = [
'emailServer' => 0, // Use the default email server
'subject' => "{$user->firstname} assigned you in {$taskName}",
'screenEmailRef' => $screen->id ?? 0, // Define here the screen to use
];

return [
'emailData' => $emailData,
'configEmail' => $configEmail,
];
}

/**
* Get due Variable
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('process_request_tokens', function (Blueprint $table) {
$table->boolean('is_emailsent')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('process_request_tokens', function (Blueprint $table) {
$table->dropColumn('is_emailsent');
});
}
};
Loading