Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:MuNuChapterHKN/HKrecruitment into fi…
Browse files Browse the repository at this point in the history
…x/sonarcloud-coverage
  • Loading branch information
AlbertoBaroso committed May 30, 2024
2 parents 6d13db1 + 7073e2a commit 6ec2116
Show file tree
Hide file tree
Showing 20 changed files with 1,737 additions and 23 deletions.
4 changes: 1 addition & 3 deletions api/src/availability/availability.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from '../../../shared/src/availability';
import { User } from 'src/users/user.entity';
import { TimeSlot } from 'src/timeslots/timeslot.entity';
import { DbAwareColumn } from 'src/utils/db-aware-column';

@Entity()
export class Availability implements AvailabilityInterface {
Expand All @@ -24,13 +23,12 @@ export class Availability implements AvailabilityInterface {
@Column({ name: 'last_modified' })
lastModified: Date;

@DbAwareColumn(() => TimeSlot, { name: 'time_slot' })
@ManyToOne(() => TimeSlot, (timeSlot) => timeSlot.availabilities)
timeSlot: Relation<TimeSlot>;

@ManyToOne(() => User, (user) => user.availabilities)
user: Relation<User>;

// @OneToOne(() => Interview)
// @OneToOne(() => Interview, (interview) => interview.availability)
// interview: Interview;
}
29 changes: 29 additions & 0 deletions api/src/availability/availability.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ describe('AvailabilityService', () => {
});
});

describe('updateAvailability', () => {
it('should update the availability with the specified id', async () => {
const mockUpdatedAvailability = {
...mockAvailability,
state: AvailabilityState.Interviewing,
};
jest
.spyOn(mockedRepository, 'save')
.mockResolvedValue(mockUpdatedAvailability);
const result = await service.updateAvailability(
mockAvailability.id,
mockUpdatedAvailability,
);
expect(result).toEqual(mockUpdatedAvailability);
expect(mockedRepository.save).toHaveBeenCalledTimes(1);
expect(mockedRepository.save).toHaveBeenCalledWith({
...mockUpdatedAvailability,
id: mockAvailability.id,
});
});
});

describe('deleteAvailability', () => {
it('should remove the specified availability from the database', async () => {
const mockAvailabilityRepository = {
Expand Down Expand Up @@ -142,6 +164,13 @@ describe('AvailabilityService', () => {
expect(result).toEqual(mockAvailability);
expect(mockedRepository.findBy).toHaveBeenCalledTimes(1);
});

it('should return null if no availability is found', async () => {
jest.spyOn(mockedRepository, 'findBy').mockResolvedValue([]);
const result = await service.findById(mockAvailability.id);
expect(result).toBeNull();
expect(mockedRepository.findBy).toHaveBeenCalledTimes(1);
});
});

describe('findByUserAndTimeSlot', () => {
Expand Down
35 changes: 33 additions & 2 deletions api/src/availability/availability.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,54 @@ export class AvailabilityService {
private dataSource: DataSource,
) {}

/**
* List all availabilities
* @returns {Promise<Availability[]>} - List of availabilities
*/
async listAvailabilities(): Promise<Availability[]> {
return await this.availabilityRepository.find();
}

/**
* Find all availabilities for a given user
* @param user - User to find availabilities for
* @returns {Promise<Availability[]>} - List of availabilities for the user
*/
async findById(id: number): Promise<Availability> {
const matches = await this.availabilityRepository.findBy({
id: id,
});
return matches.length > 0 ? matches[0] : null;
}

/**
* Find all availabilities for a given user
* @param user - User to find availabilities for
* @returns {Promise<Availability[]>} - List of availabilities for the user
*/
async findByUserAndTimeSlot(user: User, timeSlot: TimeSlot) {
const matches = await this.availabilityRepository.findBy({
user: user,
timeSlot: timeSlot,
user: user as any,
timeSlot: timeSlot as any,
});
return matches.length > 0 ? matches[0] : null;
}

/**
* Create an availability
* @param availability - Availability to create
* @returns {Promise<Availability>} - Created availability
*/
async createAvailability(availability: Availability): Promise<Availability> {
return await this.availabilityRepository.save(availability);
}

/**
* Update an availability
* @param oldAvailabilityId - ID of the
* @param newAvailability - New availability
* @returns {Promise<Availability>} - Updated availability
*/
async updateAvailability(
oldAvailabilityId: number,
newAvailability: Availability,
Expand All @@ -49,6 +74,12 @@ export class AvailabilityService {
});
}

/**
* Delete an availability
* @param availabilityId - ID of the availability to delete
* @returns {Promise<Availability>} - Deleted availability
* @throws {ConflictException} - If availability is in use
*/
async deleteAvailability(availabilityId: number): Promise<Availability> {
return await transaction(
this.dataSource,
Expand Down
90 changes: 83 additions & 7 deletions api/src/mocks/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ export const testDateTime1Hour = new Date(2023, 0, 1, 11, 30, 0);
export const testDateTime3Hours = new Date(2023, 0, 1, 13, 30, 0);
export const testDateTimeEnd = new Date(2023, 0, 1, 11, 30, 0);

export const mockTimeSlot = {
start: testDateTimeStart,
end: testDateTimeEnd,
id: 1,
availabilities: [],
} as TimeSlot & { availabilities: any[] };

export const testInterviewStart = '11:55' as unknown as Date;
export const testInterviewEnd = '20:35' as unknown as Date;
export const testDay1 = '2024-10-20' as unknown as Date;
Expand All @@ -54,6 +47,14 @@ export const mockRecruitmentSession = {
lastModified: testDateLastModified,
};

export const mockTimeSlot = {
recruitmentSession: mockRecruitmentSession,
start: testDateTimeStart,
end: testDateTimeEnd,
id: 1,
availabilities: [],
} as TimeSlot & { availabilities: any[] };

export const mockCreateRecruitmentSessionDto = {
slotDuration: 50,
interviewStart: testInterviewStart,
Expand Down Expand Up @@ -228,3 +229,78 @@ export const mockAvailability = {
export const mockCreateAvailabilityDto = {
timeSlotId: mockTimeSlot.id,
} as CreateAvailabilityDto;

export const mockTimeSlotsJoined = [
{
start: new Date('2022-01-01T09:00:00'),
end: new Date('2022-01-01T10:00:00'),
id: 1,
recruitmentSession: 3,
availabilities: [
{
state: AvailabilityState.Interviewing,
user: {
role: Role.None,
is_board: false,
is_expert: true,
},
},
{
state: AvailabilityState.Free,
user: {
role: Role.Member,
is_board: true,
is_expert: false,
},
},
{
state: AvailabilityState.Free,
user: {
role: Role.Applicant,
is_board: false,
is_expert: false,
},
},
{
state: AvailabilityState.Free,
user: {
role: Role.Member,
is_board: false,
is_expert: true,
},
},
{
state: AvailabilityState.Interviewing,
user: {
role: Role.Member,
is_board: false,
is_expert: true,
},
},
{
state: AvailabilityState.Interviewing,
user: {
role: Role.Admin,
is_board: false,
is_expert: true,
},
},
],
},
{
start: new Date('2022-01-01T9:00:00'),
end: new Date('2022-01-01T20:00:00'),
id: 2,
recruitmentSession: 3,
availabilities: [
{
state: AvailabilityState.Free,
user: {
role: Role.Member,
is_board: true,
is_expert: true,
},
},
],
},
];
Loading

0 comments on commit 6ec2116

Please sign in to comment.