From 0ab318bef055e4d45ff7392b445730cae92ecf03 Mon Sep 17 00:00:00 2001 From: Jaehyeon Kim <65964601+Jaehyeon1020@users.noreply.github.com> Date: Wed, 4 Sep 2024 13:16:51 +0000 Subject: [PATCH] feat(be): redact max-score, score when is-judge-result-visible of contest is false --- .../client/src/contest/contest.service.ts | 4 +-- .../src/problem/problem.service.spec.ts | 32 ++++++++++++++----- .../client/src/problem/problem.service.ts | 16 ++++++---- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/apps/backend/apps/client/src/contest/contest.service.ts b/apps/backend/apps/client/src/contest/contest.service.ts index 55278c5d8f..1232b7ac61 100644 --- a/apps/backend/apps/client/src/contest/contest.service.ts +++ b/apps/backend/apps/client/src/contest/contest.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@nestjs/common' -import { Prisma } from '@prisma/client' +import { Prisma, type Contest } from '@prisma/client' import { OPEN_SPACE_ID } from '@libs/constants' import { ConflictFoundException, @@ -328,7 +328,7 @@ export class ContestService { // check if the user has already registered this contest // initial value is false let isRegistered = false - let contest + let contest: Partial if (userId) { const hasRegistered = await this.prisma.contestRecord.findFirst({ where: { userId, contestId: id } diff --git a/apps/backend/apps/client/src/problem/problem.service.spec.ts b/apps/backend/apps/client/src/problem/problem.service.spec.ts index 61368d3360..6a16389795 100644 --- a/apps/backend/apps/client/src/problem/problem.service.spec.ts +++ b/apps/backend/apps/client/src/problem/problem.service.spec.ts @@ -286,7 +286,9 @@ describe('ContestProblemService', () => { getContestSpy.resolves({ startTime: faker.date.past(), endTime: faker.date.future(), - isRegistered: true + isRegistered: true, + invitationCodeExists: true, + isJudgeResultVisible: true }) db.contestProblem.findMany.resolves(mockContestProblems) db.submission.findMany.resolves([]) @@ -309,7 +311,9 @@ describe('ContestProblemService', () => { getContestSpy.resolves({ startTime: faker.date.past(), endTime: faker.date.future(), - isRegistered: true + isRegistered: true, + invitationCodeExists: true, + isJudgeResultVisible: true }) db.contestProblem.findMany.resolves(mockContestProblems) @@ -347,7 +351,9 @@ describe('ContestProblemService', () => { getContestSpy.resolves({ startTime: faker.date.future(), endTime: faker.date.future(), - isRegistered: true + isRegistered: true, + isJudgeResultVisible: true, + invitationCodeExists: true }) db.contestProblem.findMany.resolves(mockContestProblems) @@ -361,7 +367,9 @@ describe('ContestProblemService', () => { getContestSpy.resolves({ startTime: faker.date.past(), endTime: faker.date.future(), - isRegistered: false + isRegistered: false, + isJudgeResultVisible: true, + invitationCodeExists: true }) db.contestProblem.findMany.resolves(mockContestProblems) @@ -378,7 +386,9 @@ describe('ContestProblemService', () => { getContestSpy.resolves({ startTime: faker.date.past(), endTime: faker.date.future(), - isRegistered: true + isRegistered: true, + isJudgeResultVisible: true, + invitationCodeExists: true }) db.contestProblem.findUniqueOrThrow.resolves(mockContestProblem) @@ -401,7 +411,9 @@ describe('ContestProblemService', () => { getContestSpy.resolves({ startTime: faker.date.past(), endTime: faker.date.future(), - isRegistered: true + isRegistered: true, + isJudgeResultVisible: true, + invitationCodeExists: true }) db.contestProblem.findUniqueOrThrow.resolves(mockContestProblem) @@ -434,7 +446,9 @@ describe('ContestProblemService', () => { getContestSpy.resolves({ startTime: faker.date.future(), endTime: faker.date.future(), - isRegistered: true + isRegistered: true, + isJudgeResultVisible: true, + invitationCodeExists: true }) db.contestProblem.findUniqueOrThrow.resolves(mockContestProblem) await expect( @@ -447,7 +461,9 @@ describe('ContestProblemService', () => { getContestSpy.resolves({ startTime: faker.date.past(), endTime: faker.date.future(), - isRegistered: false + isRegistered: false, + isJudgeResultVisible: true, + invitationCodeExists: true }) db.contestProblem.findUniqueOrThrow.resolves(mockContestProblem) await expect( diff --git a/apps/backend/apps/client/src/problem/problem.service.ts b/apps/backend/apps/client/src/problem/problem.service.ts index 970a5565ba..af7370c946 100644 --- a/apps/backend/apps/client/src/problem/problem.service.ts +++ b/apps/backend/apps/client/src/problem/problem.service.ts @@ -97,11 +97,11 @@ export class ContestProblemService { userId ) const now = new Date() - if (contest.isRegistered && contest.startTime > now) { + if (contest.isRegistered && contest.startTime! > now) { throw new ForbiddenAccessException( 'Cannot access problems before the contest starts.' ) - } else if (!contest.isRegistered && contest.endTime > now) { + } else if (!contest.isRegistered && contest.endTime! > now) { throw new ForbiddenAccessException( 'Register to access the problems of this contest.' ) @@ -137,15 +137,17 @@ export class ContestProblemService { if (!submission) { return { ...contestProblem, - maxScore: contestProblem.score, + maxScore: contest.isJudgeResultVisible ? contestProblem.score : null, score: null, submissionTime: null } } return { ...contestProblem, - maxScore: contestProblem.score, - score: ((submission.score * contestProblem.score) / 100).toFixed(0), + maxScore: contest.isJudgeResultVisible ? contestProblem.score : null, + score: contest.isJudgeResultVisible + ? ((submission.score * contestProblem.score) / 100).toFixed(0) + : null, submissionTime: submission.createTime ?? null } }) @@ -171,11 +173,11 @@ export class ContestProblemService { userId ) const now = new Date() - if (contest.isRegistered && contest.startTime > now) { + if (contest.isRegistered && contest.startTime! > now) { throw new ForbiddenAccessException( 'Cannot access to problems before the contest starts.' ) - } else if (!contest.isRegistered && contest.endTime > now) { + } else if (!contest.isRegistered && contest.endTime! > now) { throw new ForbiddenAccessException('Register to access this problem.') }