|
16 | 16 | use App\Http\Resources\StopoverResource; |
17 | 17 | use App\Models\Status; |
18 | 18 | use App\Models\Stopover; |
| 19 | +use App\Models\Ticket; |
19 | 20 | use App\Models\Trip; |
20 | 21 | use Illuminate\Auth\Access\AuthorizationException; |
21 | 22 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
47 | 48 | new OA\Property(property: 'destinationArrivalPlanned', description: 'Destination arrival time', type: 'string', format: 'date', example: '2020-01-01 13:00:00', nullable: true), |
48 | 49 | ], |
49 | 50 | )] |
| 51 | +#[OA\Schema( |
| 52 | + schema: 'StatusAssignTicketBody', |
| 53 | + title: 'StatusAssignTicketBody', |
| 54 | + description: 'Assign or remove a ticket from a status', |
| 55 | + properties: [ |
| 56 | + new OA\Property(property: 'ticketId', description: 'UUID of the ticket to assign, or null to remove the assignment', type: 'string', format: 'uuid', example: '00000000-0000-0000-0000-000000000000', nullable: true), |
| 57 | + ], |
| 58 | +)] |
50 | 59 | #[OA\Schema( |
51 | 60 | schema: 'Polyline', |
52 | 61 | title: 'Polyline', |
@@ -564,6 +573,7 @@ public function update(Request $request, int $statusId): JsonResponse |
564 | 573 | if (array_key_exists('eventId', $validated)) { // don't use isset here as it would return false if eventId is null |
565 | 574 | $updatePayload['event_id'] = $validated['eventId']; |
566 | 575 | } |
| 576 | + |
567 | 577 | $status->update($updatePayload); |
568 | 578 |
|
569 | 579 | if (array_key_exists('manualDeparture', $validated)) { |
@@ -616,6 +626,67 @@ public function update(Request $request, int $statusId): JsonResponse |
616 | 626 | } |
617 | 627 | } |
618 | 628 |
|
| 629 | + #[OA\Put( |
| 630 | + path: '/statuses/{id}/tickets', |
| 631 | + operationId: 'assignTicketToStatus', |
| 632 | + description: 'Assign or remove a ticket from a status. Only the status owner can perform this action.', |
| 633 | + summary: 'Assign or remove a ticket from a status', |
| 634 | + security: [['passport' => ['write-statuses']], ['token' => []]], |
| 635 | + requestBody: new OA\RequestBody( |
| 636 | + required: true, |
| 637 | + content: new OA\JsonContent(ref: '#/components/schemas/StatusAssignTicketBody'), |
| 638 | + ), |
| 639 | + tags: ['Tickets'], |
| 640 | + parameters: [ |
| 641 | + new OA\Parameter( |
| 642 | + name: 'id', |
| 643 | + description: 'Status-ID', |
| 644 | + in: 'path', |
| 645 | + schema: new OA\Schema(type: 'integer'), |
| 646 | + example: 1337, |
| 647 | + ), |
| 648 | + ], |
| 649 | + responses: [ |
| 650 | + new OA\Response( |
| 651 | + response: 200, |
| 652 | + description: 'successful operation', |
| 653 | + content: new OA\JsonContent( |
| 654 | + properties: [new OA\Property(property: 'data', ref: '#/components/schemas/StatusResource')], |
| 655 | + ), |
| 656 | + ), |
| 657 | + new OA\Response(response: 404, description: 'Status or ticket not found'), |
| 658 | + ], |
| 659 | + )] |
| 660 | + public function assignTicket(Request $request, int $id): JsonResponse |
| 661 | + { |
| 662 | + $status = Status::find($id); |
| 663 | + if ($status === null || $status->user_id !== auth()->id()) { |
| 664 | + return $this->sendError('Status not found.', 404); |
| 665 | + } |
| 666 | + |
| 667 | + $validated = Validator::make($request->all(), [ |
| 668 | + 'ticketId' => ['present', 'nullable', 'uuid'], |
| 669 | + ])->validate(); |
| 670 | + |
| 671 | + if ($validated['ticketId'] !== null) { |
| 672 | + $ticket = Ticket::where('id', $validated['ticketId']) |
| 673 | + ->where('user_id', auth()->id()) |
| 674 | + ->first(); |
| 675 | + |
| 676 | + if ($ticket === null) { |
| 677 | + return $this->sendError('Ticket not found.', 404); |
| 678 | + } |
| 679 | + |
| 680 | + $status->ticket_id = $ticket->id; |
| 681 | + } else { |
| 682 | + $status->ticket_id = null; |
| 683 | + } |
| 684 | + |
| 685 | + $status->save(); |
| 686 | + |
| 687 | + return $this->sendResponse(new StatusResource($status->fresh())); |
| 688 | + } |
| 689 | + |
619 | 690 | /** |
620 | 691 | * @todo extract this to backend |
621 | 692 | * @todo does this conform to the private checkin-shit? |
|
0 commit comments