Skip to content

Commit

Permalink
feat(be): add accepted-rate calculating logic when submission created (
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaehyeon1020 authored Sep 5, 2024
1 parent 9dbb5df commit a440bd1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -75,7 +76,8 @@ const db = {
findFirstOrThrow: mockFunc
},
problem: {
update: mockFunc
update: mockFunc,
findFirstOrThrow: mockFunc
}
}

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

Expand All @@ -564,14 +567,17 @@ describe('SubmissionSubscriptionService', () => {
data: {
submissionCount: {
increment: 1
}
},
acceptedRate:
problems[0].acceptedCount / (problems[0].submissionCount + 1)
}
})
).to.be.true
})

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

Expand All @@ -587,7 +593,10 @@ describe('SubmissionSubscriptionService', () => {
},
acceptedCount: {
increment: 1
}
},
acceptedRate:
(problems[0].acceptedCount + 1) /
(problems[0].submissionCount + 1)
}
})
).to.be.true
Expand Down

0 comments on commit a440bd1

Please sign in to comment.