Skip to content

Commit

Permalink
fix(server): E11000 on problem limit
Browse files Browse the repository at this point in the history
  • Loading branch information
thezzisu committed Mar 31, 2024
1 parent 12f79e0 commit 778909a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .yarn/versions/4a42e70b.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@aoi-js/server": 1.1.0-alpha.7
40 changes: 25 additions & 15 deletions apps/server/src/routes/problem/scoped.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SProblemConfigSchema } from '@aoi-js/common'
import { BSON } from 'mongodb'
import { BSON, MongoServerError } from 'mongodb'

import { PROBLEM_CAPS, ORG_CAPS, SolutionState } from '../../db/index.js'
import { getUploadUrl, solutionDataKey } from '../../oss/index.js'
Expand Down Expand Up @@ -144,20 +144,30 @@ export const problemScopedRoutes = defineRoutes(async (s) => {
}

const newSolutionId = new BSON.UUID()
const { modifiedCount, upsertedCount } = await problemStatuses.updateOne(
{
userId: req.user.userId,
problemId: ctx._problemId,
solutionCount: maxSolutionCount ? { $lt: maxSolutionCount } : undefined
},
{
$inc: { solutionCount: 1 },
$set: { lastSolutionId: newSolutionId, lastSolutionScore: 0, lastSolutionStatus: '' },
$setOnInsert: { _id: new BSON.UUID() }
},
{ upsert: true, ignoreUndefined: true }
)
if (!(modifiedCount + upsertedCount)) return rep.preconditionFailed('Solution limit reached')
try {
const { modifiedCount, upsertedCount } = await problemStatuses.updateOne(
{
userId: req.user.userId,
problemId: ctx._problemId,
solutionCount: maxSolutionCount ? { $lt: maxSolutionCount } : undefined
},
{
$inc: { solutionCount: 1 },
$set: { lastSolutionId: newSolutionId, lastSolutionScore: 0, lastSolutionStatus: '' },
$setOnInsert: { _id: new BSON.UUID() }
},
{ upsert: true, ignoreUndefined: true }
)
if (!(modifiedCount + upsertedCount)) {
return rep.preconditionFailed('Solution limit reached')
}
} catch (err) {
// E11000 duplicate key error
if (err instanceof MongoServerError && err.code === 11000) {
return rep.preconditionFailed('Solution limit reached')
}
throw err
}

const { insertedId } = await solutions.insertOne(
{
Expand Down

0 comments on commit 778909a

Please sign in to comment.