Skip to content

Commit

Permalink
fix user statuses (#119)
Browse files Browse the repository at this point in the history
* fix user statuses

* fix

* fix

* fix user resource

* fix

* fix

* Fix

* Fix

* fix

* fix stan
  • Loading branch information
daVitekPL authored Nov 18, 2024
1 parent e87d44f commit 0544ad9
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 13 deletions.
11 changes: 11 additions & 0 deletions src/Dto/ChangeTermConsultationDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
32 changes: 32 additions & 0 deletions src/Dto/ConsultationUserResourceDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace EscolaLms\Consultations\Dto;

class ConsultationUserResourceDto extends BaseDto
{
public ?int $id = null;
public ?string $firstName = null;
public ?string $lastName = null;
public ?string $email = null;
public ?string $phone = null;
public ?bool $isActive = null;
public ?string $emailVerifiedAt = null;
public ?string $pathAvatar = null;
public ?string $gender = null;
public ?string $age = null;
public ?string $country = null;
public ?string $city = null;
public ?string $street = null;
public ?string $postcode = null;
public ?string $createdAt = null;
public ?string $updatedAt = null;
public ?int $points = null;
public ?string $notificationChannels = null;
public ?string $accessToDirectories = null;
public ?string $currentTimezone = null;
public ?string $deletedAt = null;
public ?string $deleteUserToken = null;
public ?string $avatarUrl = null;
public ?array $categories = null;
public ?string $executedStatus = null;
}
1 change: 1 addition & 0 deletions src/Http/Requests/ChangeTermConsultationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function rules(): array
'term' => ['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'],
];
}
}
2 changes: 1 addition & 1 deletion src/Http/Resources/ConsultationTermsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions src/Http/Resources/ConsultationUserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace EscolaLms\Consultations\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ConsultationUserResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $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,
];
}
}
10 changes: 8 additions & 2 deletions src/Repositories/ConsultationUserTermRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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)]),
));
}
}
Expand Down
23 changes: 17 additions & 6 deletions src/Services/ConsultationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Contracts/ConsultationServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions tests/APIs/ConsultationChangeTermTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 0544ad9

Please sign in to comment.