Skip to content

Commit

Permalink
Merge pull request #257 from w3bdesign/backend
Browse files Browse the repository at this point in the history
Fix Codacy issues
  • Loading branch information
w3bdesign authored Jan 21, 2025
2 parents 49ca703 + c8727d5 commit 6029f04
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
20 changes: 18 additions & 2 deletions backend/src/auth/guards/jwt-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, ExecutionContext, UnauthorizedException } from "@nestjs/common";
import { Injectable, ExecutionContext, UnauthorizedException, Logger } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";
import { User } from "../../users/entities/user.entity";
import { Request } from "express";
Expand All @@ -10,6 +10,7 @@ interface JwtError extends Error {

@Injectable()
export class JwtAuthGuard extends AuthGuard("jwt") {
private readonly logger = new Logger(JwtAuthGuard.name);
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
const request = context.switchToHttp().getRequest<Request>();
const authHeader = request.headers.authorization;
Expand Down Expand Up @@ -40,7 +41,22 @@ export class JwtAuthGuard extends AuthGuard("jwt") {
// Add debug logging
console.log('JWT Auth Guard - Token:', token);

return super.canActivate(context);
try {
const result = super.canActivate(context);

if (result instanceof Observable) {
return result;
}

if (result instanceof Promise) {
return result;
}

return result;
} catch (error) {
this.logger.error('Error in canActivate:', error);
throw new UnauthorizedException('Authentication failed');
}
}

handleRequest<TUser = User>(
Expand Down
5 changes: 3 additions & 2 deletions backend/src/auth/guards/roles.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ export class RolesGuard implements CanActivate {
return false;
}

const hasRole = requiredRoles.includes(user.role);
const hasRole: boolean = requiredRoles.includes(user.role);
console.log('Roles Guard - Has Required Role:', hasRole);

return hasRole;
const canActivate: boolean = hasRole;
return canActivate;
}
}
20 changes: 11 additions & 9 deletions backend/src/bookings/bookings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,21 @@ export class BookingsService {
}

async findByCustomer(customerId: string): Promise<Booking[]> {
return this.bookingRepository.find({
const bookings: Booking[] = await this.bookingRepository.find({
where: { customer: { id: customerId } },
relations: ["customer", "employee", "employee.user", "service"],
order: { startTime: "DESC" },
});
return bookings;
}

async findByEmployee(employeeId: string): Promise<Booking[]> {
return this.bookingRepository.find({
const bookings: Booking[] = await this.bookingRepository.find({
where: { employee: { id: employeeId } },
relations: ["customer", "employee", "employee.user", "service"],
order: { startTime: "DESC" },
});
return bookings;
}

async findUpcoming(): Promise<Booking[]> {
Expand All @@ -231,7 +233,7 @@ export class BookingsService {
);
this.logger.debug(`Finding bookings from ${startOfDay.toISOString()}`);

const bookings = await this.bookingRepository.find({
const bookings: Booking[] = await this.bookingRepository.find({
where: {
startTime: MoreThan(startOfDay),
status: In([BookingStatus.PENDING, BookingStatus.CONFIRMED]),
Expand All @@ -245,12 +247,12 @@ export class BookingsService {
this.logger.debug(
"No bookings found. Checking all bookings for debugging..."
);
const allBookings = await this.bookingRepository.find({
const allBookings: Booking[] = 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) => {
allBookings.slice(0, 3).forEach((booking: Booking) => {
this.logger.debug(
`Booking ${booking.id}: startTime=${booking.startTime.toISOString()}, status=${booking.status}`
);
Expand All @@ -270,7 +272,7 @@ export class BookingsService {
const endOfDay = new Date(startOfDay);
endOfDay.setDate(endOfDay.getDate() + 1);

const bookings = await this.bookingRepository.find({
const bookings: Booking[] = await this.bookingRepository.find({
where: {
startTime: Between(startOfDay, endOfDay),
status: In([BookingStatus.PENDING, BookingStatus.CONFIRMED]),
Expand All @@ -279,11 +281,11 @@ export class BookingsService {
order: { startTime: "ASC" },
});

const customers: UpcomingCustomerDto[] = bookings.map((booking, index) => {
const customers: UpcomingCustomerDto[] = bookings.map((booking: Booking, index: number) => {
// Calculate waiting time based on previous bookings' service durations
const waitingTime = bookings
const waitingTime: number = bookings
.slice(0, index)
.reduce((total, prev) => total + prev.service.duration, 0);
.reduce((total: number, prev: Booking) => total + prev.service.duration, 0);

return {
firstName: booking.customer.firstName,
Expand Down

0 comments on commit 6029f04

Please sign in to comment.