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

feat(be): redact max-score, score when is-judge-result-visible of contest is false #2071

Merged
merged 1 commit into from
Sep 5, 2024
Merged
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
4 changes: 2 additions & 2 deletions apps/backend/apps/client/src/contest/contest.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<Contest>
if (userId) {
const hasRegistered = await this.prisma.contestRecord.findFirst({
where: { userId, contestId: id }
Expand Down
32 changes: 24 additions & 8 deletions apps/backend/apps/client/src/problem/problem.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
16 changes: 9 additions & 7 deletions apps/backend/apps/client/src/problem/problem.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
)
Expand Down Expand Up @@ -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
}
})
Expand All @@ -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.')
}

Expand Down