source: app/Http/Controllers/InviteManagerController.php

Last change on this file was dfae77e, checked in by Igor Danilovski <igor_danilovski@…>, 2 years ago
  • Initial commit;
  • Property mode set to 100644
File size: 4.2 KB
Line 
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Http\Controllers\Utils\UtilController;
6use App\Models\Artist;
7use App\Models\Manager;
8use App\Models\ManagerInvite;
9use App\Models\User;
10use App\Notifications\ManagerArtistInviteNotification;
11use App\Notifications\ManagerRegisterArtistInviteNotification;
12use Carbon\Carbon;
13use Illuminate\Database\Eloquent\ModelNotFoundException;
14use Illuminate\Http\RedirectResponse;
15use Illuminate\Http\Request;
16use Illuminate\Support\Facades\Auth;
17use Exception;
18use Illuminate\Support\Facades\Hash;
19use Illuminate\Support\Facades\Validator;
20
21class InviteManagerController extends Controller
22{
23 //
24 public function __construct()
25 {
26 $this->middleware(['auth', 'verified', 'onboarding']);
27 $this->middleware(['role:artist'])->only('storeInvitation');
28 $this->middleware(['role:manager'])->only('accept');
29 }
30
31 public function storeInvitation(Request $request): RedirectResponse
32 {
33 $validator = Validator::make($request->all(), [
34 'email' => ['required', 'email', 'max:255'],
35 ]);
36
37 if ($validator->fails()) {
38 return redirect()->back()
39 ->with('error', $validator->messages()->all());
40 }
41
42 if (Artist::where('user_id', Auth::id())
43 ->whereNotNull('manager_id')
44 ->exists()) {
45 return redirect()->back()
46 ->with('error', 'You already have a manager. Cannot invite more than one!');
47 }
48
49 $invitation = new ManagerInvite();
50 $invitation->email = $request->email;
51
52 $artist = Artist::find(Auth::id());
53 $invitation->artist()->associate($artist);
54
55 $token = UtilController::generateToken($request->email);
56 $invitation->invitation_token = Hash::make($token);
57
58 try {
59 $invitation->save();
60 } catch (Exception $e) {
61 return redirect()->back()
62 ->with('error', 'Issue while inviting the manager - ' . $e->getMessage());
63 }
64
65 if (User::where('email', $request->email)->exists()) {
66 try {
67 $invitation->notify(new ManagerArtistInviteNotification($token));
68 } catch (Exception $e) {
69 return redirect()->back()
70 ->with('error', $e->getMessage());
71 }
72 } else {
73 try {
74 $invitation->notify(new ManagerRegisterArtistInviteNotification($token));
75 } catch (Exception $e) {
76 return redirect()->back()
77 ->with('error', $e->getMessage());
78 }
79 }
80
81 return redirect()->back()
82 ->with('success', 'Manager invited successfully!');
83 }
84
85 /**
86 * Accept the invitation token sent by the artist
87 * @param $token - invitation token
88 */
89 public function accept(Request $request, $token)
90 {
91 if (!$request->hasValidSignature()) {
92 abort(401);
93 }
94
95 $invitations = ManagerInvite::where([
96 ['email', $request->email],
97 ['artist_id', $request->artist_id],
98 ])->get();
99
100 foreach ($invitations as $invitation) {
101 if (Hash::check($token, $invitation->invitation_token)) {
102 $artist = Artist::where('user_id', $invitation->artist_id)->firstOrFail();
103 $user_manager = User::where('email', $invitation->email)->firstOrFail();
104 $manager = Manager::where('user_id', $user_manager->id)->firstOrFail();
105
106 $artist->manager()->associate($manager);
107
108 try {
109 $artist->save();
110 } catch (Exception $e) {
111 return redirect()->back()
112 ->with('error', $e->getMessage());
113 }
114
115 $invitation->registered_at = Carbon::now();
116
117 try {
118 $invitation->save();
119 } catch (Exception $e) {
120 return redirect()->back()
121 ->with('error', $e->getMessage());
122 }
123 }
124 }
125
126 return redirect('/explore')
127 ->with('success', 'You have been added as a manager of ' . $artist->user->name . ' 🥳');
128 }
129}
Note: See TracBrowser for help on using the repository browser.