Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#80 - Inviting to quizzes - Backend #95

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
86bf145
Add invite vue, controller, resource for admin panel
PrabuckiDominik Sep 6, 2024
656d053
fix code style
PrabuckiDominik Sep 6, 2024
b61b1c7
Merge branch 'main' of https://github.com/blumilksoftware/interns2024…
PrabuckiDominik Sep 6, 2024
dd9095a
Merge branch 'main' of https://github.com/blumilksoftware/interns2024…
PrabuckiDominik Sep 9, 2024
3fd3a2f
add pagination
PrabuckiDominik Sep 9, 2024
0a2ffbc
Move logic to quiz
PrabuckiDominik Sep 9, 2024
575416d
Add invitation
PrabuckiDominik Sep 9, 2024
45f3b98
Reset password now queue
PrabuckiDominik Sep 10, 2024
89c4472
Add invite user to quiz is queued
PrabuckiDominik Sep 10, 2024
b682746
Add jobs for sending mails
PrabuckiDominik Sep 10, 2024
af069f3
Update quiz policy
PrabuckiDominik Sep 10, 2024
a4af77d
Fix invite title
PrabuckiDominik Sep 10, 2024
dae1f07
Fix code style
PrabuckiDominik Sep 10, 2024
db94388
Add sort by name/surname with watch
PrabuckiDominik Sep 11, 2024
2e1b01e
Fix password notification link
PrabuckiDominik Sep 11, 2024
4b00c7b
Fix search while sorting by school
PrabuckiDominik Sep 11, 2024
c059f53
Fix searchbar
PrabuckiDominik Sep 11, 2024
b13dac5
Add limit to filter
PrabuckiDominik Sep 11, 2024
c349688
Add assignment to test while inviting user to it
PrabuckiDominik Sep 11, 2024
e8ca731
Merge branch 'main' of https://github.com/blumilksoftware/interns2024…
PrabuckiDominik Sep 11, 2024
ffaa248
Fix issue in policy, allowing publish ranking without quiz being publ…
PrabuckiDominik Sep 11, 2024
82591c8
Fix tests
PrabuckiDominik Sep 11, 2024
fe0299f
Fix code style
PrabuckiDominik Sep 11, 2024
eafae0b
Add assign button to quiz without sending mail to users
PrabuckiDominik Sep 11, 2024
e8c008e
Merge branch 'main' of https://github.com/blumilksoftware/interns2024…
PrabuckiDominik Oct 19, 2024
a619699
Merge branch 'main' of https://github.com/blumilksoftware/interns2024…
PrabuckiDominik Nov 1, 2024
d675bd9
Merge branch 'main' of https://github.com/blumilksoftware/interns2024…
PrabuckiDominik Nov 25, 2024
e49acce
Cleanup code, add unassign button and tests
PrabuckiDominik Nov 26, 2024
bc67325
Use school resource
PrabuckiDominik Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/Actions/AssignToQuizAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace App\Actions;

use App\Models\Quiz;
use App\Models\User;
use Illuminate\Support\Collection;

class AssignToQuizAction
{
/**
* @param Collection<User> $users
*/
public function execute(Quiz $quiz, Collection $users): void
{
$assignedUsers = $quiz->assignedUsers;
$users = User::query()->whereIn("id", $users)->get();

$users = $users->filter(fn(User $user) => !$assignedUsers->contains($user));
$quiz->assignedUsers()->attach($users);
}
}
23 changes: 23 additions & 0 deletions app/Actions/UnassignToQuizAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\Actions;

use App\Models\Quiz;
use App\Models\User;
use Illuminate\Support\Collection;

class UnassignToQuizAction
{
/**
* @param Collection<User> $users
*/
public function execute(Quiz $quiz, Collection $users): void
{
$assignedUsers = $quiz->assignedUsers;
$users = User::query()->whereIn("id", $users)->get();
$users = $users->filter(fn(User $user) => $assignedUsers->contains($user));
$quiz->assignedUsers()->detach($users);
}
}
70 changes: 70 additions & 0 deletions app/Helpers/SortHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace App\Helpers;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Lang;
use Symfony\Component\HttpFoundation\Response as Status;

class SortHelper
{
public function __construct(
private Request $request,
) {}

/**
* @param array<string> $allowedFields
* @param array<string> $ignoredFields
*/
public function sort(Builder $query, array $allowedFields, array $ignoredFields): Builder
{
[$field, $order] = $this->getSortParameters();

if (!in_array($field, $allowedFields, true)) {
if (in_array($field, $ignoredFields, true)) {
return $query;
}

abort(Status::HTTP_BAD_REQUEST, Lang::get("validation.custom.sorting.unsupported_field", ["attribute" => $field]));
}

return $query->orderBy($field, $order);
}

/**
* @return array<string>
*/
public function getSortParameters(): array
{
$field = $this->request->query("sort", "id");
$ascending = $this->request->query("order", "asc") === "asc";

return [$field, $ascending ? "asc" : "desc"];
}

public function search(Builder $query, string $field): Builder
{
$searchText = $this->request->query("search");

if ($searchText) {
return $query->where($field, "ilike", "%$searchText%");
}

return $query;
}

public function paginate(Builder $query): LengthAwarePaginator
{
$limit = (int)$this->request->query("limit", "50");

if (!$limit || $limit < 0) {
$limit = 50;
}

return $query->paginate($limit);
}
}
111 changes: 111 additions & 0 deletions app/Http/Controllers/InviteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Actions\AssignToQuizAction;
use App\Actions\UnassignToQuizAction;
use App\Helpers\SortHelper;
use App\Http\Requests\InviteQuizRequest;
use App\Http\Resources\QuizResource;
use App\Http\Resources\SchoolResource;
use App\Http\Resources\UserResource;
use App\Models\Quiz;
use App\Models\School;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class InviteController extends Controller
{
public function index(Quiz $quiz, SortHelper $sort, Request $request): Response
{
$this->authorize("invite", $quiz);
$query = User::query()->role("user")->with("school")->whereNotNull("email_verified_at");
$query = $sort->sort($query, ["id"], ["name", "school"]);
$query = $this->sortByName($query, $sort);
$query = $this->sortBySchool($query, $sort);
$query = $this->filterByName($query, $request);
$query = $this->filterBySchool($query, $request);
$schools = SchoolResource::collection(School::all());

return Inertia::render("Admin/Invite", [
"users" => UserResource::collection($sort->paginate($query)),
"quiz" => QuizResource::make($quiz),
"schools" => $schools,
"assigned" => $quiz->assignedUsers->pluck("id"),
]);
}

public function assign(Quiz $quiz, InviteQuizRequest $request, AssignToQuizAction $assignAction): RedirectResponse
{
$this->authorize("invite", $quiz);

$assignAction->execute($quiz, collect($request->input("ids")));

return redirect()
->back()
->with("status", "Użytkownicy zostali przypisani do quizu.");
}

public function unassign(Quiz $quiz, InviteQuizRequest $request, UnassignToQuizAction $unassignAction): RedirectResponse
{
$this->authorize("invite", $quiz);

$unassignAction->execute($quiz, collect($request->input("ids")));

return redirect()
->back()
->with("status", "Użytkownicy zostali wypisani z quizu.");
}

private function filterByName(Builder $query, Request $request): Builder
{
$searchName = $request->query("search");

if ($searchName) {
return $query->where("users.name", "ilike", "%$searchName%")
->orWhere("users.surname", "ilike", "%$searchName%");
}

return $query;
}

private function filterBySchool(Builder $query, Request $request): Builder
{
$searchSchool = $request->query("schoolId");

if ($searchSchool) {
return $query->where("school_id", $searchSchool);
}

return $query;
}

private function sortByName(Builder $query, SortHelper $sorter): Builder
{
[$field, $order] = $sorter->getSortParameters();

if ($field === "name") {
return $query->orderBy("surname", $order)
->orderBy("name", $order);
}

return $query;
}

private function sortBySchool(Builder $query, SortHelper $sorter): Builder
{
[$field, $order] = $sorter->getSortParameters();

if ($field === "schoolId") {
return $query->orderBy("school.name", $order);
}

return $query;
}
}
26 changes: 26 additions & 0 deletions app/Http/Requests/InviteQuizRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;

class InviteQuizRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}

/**
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
"ids.*" => ["required", "integer"],
];
}
}
27 changes: 27 additions & 0 deletions app/Http/Resources/InviteResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class InviteResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
"user" => [
"id" => $this->user->id,
"name" => $this->user->name,
"surname" => $this->user->surname,
"school" => $this->user->school,
],
"points" => $this->points,
];
}
}
34 changes: 34 additions & 0 deletions app/Jobs/SendInviteJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Models\Quiz;
use App\Models\User;
use App\Notifications\InviteUserNotification;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendInviteJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
use Batchable;

public function __construct(
protected User $user,
protected Quiz $quiz,
) {}

public function handle(): void
{
$this->user->notify(new InviteUserNotification($this->quiz));
}
}
31 changes: 31 additions & 0 deletions app/Jobs/SendPasswordResetJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Models\User;
use App\Notifications\ResetPasswordNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendPasswordResetJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

public function __construct(
protected User $user,
protected string $token,
) {}

public function handle(): void
{
$this->user->notify(new ResetPasswordNotification($this->token));
}
}
30 changes: 30 additions & 0 deletions app/Jobs/SendVerificationEmailJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Models\User;
use App\Notifications\SendVerificationEmail;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendVerificationEmailJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

public function __construct(
protected User $user,
) {}

public function handle(): void
{
$this->user->notify(new SendVerificationEmail());
}
}
Loading
Loading