From a440bd12a2ad28fd238d6069cb55f817ea9c767a Mon Sep 17 00:00:00 2001 From: Jaehyeon Kim Date: Thu, 5 Sep 2024 16:50:01 +0900 Subject: [PATCH] feat(be): add accepted-rate calculating logic when submission created (#2065) --- .../src/submission/submission-sub.service.ts | 13 ++++++++++++- .../test/submission-sub.service.spec.ts | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/apps/backend/apps/client/src/submission/submission-sub.service.ts b/apps/backend/apps/client/src/submission/submission-sub.service.ts index 0d54a4e462..b0cd8dabeb 100644 --- a/apps/backend/apps/client/src/submission/submission-sub.service.ts +++ b/apps/backend/apps/client/src/submission/submission-sub.service.ts @@ -336,11 +336,22 @@ export class SubmissionSubscriptionService implements OnModuleInit { } } + const problem = await this.prisma.problem.findFirstOrThrow({ + where: { + id + } + }) + await this.prisma.problem.update({ where: { id }, - data + data: { + ...data, + acceptedRate: isAccepted + ? (problem.acceptedCount + 1) / (problem.submissionCount + 1) + : problem.acceptedCount / (problem.submissionCount + 1) + } }) } } diff --git a/apps/backend/apps/client/src/submission/test/submission-sub.service.spec.ts b/apps/backend/apps/client/src/submission/test/submission-sub.service.spec.ts index e4c5f9d581..939020a32e 100644 --- a/apps/backend/apps/client/src/submission/test/submission-sub.service.spec.ts +++ b/apps/backend/apps/client/src/submission/test/submission-sub.service.spec.ts @@ -18,6 +18,7 @@ import { } from '@libs/constants' import { UnprocessableDataException } from '@libs/exception' import { PrismaService } from '@libs/prisma' +import { problems } from '@admin/problem/mock/mock' import { contestRecord } from '../mock/contestRecord.mock' import { submissions } from '../mock/submission.mock' import { submissionResults } from '../mock/submissionResult.mock' @@ -75,7 +76,8 @@ const db = { findFirstOrThrow: mockFunc }, problem: { - update: mockFunc + update: mockFunc, + findFirstOrThrow: mockFunc } } @@ -552,6 +554,7 @@ describe('SubmissionSubscriptionService', () => { describe('updateProblemAccepted', () => { it('should update submissionCount', async () => { const updateSpy = sandbox.stub(db.problem, 'update').resolves() + sandbox.stub(db.problem, 'findFirstOrThrow').resolves(problems[0]) const id = 1 const isAccepted = false @@ -564,7 +567,9 @@ describe('SubmissionSubscriptionService', () => { data: { submissionCount: { increment: 1 - } + }, + acceptedRate: + problems[0].acceptedCount / (problems[0].submissionCount + 1) } }) ).to.be.true @@ -572,6 +577,7 @@ describe('SubmissionSubscriptionService', () => { it('should update submissionCount and acceptedCount', async () => { const updateSpy = sandbox.stub(db.problem, 'update').resolves() + sandbox.stub(db.problem, 'findFirstOrThrow').resolves(problems[0]) const id = 1 const isAccepted = true @@ -587,7 +593,10 @@ describe('SubmissionSubscriptionService', () => { }, acceptedCount: { increment: 1 - } + }, + acceptedRate: + (problems[0].acceptedCount + 1) / + (problems[0].submissionCount + 1) } }) ).to.be.true