From d9364c0683a724c377b2974357b40b62aaee360a Mon Sep 17 00:00:00 2001 From: w3bdesign <45217974+w3bdesign@users.noreply.github.com> Date: Wed, 22 Jan 2025 00:15:39 +0100 Subject: [PATCH 1/4] Update jwt-auth.guard.ts --- backend/src/auth/guards/jwt-auth.guard.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/src/auth/guards/jwt-auth.guard.ts b/backend/src/auth/guards/jwt-auth.guard.ts index aeb8324..945806c 100644 --- a/backend/src/auth/guards/jwt-auth.guard.ts +++ b/backend/src/auth/guards/jwt-auth.guard.ts @@ -40,7 +40,17 @@ export class JwtAuthGuard extends AuthGuard("jwt") { // Add debug logging console.log('JWT Auth Guard - Token:', token); - return super.canActivate(context); + const result = super.canActivate(context); + + if (result instanceof Observable) { + return result; + } + + if (result instanceof Promise) { + return result; + } + + return result; } handleRequest( From cff23e62f88a94abbac47943a72c8299d43419d3 Mon Sep 17 00:00:00 2001 From: w3bdesign <45217974+w3bdesign@users.noreply.github.com> Date: Wed, 22 Jan 2025 00:21:38 +0100 Subject: [PATCH 2/4] Update roles.guard.ts --- backend/src/auth/guards/roles.guard.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/src/auth/guards/roles.guard.ts b/backend/src/auth/guards/roles.guard.ts index dba7733..c4a1178 100644 --- a/backend/src/auth/guards/roles.guard.ts +++ b/backend/src/auth/guards/roles.guard.ts @@ -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; } } From 1480748d799b7e0c3746a80babe4eff4ea64c289 Mon Sep 17 00:00:00 2001 From: w3bdesign <45217974+w3bdesign@users.noreply.github.com> Date: Wed, 22 Jan 2025 00:27:26 +0100 Subject: [PATCH 3/4] Codacy --- backend/src/auth/guards/jwt-auth.guard.ts | 23 ++++++++++++++--------- backend/src/bookings/bookings.service.ts | 20 +++++++++++--------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/backend/src/auth/guards/jwt-auth.guard.ts b/backend/src/auth/guards/jwt-auth.guard.ts index 945806c..054dca5 100644 --- a/backend/src/auth/guards/jwt-auth.guard.ts +++ b/backend/src/auth/guards/jwt-auth.guard.ts @@ -40,17 +40,22 @@ export class JwtAuthGuard extends AuthGuard("jwt") { // Add debug logging console.log('JWT Auth Guard - Token:', token); - const result = super.canActivate(context); - - if (result instanceof Observable) { - return result; - } - - if (result instanceof Promise) { + 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'); } - - return result; } handleRequest( diff --git a/backend/src/bookings/bookings.service.ts b/backend/src/bookings/bookings.service.ts index 05c9f4e..7735def 100644 --- a/backend/src/bookings/bookings.service.ts +++ b/backend/src/bookings/bookings.service.ts @@ -207,19 +207,21 @@ export class BookingsService { } async findByCustomer(customerId: string): Promise { - 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 { - 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 { @@ -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]), @@ -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}` ); @@ -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]), @@ -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, From c8727d52c6c41ab3f40ae60dd859399241589d64 Mon Sep 17 00:00:00 2001 From: w3bdesign <45217974+w3bdesign@users.noreply.github.com> Date: Wed, 22 Jan 2025 00:30:31 +0100 Subject: [PATCH 4/4] Update jwt-auth.guard.ts --- backend/src/auth/guards/jwt-auth.guard.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/src/auth/guards/jwt-auth.guard.ts b/backend/src/auth/guards/jwt-auth.guard.ts index 054dca5..5e10571 100644 --- a/backend/src/auth/guards/jwt-auth.guard.ts +++ b/backend/src/auth/guards/jwt-auth.guard.ts @@ -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"; @@ -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 | Observable { const request = context.switchToHttp().getRequest(); const authHeader = request.headers.authorization;