Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Codacy issues #257

Merged
merged 6 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading