Skip to content

Commit 3e1f860

Browse files
authored
chore: update Mongoose from 8.x to 9.x
Merge pull request #32 from net-escape/copilot/update-mongoose-dependency-backend
2 parents 2825559 + 68bddd3 commit 3e1f860

22 files changed

Lines changed: 136 additions & 162 deletions

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"koa-session": "^7.0.2",
4545
"koa-static": "^5.0.0",
4646
"lodash": "^4.17.21",
47-
"mongoose": "^8.20.4",
47+
"mongoose": "^9.0.2",
4848
"mongoose-paginate-v2": "^1.9.1",
4949
"pm2": "^6.0.14",
5050
"superagent": "^10.2.3",

backend/src/controllers/contest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const findContests = async (ctx: Context) => {
108108
if (role.manageContest) {
109109
showAll = true
110110
}
111-
courseDocId = course.id
111+
courseDocId = course._id
112112
}
113113

114114
const paginateOption = parsePaginateOption(opt, 20)
@@ -217,7 +217,7 @@ const createContest = async (ctx: Context) => {
217217
let courseDocId: Types.ObjectId | undefined
218218
if (opt.course) {
219219
const { course } = await loadCourse(ctx, opt.course)
220-
courseDocId = course.id
220+
courseDocId = course._id
221221
}
222222

223223
try {
@@ -258,7 +258,7 @@ const updateContest = async (ctx: Context) => {
258258
courseDocId = null
259259
} else {
260260
const { course } = await loadCourse(ctx, opt.course)
261-
courseDocId = course.id
261+
courseDocId = course._id
262262
}
263263
}
264264

backend/src/controllers/course.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const joinCourse = async (ctx: Context) => {
105105

106106
const profile = await loadProfile(ctx)
107107
const result = await courseService.updateCourseMember(
108-
course.id, profile.id,
108+
course._id, profile._id,
109109
{ ...role, basic: true },
110110
)
111111

@@ -166,7 +166,7 @@ const findCourseMembers = async (ctx: Context) => {
166166
const { page, pageSize } = parsePaginateOption(opt, 30, 200)
167167

168168
const response: Paginated<CourseMemberView>
169-
= await courseService.findCourseMembers(course.id, { page, pageSize })
169+
= await courseService.findCourseMembers(course._id, { page, pageSize })
170170
ctx.body = response
171171
}
172172

@@ -181,7 +181,7 @@ const getCourseMember = async (ctx: Context) => {
181181
return ctx.throw(400, 'Missing uid')
182182
}
183183

184-
const member = await courseService.getCourseMember(course.id, userId)
184+
const member = await courseService.getCourseMember(course._id, userId)
185185
if (!member) {
186186
return ctx.throw(...ERR_NOT_FOUND)
187187
}
@@ -227,8 +227,8 @@ const updateCourseMember = async (ctx: Context) => {
227227
}
228228

229229
const result = await courseService.updateCourseMember(
230-
course.id,
231-
user.id,
230+
course._id,
231+
user._id,
232232
newRole as CourseRole,
233233
)
234234
ctx.auditLog.info(`<Course:${course.courseId}> member <User:${userId}> updated by <User:${profile.uid}>`)
@@ -251,7 +251,7 @@ const removeCourseMember = async (ctx: Context) => {
251251
return ctx.throw(400, 'Cannot remove yourself from the course')
252252
}
253253

254-
const result = await courseService.removeCourseMember(course.id, userId)
254+
const result = await courseService.removeCourseMember(course._id, userId)
255255
const response: { success: boolean } = { success: result }
256256
ctx.auditLog.info(`<Course:${course.courseId}> member <User:${userId}> removed by <User:${profile.uid}>`)
257257
ctx.body = response
@@ -269,7 +269,7 @@ const addCourseProblems = async (ctx: Context) => {
269269
if (!problem) {
270270
return false
271271
}
272-
return await courseService.addCourseProblem(course.id, problem.id)
272+
return await courseService.addCourseProblem(course._id, problem._id)
273273
}))
274274

275275
const successCount = result.filter(v => v).length
@@ -291,15 +291,15 @@ const moveCourseProblem = async (ctx: Context) => {
291291
return ctx.throw(...ERR_INVALID_ID)
292292
}
293293
const result = await courseService.moveCourseProblem(
294-
course.id, problem.id, beforePos,
294+
course._id, problem._id, beforePos,
295295
)
296296
ctx.body = { success: result }
297297
}
298298

299299
const rearrangeCourseProblem = async (ctx: Context) => {
300300
const { course } = await loadCourse(ctx)
301301
try {
302-
await courseService.rearrangeCourseProblem(course.id)
302+
await courseService.rearrangeCourseProblem(course._id)
303303
ctx.body = { success: true }
304304
} catch (e: any) {
305305
ctx.throw(500, `Failed to rearrange course problems: ${e.message}`)
@@ -313,7 +313,7 @@ const removeCourseProblem = async (ctx: Context) => {
313313
if (!problem) {
314314
return ctx.throw(...ERR_INVALID_ID)
315315
}
316-
const result = await courseService.removeCourseProblem(course.id, problem.id)
316+
const result = await courseService.removeCourseProblem(course._id, problem._id)
317317
const profile = await loadProfile(ctx)
318318
ctx.auditLog.info(`<Course:${course.courseId}> removed <Problem:${problemId}> by <User:${profile.uid}>`)
319319
ctx.body = { success: result }

backend/src/controllers/problem.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export async function loadProblem (
7474
}
7575

7676
if (profile && await courseService.hasProblemRole(
77-
profile.id, problem.id, 'basic',
77+
profile._id, problem._id, 'basic',
7878
)) {
7979
ctx.state.problem = problem
8080
return problem
@@ -125,14 +125,14 @@ const findProblems = async (ctx: Context) => {
125125
...paginateOption,
126126
...filterOption,
127127
showReserved,
128-
includeOwner: profile?.id ?? null,
128+
includeOwner: profile?._id ?? null,
129129
},
130130
)
131131
}
132132
list.docs = list.docs.map(doc => ({
133133
...doc,
134-
isOwner: profile?.id && doc.owner
135-
? doc.owner.toString() === profile.id.toString()
134+
isOwner: profile?._id && doc.owner
135+
? doc.owner.equals(profile._id)
136136
: false,
137137
owner: undefined,
138138
}))
@@ -190,8 +190,8 @@ const getProblem = async (ctx: Context) => {
190190
const problem = await loadProblem(ctx)
191191
const profile = ctx.state.profile
192192

193-
const isOwner = (profile?.id && problem.owner)
194-
? profile.id.toString() === problem.owner.toString()
193+
const isOwner = (profile?._id && problem.owner)
194+
? problem.owner.equals(profile._id)
195195
: false
196196
const canManage = profile?.isAdmin ?? isOwner
197197

@@ -236,7 +236,7 @@ const createProblem = async (ctx: Context) => {
236236
owner,
237237
})
238238
if (course) {
239-
await courseService.addCourseProblem(course.id, problem.id)
239+
await courseService.addCourseProblem(course._id, problem._id)
240240
}
241241
ctx.auditLog.info(`<Problem:${problem.pid}> created by <User:${profile.uid}>`)
242242
const response: Pick<ProblemEntity, 'pid'>

backend/src/controllers/solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const create = async (ctx: Context) => {
104104
ctx.throw(400, 'No such a problem in the contest')
105105
}
106106
if (contest.course) {
107-
course = contest.course.id
107+
course = contest.course._id
108108
}
109109
}
110110
const problem = await Problem.findOne({ pid })
@@ -115,7 +115,7 @@ const create = async (ctx: Context) => {
115115
* @TODO
116116
*/
117117
// if (problem.course && !course) {
118-
// course = problem.course.id
118+
// course = problem.course._id
119119
// }
120120

121121
try {

backend/src/controllers/testcase.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { loadProblem } from './problem'
1616
export async function findTestcases (ctx: Context) {
1717
const problem = await loadProblem(ctx)
1818
const profile = await loadProfile(ctx)
19-
if (!(profile.isAdmin || (problem.owner && problem.owner === profile.id))) {
19+
if (!(profile.isAdmin || (problem.owner && problem.owner.equals(profile._id)))) {
2020
ctx.throw(...ERR_PERM_DENIED)
2121
}
2222

@@ -40,9 +40,9 @@ export async function exportTestcases (ctx: Context) {
4040
const profile = await loadProfile(ctx)
4141
if (!(
4242
profile.isAdmin
43-
|| (problem.owner && problem.owner === profile.id)
43+
|| (problem.owner && problem.owner.equals(profile._id))
4444
|| courseService.hasProblemRole(
45-
profile.id, problem.id, 'viewTestcase',
45+
profile._id, problem._id, 'viewTestcase',
4646
)
4747
)) {
4848
ctx.throw(...ERR_PERM_DENIED)
@@ -104,7 +104,7 @@ export async function exportTestcases (ctx: Context) {
104104
export async function createTestcase (ctx: Context) {
105105
const problem = await loadProblem(ctx)
106106
const profile = await loadProfile(ctx)
107-
if (!(profile.isAdmin || (problem.owner && problem.owner === profile.id))) {
107+
if (!(profile.isAdmin || (problem.owner && problem.owner.equals(profile._id)))) {
108108
ctx.throw(...ERR_PERM_DENIED)
109109
}
110110

@@ -147,7 +147,7 @@ export async function createTestcase (ctx: Context) {
147147
export async function removeTestcase (ctx: Context) {
148148
const problem = await loadProblem(ctx)
149149
const profile = await loadProfile(ctx)
150-
if (!(profile.isAdmin || (problem.owner && problem.owner === profile.id))) {
150+
if (!(profile.isAdmin || (problem.owner && problem.owner.equals(profile._id)))) {
151151
ctx.throw(...ERR_PERM_DENIED)
152152
}
153153

@@ -180,9 +180,9 @@ export async function getTestcase (ctx: Context) {
180180
const profile = await loadProfile(ctx)
181181
if (!(
182182
profile.isAdmin
183-
|| (problem.owner && problem.owner === profile.id)
183+
|| (problem.owner && problem.owner.equals(profile._id))
184184
|| courseService.hasProblemRole(
185-
profile.id, problem.id, 'viewTestcase',
185+
profile._id, problem._id, 'viewTestcase',
186186
)
187187
)) {
188188
ctx.throw(...ERR_PERM_DENIED)

backend/src/models/Comment.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ const commentSchema = new mongoose.Schema({
4141
timestamps: true,
4242
})
4343

44-
commentSchema.pre('save', async function (next) {
44+
commentSchema.pre('save', async function () {
4545
if (this.commentId === -1) {
4646
this.commentId = await ID.generateId('Comment')
4747
}
48-
next()
4948
})
5049

5150
const Comment

backend/src/models/Contest.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,15 @@ const contestSchema = new mongoose.Schema({
108108

109109
contestSchema.plugin(mongoosePaginate)
110110

111-
contestSchema.pre('validate', async function (this: ContestDocument, next) {
111+
contestSchema.pre('validate', async function (this: ContestDocument) {
112112
if (this.start >= this.end) {
113-
next(new Error('The contest end time must be later than the start time!'))
113+
throw new Error('The contest end time must be later than the start time!')
114114
}
115-
next()
116115
})
117-
contestSchema.pre('save', async function (this: ContestDocument, next) {
116+
contestSchema.pre('save', async function (this: ContestDocument) {
118117
if (this.cid === -1) {
119118
this.cid = await ID.generateId('Contest')
120119
}
121-
next()
122120
})
123121

124122
const Contest

backend/src/models/Course.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@ courseSchema.virtual('canJoin').get(function (this: CourseDocument): boolean {
7676
return (this.joinCode?.length ?? 0) > 0
7777
})
7878

79-
courseSchema.pre('save', async function (this: CourseDocument, next) {
79+
courseSchema.pre('save', async function (this: CourseDocument) {
8080
if (this.courseId === -1) {
8181
this.courseId = await ID.generateId('Course')
8282
}
83-
next()
8483
})
8584

8685
const Course

backend/src/models/Discussion.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const discussionSchema = new mongoose.Schema({
3131
},
3232
type: {
3333
type: Number,
34-
enum: DiscussionType,
34+
enum: Object.values(DiscussionType).filter((v): v is number => typeof v === 'number'),
3535
default: DiscussionType.PrivateClarification,
3636
},
3737
pinned: {
@@ -58,11 +58,10 @@ const discussionSchema = new mongoose.Schema({
5858
timestamps: true,
5959
})
6060

61-
discussionSchema.pre('save', async function (next) {
61+
discussionSchema.pre('save', async function () {
6262
if (this.discussionId === -1) {
6363
this.discussionId = await ID.generateId('Discussion')
6464
}
65-
next()
6665
})
6766

6867
const Discussion

0 commit comments

Comments
 (0)