Skip to content

Commit

Permalink
fix(be): change import problem transaction error logic (#2020)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimin9038 authored Aug 27, 2024
1 parent d595421 commit 0c41588
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 99 deletions.
13 changes: 5 additions & 8 deletions apps/backend/apps/admin/src/contest/contest.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ContestProblem, Group, ContestRecord } from '@generated'
import { Problem } from '@generated'
import { Contest } from '@generated'
import { faker } from '@faker-js/faker'
import { Prisma } from '@prisma/client'
import type { Cache } from 'cache-manager'
import { expect } from 'chai'
import { stub } from 'sinon'
Expand Down Expand Up @@ -181,14 +180,16 @@ const db = {
},
contestProblem: {
create: stub().resolves(ContestProblem),
findMany: stub().resolves([ContestProblem])
findMany: stub().resolves([ContestProblem]),
findFirst: stub().resolves(ContestProblem)
},
contestRecord: {
findMany: stub().resolves([ContestRecord]),
create: stub().resolves(ContestRecord)
},
problem: {
update: stub().resolves(Problem),
updateMany: stub().resolves([Problem]),
findFirstOrThrow: stub().resolves(Problem)
},
group: {
Expand Down Expand Up @@ -327,6 +328,7 @@ describe('ContestService', () => {
db.contest.findUnique.resolves(contest)
db.problem.update.resolves(problem)
db.contestProblem.create.resolves(contestProblem)
db.contestProblem.findFirst.resolves(null)

const res = await Promise.all(
await service.importProblemsToContest(groupId, contestId, [
Expand All @@ -340,12 +342,7 @@ describe('ContestService', () => {
it('should return an empty array when the problem already exists in contest', async () => {
db.contest.findUnique.resolves(contest)
db.problem.update.resolves(problem)
db.contestProblem.create.throws(
new Prisma.PrismaClientKnownRequestError(
'ContestProblem already exists',
{ code: 'P2002', clientVersion: 'version' }
)
)
db.contestProblem.findFirst.resolves(ContestProblem)

const res = await service.importProblemsToContest(groupId, contestId, [
problemIdsWithScore
Expand Down
185 changes: 94 additions & 91 deletions apps/backend/apps/admin/src/contest/contest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,48 +359,55 @@ export class ContestService {
const contestProblems: ContestProblem[] = []

for (const { problemId, score } of problemIdsWithScore) {
try {
const [contestProblem] = await this.prisma.$transaction([
this.prisma.contestProblem.create({
data: {
// 원래 id: 'temp'이었는데, contestProblem db schema field가 바뀌어서
// 임시 방편으로 order: 0으로 설정합니다.
order: 0,
contestId,
problemId,
score
}
}),
this.prisma.problem.update({
where: {
id: problemId,
OR: [
{
visibleLockTime: {
equals: MIN_DATE
}
},
{
visibleLockTime: {
equals: MAX_DATE
}
},
{
visibleLockTime: {
lte: contest.endTime
}
}
]
},
data: {
visibleLockTime: contest.endTime
}
})
])
contestProblems.push(contestProblem)
} catch (error) {
const isProblemAlreadyImported =
await this.prisma.contestProblem.findFirst({
where: {
contestId,
problemId
}
})
if (isProblemAlreadyImported) {
continue
}

const [contestProblem] = await this.prisma.$transaction([
this.prisma.contestProblem.create({
data: {
// 원래 id: 'temp'이었는데, contestProblem db schema field가 바뀌어서
// 임시 방편으로 order: 0으로 설정합니다.
order: 0,
contestId,
problemId,
score
}
}),
this.prisma.problem.updateMany({
where: {
id: problemId,
OR: [
{
visibleLockTime: {
equals: MIN_DATE
}
},
{
visibleLockTime: {
equals: MAX_DATE
}
},
{
visibleLockTime: {
lte: contest.endTime
}
}
]
},
data: {
visibleLockTime: contest.endTime
}
})
])
contestProblems.push(contestProblem)
}

return contestProblems
Expand All @@ -424,64 +431,60 @@ export class ContestService {
const contestProblems: ContestProblem[] = []

for (const problemId of problemIds) {
try {
// 문제가 포함된 대회 중 가장 늦게 끝나는 대회의 종료시각으로 visibleLockTime 설정 (없을시 비공개 전환)
let visibleLockTime = MAX_DATE
// 문제가 포함된 대회 중 가장 늦게 끝나는 대회의 종료시각으로 visibleLockTime 설정 (없을시 비공개 전환)
let visibleLockTime = MAX_DATE

const contestIds = (
await this.prisma.contestProblem.findMany({
where: {
problemId
}
})
)
.filter((contestProblem) => contestProblem.contestId !== contestId)
.map((contestProblem) => contestProblem.contestId)
const contestIds = (
await this.prisma.contestProblem.findMany({
where: {
problemId
}
})
)
.filter((contestProblem) => contestProblem.contestId !== contestId)
.map((contestProblem) => contestProblem.contestId)

if (contestIds.length) {
const latestContest = await this.prisma.contest.findFirstOrThrow({
where: {
id: {
in: contestIds
}
},
orderBy: {
endTime: 'desc'
},
select: {
endTime: true
if (contestIds.length) {
const latestContest = await this.prisma.contest.findFirstOrThrow({
where: {
id: {
in: contestIds
}
})
visibleLockTime = latestContest.endTime
}
},
orderBy: {
endTime: 'desc'
},
select: {
endTime: true
}
})
visibleLockTime = latestContest.endTime
}

const [, contestProblem] = await this.prisma.$transaction([
this.prisma.problem.updateMany({
where: {
id: problemId,
visibleLockTime: {
lte: contest.endTime
}
},
data: {
visibleLockTime
const [, contestProblem] = await this.prisma.$transaction([
this.prisma.problem.updateMany({
where: {
id: problemId,
visibleLockTime: {
lte: contest.endTime
}
}),
this.prisma.contestProblem.delete({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
contestId_problemId: {
contestId,
problemId
}
},
data: {
visibleLockTime
}
}),
this.prisma.contestProblem.delete({
where: {
// eslint-disable-next-line @typescript-eslint/naming-convention
contestId_problemId: {
contestId,
problemId
}
})
])
}
})
])

contestProblems.push(contestProblem)
} catch (error) {
continue
}
contestProblems.push(contestProblem)
}

return contestProblems
Expand Down

0 comments on commit 0c41588

Please sign in to comment.