getInputs(); $id = $inputs['appointment_id'] ?? '?'; $changes = []; if ( ! empty( $inputs['status'] ) ) { $changes[] = "status to \"{$inputs['status']}\""; } if ( isset( $inputs['note'] ) ) { $changes[] = 'note'; } if ( isset( $inputs['total_attendees'] ) && '' !== $inputs['total_attendees'] ) { $changes[] = "attendees to {$inputs['total_attendees']}"; } // Resolve so the approver sees which appointment, not a bare ID. $detail = $this->describe_appointment( $inputs['appointment_id'] ?? 0 ); $summary = '' !== $detail ? "Update appointment #{$id} — {$detail}" : "Update appointment #{$id}"; if ( ! empty( $changes ) ) { $summary .= ': ' . implode( ', ', $changes ); } return $summary; } /** * Human-readable inputs shown in the approval card table. * * @return array */ public function get_approval_inputs(): array { $inputs = $this->getInputs(); // Surface who/what/when for the approver. $detail = $this->describe_appointment( $inputs['appointment_id'] ?? 0 ); if ( '' !== $detail ) { $inputs['appointment'] = $detail; } return $inputs; } /** * Execute the tool after human approval. * * @param int $appointment_id Appointment ID. * @param string|null $status New status. * @param string|null $note New note. * @param int|null $total_attendees New attendee count. * @return array */ public function __invoke( int $appointment_id, ?string $status = null, ?string $note = null, ?int $total_attendees = null ): array { $check = $this->guard_staff(); if ( $check ) { return $check; } // Team members may only touch their own appointments. $access = $this->assert_appointment_access( $appointment_id ); if ( $access ) { return $access; } if ( null !== $status && ! in_array( $status, self::ALLOWED_STATUSES, true ) ) { return [ 'error' => "Invalid status '{$status}'. Accepted values: " . implode( ', ', self::ALLOWED_STATUSES ) . '. To cancel, use booktics-cancel-appointment.' ]; } $body = []; if ( null !== $status ) { $body['status'] = $status; } if ( null !== $note ) { $body['note'] = sanitize_textarea_field( $note ); } if ( null !== $total_attendees ) { $body['total_attendees'] = max( 1, $total_attendees ); } if ( empty( $body ) ) { return [ 'error' => 'No fields provided to update.' ]; } try { $request = $this->build_json_request( 'PUT', '/booktics/v1/appointments/' . $appointment_id, $body, $appointment_id ); if ( is_array( $request ) ) { return $request; } $controller = new \Booktics\Appointment\Controllers\Appointment_Controller(); $data = $this->extract_response( $controller->update_item( $request ), 'Failed to update appointment.' ); if ( isset( $data['error'] ) ) { return $data; } return [ 'success' => true, 'appointment_id' => $appointment_id, 'updated_fields' => array_keys( $body ), 'message' => "Appointment #{$appointment_id} updated successfully.", ]; } catch ( \Throwable $e ) { Log::error( 'Booktics_Update_Appointment_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to update appointment: ' . $e->getMessage() ]; } } }