Skip to content

Commit

Permalink
not def
Browse files Browse the repository at this point in the history
  • Loading branch information
whiitex committed May 19, 2024
1 parent a4a43ad commit bbb997f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 75 deletions.
4 changes: 2 additions & 2 deletions api/src/timeslots/timeslots.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ describe('TimeslotsController', () => {
await app.close();
});

describe(' GET /timeslots', () => {
describe('GET /timeslots', () => {
beforeEach(async () => {
for (const user of mockUsers) {
await usersService.create(user);
Expand All @@ -879,7 +879,7 @@ describe('TimeslotsController', () => {
const availability = {
id: a.id,
state: a.state,
lastModified: a.lastModified,
lastModified: a.lastModified ,
timeSlot: timeSlotsService.findById(a.timeSlot),
user: mockUsers.find((u) => u.oauthId === a.userOauthId),
} as unknown as Availability;
Expand Down
108 changes: 54 additions & 54 deletions api/src/timeslots/timeslots.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,64 +196,64 @@ describe('TimeSlotsService', () => {
});
});

// describe('findAvailableTimeSlots', () => {
// it('should correctly call all functions provided for database query', async () => {
// // Mock the query builder and its methods
// const mockQueryBuilder = {
// innerJoinAndSelect: jest.fn().mockReturnThis(),
// where: jest.fn().mockReturnThis(),
// andWhere: jest.fn().mockReturnThis(),
// getMany: jest.fn().mockResolvedValue([]),
// };
describe('findAvailableTimeSlots', () => {
it('should correctly call all functions provided for database query', async () => {
// Mock the query builder and its methods
const mockQueryBuilder = {
innerJoinAndSelect: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
getMany: jest.fn().mockResolvedValue([]),
};

// // Mock the timeSlotRepository and its methods
// const mockTimeSlotRepository = {
// createQueryBuilder: jest.fn().mockReturnValue(mockQueryBuilder),
// };
// Mock the timeSlotRepository and its methods
const mockTimeSlotRepository = {
createQueryBuilder: jest.fn().mockReturnValue(mockQueryBuilder),
};

// const timeSlotService = new TimeSlotsService(
// mockTimeSlotRepository as any,
// );
// const result = await timeSlotService.findAvailableTimeSlots();
const timeSlotService = new TimeSlotsService(
mockTimeSlotRepository as any,
);
const result = await timeSlotService.findAvailableTimeSlots();

// // Assert that the query builder methods were called correctly
// expect(mockQueryBuilder.innerJoinAndSelect).toHaveBeenCalledWith(
// 'TimeSlot.availabilities',
// 'availability',
// );
// expect(mockQueryBuilder.innerJoinAndSelect).toHaveBeenCalledWith(
// 'TimeSlot.recruitmentSession',
// 'recruitmentSession',
// );
// expect(mockQueryBuilder.innerJoinAndSelect).toHaveBeenCalledWith(
// 'availability.user',
// 'user',
// );
// expect(mockQueryBuilder.where).toHaveBeenCalledWith(
// 'recruitmentSession.state = :recruitmentSessionState',
// {
// recruitmentSessionState: RecruitmentSessionState.Active,
// },
// );
// expect(mockQueryBuilder.andWhere).toHaveBeenCalledWith(
// 'user.role NOT IN (:...roles)',
// {
// roles: [Role.None, Role.Applicant],
// },
// );
// expect(mockQueryBuilder.andWhere).toHaveBeenCalledWith(
// 'availability.state = :availabilityState AND (user.is_board = true OR user.is_expert = true)',
// {
// availabilityState: AvailabilityState.Free,
// },
// );
// expect(mockQueryBuilder.andWhere).toHaveBeenCalledWith(
// '(SELECT COUNT(availability.id) FROM Availability availability WHERE availability.timeSlotId = TimeSlot.id) > 1',
// );
// Assert that the query builder methods were called correctly
expect(mockQueryBuilder.innerJoinAndSelect).toHaveBeenCalledWith(
'TimeSlot.availabilities',
'availability',
);
expect(mockQueryBuilder.innerJoinAndSelect).toHaveBeenCalledWith(
'TimeSlot.recruitmentSession',
'recruitmentSession',
);
expect(mockQueryBuilder.innerJoinAndSelect).toHaveBeenCalledWith(
'availability.user',
'user',
);
expect(mockQueryBuilder.where).toHaveBeenCalledWith(
'recruitmentSession.state = :recruitmentSessionState',
{
recruitmentSessionState: RecruitmentSessionState.Active,
},
);
expect(mockQueryBuilder.andWhere).toHaveBeenCalledWith(
'user.role NOT IN (:...roles)',
{
roles: [Role.None, Role.Applicant],
},
);
expect(mockQueryBuilder.andWhere).toHaveBeenCalledWith(
'availability.state = :availabilityState AND (user.is_board = true OR user.is_expert = true)',
{
availabilityState: AvailabilityState.Free,
},
);
expect(mockQueryBuilder.andWhere).toHaveBeenCalledWith(
'(SELECT COUNT(availability.id) FROM Availability availability WHERE availability.timeSlotId = TimeSlot.id) > 1',
);

// expect(result).toEqual([]);
// });
// });
expect(result).toEqual([]);
});
});
});

function testTimeSlotsGeneration(
Expand Down
35 changes: 16 additions & 19 deletions api/src/timeslots/timeslots.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
AvailabilityState,
Role,
} from '@hkrecruitment/shared';
import { CreateTimeSlotDto } from './create-timeslot.dto';
import { count } from 'console';

@Injectable()
export class TimeSlotsService {
Expand Down Expand Up @@ -185,23 +183,22 @@ export class TimeSlotsService {
'(SELECT COUNT(availability.id) FROM Availability availability WHERE availability.timeSlotId = TimeSlot.id) > 1',
);

queryBuilder.getMany();
return await this.timeSlotRepository.find({
relations: [
'availabilities',
'availabilities.user',
'recruitmentSession',
],
where: {
availabilities: {
state: AvailabilityState.Free,
user: {
role: Not(In([Role.Applicant, Role.None])),
},
},
},
});
let allMatches = await queryBuilder.getMany();
const allMatches = await queryBuilder.getMany();
// const allMatches = await this.timeSlotRepository.find({
// relations: [
// 'availabilities',
// 'availabilities.user',
// 'recruitmentSession',
// ],
// where: {
// availabilities: {
// state: AvailabilityState.Free,
// user: {
// role: Not(In([Role.Applicant, Role.None])),
// },
// },
// },
// });

let goodTimeSlots: TimeSlot[] = [];
allMatches.forEach((timeSlot) => {
Expand Down

0 comments on commit bbb997f

Please sign in to comment.