Skip to content

Commit 23669a8

Browse files
author
Your Name
committed
wip
1 parent df16c8c commit 23669a8

1 file changed

Lines changed: 30 additions & 13 deletions

File tree

backend/src/services/rating-service.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ import { Inject, Service, Token } from "typedi";
33
import { Repository } from "typeorm";
44
import { IService } from ".";
55
import { DatabaseServiceToken, IDatabaseService } from "./database-service";
6+
import { ISettingsService, SettingsServiceToken } from "./settings-service";
67
import { Rating } from "../entities/rating";
78
import {
8-
RatingResponseDTO,
9+
RatingDTO,
10+
CriteriaDTO,
911
convertBetweenEntityAndDTO,
1012
} from "../controllers/dto";
1113
import { User } from "../entities/user";
1214
import { Team } from "../entities/team";
1315
import { Project } from "../entities/project";
1416
import { Criteria } from "../entities/criteria";
15-
import { Rating } from "../entities/rating";
1617

1718
export interface IRatingService extends IService {
1819
/**
@@ -22,15 +23,15 @@ export interface IRatingService extends IService {
2223
/**
2324
* Create new rating
2425
*/
25-
createRating(rating: Rating): Promise<Rating>;
26+
createRating(rating: Rating, user: User): Promise<Rating>;
2627
/**
2728
* Update rating
2829
*/
29-
updateRating(rating: Rating): Promise<Rating>;
30+
updateRating(rating: Rating, user: User): Promise<Rating>;
3031
/**
3132
* Get rating by id
3233
*/
33-
getRatingByID(id: number): Promise<RatingResponseDTO | undefined>;
34+
getRatingByID(id: number): Promise<RatingDTO | undefined>;
3435
/**
3536
* Delete single rating by id
3637
*/
@@ -52,7 +53,7 @@ export interface IRatingService extends IService {
5253
/**
5354
* Get criteria by id
5455
*/
55-
getCriteriaByID(id: number): Promise<CriteriaResponseDTO | undefined>;
56+
getCriteriaByID(id: number): Promise<CriteriaDTO | undefined>;
5657
/**
5758
* Delete single criteria by id
5859
*/
@@ -76,13 +77,16 @@ export class RatingService implements IRatingService {
7677

7778
public constructor(
7879
@Inject(DatabaseServiceToken) private readonly _database: IDatabaseService,
80+
@Inject(SettingsServiceToken) private readonly _settings: ISettingsService,
7981
) {}
8082

8183
/**
8284
* Sets up the user service.
8385
*/
8486
public async bootstrap(): Promise<void> {
8587
this._ratings = this._database.getRepository(Rating);
88+
this._projects = this._database.getRepository(Project);
89+
this._teams = this._database.getRepository(Team);
8690
this._users = this._database.getRepository(User);
8791
}
8892

@@ -104,7 +108,10 @@ export class RatingService implements IRatingService {
104108

105109
// TODO only if user matches
106110
await this.checkPermission(rating, user);
107-
const originRatingUser = originRating.users;
111+
if (!originRating) {
112+
throw new ForbiddenError("Rating not found");
113+
}
114+
const originRatingUser = originRating.user;
108115
if (user.id != originRatingUser.id) {
109116
throw new Error("")
110117
}
@@ -116,7 +123,7 @@ export class RatingService implements IRatingService {
116123
* Creates a rating.
117124
* @param rating The rating to create
118125
*/
119-
public async createRating(rating: Rating): Promise<Rating> {
126+
public async createRating(rating: Rating, user: User): Promise<Rating> {
120127
// TODO validate
121128
this.checkPermission(rating, user);
122129
return this._ratings.save(rating);
@@ -126,7 +133,7 @@ export class RatingService implements IRatingService {
126133
* Gets a rating by its id.
127134
* @param id The id of the rating
128135
*/
129-
public async getRatingByID(id: number): Promise<RatingResponseDTO | undefined> {
136+
public async getRatingByID(id: number): Promise<RatingDTO | undefined> {
130137
const rating = await this._ratings.findOneBy({ id });
131138
return rating || undefined;
132139
}
@@ -138,7 +145,11 @@ export class RatingService implements IRatingService {
138145
public async deleteRatingByID(id: number, currentUserId: User): Promise<void> {
139146
const rating = await this._ratings.findOneBy({ id });
140147

141-
await this.checkPermission(rating, user);
148+
if (!rating) {
149+
throw new ForbiddenError("Rating not found");
150+
}
151+
152+
await this.checkPermission(rating, currentUserId);
142153

143154
await this._ratings.delete(id);
144155

@@ -154,14 +165,20 @@ export class RatingService implements IRatingService {
154165
throw new ForbiddenError("Cannot create rating due to application settings")
155166
}
156167

157-
const project = await await this._projects.findOneBy({ id: data.id });
168+
const project = await this._projects.findOneBy({ id: rating.project.id });
169+
if (!project) {
170+
throw new ForbiddenError("Project not found");
171+
}
158172
if (!project.allowRating) {
159173
// TODO test
160174
throw new ForbiddenError("Creating a rating for this project is not allowed")
161175
}
162176

163-
const team = await this._teams.findOneById(project.teamId)
164-
if (team.users.inclues(user.id)) {
177+
const team = await this._teams.findOneBy({ id: project.team.id })
178+
if (!team) {
179+
throw new ForbiddenError("Team not found");
180+
}
181+
if (team.users.includes(user.id)) {
165182
// TODO test
166183
throw new ForbiddenError("You can't rate your own project")
167184
}

0 commit comments

Comments
 (0)