Skip to content

Commit 92562a9

Browse files
authored
Merge pull request #114 from hackaburg/copilot/update-make-project-ratable-endpoint
Merge `allowRating` control into PUT /projects/project/:id; restrict to admins only
2 parents 81ac6a0 + c4c38a7 commit 92562a9

2 files changed

Lines changed: 19 additions & 23 deletions

File tree

backend/src/controllers/rating-controller.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,6 @@ export class RatingController {
2323
@Inject(RatingServiceToken) private readonly _ratings: IRatingService,
2424
) {}
2525

26-
/**
27-
* Allow users to rate a specific project (if ratings are enabled in the application
28-
* settings).
29-
*
30-
* By using the application setting, admins can prepare the projects that can be
31-
* rated, and then allow all of them at the same time. And when the rating is closed,
32-
* disable all of them at the same time. This is done in the settings-controller.
33-
*
34-
* TODO probably move to the project controller, allow changing this setting only
35-
* if an admin
36-
*
37-
* TODO write test that the attribute cannot be changed by the project put endpoint
38-
* by regular users
39-
*/
40-
@Post("/make-project-ratable")
41-
@Authorized(UserRole.Root)
42-
public async enableRatingForProject(): Promise<void> {
43-
// TODO set allowRating of project
44-
}
45-
4626
/**
4727
* Rate a project
4828
*

backend/src/services/project-service.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { NotFoundError } from "routing-controllers";
12
import { Inject, Service, Token } from "typedi";
23
import { Repository } from "typeorm";
34
import { IService } from ".";
@@ -8,6 +9,7 @@ import {
89
convertBetweenEntityAndDTO,
910
} from "../controllers/dto";
1011
import { User } from "../entities/user";
12+
import { UserRole } from "../entities/user-role";
1113

1214
/**
1315
* An interface describing user handling.
@@ -72,9 +74,23 @@ export class ProjectService implements IProjectService {
7274
* @param project The project to update
7375
*/
7476
public async updateProject(project: Project, user: User): Promise<Project> {
75-
// TODO
76-
await this.checkPermission(project, user);
77-
// TODO allow changing allowRating only if admin
77+
const existing = await this._projects.findOneBy({ id: project.id });
78+
79+
if (!existing) {
80+
throw new NotFoundError();
81+
}
82+
83+
await this.checkPermission(existing, user);
84+
85+
existing.title = project.title;
86+
existing.description = project.description;
87+
88+
// Only admins may change allowRating
89+
if (user.role === UserRole.Root) {
90+
existing.allowRating = project.allowRating;
91+
}
92+
93+
return this._projects.save(existing);
7894
}
7995

8096
/**

0 commit comments

Comments
 (0)