Skip to content
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
75 changes: 42 additions & 33 deletions ProcessMaker/Repositories/CaseParticipatedRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

class CaseParticipatedRepository
{
/**
* This property is used to store an instance of `CaseParticipated`
* when a case participated is updated.
* @var CaseParticipated|null
*/
protected ?CaseParticipated $caseParticipated;

/**
* Store a new case participated.
*
Expand All @@ -17,24 +24,21 @@ class CaseParticipatedRepository
*/
public function create(CaseStarted $case, TokenInterface $token): void
{
if ($this->checkIfCaseParticipatedExist($token->user->id, $case->case_number)) {
return;
}

try {
CaseParticipated::create([
'user_id' => $token->user->id,
'case_number' => $case->case_number,
'case_title' => $case->case_title,
'case_title_formatted' => $case->case_title_formatted,
'case_status' => $case->case_status,
'processes' => $case->processes,
'requests' => $case->requests,
'request_tokens' => [$token->getKey()],
'tasks' => [
[
'id' => $token->getKey(),
'element_id' => $token->element_id,
'name' => $token->element_name,
'process_id' => $token->process_id,
],
],
'processes' => CaseUtils::storeProcesses($token->processRequest, collect()),
'requests' => CaseUtils::storeRequests($token->processRequest, collect()),
'request_tokens' => CaseUtils::storeRequestTokens($token->getKey(), collect()),
'tasks' => CaseUtils::storeTasks($token, collect()),
'participants' => $case->participants,
'initiated_at' => $case->initiated_at,
'completed_at' => null,
Expand All @@ -54,32 +58,18 @@ public function create(CaseStarted $case, TokenInterface $token): void
public function update(CaseStarted $case, TokenInterface $token)
{
try {
$caseParticipated = CaseParticipated::where('user_id', $token->user->id)
->where('case_number', $case->case_number)
->first();

// Add the token data to the request_tokens
$requestTokens = $caseParticipated->request_tokens->push($token->getKey())
->unique()
->values();
// Add the task data to the tasks
$tasks = $caseParticipated->tasks->push([
'id' => $token->getKey(),
'element_id' => $token->element_id,
'name' => $token->element_name,
'process_id' => $token->process_id,
])
->unique('id')
->values();
if (!$this->checkIfCaseParticipatedExist($token->user->id, $case->case_number)) {
return;
}

$caseParticipated->update([
$this->caseParticipated->updateOrFail([
'case_title' => $case->case_title,
'case_title_formatted' => $case->case_title_formatted,
'case_status' => $case->case_status,
'processes' => $case->processes,
'requests' => $case->requests,
'request_tokens' => $requestTokens,
'tasks' => $tasks,
'processes' => CaseUtils::storeProcesses($token->processRequest, $this->caseParticipated->processes),
'requests' => CaseUtils::storeRequests($token->processRequest, $this->caseParticipated->requests),
'request_tokens' => CaseUtils::storeRequestTokens($token->getKey(), $this->caseParticipated->request_tokens),
'tasks' => CaseUtils::storeTasks($token, $this->caseParticipated->tasks),
'participants' => $case->participants,
]);
} catch (\Exception $e) {
Expand All @@ -103,4 +93,23 @@ public function updateStatus(int $caseNumber, array $statusData)
\Log::error($e->getMessage());
}
}

/**
* Check if a case participated exists.
* If it exists, store the instance in the property.
* The property is used to update the JSON fields of the case participated.
*
* @param int $userId
* @param int $caseNumber
*
* @return bool
*/
private function checkIfCaseParticipatedExist(int $userId, int $caseNumber): bool
{
$this->caseParticipated = CaseParticipated::where('user_id', $userId)
->where('case_number', $caseNumber)
->first();

return !is_null($this->caseParticipated);
}
}
117 changes: 71 additions & 46 deletions ProcessMaker/Repositories/CaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@

class CaseRepository implements CaseRepositoryInterface
{
const CASE_STATUS_ACTIVE = 'ACTIVE';

/**
* This property is used to store an instance of `CaseStarted`
* when a case started is updated.
* @var CaseStarted|null
*/
protected ?CaseStarted $case;

public function __construct(protected CaseParticipatedRepository $caseParticipatedRepository)
{
}
Expand All @@ -21,34 +30,27 @@ public function __construct(protected CaseParticipatedRepository $caseParticipat
*/
public function create(ExecutionInstanceInterface $instance): void
{
if (is_null($instance->case_number) || $this->checkIfCaseStartedExist($instance->case_number)) {
if (is_null($instance->case_number)) {
\Log::error('Case number is required. instance: ' . $instance->getKey());

return;
}

try {
$processes = [
[
'id' => $instance->process->id,
'name' => $instance->process->name,
],
];
if ($this->checkIfCaseStartedExist($instance->case_number)) {
$this->updateSubProcesses($instance);

$requests = [
[
'id' => $instance->id,
'name' => $instance->name,
'parent_request_id' => $instance->parentRequest->id ?? 0,
],
];
return;
}

try {
CaseStarted::create([
'case_number' => $instance->case_number,
'user_id' => $instance->user_id,
'case_title' => $instance->case_title,
'case_title_formatted' => $instance->case_title_formatted,
'case_status' => 'IN_PROGRESS',
'processes' => $processes,
'requests' => $requests,
'case_status' => $instance->status === self::CASE_STATUS_ACTIVE ? 'IN_PROGRESS' : $instance->status,
'processes' => CaseUtils::storeProcesses($instance, collect()),
'requests' => CaseUtils::storeRequests($instance, collect()),
'request_tokens' => [],
'tasks' => [],
'participants' => [],
Expand All @@ -57,6 +59,7 @@ public function create(ExecutionInstanceInterface $instance): void
]);
} catch (\Exception $e) {
\Log::error($e->getMessage());
\Log::error($e->getTraceAsString());
}
}

Expand All @@ -69,34 +72,27 @@ public function create(ExecutionInstanceInterface $instance): void
*/
public function update(ExecutionInstanceInterface $instance, TokenInterface $token): void
{
if (is_null($instance->case_number)) {
\Log::error('Case number is required. instance: ' . $instance->getKey());

return;
}

try {
$case = CaseStarted::where('case_number', $instance->case_number)->first();
if (!$this->checkIfCaseStartedExist($instance->case_number)) {
\Log::error('Case number not found. instance: ' . $instance->id);

if (is_null($case)) {
return;
}

$case->case_title = $instance->case_title;
$case->case_status = $instance->status === 'ACTIVE' ? 'IN_PROGRESS' : $instance->status;

$case->request_tokens = $case->request_tokens->push($token->getKey())
->unique()
->values();

if (!in_array($token->element_type, ['scriptTask'])) {
$case->tasks = $case->tasks->push([
'id' => $token->getKey(),
'element_id' => $token->element_id,
'name' => $token->element_name,
'process_id' => $token->process_id,
])
->unique('id')
->values();
}
$this->case->case_title = $instance->case_title;
$this->case->case_status = $instance->status === self::CASE_STATUS_ACTIVE ? 'IN_PROGRESS' : $instance->status;
$this->case->request_tokens = CaseUtils::storeRequestTokens($token->getKey(), $this->case->request_tokens);
$this->case->tasks = CaseUtils::storeTasks($token, $this->case->tasks);

$this->updateParticipants($case, $token);
$this->updateParticipants($token);

$case->saveOrFail();
$this->case->saveOrFail();
} catch (\Exception $e) {
\Log::error($e->getMessage());
}
Expand All @@ -110,6 +106,11 @@ public function update(ExecutionInstanceInterface $instance, TokenInterface $tok
*/
public function updateStatus(ExecutionInstanceInterface $instance): void
{
// If a sub-process is completed, do not update the case started status
if (!is_null($instance->parent_request_id)) {
return;
}

try {
$data = [
'case_status' => $instance->status,
Expand All @@ -130,34 +131,33 @@ public function updateStatus(ExecutionInstanceInterface $instance): void
/**
* Update the participants of the case started.
*
* @param CaseStarted $case
* @param TokenInterface $token
* @return void
*/
private function updateParticipants(CaseStarted $case, TokenInterface $token): void
private function updateParticipants(TokenInterface $token): void
{
$user = $token->user;

if (!$user) {
return;
}

$participantExists = $case->participants->contains(function ($participant) use ($user) {
$participantExists = $this->case->participants->contains(function ($participant) use ($user) {
return $participant['id'] === $user->id;
});

if (!$participantExists) {
$case->participants->push([
$this->case->participants->push([
'id' => $user->id,
'name' => $user->getFullName(),
'title' => $user->title,
'avatar' => $user->avatar,
]);

$this->caseParticipatedRepository->create($case, $token);
$this->caseParticipatedRepository->create($this->case, $token);
}

$this->caseParticipatedRepository->update($case, $token);
$this->caseParticipatedRepository->update($this->case, $token);
}

/**
Expand All @@ -168,6 +168,31 @@ private function updateParticipants(CaseStarted $case, TokenInterface $token): v
*/
private function checkIfCaseStartedExist(int $caseNumber): bool
{
return CaseStarted::where('case_number', $caseNumber)->count() > 0;
$this->case = CaseStarted::where('case_number', $caseNumber)->first();

return !is_null($this->case);
}

/**
* Update the processes and requests of the case started.
*
* @param ExecutionInstanceInterface $instance
* @return void
*/
private function updateSubProcesses(ExecutionInstanceInterface $instance): void
{
if (is_null($instance->parent_request_id)) {
return;
}

try {
// Store the sub-processes and requests
$this->case->processes = CaseUtils::storeProcesses($instance, $this->case->processes);
$this->case->requests = CaseUtils::storeRequests($instance, $this->case->requests);

$this->case->saveOrFail();
} catch (\Exception $th) {
\Log::error($th->getMessage());
}
}
}
Loading