From 0a819fbadff58bc070b1f7ceb9dee0cd12615a38 Mon Sep 17 00:00:00 2001 From: Romulus21 Date: Thu, 20 Aug 2020 14:47:44 +0200 Subject: [PATCH] finish event with log user participation --- app/Http/Controllers/EventController.php | 20 ++++++ app/Http/Resources/User.php | 3 + app/Policies/EventPolicy.php | 22 +++++- routes/api.php | 2 + tests/Feature/EventsTest.php | 92 ++++++++++++++++++++++-- 5 files changed, 134 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/EventController.php b/app/Http/Controllers/EventController.php index a4a2088..3bcd95d 100644 --- a/app/Http/Controllers/EventController.php +++ b/app/Http/Controllers/EventController.php @@ -130,4 +130,24 @@ class EventController extends Controller ->response() ->setStatusCode(200); } + + public function userConfirmParticipation(Event $event) + { + $this->authorize('participation', $event); + + $event->guests()->updateExistingPivot(auth()->user(), ['validated_at' => now()->toDateTimeString()], false); + + return (new EventResource($event)) + ->response() + ->setStatusCode(200); + } + + public function userDeleteInvitation(Event $event) + { + $this->authorize('participation', $event); + + $event->guests()->detach(auth()->user()); + + return response([], 204); + } } diff --git a/app/Http/Resources/User.php b/app/Http/Resources/User.php index fd013dc..6821122 100644 --- a/app/Http/Resources/User.php +++ b/app/Http/Resources/User.php @@ -30,6 +30,9 @@ class User extends JsonResource 'is_staff' => $this->whenPivotLoaded('event_guest', function () { return (int) $this->pivot->is_staff; }), + 'validated_at' => $this->whenPivotLoaded('event_guest', function () { + return (int) $this->pivot->validated_at; + }), ], ], 'links' => [ diff --git a/app/Policies/EventPolicy.php b/app/Policies/EventPolicy.php index af7a01f..9f0d8a2 100644 --- a/app/Policies/EventPolicy.php +++ b/app/Policies/EventPolicy.php @@ -58,7 +58,11 @@ class EventPolicy } else { $testedUser = $event->guests()->where('users.id', $user->id)->first(); if ($testedUser !== null) { - return $testedUser->pivot->is_staff; + if($testedUser->pivot->is_staff) { + return $testedUser->pivot->is_staff; + } else if (!$event->private) { + return true; + } } } return false; @@ -99,4 +103,20 @@ class EventPolicy { return false; } + + /** + * Determine whether the guest can permanently delete invitation. + * + * @param \App\User $user + * @param \App\Models\Event $event + * @return mixed + */ + public function participation(User $user, Event $event) + { + $testedUser = $event->guests()->where('users.id', $user->id)->first(); + if ($testedUser !== null) { + return true; + } + return false; + } } diff --git a/routes/api.php b/routes/api.php index b48fb57..62427fc 100644 --- a/routes/api.php +++ b/routes/api.php @@ -20,6 +20,8 @@ Route::middleware('auth:api')->group(function () { Route::get('/memos/home', 'MemosController@home'); + Route::delete('/events/{event}/invite/delete', 'EventController@userDeleteInvitation'); + Route::delete('/events/{event}/invite/validation', 'EventController@userConfirmParticipation'); Route::post('/events/{event}/invite/{user}', 'EventController@inviteUser'); Route::delete('/events/{event}/invite/{user}', 'EventController@removeInviteUser'); Route::post('/events/{event}/staff/{user}', 'EventController@addGuestToStaffEvent'); diff --git a/tests/Feature/EventsTest.php b/tests/Feature/EventsTest.php index 0856ec1..d44cad0 100644 --- a/tests/Feature/EventsTest.php +++ b/tests/Feature/EventsTest.php @@ -507,7 +507,6 @@ class EventsTest extends TestCase /** @test */ public function staff_can_add_guest() { - $this->withoutExceptionHandling(); $user = factory(User::class)->create(); $event = factory(Event::class)->create(['user_id' => $user->id]); $this->actingAs($userTwo = factory(User::class)->create(), 'api'); @@ -551,11 +550,96 @@ class EventsTest extends TestCase ]); } - // guest_can_invite_another_guest_if_public_event + /** @test */ + public function guest_can_invite_another_guest_if_public_event() + { + $user = factory(User::class)->create(); + $event = factory(Event::class)->create(['user_id' => $user->id,'private' => false]); + $this->actingAs($userTwo = factory(User::class)->create(), 'api'); + $event->guests()->attach($userTwo); - // guest_can_validate_event_participation + $nexGuestUser = factory(User::class)->create(); - // guest_can_delete_invitation + $response = $this->post('api/events/'.$event->id.'/invite/'.$nexGuestUser->id); + + $event = $event->fresh(); + + $this->assertCount(2, $event->guests); + $response->assertJson([ + 'data' => [ + 'event_id' => $event->id, + 'attributes' => [ + 'data' => [ + 'invitations' => [ + [ + 'data' => [ + 'user_id' => $userTwo->id, + ], + ], + [ + 'data' => [ + 'user_id' => $nexGuestUser->id, + ] + ] + ], + ] + ] + ] + ]); + } + + /** @test */ + public function guest_can_validate_event_participation() + { + $this->withoutExceptionHandling(); + $this->withoutExceptionHandling(); + $user = factory(User::class)->create(); + $event = factory(Event::class)->create(['user_id' => $user->id,'private' => false]); + $this->actingAs($userTwo = factory(User::class)->create(), 'api'); + $event->guests()->attach($userTwo); + + $response = $this->delete('api/events/'.$event->id.'/invite/validation'); + + $event = $event->fresh(); + + $this->assertCount(1, $event->guests); + $response->assertJson([ + 'data' => [ + 'event_id' => $event->id, + 'attributes' => [ + 'data' => [ + 'invitations' => [ + [ + 'data' => [ + 'user_id' => $userTwo->id, + 'attributes' => [ + 'validated_at' => now()->toDateTimeString(), + ] + ], + ], + ], + ] + ] + ] + ]); + } + + /** @test */ + public function guest_can_delete_invitation() + { + $this->withoutExceptionHandling(); + $user = factory(User::class)->create(); + $event = factory(Event::class)->create(['user_id' => $user->id,'private' => false]); + $this->actingAs($userTwo = factory(User::class)->create(), 'api'); + $event->guests()->attach($userTwo); + + $response = $this->delete('api/events/'.$event->id.'/invite/delete'); + + $event = $event->fresh(); + + $response->assertStatus(204); + $this->assertCount(0, $event->guests); + } // owner_can_invite_a_non_user_with_an_email