From 0544ad9f3dae8d18c5e220176bda55954778e47b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Wi=C5=9Bniewski?= <58150098+daVitekPL@users.noreply.github.com> Date: Mon, 18 Nov 2024 12:42:58 +0100 Subject: [PATCH] fix user statuses (#119) * fix user statuses * fix * fix * fix user resource * fix * fix * Fix * Fix * fix * fix stan --- src/Dto/ChangeTermConsultationDto.php | 11 ++++++ src/Dto/ConsultationUserResourceDto.php | 32 +++++++++++++++ .../ChangeTermConsultationRequest.php | 1 + .../Resources/ConsultationTermsResource.php | 2 +- .../Resources/ConsultationUserResource.php | 39 +++++++++++++++++++ .../ConsultationUserTermRepository.php | 10 ++++- src/Services/ConsultationService.php | 23 ++++++++--- .../Contracts/ConsultationServiceContract.php | 2 +- tests/APIs/ConsultationChangeTermTest.php | 6 +-- 9 files changed, 113 insertions(+), 13 deletions(-) create mode 100644 src/Dto/ConsultationUserResourceDto.php create mode 100644 src/Http/Resources/ConsultationUserResource.php diff --git a/src/Dto/ChangeTermConsultationDto.php b/src/Dto/ChangeTermConsultationDto.php index ca6d577..14b3ff4 100644 --- a/src/Dto/ChangeTermConsultationDto.php +++ b/src/Dto/ChangeTermConsultationDto.php @@ -7,6 +7,7 @@ class ChangeTermConsultationDto extends BaseDto protected string $executedAt; protected string $term; protected ?int $userId = null; + protected ?bool $accept = null; protected function setExecutedAt(string $executedAt): void { @@ -37,4 +38,14 @@ public function getUserId(): ?int { return $this->userId; } + + protected function setAccept(bool $accept): void + { + $this->accept = $accept; + } + + public function getAccept(): ?bool + { + return $this->accept; + } } diff --git a/src/Dto/ConsultationUserResourceDto.php b/src/Dto/ConsultationUserResourceDto.php new file mode 100644 index 0000000..8ec9f80 --- /dev/null +++ b/src/Dto/ConsultationUserResourceDto.php @@ -0,0 +1,32 @@ + ['required', 'date', new UserTermExist(request('consultationTermId') ? (int) request('consultationTermId') : null)], 'executed_at' => ['required', 'date', 'after_or_equal:now'], 'user_id' => ['nullable', 'exists:users,id'], + 'accept' => ['boolean'], ]; } } 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..7fbb486 --- /dev/null +++ b/src/Http/Resources/ConsultationUserResource.php @@ -0,0 +1,39 @@ + $this->resource->id, + 'first_name' => $this->resource->firstName, + 'last_name' => $this->resource->lastName, + 'email' => $this->resource->email, + 'phone' => $this->resource->phone, + 'is_active' => $this->resource->isActive, + 'email_verified_at' => $this->resource->emailVerifiedAt, + 'path_avatar' => $this->resource->pathAvatar, + '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->createdAt, + 'updated_at' => $this->resource->updatedAt, + 'points' => $this->resource->points, + 'notification_channels' => $this->resource->notificationChannels, + 'access_to_directories' => $this->resource->accessToDirectories, + 'current_timezone' => $this->resource->currentTimezone, + 'deleted_at' => $this->resource->deletedAt, + 'delete_user_token' => $this->resource->deleteUserToken, + 'avatar_url' => $this->resource->avatarUrl, + 'categories' => $this->resource->categories, + 'executed_status' => $this->resource->executedStatus, + ]; + } +} diff --git a/src/Repositories/ConsultationUserTermRepository.php b/src/Repositories/ConsultationUserTermRepository.php index a2f918f..e136d86 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,13 @@ 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); + // @phpstan-ignore-next-line + $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 +147,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..fa3282d 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 = null): 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->executed_status === ConsultationTermStatusEnum::APPROVED) { + return true; + } + } + } else { + return $status === ConsultationTermStatusEnum::APPROVED; + } + } } return false; } @@ -476,7 +487,7 @@ public function changeTerm(int $consultationTermId, ChangeTermConsultationDto $d /** @var ConsultationUserTerm $consultationUserTerm */ $consultationUserTerm = $this->consultationUserTermRepository->update([ 'executed_at' => $dto->getExecutedAt(), - 'executed_status' => ConsultationTermStatusEnum::APPROVED, + 'executed_status' => $dto->getAccept() ? ConsultationTermStatusEnum::APPROVED : ConsultationTermStatusEnum::REPORTED, ], $userTerm->getKey()); if (!$consultationUserTerm->consultationUser->user) { 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; diff --git a/tests/APIs/ConsultationChangeTermTest.php b/tests/APIs/ConsultationChangeTermTest.php index bdba3af..449b90b 100644 --- a/tests/APIs/ConsultationChangeTermTest.php +++ b/tests/APIs/ConsultationChangeTermTest.php @@ -48,7 +48,7 @@ public function testChangeTermWithAdmin() $newTerm = now()->modify('+2 hours')->format('Y-m-d H:i:s'); $this->response = $this->actingAs($this->user, 'api')->post( '/api/admin/consultations/change-term/' . $term->getKey(), - ['executed_at' => $newTerm, 'term' => $userTerm->executed_at] + ['executed_at' => $newTerm, 'term' => $userTerm->executed_at, 'accept' => true] ); $this->response->assertOk(); $userTerm->refresh(); @@ -89,7 +89,7 @@ public function testChangeTermForOneUser() $newTerm = now()->modify('+2 hours')->format('Y-m-d H:i:s'); $this->response = $this->actingAs($this->user, 'api')->post( '/api/admin/consultations/change-term/' . $term->getKey(), - ['executed_at' => $newTerm, 'term' => $userChangedTerm->executed_at, 'user_id' => $user->getKey()] + ['executed_at' => $newTerm, 'term' => $userChangedTerm->executed_at, 'user_id' => $user->getKey(), 'accept' => true] ); $this->response->assertOk(); $userTerm->refresh(); @@ -120,7 +120,7 @@ public function testChangeTermWithUser() $this->response->assertOk(); $userTerm->refresh(); $this->assertTrue($userTerm->executed_at === $newTerm); - $this->assertTrue($userTerm->executed_status === ConsultationTermStatusEnum::APPROVED); + $this->assertTrue($userTerm->executed_status === ConsultationTermStatusEnum::REPORTED); Event::assertDispatched(ChangeTerm::class); }