finish event with log user participation

This commit is contained in:
2020-08-20 14:47:44 +02:00
parent 98fc72d6dc
commit 0a819fbadf
5 changed files with 134 additions and 5 deletions

View File

@@ -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);
}
}

View File

@@ -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' => [

View File

@@ -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;
}
}

View File

@@ -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');

View File

@@ -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