Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: prevent review submission for 2025-1 lectures #162

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/common/decorators/prohibit-review.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { SetMetadata } from '@nestjs/common';

export const IS_REVIEW_PROHIBITED_KEY = 'isReviewProhibited';
export const ReviewProhibited = () =>
SetMetadata(IS_REVIEW_PROHIBITED_KEY, true);
11 changes: 8 additions & 3 deletions src/modules/auth/auth.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IsPublicCommand } from './command/isPublic.command';
import { JwtCommand } from './command/jwt.command';
import { SidCommand } from './command/sid.command';
import { SyncApiKeyCommand } from './command/syncApiKey.command';
import { IsReviewProhibitedCommand } from '@src/modules/auth/command/isReviewProhibited.command';

@Injectable()
export class AuthConfig {
Expand All @@ -13,6 +14,7 @@ export class AuthConfig {
private readonly sidCommand: SidCommand,
private readonly isPublicCommand: IsPublicCommand,
private readonly syncApiKeyCommand: SyncApiKeyCommand,
private readonly isReviewProhibitedCommand: IsReviewProhibitedCommand,
) {}

public async config(env: string) {
Expand All @@ -27,21 +29,24 @@ export class AuthConfig {
.register(this.isPublicCommand)
.register(this.sidCommand)
.register(this.jwtCommand)
.register(this.syncApiKeyCommand);
.register(this.syncApiKeyCommand)
.register(this.isReviewProhibitedCommand);
};

private getDevGuardConfig = () => {
return this.authChain
.register(this.isPublicCommand)
.register(this.sidCommand)
.register(this.jwtCommand)
.register(this.syncApiKeyCommand);
.register(this.syncApiKeyCommand)
.register(this.isReviewProhibitedCommand);
};

private getProdGuardConfig = () => {
return this.authChain
.register(this.jwtCommand)
.register(this.isPublicCommand)
.register(this.syncApiKeyCommand);
.register(this.syncApiKeyCommand)
.register(this.isReviewProhibitedCommand);
};
}
4 changes: 4 additions & 0 deletions src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { JwtCommand } from './command/jwt.command';
import { SidCommand } from './command/sid.command';
import { SyncApiKeyCommand } from './command/syncApiKey.command';
import { JwtCookieStrategy } from './strategy/jwt-cookie.strategy';
import { IsReviewProhibitedCommand } from '@src/modules/auth/command/isReviewProhibited.command';
import { LecturesService } from '@src/modules/lectures/lectures.service';

@Module({
imports: [
Expand All @@ -28,11 +30,13 @@ import { JwtCookieStrategy } from './strategy/jwt-cookie.strategy';
JwtCookieStrategy,
UserService,
UserRepository,
LecturesService,
AuthChain,
IsPublicCommand,
JwtCommand,
SidCommand,
SyncApiKeyCommand,
IsReviewProhibitedCommand,
AuthConfig,
],
exports: [AuthService, AuthConfig, AuthChain],
Expand Down
48 changes: 48 additions & 0 deletions src/modules/auth/command/isReviewProhibited.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ExecutionContext, Injectable } from '@nestjs/common';
import { IS_REVIEW_PROHIBITED_KEY } from '../../../common/decorators/prohibit-review.decorator';
import { Reflector } from '@nestjs/core';
import { AuthCommand, AuthResult } from '../auth.command';
import { LecturesService } from '@src/modules/lectures/lectures.service';
import { Request } from 'express';

@Injectable()
export class IsReviewProhibitedCommand implements AuthCommand {
constructor(
private reflector: Reflector,
private lectureService: LecturesService,
) {}

public async next(
context: ExecutionContext,
prevResult: AuthResult,
): Promise<AuthResult> {
const isReviewProhibited = this.reflector.getAllAndOverride<boolean>(
IS_REVIEW_PROHIBITED_KEY,
[context.getHandler()],
);

const request = context.switchToHttp().getRequest<Request>();
const reviewsBody = request.body;

if (isReviewProhibited) {
try {
if (!reviewsBody || !reviewsBody.lecture) {
throw new Error('lecture info are not found from request');
}
const lecture = await this.lectureService.getLectureById(
reviewsBody.lecture,
);
// TODO: implement logic to replace hardcoded values
if (lecture.year == 2025 && lecture.semester == 1) {
prevResult.authorization = false;
}
return Promise.resolve(prevResult);
} catch (e) {
console.log(e);
return Promise.resolve(prevResult);
}
}

return Promise.resolve(prevResult);
}
}
2 changes: 2 additions & 0 deletions src/modules/reviews/reviews.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { IReview } from 'src/common/interfaces/IReview';
import { ReviewsService } from './reviews.service';
import { EReview } from '../../common/entities/EReview';
import EReviewVote = EReview.EReviewVote;
import { ReviewProhibited } from '@src/common/decorators/prohibit-review.decorator';

@Controller('api/reviews')
export class ReviewsController {
Expand All @@ -37,6 +38,7 @@ export class ReviewsController {
return result;
}

@ReviewProhibited()
@Post()
async createReviews(
@Body() reviewsBody: IReview.CreateDto,
Expand Down
4 changes: 2 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ const getPrismaConfig = (): Prisma.PrismaClientOptions => {
errorFormat: 'pretty',
log: [
// {
// emit: 'event',
// level: 'query',
// emit: 'event',
// level: 'query',
// },
{
emit: 'stdout',
Expand Down
Loading