added support for inviting list collaborators
This commit is contained in:
parent
cd2c8adaa8
commit
0787b75780
21 changed files with 393 additions and 34 deletions
|
|
@ -2,11 +2,28 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\CreateInvitationRequest;
|
||||
use App\Mail\ListCollaboratorInvite;
|
||||
use App\Models\Invitation;
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
class InvitationController extends Controller
|
||||
{
|
||||
private Invitation $invitation;
|
||||
|
||||
public function __construct(Invitation $invitation)
|
||||
{
|
||||
$this->invitation = $invitation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
|
|
@ -16,19 +33,32 @@ class InvitationController extends Controller
|
|||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
public function accept(Request $request, string $token)
|
||||
{
|
||||
//
|
||||
}
|
||||
try {
|
||||
$invitation = $this->invitation::where('token', $token)->firstOrFail();
|
||||
} catch (ModelNotFoundException $e) {
|
||||
return response()->json(['message' => 'Invitation not found', 'status' => 'not_found'], 404);
|
||||
} catch (Throwable $e) {
|
||||
Log::error('Failed to accept invitation: '.$e->getMessage());
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Invitation $invitation)
|
||||
{
|
||||
//
|
||||
return response()->json(['message' => 'Failed to accept invitation', 'status' => 'failed'], 500);
|
||||
}
|
||||
|
||||
$user = Auth::user();
|
||||
if ($user) {
|
||||
$user->sharedLists()->attach($invitation->movie_list_id, ['role' => 'viewer']);
|
||||
$invitation->update(['status' => 'accepted']);
|
||||
$invitation->delete();
|
||||
} else {
|
||||
$invitation->update(['status' => 'accepted_login_pending']);
|
||||
|
||||
return response()->json(['message' => 'Unauthorized', 'status' => 'pending'], 401);
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'Invitation accepted', 'status' => 'accepted']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -39,6 +69,54 @@ class InvitationController extends Controller
|
|||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function decline()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(CreateInvitationRequest $request)
|
||||
{
|
||||
$validated = $request->validated();
|
||||
$invitations = [];
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($validated, &$invitations) {
|
||||
foreach ($validated['emails'] as $email) {
|
||||
$invitations[] = Invitation::create([
|
||||
'email' => $email,
|
||||
'movie_list_id' => $validated['movie_list_id'],
|
||||
'token' => Str::uuid(),
|
||||
'expires_at' => now()->addDays(Invitation::EXPIRATION_DAYS),
|
||||
]);
|
||||
}
|
||||
});
|
||||
} catch (Exception $e) {
|
||||
logger()->error('Failed to create invitation: '.$e->getMessage());
|
||||
|
||||
return response()->json(['message' => 'Failed to create invitations.'], 500);
|
||||
}
|
||||
|
||||
foreach ($invitations as $invitation) {
|
||||
Mail::to($invitation->email)->queue(new ListCollaboratorInvite(Auth::user(), $invitation));
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'Invitation created successfully'], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Invitation $invitation)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue