Skip to content

Commit fd20fff

Browse files
refactor + test fix
1 parent 0882c18 commit fd20fff

File tree

71 files changed

+331
-153
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+331
-153
lines changed

backend/src/config/services.ts

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,47 @@ import * as UserServices from "../core/services/user";
2121
export const services = {
2222
assignment: {
2323
get: new AssignmentServices.GetAssignment(repos.assignment),
24-
update: new AssignmentServices.UpdateAssignment(repos.assignment),
25-
remove: new AssignmentServices.DeleteAssignment(repos.assignment),
26-
create: new AssignmentServices.CreateAssignment(repos.assignment),
27-
getUserAssignments: new AssignmentServices.GetUserAssignments(repos.assignment),
24+
update: new AssignmentServices.UpdateAssignment(repos.assignment, repos.user),
25+
remove: new AssignmentServices.DeleteAssignment(repos.assignment, repos.user),
26+
create: new AssignmentServices.CreateAssignment(repos.assignment, repos.user),
27+
getUserAssignments: new AssignmentServices.GetUserAssignments(repos.assignment, repos.user),
2828
},
2929
authentication: {
3030
register: new UserServices.CreateUser(repos.user),
3131
},
3232
class: {
33-
get: new ClassServices.GetClass(repos.class),
34-
update: new ClassServices.UpdateClass(repos.class),
35-
remove: new ClassServices.DeleteClass(repos.class),
36-
create: new ClassServices.CreateClass(repos.class),
37-
getUserClasses: new ClassServices.GetUserClasses(repos.class),
33+
get: new ClassServices.GetClass(repos.class, repos.user),
34+
update: new ClassServices.UpdateClass(repos.class, repos.user),
35+
remove: new ClassServices.DeleteClass(repos.class, repos.user),
36+
create: new ClassServices.CreateClass(repos.class, repos.user),
37+
getUserClasses: new ClassServices.GetUserClasses(repos.class, repos.user),
3838
},
3939
group: {
40-
get: new GroupServices.GetGroup(repos.group),
41-
update: new GroupServices.UpdateGroup(repos.group),
42-
remove: new GroupServices.DeleteGroup(repos.group),
43-
create: new GroupServices.CreateGroup(repos.group),
44-
getUserGroups: new GroupServices.GetUserGroups(repos.group),
45-
getAssignmentGroups: new GroupServices.GetAssignmentGroups(repos.group),
40+
get: new GroupServices.GetGroup(repos.group, repos.user),
41+
update: new GroupServices.UpdateGroup(repos.group, repos.user),
42+
remove: new GroupServices.DeleteGroup(repos.group, repos.user),
43+
create: new GroupServices.CreateGroup(repos.group, repos.user),
44+
getUserGroups: new GroupServices.GetUserGroups(repos.group, repos.user),
45+
getAssignmentGroups: new GroupServices.GetAssignmentGroups(repos.group, repos.user),
4646
},
4747
joinCode: {
48-
get: new JoinCodeServices.GetJoinCode(repos.joinCode),
49-
update: new JoinCodeServices.UpdateJoinCode(repos.joinCode),
50-
remove: new JoinCodeServices.DeleteJoinCode(repos.joinCode),
51-
create: new JoinCodeServices.CreateJoinCode(repos.joinCode),
52-
getClassJoinCodes: new JoinCodeServices.GetClassJoinCodes(repos.joinCode),
48+
get: new JoinCodeServices.GetJoinCode(repos.joinCode, repos.user),
49+
update: new JoinCodeServices.UpdateJoinCode(repos.joinCode, repos.user),
50+
remove: new JoinCodeServices.DeleteJoinCode(repos.joinCode, repos.user),
51+
create: new JoinCodeServices.CreateJoinCode(repos.joinCode, repos.user),
52+
getClassJoinCodes: new JoinCodeServices.GetClassJoinCodes(repos.joinCode, repos.user),
5353
},
5454
joinRequest: {
55-
get: new JoinRequestServices.GetJoinRequest(repos.joinRequest),
56-
update: new JoinRequestServices.AcceptJoinRequest(repos.joinRequest, repos.class),
57-
remove: new JoinRequestServices.DeleteJoinRequest(repos.joinRequest),
58-
create: new JoinRequestServices.CreateJoinRequest(repos.joinRequest, repos.class),
59-
getUserJoinRequests: new JoinRequestServices.GetUserJoinRequests(repos.joinRequest),
60-
getClassJoinRequests: new JoinRequestServices.GetClassJoinRequests(repos.joinRequest),
55+
get: new JoinRequestServices.GetJoinRequest(repos.joinRequest, repos.user),
56+
update: new JoinRequestServices.AcceptJoinRequest(repos.joinRequest, repos.user, repos.class),
57+
remove: new JoinRequestServices.DeleteJoinRequest(repos.joinRequest, repos.user),
58+
create: new JoinRequestServices.CreateJoinRequest(repos.joinRequest, repos.user, repos.class),
59+
getUserJoinRequests: new JoinRequestServices.GetUserJoinRequests(repos.joinRequest, repos.user),
60+
getClassJoinRequests: new JoinRequestServices.GetClassJoinRequests(repos.joinRequest, repos.user),
6161
},
6262
message: {
6363
get: new MessageServices.GetMessage(repos.message),
64-
update: new MessageServices.UpdateMessage(repos.message),
64+
update: new MessageServices.UpdateMessage(repos.message, repos.user),
6565
remove: new MessageServices.DeleteMessage(repos.message),
6666
create: new MessageServices.CreateMessage(repos.message),
6767
getThreadMessages: new MessageServices.GetThreadMessages(repos.questionThread),
@@ -75,18 +75,23 @@ export const services = {
7575
},
7676
submission: {
7777
get: new SubmissionServices.GetSubmission(repos.submission),
78-
update: new SubmissionServices.UpdateSubmission(repos.submission),
78+
update: new SubmissionServices.UpdateSubmission(repos.submission, repos.user),
7979
remove: new SubmissionServices.DeleteSubmission(repos.submission),
8080
create: new SubmissionServices.CreateSubmission(repos.submission),
8181
getUserSubmissions: new SubmissionServices.GetUserSubmissions(repos.submission),
8282
},
8383
progress: {
84-
getUserProgress: new ProgressServices.GetUserProgress(repos.submission, repos.assignment, repos.learningPath),
85-
getAssignmentProgress: new ProgressServices.GetAssignmentProgress(
84+
getUserProgress: new ProgressServices.GetUserProgress(
85+
repos.submission,
86+
repos.assignment,
87+
repos.learningPath,
8688
repos.user,
89+
),
90+
getAssignmentProgress: new ProgressServices.GetAssignmentProgress(
8791
repos.submission,
8892
repos.assignment,
8993
repos.learningPath,
94+
repos.user,
9095
),
9196
getGroupProgress: new ProgressServices.GetGroupProgress(
9297
repos.group,
@@ -95,9 +100,14 @@ export const services = {
95100
repos.assignment,
96101
repos.learningPath,
97102
),
98-
get: new ProgressServices.GetUserAssignmentProgress(repos.submission, repos.assignment, repos.learningPath),
103+
get: new ProgressServices.GetUserAssignmentProgress(
104+
repos.submission,
105+
repos.assignment,
106+
repos.learningPath,
107+
repos.user,
108+
),
99109
getClassCompletion: new GetClassCompletion(repos.submission, repos.user, repos.assignment, repos.learningPath),
100-
getSubmissionActivity: new GetSubmissionActivity(repos.submission),
110+
getSubmissionActivity: new GetSubmissionActivity(repos.submission, repos.user),
101111
},
102112
user: {
103113
get: new UserServices.GetUser(repos.user),
@@ -120,10 +130,10 @@ export const services = {
120130
getAll: new LearningPathServices.GetAllLearningPaths(repos.learningPath),
121131
},
122132
task: {
123-
create: new TaskServices.CreateTask(repos.task),
124-
get: new TaskServices.GetTask(repos.task),
125-
getTasks: new TaskServices.GetTasks(repos.task),
126-
remove: new TaskServices.DeleteTask(repos.task),
127-
update: new TaskServices.UpdateTask(repos.task),
133+
create: new TaskServices.CreateTask(repos.task, repos.user),
134+
get: new TaskServices.GetTask(repos.task, repos.user),
135+
getTasks: new TaskServices.GetTasks(repos.task, repos.user),
136+
remove: new TaskServices.DeleteTask(repos.task, repos.user),
137+
update: new TaskServices.UpdateTask(repos.task, repos.user),
128138
},
129139
};

backend/src/core/helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ApiError, ErrorCode } from "../application/types";
22
import { EntityNotFoundError, ExpiredError } from "../config/error";
33
import { UserType } from "./entities/user";
4-
import { UserRepositoryTypeORM } from "../infrastructure/repositories/userRepositoryTypeORM";
4+
import { IUserRepository } from "./repositories/userRepositoryInterface";
55

66
/**
77
* @description Tiny helper to wrap repo calls and handle errors.
@@ -39,6 +39,7 @@ export async function tryRepoEntityOperation<T>(
3939

4040
export async function validateUserRights(
4141
userId: string,
42+
userRepository: IUserRepository,
4243
supposedUserType?: UserType,
4344
supposedUserId?: string,
4445
): Promise<void> {
@@ -48,8 +49,7 @@ export async function validateUserRights(
4849
message: `User with id ${userId} can not access this information`,
4950
};
5051
}
51-
const userRes = new UserRepositoryTypeORM();
52-
const user = await userRes.getById(userId);
52+
const user = await userRepository.getById(userId);
5353
if (supposedUserType && user.userType !== supposedUserType) {
5454
throw {
5555
code: ErrorCode.FORBIDDEN,
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { Service } from "../../../config/service";
22
import { IAssignmentRepository } from "../../repositories/assignmentRepositoryInterface";
3+
import { IUserRepository } from "../../repositories/userRepositoryInterface";
34

45
/**
56
* Abstract class for Assignment Services
67
*/
78
export abstract class AssignmentService<T> implements Service<T> {
8-
constructor(protected assignmentRepository: IAssignmentRepository) {}
9+
constructor(
10+
protected assignmentRepository: IAssignmentRepository,
11+
protected userRepository: IUserRepository,
12+
) {}
913
abstract execute(userId: string, input: T): Promise<object>;
1014
}

backend/src/core/services/assignment/createAssignment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class CreateAssignment extends AssignmentService<CreateAssignmentInput> {
1818
* @throws {ApiError} If the class with the given classId is not found or if the creation fails.
1919
*/
2020
async execute(userId: string, input: CreateAssignmentInput): Promise<object> {
21-
await validateUserRights(userId, UserType.TEACHER);
21+
await validateUserRights(userId, this.userRepository, UserType.TEACHER, undefined);
2222

2323
const assignment: Assignment = new Assignment(
2424
input.classId,

backend/src/core/services/assignment/deleteAssignment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class DeleteAssignment extends AssignmentService<DeleteAssignmentInput> {
1717
* @throws {ApiError} If the assignment with the given id is not found.
1818
*/
1919
async execute(userId: string, input: DeleteAssignmentInput): Promise<object> {
20-
await validateUserRights(userId, UserType.TEACHER);
20+
await validateUserRights(userId, this.userRepository, UserType.TEACHER, undefined);
2121

2222
await tryRepoEntityOperation(this.assignmentRepository.delete(input.id), "Assignment", input.id, true);
2323
return {};

backend/src/core/services/assignment/getUserAssignments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class GetUserAssignments extends AssignmentService<GetUserAssignmentsInpu
1717
* @throws {ApiError} If the user with the given id is not found.
1818
*/
1919
async execute(userId: string, input: GetUserAssignmentsInput): Promise<object> {
20-
await validateUserRights(userId, undefined, input.idParent);
20+
await validateUserRights(userId, this.userRepository, undefined, input.idParent);
2121

2222
const assignments: Assignment[] = await tryRepoEntityOperation(
2323
this.assignmentRepository.getByUserId(input.idParent),

backend/src/core/services/assignment/updateAssignment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class UpdateAssignment extends AssignmentService<UpdateAssignmentInput> {
1717
* @throws {ApiError} If the assignment with the given id is not found.
1818
*/
1919
async execute(userId: string, input: UpdateAssignmentInput): Promise<object> {
20-
await validateUserRights(userId, UserType.TEACHER);
20+
await validateUserRights(userId, this.userRepository, UserType.TEACHER, undefined);
2121

2222
const assignment = await this.assignmentRepository.getById(input.id);
2323

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { Service } from "../../../config/service";
22
import { IClassRepository } from "../../repositories/classRepositoryInterface";
3+
import { IUserRepository } from "../../repositories/userRepositoryInterface";
34

45
/**
56
* Class used for every service-implementation. They all need a class repository.
67
*/
78
export abstract class ClassBaseService<T> implements Service<T> {
8-
constructor(protected classRepository: IClassRepository) {}
9+
constructor(
10+
protected classRepository: IClassRepository,
11+
protected userRepository: IUserRepository,
12+
) {}
913
abstract execute(userId: string, input: T): Promise<object>;
1014
}

backend/src/core/services/class/createClass.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class CreateClass extends ClassBaseService<CreateClassInput> {
1515
* @throws {ApiError} If the class with the given teacherId is not found or if the creation fails.
1616
*/
1717
async execute(userId: string, input: CreateClassInput): Promise<object> {
18-
await validateUserRights(userId, UserType.TEACHER);
18+
await validateUserRights(userId, this.userRepository, UserType.TEACHER, undefined);
1919

2020
const newClass = new Class(input.name, input.description, input.targetAudience, input.teacherId);
2121

backend/src/core/services/class/deleteClass.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class DeleteClass extends ClassBaseService<DeleteClassInput> {
1414
* @throws {ApiError} If the class with the given id is not found.
1515
*/
1616
async execute(userId: string, input: DeleteClassInput): Promise<object> {
17-
await validateUserRights(userId, UserType.TEACHER);
17+
await validateUserRights(userId, this.userRepository, UserType.TEACHER, undefined);
1818
await tryRepoEntityOperation(this.classRepository.delete(input.id), "Class", input.id, true);
1919
return {};
2020
}

0 commit comments

Comments
 (0)