Skip to content

Commit

Permalink
fix user statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
daVitekPL committed Nov 18, 2024
1 parent e87d44f commit 4c4489f
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 9 deletions.
88 changes: 88 additions & 0 deletions src/Dto/ConsultationUserResourceDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace EscolaLms\Consultations\Dto;

class ConsultationUserResourceDto
{
public ?int $id;
public ?string $first_name;
public ?string $last_name;
public ?string $email;
public ?string $phone;
public ?bool $is_active;
public ?string $email_verified_at;
public ?string $path_avatar;
public ?string $gender;
public ?string $age;
public ?string $country;
public ?string $city;
public ?string $street;
public ?string $postcode;
public ?string $created_at;
public ?string $updated_at;
public ?int $points;
public ?string $notification_channels;
public ?string $access_to_directories;
public ?string $current_timezone;
public ?string $deleted_at;
public ?string $delete_user_token;
public ?string $avatar_url;
public ?array $categories;
public ?string $executed_status;

/**
* @param int|null $id
* @param string|null $first_name
* @param string|null $last_name
* @param string|null $email
* @param string|null $phone
* @param bool|null $is_active
* @param string|null $email_verified_at
* @param string|null $path_avatar
* @param string|null $gender
* @param string|null $age
* @param string|null $country
* @param string|null $city
* @param string|null $street
* @param string|null $postcode
* @param string|null $created_at
* @param string|null $updated_at
* @param int|null $points
* @param string|null $notification_channels
* @param string|null $access_to_directories
* @param string|null $current_timezone
* @param string|null $deleted_at
* @param string|null $delete_user_token
* @param string|null $avatar_url
* @param array|null $categories
* @param string|null $executed_status
*/
public function __construct(?int $id, ?string $first_name, ?string $last_name, ?string $email, ?string $phone, ?bool $is_active, ?string $email_verified_at, ?string $path_avatar, ?string $gender, ?string $age, ?string $country, ?string $city, ?string $street, ?string $postcode, ?string $created_at, ?string $updated_at, ?int $points, ?string $notification_channels, ?string $access_to_directories, ?string $current_timezone, ?string $deleted_at, ?string $delete_user_token, ?string $avatar_url, ?array $categories, ?string $executed_status)
{
$this->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;
}
}
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->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,
];
}
}
9 changes: 7 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,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;

Check failure on line 138 in src/Repositories/ConsultationUserTermRepository.php

View workflow job for this annotation

GitHub Actions / php82-laravel-latest-phpstan-postgres

Strict comparison using === between EscolaLms\Consultations\Enum\ConsultationTermStatusEnum and 'approved' will always evaluate to false.
$userTerm->users->push(new ConsultationUserResourceDto(...$user));
} else {
$result->push(new ConsultationUserTermResourceDto(
$term->consultation_user_id,
Expand All @@ -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)]),
));
}
}
Expand Down
21 changes: 16 additions & 5 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): 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;
}
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

0 comments on commit 4c4489f

Please sign in to comment.