From a51b103ceb49368f4bf7fd6f239e56036f3d43b8 Mon Sep 17 00:00:00 2001 From: Paula Quispe Date: Fri, 10 Jan 2025 16:10:05 -0400 Subject: [PATCH 1/3] FOUR-20673 --- ProcessMaker/Models/ProcessRequestToken.php | 1 + ProcessMaker/Repositories/TokenRepository.php | 36 +++++++++++++++++-- ...olumn_for_process_request_tokens_table.php | 27 ++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 database/migrations/2025_01_10_183851_add_is_emailsent_column_for_process_request_tokens_table.php diff --git a/ProcessMaker/Models/ProcessRequestToken.php b/ProcessMaker/Models/ProcessRequestToken.php index 9e266fe1cd..34dbdca57b 100644 --- a/ProcessMaker/Models/ProcessRequestToken.php +++ b/ProcessMaker/Models/ProcessRequestToken.php @@ -154,6 +154,7 @@ class ProcessRequestToken extends ProcessMakerModel implements TokenInterface 'token_properties' => 'array', 'is_priority' => 'boolean', 'is_actionbyemail' => 'boolean', + 'is_emailsent' => 'boolean', ]; /** diff --git a/ProcessMaker/Repositories/TokenRepository.php b/ProcessMaker/Repositories/TokenRepository.php index 74a8e79e0b..129d3036ba 100644 --- a/ProcessMaker/Repositories/TokenRepository.php +++ b/ProcessMaker/Repositories/TokenRepository.php @@ -134,7 +134,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 @@ -172,7 +172,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()); } @@ -183,7 +186,6 @@ public function persistActivityActivated(ActivityInterface $activity, TokenInter * @param ActivityInterface $activity * @param TokenInterface $token * @param string $to - * @param array $data * * @return void */ @@ -225,6 +227,36 @@ private function validateAndSendActionByEmail(ActivityInterface $activity, Token } } + /** + * Validate email configuration and send email + * + * @param User $user + * + * @return void + */ + private function validateEmailUserNotification(TokenInterface $token, User $user) + { + try { + Log::Info('User isEmailTaskEnable: ' . $user->email_task_notification); + // If the email task notification is checked in user profile + if ($user->email_task_notification && !empty($user->email)) { + $taskName = $token->element_name ?? ''; + $data['some_data'] = ''; + $configEmail['emailServer'] = 0; // Use the default email server + $configEmail['subject'] = $user->firstname . ' assigned you in ' . $taskName; + $configEmail['screenEmailRef'] = 0; // Define here the screen to use + + // Send Email + return (new TaskActionByEmail())->sendAbeEmail($configEmail, $user->email, $data); + } + } catch (\Exception $e) { + // Catch and log the error + Log::error('Failed to validate and send action by email', [ + 'error' => $e->getMessage(), + ]); + } + } + /** * Get due Variable * diff --git a/database/migrations/2025_01_10_183851_add_is_emailsent_column_for_process_request_tokens_table.php b/database/migrations/2025_01_10_183851_add_is_emailsent_column_for_process_request_tokens_table.php new file mode 100644 index 0000000000..52d47aed34 --- /dev/null +++ b/database/migrations/2025_01_10_183851_add_is_emailsent_column_for_process_request_tokens_table.php @@ -0,0 +1,27 @@ +boolean('is_emailsent')->default(false); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('process_request_tokens', function (Blueprint $table) { + $table->dropColumn('is_emailsent'); + }); + } +}; From e0204b927a93732cd12f8ced53bb9e6f53818258 Mon Sep 17 00:00:00 2001 From: Paula Quispe Date: Mon, 13 Jan 2025 11:53:12 -0400 Subject: [PATCH 2/3] FOUR-20673: adding values for the email --- ProcessMaker/Repositories/TokenRepository.php | 59 ++++++++++++++----- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/ProcessMaker/Repositories/TokenRepository.php b/ProcessMaker/Repositories/TokenRepository.php index 129d3036ba..555cdf2c41 100644 --- a/ProcessMaker/Repositories/TokenRepository.php +++ b/ProcessMaker/Repositories/TokenRepository.php @@ -228,35 +228,64 @@ private function validateAndSendActionByEmail(ActivityInterface $activity, Token } /** - * Validate email configuration and send email + * Validates the user's email notification settings and sends an email if enabled. * - * @param User $user - * - * @return void + * @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); - // If the email task notification is checked in user profile - if ($user->email_task_notification && !empty($user->email)) { - $taskName = $token->element_name ?? ''; - $data['some_data'] = ''; - $configEmail['emailServer'] = 0; // Use the default email server - $configEmail['subject'] = $user->firstname . ' assigned you in ' . $taskName; - $configEmail['screenEmailRef'] = 0; // Define here the screen to use - - // Send Email - return (new TaskActionByEmail())->sendAbeEmail($configEmail, $user->email, $data); + // 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 action by email', [ + 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) + { + $taskName = $token->element_name ?? ''; + // Prepare the email data + $emailData = [ + 'firstname' => $user->firstname ?? '', + 'assigned_by' => '', // You may want to populate this if needed + 'element_name' => $taskName, + 'case_title' => '', // Populate this if needed + 'due_date' => $token->due_at ?? '', + ]; + // Prepare the email configuration + $configEmail = [ + 'emailServer' => 0, // Use the default email server + 'subject' => "{$user->firstname} assigned you in {$taskName}", + 'screenEmailRef' => 0, // Define here the screen to use + ]; + + return [ + 'emailData' => $emailData, + 'configEmail' => $configEmail, + ]; + } + /** * Get due Variable * From feb68dbe84df4461a352923d03fdb0ed60cb3f64 Mon Sep 17 00:00:00 2001 From: Paula Quispe Date: Wed, 15 Jan 2025 17:01:46 -0400 Subject: [PATCH 3/3] FOUR-20673: Adding other params for the email --- ProcessMaker/Repositories/TokenRepository.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/ProcessMaker/Repositories/TokenRepository.php b/ProcessMaker/Repositories/TokenRepository.php index 555cdf2c41..2dab177fed 100644 --- a/ProcessMaker/Repositories/TokenRepository.php +++ b/ProcessMaker/Repositories/TokenRepository.php @@ -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; @@ -264,20 +266,25 @@ private function validateEmailUserNotification(TokenInterface $token, User $user */ private function prepareEmailData(TokenInterface $token, User $user) { - $taskName = $token->element_name ?? ''; + // 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' => '', // You may want to populate this if needed + 'assigned_by' => Auth::user()->fullname ?? '', 'element_name' => $taskName, - 'case_title' => '', // Populate this if needed + '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' => 0, // Define here the screen to use + 'screenEmailRef' => $screen->id ?? 0, // Define here the screen to use ]; return [