Skip to content

Commit

Permalink
🧑‍💻 improve active status response (#3119)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Jan 11, 2025
1 parent 3de2382 commit 9c51451
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
25 changes: 10 additions & 15 deletions app/Http/Controllers/API/v1/StatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use Illuminate\Validation\Rules\Enum;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use OpenApi\Annotations as OA;

class StatusController extends Controller
{
Expand Down Expand Up @@ -419,7 +418,7 @@ public function update(Request $request, int $statusId): JsonResponse {
$this->authorize('update', $status);

//Check for disallowed status visibility changes
if(auth()->user()->can('disallow-status-visibility-change') && $validated['visibility'] !== StatusVisibility::PRIVATE->value) {
if (auth()->user()->can('disallow-status-visibility-change') && $validated['visibility'] !== StatusVisibility::PRIVATE->value) {
return $this->sendError('You are not allowed to change the visibility to anything else than private', 403);
}

Expand Down Expand Up @@ -612,26 +611,22 @@ public function getStopovers(string $parameters): JsonResponse {
* )
* ),
* @OA\Response(response=401, description="Unauthorized"),
* @OA\Response(response=404, description="No active checkin"),
* @OA\Response(response=204, description="No active checkin"),
* security={
* {"passport": {"read-statuses"}}, {"token": {}}
*
* }
* )
*
* @return JsonResponse
*/
public function getActiveStatus(): JsonResponse {
public function getActiveStatus(): StatusResource|JsonResponse {
$latestStatuses = UserBackend::statusesForUser(user: Auth::user());
if ($latestStatuses->count() === 0) {
return $this->sendError('User doesn\'t have any checkins');
}
foreach ($latestStatuses as $status) {
if ($status->checkin->originStopover->departure->isPast()
&& $status->checkin->destinationStopover->arrival->isFuture()) {
return $this->sendResponse(new StatusResource($status));
if ($latestStatuses->count() > 0) {
foreach ($latestStatuses as $status) {
if ($status->checkin->originStopover->departure->isPast()
&& $status->checkin->destinationStopover->arrival->isFuture()) {
return new StatusResource($status);
}
}
}
return $this->sendError('No active status');
return response()->json(null, 204);
}
}
19 changes: 2 additions & 17 deletions storage/api-docs/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -420,21 +420,6 @@
]
}
},
"/activeEvents": {
"get": {
"tags": [
"Events"
],
"summary": "DEPRECATED - USE /events - removed after 2024-08",
"description": "DEPRECATED - USE /events - removed after 2024-08",
"operationId": "getActiveEvents",
"responses": {
"200": {
"description": "The events"
}
}
}
},
"/user/{id}/follow": {
"post": {
"tags": [
Expand Down Expand Up @@ -2781,7 +2766,7 @@
"401": {
"description": "Unauthorized"
},
"404": {
"204": {
"description": "No active checkin"
}
},
Expand Down Expand Up @@ -4386,7 +4371,7 @@
{
"name": "username",
"in": "query",
"description": "Search for Username"
"description": "Search for parts username"
},
{
"name": "name",
Expand Down
6 changes: 2 additions & 4 deletions tests/Feature/APIv1/StatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public function testActiveStatusesWithoutAnyStatus(): void {
Passport::actingAs($user, ['*']);

$response = $this->get('/api/v1/user/statuses/active');
$response->assertNotFound();
$this->assertEquals('User doesn\'t have any checkins', $response->json('message'));
$response->assertNoContent();
}

public function testActiveStatusesShowStatusesCurrentlyUnderway(): void {
Expand Down Expand Up @@ -89,8 +88,7 @@ public function testActiveStatusesDontShowStatusesFromTheFuture(): void {
])->create();

$response = $this->get('/api/v1/user/statuses/active');
$response->assertNotFound();
$this->assertEquals('No active status', $response->json('message'));
$response->assertNoContent();
}

public function testStatusUpdate(): void {
Expand Down

0 comments on commit 9c51451

Please sign in to comment.