Skip to content
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
9 changes: 9 additions & 0 deletions backend/src/core/entities/joinRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ export class JoinRequest {
public set id(newId: string) {
this._id = newId;
}

public toObject(): object {
return {
requester: this._requester,
classId: this._classId,
type: this._type,
id: this._id
};
}
}
83 changes: 77 additions & 6 deletions backend/src/core/services/join_request/getJoinRequest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,80 @@
import { Service, ServiceParams } from "../../../config/service";
import { ApiError, ErrorCode } from '../../../application/types';
import { Service, ServiceParams } from '../../../config/service';
import { JoinRequest } from '../../entities/joinRequest';
import { IJoinRequestRepository } from '../../repositories/joinRequestRepositoryInterface';

export class GetJoinRequest implements Service<ServiceParams> {
constructor() {}
/**
* @description paramaters to get all joinRequests of a user
*
* @param _userId The id of the user.
*/
export class GetJoinRequestsParams implements ServiceParams {
constructor(protected _userId: string) {}

async execute(input: ServiceParams): Promise<object> {
return {};
get userId() {
return this._userId;
}
}
}

/**
* @description paramaters to get a single joinRequest of a user
* @param _userId The id of the user.
* @param _requestId The id of the joinRequest.
*/
export class GetJoinRequestParams extends GetJoinRequestsParams {
constructor(userId: string, private _requestId: string) {
super(userId);
}

get requestId(): string {
return this._requestId;
}
}

/**
* @description class representing service to get all joinRequests of a user
*
*/
export class GetJoinRequests implements Service<GetJoinRequestsParams> {
constructor(private joinRequestRepository: IJoinRequestRepository) {}

async execute(input: GetJoinRequestsParams): Promise<object> {
// Get all requests for user
const requests: JoinRequest[] =
await this.joinRequestRepository.getJoinRequestByRequesterId(
input.userId,
);
return {
requests: requests.map(request => request.toObject()),
};
}
}

/**
* @description class representing service to get a single joinRequest of a user
*/
export class GetJoinRequest implements Service<GetJoinRequestParams> {
constructor(private joinRequestRepository: IJoinRequestRepository) {}

async execute(input: GetJoinRequestParams): Promise<object> {
// Get all requests
const requests: JoinRequest[] =
await this.joinRequestRepository.getJoinRequestByRequesterId(
input.userId,
);

// Search for request with id
const joinRequest: JoinRequest[] = requests.filter(
request => request.id === input.requestId,
);

// No request found for this user with the given id.
if (joinRequest.length === 0) {
throw {
code: ErrorCode.NOT_FOUND,
message: 'joinRequest not found.',
} as ApiError;
}
return { request: joinRequest[0].toObject() };
}
}
73 changes: 73 additions & 0 deletions backend/test/core/services/joinRequest/getJoinRequest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { GetJoinRequests, GetJoinRequest, GetJoinRequestsParams, GetJoinRequestParams } from '../../../../src/core/services/join_request/getJoinRequest';
import { IJoinRequestRepository } from '../../../../src/core/repositories/joinRequestRepositoryInterface';
import { JoinRequest, JoinRequestType } from '../../../../src/core/entities/joinRequest';
import { ApiError, ErrorCode } from '../../../../src/application/types';

describe('GetJoinRequests Service', () => {
let getJoinRequestsService: GetJoinRequests;
let mockJoinRequestRepository: jest.Mocked<IJoinRequestRepository>;
let params: GetJoinRequestsParams;

beforeEach(() => {
mockJoinRequestRepository = {
getJoinRequestByRequesterId: jest.fn(),
} as unknown as jest.Mocked<IJoinRequestRepository>;

getJoinRequestsService = new GetJoinRequests(mockJoinRequestRepository);

params = new GetJoinRequestsParams('user1');
});

test('Should return all join requests for a user', async () => {
const joinRequests: JoinRequest[] = [
new JoinRequest('user1', 'class1', JoinRequestType.STUDENT, "1"),
new JoinRequest('user1', 'class2', JoinRequestType.TEACHER, "2"),
];

mockJoinRequestRepository.getJoinRequestByRequesterId.mockResolvedValue(joinRequests);

await expect(getJoinRequestsService.execute(params)).resolves.toEqual({
requests: joinRequests.map(request => request.toObject()),
});
expect(mockJoinRequestRepository.getJoinRequestByRequesterId).toHaveBeenCalledWith(params.userId);
});
});

describe('GetJoinRequest Service', () => {
let getJoinRequestService: GetJoinRequest;
let mockJoinRequestRepository: jest.Mocked<IJoinRequestRepository>;
let params: GetJoinRequestParams;

beforeEach(() => {
mockJoinRequestRepository = {
getJoinRequestByRequesterId: jest.fn(),
} as unknown as jest.Mocked<IJoinRequestRepository>;

getJoinRequestService = new GetJoinRequest(mockJoinRequestRepository);

params = new GetJoinRequestParams('user1', '1');
});

test('Should return a single join request for a user', async () => {
const joinRequests: JoinRequest[] = [
new JoinRequest('user1', 'class1', JoinRequestType.TEACHER, "1"),
new JoinRequest('user1', 'class2', JoinRequestType.STUDENT, "2"),
];

mockJoinRequestRepository.getJoinRequestByRequesterId.mockResolvedValue(joinRequests);

await expect(getJoinRequestService.execute(params)).resolves.toEqual({
request: joinRequests[0].toObject(),
});
expect(mockJoinRequestRepository.getJoinRequestByRequesterId).toHaveBeenCalledWith(params.userId);
});

test('Should throw error if join request not found', async () => {
mockJoinRequestRepository.getJoinRequestByRequesterId.mockResolvedValue([]);

await expect(getJoinRequestService.execute(params)).rejects.toEqual({
code: ErrorCode.NOT_FOUND,
message: 'joinRequest not found.',
} as ApiError);
});
});