From 4c4489f244d547182ceab7039c043d06e496a1ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Wis=CC=81niewski?= Date: Mon, 18 Nov 2024 09:45:33 +0100 Subject: [PATCH] fix user statuses --- src/Dto/ConsultationUserResourceDto.php | 88 +++++++++++++++++++ .../Resources/ConsultationTermsResource.php | 2 +- .../Resources/ConsultationUserResource.php | 39 ++++++++ .../ConsultationUserTermRepository.php | 9 +- src/Services/ConsultationService.php | 21 +++-- .../Contracts/ConsultationServiceContract.php | 2 +- 6 files changed, 152 insertions(+), 9 deletions(-) create mode 100644 src/Dto/ConsultationUserResourceDto.php create mode 100644 src/Http/Resources/ConsultationUserResource.php diff --git a/src/Dto/ConsultationUserResourceDto.php b/src/Dto/ConsultationUserResourceDto.php new file mode 100644 index 0000000..aa68b3e --- /dev/null +++ b/src/Dto/ConsultationUserResourceDto.php @@ -0,0 +1,88 @@ +id = $id; + $this->first_name = $first_name; + $this->last_name = $last_name; + $this->email = $email; + $this->phone = $phone; + $this->is_active = $is_active; + $this->email_verified_at = $email_verified_at; + $this->path_avatar = $path_avatar; + $this->gender = $gender; + $this->age = $age; + $this->country = $country; + $this->city = $city; + $this->street = $street; + $this->postcode = $postcode; + $this->created_at = $created_at; + $this->updated_at = $updated_at; + $this->points = $points; + $this->notification_channels = $notification_channels; + $this->access_to_directories = $access_to_directories; + $this->current_timezone = $current_timezone; + $this->deleted_at = $deleted_at; + $this->delete_user_token = $delete_user_token; + $this->avatar_url = $avatar_url; + $this->categories = $categories; + $this->executed_status = $executed_status; + } +} diff --git a/src/Http/Resources/ConsultationTermsResource.php b/src/Http/Resources/ConsultationTermsResource.php index 0443062..d69c3e6 100644 --- a/src/Http/Resources/ConsultationTermsResource.php +++ b/src/Http/Resources/ConsultationTermsResource.php @@ -89,7 +89,7 @@ public function toArray($request) 'date' => Carbon::make($this->resource->executed_at) ?? '', 'status' => $this->resource->executed_status ?? '', 'duration' => $this->resource->duration, - 'users' => ConsultationAuthorResource::collection($this->resource->users), + 'users' => ConsultationUserResource::collection($this->resource->users), 'is_started' => $consultationServiceContract->isStarted( $this->resource->executed_at, $this->resource->executed_status, diff --git a/src/Http/Resources/ConsultationUserResource.php b/src/Http/Resources/ConsultationUserResource.php new file mode 100644 index 0000000..cc897c0 --- /dev/null +++ b/src/Http/Resources/ConsultationUserResource.php @@ -0,0 +1,39 @@ + $this->resource->id, + 'first_name' => $this->resource->first_name, + 'last_name' => $this->resource->last_name, + 'email' => $this->resource->email, + 'phone' => $this->resource->phone, + 'is_active' => $this->resource->is_active, + 'email_verified_at' => $this->resource->email_verified_at, + 'path_avatar' => $this->resource->path_avatar, + 'gender' => $this->resource->gender, + 'age' => $this->resource->age, + 'country' => $this->resource->country, + 'city' => $this->resource->city, + 'street' => $this->resource->street, + 'postcode' => $this->resource->postcode, + 'created_at' => $this->resource->created_at, + 'updated_at' => $this->resource->updated_at, + 'points' => $this->resource->points, + 'notification_channels' => $this->resource->notification_channels, + 'access_to_directories' => $this->resource->access_to_directories, + 'current_timezone' => $this->resource->current_timezone, + 'deleted_at' => $this->resource->deleted_at, + 'delete_user_token' => $this->resource->delete_user_token, + 'avatar_url' => $this->resource->avatar_url, + 'categories' => $this->resource->categories, + 'executed_status' => $this->resource->executed_status, + ]; + } +} diff --git a/src/Repositories/ConsultationUserTermRepository.php b/src/Repositories/ConsultationUserTermRepository.php index a2f918f..42a0b98 100644 --- a/src/Repositories/ConsultationUserTermRepository.php +++ b/src/Repositories/ConsultationUserTermRepository.php @@ -2,6 +2,7 @@ namespace EscolaLms\Consultations\Repositories; +use EscolaLms\Consultations\Dto\ConsultationUserResourceDto; use EscolaLms\Consultations\Dto\ConsultationUserTermResourceDto; use EscolaLms\Consultations\Dto\FilterConsultationTermsListDto; use EscolaLms\Consultations\Enum\ConsultationTermStatusEnum; @@ -130,8 +131,12 @@ public function getByCurrentUserTutor(): Collection // @phpstan-ignore-next-line $userTerm = $result->first(fn (ConsultationUserTermResourceDto $dto) => $dto->consultation_id === $term->consultationUser->consultation_id && $term->executed_at === $dto->executed_at); + $user = $term->consultationUser->user->toArray(); + $user['categories'] = $term->consultationUser->user->categories->toArray(); + $user['executed_status'] = $term->executed_status; if ($userTerm) { - $userTerm->users->push($term->consultationUser->user); + $userTerm->executed_status = $term->executed_status === ConsultationTermStatusEnum::APPROVED ? $term->executed_status : $userTerm->executed_status; + $userTerm->users->push(new ConsultationUserResourceDto(...$user)); } else { $result->push(new ConsultationUserTermResourceDto( $term->consultation_user_id, @@ -141,7 +146,7 @@ public function getByCurrentUserTutor(): Collection $term->consultationUser->consultation->getDuration(), $term->consultationUser->consultation->author, $term->finished_at, - collect([$term->consultationUser->user]), + collect([new ConsultationUserResourceDto(...$user)]), )); } } diff --git a/src/Services/ConsultationService.php b/src/Services/ConsultationService.php index 24c3be0..71bf985 100644 --- a/src/Services/ConsultationService.php +++ b/src/Services/ConsultationService.php @@ -223,7 +223,8 @@ public function generateJitsi(int $consultationTermId, ConsultationUserTermDto $ if (!$this->canGenerateJitsi( $term->executed_at, $term->executed_status, - $consultationTerm->consultation->getDuration() + $consultationTerm->consultation->getDuration(), + $consultationTerm->consultation, )) { throw new NotFoundHttpException(__('Consultation term is not available')); } @@ -257,14 +258,24 @@ public function generateJitsi(int $consultationTermId, ConsultationUserTermDto $ ); } - public function canGenerateJitsi(?string $executedAt, ?string $status, ?string $duration): bool + public function canGenerateJitsi(?string $executedAt, ?string $status, ?string $duration, ?Consultation $consultation): bool { $now = now(); if (isset($executedAt)) { $dateTo = Carbon::make($executedAt); - return in_array($status, [ConsultationTermStatusEnum::APPROVED]) && - $now->getTimestamp() >= $dateTo->getTimestamp() && - !$this->isEnded($executedAt, $duration); + if ($now->getTimestamp() >= $dateTo->getTimestamp() && !$this->isEnded($executedAt, $duration)) { + if ($consultation && Auth::user()->getKey() === $consultation->author_id || in_array(Auth::user()->getKey(), $consultation->teachers()->pluck('users.id')->toArray())) { + $terms = $this->consultationUserTermRepository->getAllUserTermsByConsultationIdAndExecutedAt($consultation->getKey(), $executedAt); + + foreach ($terms as $term) { + if ($term->status === ConsultationTermStatusEnum::APPROVED) { + return true; + } + } + } else { + return $status === ConsultationTermStatusEnum::APPROVED; + } + } } return false; } diff --git a/src/Services/Contracts/ConsultationServiceContract.php b/src/Services/Contracts/ConsultationServiceContract.php index c103da8..5babf16 100644 --- a/src/Services/Contracts/ConsultationServiceContract.php +++ b/src/Services/Contracts/ConsultationServiceContract.php @@ -31,7 +31,7 @@ public function approveTerm(int $consultationTermId, ConsultationUserTermDto $dt public function rejectTerm(int $consultationTermId, ConsultationUserTermDto $dto): bool; public function setStatus(ConsultationUserPivot $consultationTerm, string $status, string $executedAt): ConsultationUserTerm; public function generateJitsi(int $consultationTermId, ConsultationUserTermDto $dto): array; - public function canGenerateJitsi(?string $executedAt, ?string $status, ?string $duration): bool; + public function canGenerateJitsi(?string $executedAt, ?string $status, ?string $duration, ?Consultation $consultation): bool; public function generateJitsiUrlForEmail(int $consultationTermId, int $userId, string $executedAt): ?string; public function proposedTerms(int $consultationTermId): ?array; public function setRelations(Consultation $consultation, array $relations = []): void;