Files
edr-platform/IMPLEMENTATION_SUMMARY.md
2026-06-03 15:36:37 +03:00

104 lines
3.5 KiB
Markdown

# User Booking History Implementation - COMPLETED
## Changes Made
### Backend Implementation
#### 1. **BookingsService** (`apps/edr-passenger-api/src/modules/bookings/bookings.service.ts`)
Added new method `findByPassengerId()`:
- Retrieves all bookings for an authenticated user by their passengerId
- Supports filtering by: search (booking ref, station names), status, pagination (page/pageSize)
- Returns paginated list with booking details, schedule, train, and payment info
- Structured response includes meta data (page, pageSize, total, totalPages)
**Method Signature:**
```typescript
async findByPassengerId(passengerId: string, filters: BookingFilters = {})
```
#### 2. **BookingsController** (`apps/edr-passenger-api/src/modules/bookings/bookings.controller.ts`)
Added new endpoint `GET /bookings/my/bookings`:
- Protected by `JwtGuard` (requires valid JWT token)
- Extracts passengerId from authenticated user object in request
- Passes through search, status, page, pageSize query parameters
- Fully documented with Swagger decorators and API responses
**Endpoint Details:**
```
GET /bookings/my/bookings
Authorization: Bearer <jwt-token>
Query Parameters:
- search?: string (search by booking ref or station names)
- status?: string (filter by booking status)
- page?: string (default: 1)
- pageSize?: string (default: 20)
Response: Paginated list of user's bookings with schedule and payment details
```
## Architecture
### Data Flow
1. User sends GET request to `/bookings/my/bookings` with JWT token
2. JwtGuard validates token and populates `req.user` with decoded payload
3. Controller extracts `passengerId` from `req.user`
4. `findByPassengerId()` queries Prisma for bookings where `passengerId` matches
5. Returns paginated, formatted booking list
### Key Features
- **Multi-level filtering**: Search across booking reference and station names
- **Pagination support**: Full pagination with total count and page info
- **Authenticated access**: Only authenticated users can access their bookings
- **Swagger documentation**: Complete API documentation with query parameters and response schema
- **Related data**: Includes schedule (train, stations, times), payment info, and seat count
## Verification
✅ TypeScript compilation successful (no type errors)
✅ New method added to BookingsService
✅ New endpoint added to BookingsController
✅ JWT authentication guard applied
✅ Swagger documentation complete
✅ Imports updated (added Req from @nestjs/common)
## Testing
To test the new endpoint:
```bash
# 1. Login to get JWT token
POST /auth/login
{
"email": "passenger@example.com",
"password": "password123"
}
# 2. Copy the accessToken from response
# 3. Call the new endpoint
GET /bookings/my/bookings?page=1&pageSize=10
Authorization: Bearer <accessToken>
# Optional filtering
GET /bookings/my/bookings?search=ABC&status=CONFIRMED&page=1
```
## Frontend Integration Notes
The frontend can now:
- Fetch authenticated user's booking history without manually managing passengerId
- Filter bookings by status (PENDING_PAYMENT, CONFIRMED, CANCELLED)
- Search bookings by reference or station names
- Handle pagination with page/pageSize parameters
- Display booking history in a user profile/dashboard view
## Database Query Structure
The implementation queries:
- `Booking` table filtered by passengerId
- Joins with `TrainSchedule` (includes train, origin/destination stations)
- Joins with `PaymentIntent` (payment info)
- Joins with `BookingSeat` (seat count)
No N+1 query issues due to Prisma's include optimization.