Skip to content

Commit

Permalink
Show future bookings
Browse files Browse the repository at this point in the history
  • Loading branch information
w3bdesign committed Nov 21, 2024
1 parent 1427600 commit 84c6dd4
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions backend/src/bookings/bookings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import {
Injectable,
NotFoundException,
BadRequestException,
Logger,
} from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository, MoreThan } from "typeorm";
import { Repository, MoreThan, In } from "typeorm";
import { Booking, BookingStatus } from "./entities/booking.entity";
import { CreateBookingDto } from "./dto/create-booking.dto";
import { UpdateBookingDto } from "./dto/update-booking.dto";
Expand All @@ -14,6 +15,8 @@ import { ServicesService } from "../services/services.service";

@Injectable()
export class BookingsService {
private readonly logger = new Logger(BookingsService.name);

constructor(
@InjectRepository(Booking)
private readonly bookingRepository: Repository<Booking>,
Expand Down Expand Up @@ -149,22 +152,39 @@ export class BookingsService {

async findUpcoming(): Promise<Booking[]> {
const now = new Date();
return this.bookingRepository.find({
this.logger.debug(`Finding upcoming bookings after ${now.toISOString()}`);

const bookings = await this.bookingRepository.find({
where: {
startTime: MoreThan(now),
status: BookingStatus.CONFIRMED,
status: In([BookingStatus.PENDING, BookingStatus.CONFIRMED]),
},
relations: ["customer", "employee", "employee.user", "service"],
order: { startTime: "ASC" },
});

this.logger.debug(`Found ${bookings.length} upcoming bookings`);
if (bookings.length === 0) {
this.logger.debug('No upcoming bookings found. Checking all bookings for debugging...');
const allBookings = await this.bookingRepository.find({
relations: ["customer", "employee", "employee.user", "service"],
});
this.logger.debug(`Total bookings in database: ${allBookings.length}`);
this.logger.debug('Sample booking dates:');
allBookings.slice(0, 3).forEach(booking => {
this.logger.debug(`Booking ${booking.id}: startTime=${booking.startTime}, status=${booking.status}`);
});
}

return bookings;
}

async getUpcomingCount(): Promise<number> {
const now = new Date();
return this.bookingRepository.count({
where: {
startTime: MoreThan(now),
status: BookingStatus.CONFIRMED,
status: In([BookingStatus.PENDING, BookingStatus.CONFIRMED]),
},
});
}
Expand Down

0 comments on commit 84c6dd4

Please sign in to comment.