|
| 1 | +import { Inject, Service, Token } from "typedi"; |
| 2 | +import { Repository } from "typeorm"; |
| 3 | +import { IService } from "."; |
| 4 | +import { DatabaseServiceToken, IDatabaseService } from "./database-service"; |
| 5 | +import { Rating } from "../entities/rating"; |
| 6 | +import { |
| 7 | + RatingResponseDTO, |
| 8 | + convertBetweenEntityAndDTO, |
| 9 | +} from "../controllers/dto"; |
| 10 | +import { User } from "../entities/user"; |
| 11 | + |
| 12 | +export interface IRatingService extends IService { |
| 13 | + /** |
| 14 | + * Get all ratings |
| 15 | + */ |
| 16 | + getAllRatings(): Promise<readonly Rating[]>; |
| 17 | + /** |
| 18 | + * Create new rating |
| 19 | + */ |
| 20 | + createRating(rating: Rating): Promise<Rating>; |
| 21 | + /** |
| 22 | + * Update rating |
| 23 | + */ |
| 24 | + updateRating(rating: Rating): Promise<Rating>; |
| 25 | + /** |
| 26 | + * Get rating by id |
| 27 | + */ |
| 28 | + getRatingByID(id: number): Promise<RatingResponseDTO | undefined>; |
| 29 | + /** |
| 30 | + * Delete single rating by id |
| 31 | + */ |
| 32 | + deleteRatingByID(id: number, currentUserId: User): Promise<void>; |
| 33 | + /** |
| 34 | + * Request to join a rating |
| 35 | + */ |
| 36 | + requestToJoinRating(ratingId: number, user: User): Promise<void>; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * A token used to inject a concrete user service. |
| 41 | + */ |
| 42 | +export const RatingServiceToken = new Token<IRatingService>(); |
| 43 | + |
| 44 | +/** |
| 45 | + * A service to handle users. |
| 46 | + */ |
| 47 | +@Service(RatingServiceToken) |
| 48 | +export class RatingService implements IRatingService { |
| 49 | + private _ratings!: Repository<Rating>; |
| 50 | + private _users!: Repository<User>; |
| 51 | + |
| 52 | + public constructor( |
| 53 | + @Inject(DatabaseServiceToken) private readonly _database: IDatabaseService, |
| 54 | + ) {} |
| 55 | + |
| 56 | + /** |
| 57 | + * Sets up the user service. |
| 58 | + */ |
| 59 | + public async bootstrap(): Promise<void> { |
| 60 | + this._ratings = this._database.getRepository(Rating); |
| 61 | + this._users = this._database.getRepository(User); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Gets all ratings. |
| 66 | + */ |
| 67 | + public async getAllRatings(): Promise<readonly Rating[]> { |
| 68 | + return this._database.getRepository(Rating).find(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Updates a rating. |
| 73 | + * @param rating The rating to update |
| 74 | + */ |
| 75 | + public async updateRating(rating: Rating, user: User): Promise<Rating> { |
| 76 | + // TODO validate, throw Errors |
| 77 | + |
| 78 | + const originRating = await this._ratings.findOneBy({ id: rating.id }); |
| 79 | + |
| 80 | + // TODO only if user matches |
| 81 | + await this.checkPermission(rating, user); |
| 82 | + const originRatingUser = originRating.users; |
| 83 | + if (user.id != originRatingUser.id) { |
| 84 | + throw new Error("") |
| 85 | + } |
| 86 | + |
| 87 | + return this._ratings.save(rating); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Creates a rating. |
| 92 | + * @param rating The rating to create |
| 93 | + */ |
| 94 | + public async createRating(rating: Rating): Promise<Rating> { |
| 95 | + // TODO validate |
| 96 | + this.checkPermission(rating, user); |
| 97 | + return this._ratings.save(rating); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Gets a rating by its id. |
| 102 | + * @param id The id of the rating |
| 103 | + */ |
| 104 | + public async getRatingByID(id: number): Promise<RatingResponseDTO | undefined> { |
| 105 | + const rating = await this._ratings.findOneBy({ id }); |
| 106 | + return rating || undefined; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Deletes a rating by its id. |
| 111 | + * @param id The id of the rating |
| 112 | + */ |
| 113 | + public async deleteRatingByID(id: number, currentUserId: User): Promise<void> { |
| 114 | + const rating = await this._ratings.findOneBy({ id }); |
| 115 | + |
| 116 | + await this.checkPermission(rating, user); |
| 117 | + |
| 118 | + await this._ratings.delete(id); |
| 119 | + |
| 120 | + return Promise.resolve(); |
| 121 | + } |
| 122 | + |
| 123 | + private async checkPermission(rating: Rating, user: User): Promise<void> { |
| 124 | + // TODO use this._database.blabla instead of _settings and such |
| 125 | + |
| 126 | + const settings = await this._settings.getSettings(); |
| 127 | + if (!settings.allowRating) { |
| 128 | + // TODO test |
| 129 | + throw new ForbiddenError("Cannot create rating due to application settings") |
| 130 | + } |
| 131 | + |
| 132 | + const project = await this._projects.getProject(data.id); |
| 133 | + if (!project.allowRating) { |
| 134 | + // TODO test |
| 135 | + throw new ForbiddenError("Creating a rating for this project is not allowed") |
| 136 | + } |
| 137 | + |
| 138 | + const team = await this._teams.getTeamById(project.teamId) |
| 139 | + if (team.users.inclues(user.id)) { |
| 140 | + // TODO test |
| 141 | + throw new ForbiddenError("You can't rate your own project") |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments