81a9f84b26
後端: - 新增 reviews / review_edits / review_votes migration(含索引) - Review / ReviewEdit / ReviewVote Model - ReviewController:評價 CRUD、資格驗證(completed booking)、rating 即時重算 - toggleHelpful:Member 限定、GREATEST 原子防負、DB transaction 同步 - AdminReviewController:全量列表、刪除(含重算) - AdminBookingController:全量列表、手動標記 completed - ProviderBookingController 新增 complete 方法(教練手動完成預約) - DevelopmentSeeder:快速重建測試資料(admin/coach/member + offers + bookings) - EnsureAdmin middleware 正式納入 bootstrap/app.php - Nginx server_name 加入 cfdive.local 前端: - 課程詳情頁加入評價區塊(星等分布、排序切換、撰寫/修改/刪除、有幫助 Toggle) - Coach Portal 新增「課程評價」頁(只讀,依課程分組) - Coach 預約管理加入「完成」按鈕 - Admin 新增「預約管理」頁(標記完成)、「評價管理」頁(刪除) - Admin / Coach Navbar 新增對應連結 OpenSpec: - review-system change 歸檔至 archive/2026-05-12-review-system - 新增 specs/review-lifecycle 與 specs/review-voting 主規格 - review-voting spec 補充 Member 限定與 GREATEST 原子更新說明 Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
135 lines
5.1 KiB
PHP
135 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\Enums\BookingStatus;
|
|
use App\Enums\ScheduleStatus;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Booking;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ProviderBookingController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$provider = $request->user();
|
|
$bookings = Booking::with(['member', 'schedule.divingOffer'])
|
|
->whereHas('schedule', fn($q) => $q->where('provider_id', $provider->id))
|
|
->orderByDesc('created_at')
|
|
->get()
|
|
->map(fn($b) => $this->formatBooking($b));
|
|
|
|
return response()->json(['status' => true, 'data' => $bookings]);
|
|
}
|
|
|
|
public function confirm(Request $request, int $id)
|
|
{
|
|
$booking = Booking::with('schedule')->findOrFail($id);
|
|
$this->authorizeProvider($request, $booking);
|
|
|
|
if (!$booking->canTransitionTo(BookingStatus::Confirmed)) {
|
|
return response()->json(['status' => false, 'message' => '當前狀態無法確認'], 422);
|
|
}
|
|
|
|
try {
|
|
DB::transaction(function () use ($booking) {
|
|
$schedule = $booking->schedule()->lockForUpdate()->first();
|
|
$remaining = $schedule->max_participants - $schedule->current_participants;
|
|
|
|
if ($booking->participants > $remaining) {
|
|
throw new \RuntimeException('名額不足,無法確認此預約');
|
|
}
|
|
|
|
$booking->update(['status' => BookingStatus::Confirmed]);
|
|
$schedule->increment('current_participants', $booking->participants);
|
|
$schedule->refresh();
|
|
|
|
if ($schedule->current_participants >= $schedule->max_participants) {
|
|
$schedule->update(['status' => ScheduleStatus::Full]);
|
|
}
|
|
});
|
|
} catch (\RuntimeException $e) {
|
|
return response()->json(['status' => false, 'message' => $e->getMessage()], 422);
|
|
}
|
|
|
|
return response()->json(['status' => true, 'message' => '預約已確認', 'data' => $this->formatBooking($booking->fresh(['member', 'schedule.divingOffer']))]);
|
|
}
|
|
|
|
public function reject(Request $request, int $id)
|
|
{
|
|
$booking = Booking::with('schedule')->findOrFail($id);
|
|
$this->authorizeProvider($request, $booking);
|
|
|
|
if (!$booking->canTransitionTo(BookingStatus::Rejected)) {
|
|
return response()->json(['status' => false, 'message' => '當前狀態無法拒絕'], 422);
|
|
}
|
|
|
|
$booking->update(['status' => BookingStatus::Rejected]);
|
|
|
|
return response()->json(['status' => true, 'message' => '預約已拒絕']);
|
|
}
|
|
|
|
public function cancel(Request $request, int $id)
|
|
{
|
|
$booking = Booking::with('schedule')->findOrFail($id);
|
|
$this->authorizeProvider($request, $booking);
|
|
|
|
if (!$booking->canTransitionTo(BookingStatus::ProviderCancelled)) {
|
|
return response()->json(['status' => false, 'message' => '當前狀態無法取消'], 422);
|
|
}
|
|
|
|
DB::transaction(function () use ($booking) {
|
|
$schedule = $booking->schedule()->lockForUpdate()->first();
|
|
$booking->update(['status' => BookingStatus::ProviderCancelled]);
|
|
$schedule->decrement('current_participants', $booking->participants);
|
|
$schedule->refresh();
|
|
|
|
if ($schedule->current_participants < $schedule->max_participants
|
|
&& $schedule->status === ScheduleStatus::Full) {
|
|
$schedule->update(['status' => ScheduleStatus::Open]);
|
|
}
|
|
});
|
|
|
|
return response()->json(['status' => true, 'message' => '預約已取消']);
|
|
}
|
|
|
|
public function complete(Request $request, int $id)
|
|
{
|
|
$booking = Booking::with('schedule')->findOrFail($id);
|
|
$this->authorizeProvider($request, $booking);
|
|
|
|
if (!$booking->canTransitionTo(BookingStatus::Completed)) {
|
|
return response()->json(['status' => false, 'message' => '只有已確認的預約才能標記完成'], 422);
|
|
}
|
|
|
|
$booking->update(['status' => BookingStatus::Completed]);
|
|
|
|
return response()->json(['status' => true, 'message' => '預約已標記為完成']);
|
|
}
|
|
|
|
private function authorizeProvider(Request $request, Booking $booking): void
|
|
{
|
|
if ($booking->schedule->provider_id !== $request->user()->id) {
|
|
abort(403, '無權操作此預約');
|
|
}
|
|
}
|
|
|
|
private function formatBooking(Booking $b): array
|
|
{
|
|
return [
|
|
'id' => $b->id,
|
|
'member_name' => $b->member?->name,
|
|
'member_email' => $b->member?->email,
|
|
'offer_title' => $b->schedule?->divingOffer?->title,
|
|
'scheduled_date' => $b->schedule?->scheduled_date?->toDateString(),
|
|
'start_time' => $b->schedule?->start_time,
|
|
'participants' => $b->participants,
|
|
'total_price' => $b->total_price,
|
|
'status' => $b->status->value,
|
|
'notes' => $b->notes,
|
|
'created_at' => $b->created_at?->toISOString(),
|
|
];
|
|
}
|
|
}
|