Skip to content

Commit

Permalink
Merge pull request ProcessMaker#7539 from ProcessMaker/feature/FOUR-1…
Browse files Browse the repository at this point in the history
…9490

FOUR-19490: The store comments needs to consider populate the comments.case_number
  • Loading branch information
pmPaulis authored Oct 15, 2024
2 parents 7dec4b0 + c4cc694 commit f007b85
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 5 deletions.
28 changes: 28 additions & 0 deletions ProcessMaker/Http/Controllers/Api/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ public function store(Request $request)
$this->authorizeComment($request);

$data['user_id'] = Auth::user()->id;
// Get the case_number if was not send
$caseNumber = $request->input('case_number', null);
if (is_null($caseNumber)) {
$caseNumber = $this->getCaseNumber($request);
}
$data['case_number'] = $caseNumber;
$request->merge($data);
$request->validate(Comment::rules());

Expand Down Expand Up @@ -194,6 +200,12 @@ public function update(Comment $comment, Request $request)
abort(403);
}
$data['user_id'] = Auth::user()->id;
// Get the case_number if was not send
$caseNumber = $request->input('case_number', null);
if (is_null($caseNumber)) {
$caseNumber = $this->getCaseNumber($request);
}
$data['case_number'] = $caseNumber;
$request->merge($data);
$request->validate(Comment::rules());

Expand Down Expand Up @@ -227,4 +239,20 @@ public function destroy(Comment $comment)

return response([], 204);
}

private function getCaseNumber(Request $request)
{
$requestId = null;
$commentableType = $request->input('commentable_type');
if ($commentableType === 'ProcessMaker\Models\ProcessRequestToken') {
$requestId = ProcessRequestToken::where('id', $request->input('commentable_id'))->value('process_request_id');
} elseif ($commentableType === 'ProcessMaker\Models\ProcessRequest') {
$requestId = $request->input('commentable_id');
}
if ($requestId) {
return ProcessRequest::where('id', $requestId)->value('case_number');
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ public function update(ProcessRequest $request, Request $httpRequest)
'commentable_id' => $request->id,
'subject' => 'Data edited',
'body' => $user_name . ' ' . $text,
'case_number' => isset($request->case_number) ? $request->case_number : null,
]);
} else {
$httpRequest->validate(ProcessRequest::rules($request));
Expand Down Expand Up @@ -629,6 +630,7 @@ private function completeRequest(ProcessRequest $request)
'commentable_id' => $request->id,
'subject' => __('Process Manually Completed'),
'body' => $user->fullname . ' ' . __('manually completed the request from an error state'),
'case_number' => isset($request->case_number) ? $request->case_number : null,
]);
}

Expand Down
10 changes: 7 additions & 3 deletions ProcessMaker/Listeners/BpmnSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function onScriptTaskActivated(ScriptTaskInterface $scriptTask, TokenInte
try {
WorkflowManager::runScripTask($scriptTask, $token);
} catch (\Throwable $e) {
\Illuminate\Support\Facades\Log::Error('Unhandled error when running a script task:' . $e->getMessage());
Log::Error('Unhandled error when running a script task:' . $e->getMessage());
}
}

Expand All @@ -211,7 +211,7 @@ public function onServiceTaskActivated(ServiceTaskInterface $serviceTask, TokenI
try {
WorkflowManager::runServiceTask($serviceTask, $token);
} catch (\Throwable $e) {
\Illuminate\Support\Facades\Log::Error('Unhandled error when running a service task:' . $e->getMessage());
Log::Error('Unhandled error when running a service task:' . $e->getMessage());
}
}

Expand All @@ -222,6 +222,9 @@ public function onIntermediateCatchEventActivated(IntermediateCatchEventInterfac
TimerEventDefinitionInterface::class => 'System is waiting for the scheduled timer: ":event"',
ConditionalEventDefinitionInterface::class => 'System is waiting for the conditional event: ":event"',
];
// Get the case_number
$caseNumber = ProcessRequest::where('id', $token->getInstance()->id)->value('case_number');

foreach ($event->getEventDefinitions() as $eventDefinition) {
foreach ($messages as $interface => $message) {
if (is_subclass_of($eventDefinition, $interface)) {
Expand All @@ -232,6 +235,7 @@ public function onIntermediateCatchEventActivated(IntermediateCatchEventInterfac
'subject' => __($message, ['event' => $event->getName()]),
'body' => __($message, ['event' => $event->getName()]),
'type' => 'LOG',
'case_number' => $caseNumber,
]);
$comment->save();
break;
Expand Down Expand Up @@ -279,7 +283,7 @@ public function updateDataWithFlowTransition($transition, $flow, $instance)
$instance->data = $data;
$instance->saveOrFail();
}
} catch (\Exception $e) {
} catch (Exception $e) {
Log::error('The expression used in the flow generated and error: ', [$e->getMessage()]);
$instance->logError($e, $transition->getOwner());
}
Expand Down
9 changes: 9 additions & 0 deletions ProcessMaker/Listeners/CommentsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function onActivityCompleted(ActivityCompletedEvent $event)
if ($token->is_actionbyemail) {
$message = $message . ' via email';
}
// Get the case_number
$caseNumber = ProcessRequest::where('id', $token->process_request_id)->value('case_number');

Comment::create([
'type' => 'LOG',
Expand All @@ -42,6 +44,7 @@ public function onActivityCompleted(ActivityCompletedEvent $event)
'commentable_id' => $token->process_request_id,
'subject' => 'Task Complete',
'body' => __($message, ['user' => $user_name, 'task_name' => $token->element_name]),
'case_number' => $caseNumber,
]);
}

Expand All @@ -59,6 +62,8 @@ public function onActivitySkipped(ActivityInterface $activity, ProcessRequestTok
if (!is_int($token->getInstance()->getId())) {
return;
}
// Get the case number
$caseNumber = ProcessRequest::where('id', $token->getInstance()->getId())->value('case_number');

Comment::create([
'type' => 'LOG',
Expand All @@ -67,6 +72,7 @@ public function onActivitySkipped(ActivityInterface $activity, ProcessRequestTok
'commentable_id' => $token->getInstance()->getId(),
'subject' => 'Task Skipped',
'body' => __('The task :task_name was skipped', ['task_name' => $taskName]),
'case_number' => $caseNumber,
]);
}

Expand Down Expand Up @@ -112,6 +118,8 @@ public function onGatewayPassed($gateway, $transition = null, $tokens = null)
if (!is_int($token->getInstance()->getId())) {
return;
}
// Get the case number
$caseNumber = ProcessRequest::where('id', $token->getInstance()->getId())->value('case_number');

Comment::create([
'type' => 'LOG',
Expand All @@ -120,6 +128,7 @@ public function onGatewayPassed($gateway, $transition = null, $tokens = null)
'commentable_id' => $token->getInstance()->getId(),
'subject' => 'Gateway',
'body' => $sourceLabel . ': ' . $flowLabel,
'case_number' => $caseNumber,
]);
}
}
Expand Down
12 changes: 11 additions & 1 deletion ProcessMaker/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,17 @@ class Comment extends ProcessMakerModel
use SoftDeletes;

protected $fillable = [
'user_id', 'parent_id', 'group_id', 'group_name', 'commentable_id', 'commentable_type', 'subject', 'body', 'hidden', 'type',
'user_id',
'parent_id',
'group_id',
'group_name',
'commentable_id',
'commentable_type',
'subject',
'body',
'hidden',
'type',
'case_number',
];

protected $casts = [
Expand Down
3 changes: 2 additions & 1 deletion ProcessMaker/RetryProcessRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function retry(): void
/**
* Clear previous ProcessRequestToken errors off of its parent ProcessRequest(s)
*
* @param \ProcessMaker\Models\ProcessRequestToken $token
* @param ProcessRequestToken $token
*
* @return void
*/
Expand Down Expand Up @@ -153,6 +153,7 @@ public function createRequestComment(): void
'commentable_type' => ProcessRequest::class,
'commentable_id' => $this->processRequest->id,
'subject' => __('Request Retried'),
'case_number' => isset($this->processRequest->case_number) ? $this->processRequest->case_number : null,
]);

if ($user = $this->initializingUser()) {
Expand Down
1 change: 1 addition & 0 deletions ProcessMaker/RollbackProcessRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ private function addComment() : void
'failed_task_name' => $this->currentTask->element_name,
'new_task_name' => $this->newTask->element_name,
]),
'case_number' => isset($this->currentTask->case_number) ? $this->currentTask->case_number : null,
]);
}
}
1 change: 1 addition & 0 deletions tests/Feature/Api/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CommentTest extends TestCase
'type',
'updated_at',
'created_at',
'case_number',
];

protected function withUserSetup()
Expand Down

0 comments on commit f007b85

Please sign in to comment.