Skip to content

Commit

Permalink
Added user_id (#118)
Browse files Browse the repository at this point in the history
* Added user_id

* fix
  • Loading branch information
daVitekPL authored Nov 15, 2024
1 parent 5b08230 commit e87d44f
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 28 deletions.
10 changes: 5 additions & 5 deletions src/Dto/ChangeTermConsultationDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ChangeTermConsultationDto extends BaseDto
{
protected string $executedAt;
protected string $term;
protected bool $forAllUsers = false;
protected ?int $userId = null;

protected function setExecutedAt(string $executedAt): void
{
Expand All @@ -28,13 +28,13 @@ public function getTerm(): string
return $this->term;
}

protected function setForAllUsers(bool $forAllUsers): void
protected function setUserId(int $userId): void
{
$this->forAllUsers = $forAllUsers;
$this->userId = $userId;
}

public function getForAllUsers(): bool
public function getUserId(): ?int
{
return $this->forAllUsers;
return $this->userId;
}
}
10 changes: 5 additions & 5 deletions src/Dto/ConsultationUserTermDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class ConsultationUserTermDto extends BaseDto
{
protected string $term;
protected bool $forAllUsers = false;
protected ?int $userId = null;

protected function setTerm(string $term): void
{
Expand All @@ -17,13 +17,13 @@ public function getTerm(): string
return $this->term;
}

protected function setForAllUsers(bool $forAllUsers): void
protected function setUserId(int $userId): void
{
$this->forAllUsers = $forAllUsers;
$this->userId = $userId;
}

public function getForAllUsers(): bool
public function getUserId(): ?int
{
return $this->forAllUsers;
return $this->userId;
}
}
8 changes: 4 additions & 4 deletions src/Http/Controllers/Swagger/ConsultationAPISwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ public function reportTerm(int $orderItemId, ReportTermConsultationRequest $requ
* )
* ),
* @OA\Parameter(
* name="for_all_users",
* name="user_id",
* required=false,
* in="query",
* @OA\Schema(
* type="boolean"
* type="int"
* )
* ),
* @OA\Response(
Expand Down Expand Up @@ -166,11 +166,11 @@ public function approveTerm(ConsultationUserTermRequest $request, int $consultat
* )
* ),
* @OA\Parameter(
* name="for_all_users",
* name="user_id",
* required=false,
* in="query",
* @OA\Schema(
* type="boolean"
* type="int"
* )
* ),
* @OA\Response(
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/Swagger/ConsultationSwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ public function schedule(int $id, ScheduleConsultationRequest $scheduleConsultat
* example="2024-10-31 10:45",
* ),
* @OA\Property(
* property="for_all_users",
* type="boolean",
* property="user_id",
* type="int",
* ),
* )
* ),
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Requests/ChangeTermConsultationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function rules(): array
return [
'term' => ['required', 'date', new UserTermExist(request('consultationTermId') ? (int) request('consultationTermId') : null)],
'executed_at' => ['required', 'date', 'after_or_equal:now'],
'for_all_users' => ['boolean'],
'user_id' => ['nullable', 'exists:users,id'],
];
}
}
2 changes: 1 addition & 1 deletion src/Http/Requests/ConsultationUserTermRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function rules(): array
return [
'term' => ['required', 'date', new UserTermExist(request('consultationTermId'))],
'finished_at' => ['nullable', 'date'],
'for_all_users' => ['nullable', 'boolean'],
'user_id' => ['nullable', 'exists:users,id'],
];
}
}
4 changes: 2 additions & 2 deletions src/Repositories/ConsultationUserTermRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ public function getAllUserTermsByConsultationIdAndExecutedAt(int $consultationId
->get();
}

public function getUserTermByConsultationUserIdAndExecutedAt(int $consultationUserId, string $executedAt): ConsultationUserTerm
public function getUserTermByUserIdAndExecutedAt(int $userId, string $executedAt): ConsultationUserTerm
{
/** @var ConsultationUserTerm $model */
$model = $this->model->newQuery()
->where('consultation_user_id', '=', $consultationUserId)
->whereHas('consultationUser', fn (Builder $query) => $query->where('user_id', '=', $userId))
->where('executed_at', '=', $executedAt)
->firstOrFail();
return $model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function allQueryBuilder(?FilterConsultationTermsListDto $filterConsultat
*/
public function getBusyTerms(int $consultationId, ?string $date = null): Collection;
public function getAllUserTermsByConsultationIdAndExecutedAt(int $consultationId, string $executedAt): Collection;
public function getUserTermByConsultationUserIdAndExecutedAt(int $consultationUserId, string $executedAt): ConsultationUserTerm;
public function getUserTermByUserIdAndExecutedAt(int $userId, string $executedAt): ConsultationUserTerm;
public function updateModels(Collection $models, array $data): void;
public function getByCurrentUserTutor(): Collection;
public function updateModel(ConsultationUserTerm $userTerm, array $data): ConsultationUserTerm;
Expand Down
12 changes: 6 additions & 6 deletions src/Services/ConsultationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public function approveTerm(int $consultationTermId, ConsultationUserTermDto $dt
/** @var User $authUser */
$authUser = auth()->user();

$userTerms = $dto->getForAllUsers() ? $this->consultationUserTermRepository->getAllUserTermsByConsultationIdAndExecutedAt($consultationTerm->consultation_id, $dto->getTerm())
: collect([$this->consultationUserTermRepository->getUserTermByConsultationUserIdAndExecutedAt($consultationTermId, $dto->getTerm())]);
$userTerms = $dto->getUserId() ? collect([$this->consultationUserTermRepository->getUserTermByUserIdAndExecutedAt($dto->getUserId(), $dto->getTerm())])
: $this->consultationUserTermRepository->getAllUserTermsByConsultationIdAndExecutedAt($consultationTerm->consultation_id, $dto->getTerm());

DB::transaction(function () use ($userTerms, $authUser) {
/** @var ConsultationUserTerm $userTerm */
Expand All @@ -191,8 +191,8 @@ public function rejectTerm(int $consultationTermId, ConsultationUserTermDto $dto
/** @var User $authUser */
$authUser = auth()->user();

$userTerms = $dto->getForAllUsers() ? $this->consultationUserTermRepository->getAllUserTermsByConsultationIdAndExecutedAt($consultationTerm->consultation_id, $dto->getTerm())
: collect([$this->consultationUserTermRepository->getUserTermByConsultationUserIdAndExecutedAt($consultationTermId, $dto->getTerm())]);
$userTerms = $dto->getUserId() ? collect([$this->consultationUserTermRepository->getUserTermByUserIdAndExecutedAt($dto->getUserId(), $dto->getTerm())])
: $this->consultationUserTermRepository->getAllUserTermsByConsultationIdAndExecutedAt($consultationTerm->consultation_id, $dto->getTerm());

DB::transaction(function () use ($userTerms, $authUser) {
/** @var ConsultationUserTerm $userTerm */
Expand Down Expand Up @@ -468,8 +468,8 @@ public function changeTerm(int $consultationTermId, ChangeTermConsultationDto $d
/** @var ConsultationUserPivot $consultationTerm */
$consultationTerm = $this->consultationUserRepositoryContract->find($consultationTermId);

$userTerms = $dto->getForAllUsers() ? $this->consultationUserTermRepository->getAllUserTermsByConsultationIdAndExecutedAt($consultationTerm->consultation_id, $dto->getTerm())
: collect([$this->consultationUserTermRepository->getUserTermByConsultationUserIdAndExecutedAt($consultationTermId, $dto->getTerm())]);
$userTerms = $dto->getUserId() ? collect([$this->consultationUserTermRepository->getUserTermByUserIdAndExecutedAt($dto->getUserId(), $dto->getTerm())])
: $this->consultationUserTermRepository->getAllUserTermsByConsultationIdAndExecutedAt($consultationTerm->consultation_id, $dto->getTerm());

/** @var ConsultationUserTerm $userTerm */
foreach ($userTerms as $userTerm) {
Expand Down
44 changes: 44 additions & 0 deletions tests/APIs/ConsultationChangeTermTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,50 @@ public function testChangeTermWithAdmin()
Event::assertDispatched(ChangeTerm::class);
}

public function testChangeTermForOneUser()
{
Event::fake();
/** @var ConsultationUserPivot $term */
$term = $this->consultation->terms()->first();

$userTerm = $term->userTerms()->create([
'executed_at' => now()->format('Y-m-d H:i:s'),
'executed_status' => ConsultationTermStatusEnum::REPORTED,
]);

$user = User::factory()->create();
$user->guard_name = 'api';
$user->assignRole('student');

$this->consultation->attachToConsultationPivot([
'consultation_id' => $this->consultation->getKey(),
'user_id' => $user->getKey(),
'executed_status' => ConsultationTermStatusEnum::NOT_REPORTED,
]);

/** @var ConsultationUserPivot $term */
$changedTerm = $this->consultation->terms()->where('user_id', '=', $user->getKey())->first();

$userChangedTerm = $changedTerm->userTerms()->create([
'executed_at' => now()->format('Y-m-d H:i:s'),
'executed_status' => ConsultationTermStatusEnum::REPORTED,
]);

$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()]
);
$this->response->assertOk();
$userTerm->refresh();
$userChangedTerm->refresh();
$this->assertTrue($userChangedTerm->executed_at === $newTerm);
$this->assertTrue($userChangedTerm->executed_status === ConsultationTermStatusEnum::APPROVED);
$this->assertTrue($userTerm->executed_at !== $newTerm);
$this->assertTrue($userTerm->executed_status === ConsultationTermStatusEnum::REPORTED);
Event::assertDispatched(ChangeTerm::class);
}

public function testChangeTermWithUser()
{
Event::fake();
Expand Down
93 changes: 92 additions & 1 deletion tests/APIs/ConsultationReportTermTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public function testConsultationMultipleTermApproved(): void
$this->response = $this->actingAs($this->user, 'api')->json(
'GET',
'/api/consultations/approve-term/' . $this->consultationUserPivot->getKey(),
['term' => $now->format('Y-m-d H:i:s'), 'for_all_users' => true]
['term' => $now->format('Y-m-d H:i:s')]
)->assertOk();

$this->consultationUserPivot->refresh();
Expand Down Expand Up @@ -296,6 +296,97 @@ public function testConsultationMultipleTermApproved(): void
$this->assertTrue($userTermStudent2->executed_status === ConsultationTermStatusEnum::APPROVED);
}

public function testConsultationMultipleTermOneApproved(): void
{
Event::fake([
ApprovedTerm::class,
ApprovedTermWithTrainer::class,
]);
$this->initVariable();
$now = now()->modify('+1 day');

$this->consultation->update([
'max_session_students' => 4,
]);

$student1 = User::factory()->create();
$student1->assignRole('student');

$student2 = User::factory()->create();
$student2->assignRole('student');

$student3 = User::factory()->create();
$student3->assignRole('student');

/** @var ConsultationUserPivot $consultationStudent1 */
$consultationStudent1 = ConsultationUserPivot::factory([
'consultation_id' => $this->consultation->getKey(),
'user_id' => $student1->getKey()
])->create();

$userTermStudent1 = $consultationStudent1->userTerms()->create([
'executed_at' => $now->format('Y-m-d H:i:s'),
'executed_status' => ConsultationTermStatusEnum::REPORTED,
]);

$consultationStudent2 = ConsultationUserPivot::factory([
'consultation_id' => $this->consultation->getKey(),
'user_id' => $student2->getKey()
])->create();

$userTermStudent2 = $consultationStudent2->userTerms()->create([
'executed_at' => $now->format('Y-m-d H:i:s'),
'executed_status' => ConsultationTermStatusEnum::REPORTED,
]);

$this->consultationUserTerm = $this->consultationUserPivot->userTerms()->create([
'executed_at' => $now->format('Y-m-d H:i:s'),
'executed_status' => ConsultationTermStatusEnum::REPORTED,
]);

$this->response = $this->actingAs($this->user, 'api')->json(
'GET',
'/api/consultations/approve-term/' . $this->consultationUserPivot->getKey(),
['term' => $now->format('Y-m-d H:i:s'), 'user_id' => $student1->getKey()]
)->assertOk();

$this->consultationUserPivot->refresh();
$this->consultationUserTerm->refresh();
Event::assertNotDispatched(ApprovedTerm::class, fn (ApprovedTerm $event) =>
$event->getUser()->getKey() === $this->user->getKey() &&
$event->getConsultationTerm()->getKey() === $this->consultationUserPivot->getKey()
);
Event::assertNotDispatched(ApprovedTermWithTrainer::class, fn (ApprovedTermWithTrainer $event) =>
$event->getUser()->getKey() === $this->user->getKey() &&
$event->getConsultationTerm()->getKey() === $this->consultationUserPivot->getKey()
);
$this->assertTrue($this->consultationUserTerm->executed_status === ConsultationTermStatusEnum::REPORTED);

$consultationStudent1->refresh();
$userTermStudent1->refresh();
Event::assertDispatched(ApprovedTerm::class, fn (ApprovedTerm $event) =>
$event->getUser()->getKey() === $student1->getKey() &&
$event->getConsultationTerm()->getKey() === $consultationStudent1->getKey()
);
Event::assertDispatched(ApprovedTermWithTrainer::class, fn (ApprovedTermWithTrainer $event) =>
$event->getUser()->getKey() === $this->user->getKey() &&
$event->getConsultationTerm()->getKey() === $consultationStudent1->getKey()
);
$this->assertTrue($userTermStudent1->executed_status === ConsultationTermStatusEnum::APPROVED);

$consultationStudent2->refresh();
$userTermStudent2->refresh();
Event::assertNotDispatched(ApprovedTerm::class, fn (ApprovedTerm $event) =>
$event->getUser()->getKey() === $student2->getKey() &&
$event->getConsultationTerm()->getKey() === $consultationStudent2->getKey()
);
Event::assertNotDispatched(ApprovedTermWithTrainer::class, fn (ApprovedTermWithTrainer $event) =>
$event->getUser()->getKey() === $this->user->getKey() &&
$event->getConsultationTerm()->getKey() === $consultationStudent2->getKey()
);
$this->assertTrue($userTermStudent2->executed_status === ConsultationTermStatusEnum::REPORTED);
}

public function testConsultationTermApprovedUnauthorized(): void
{
$this->response = $this->json(
Expand Down

0 comments on commit e87d44f

Please sign in to comment.