Skip to content

Commit 309e788

Browse files
committed
chore: remove deprecated constants and alias in config
1 parent d788018 commit 309e788

23 files changed

Lines changed: 81 additions & 94 deletions

File tree

backend/package.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@
77
"node": ">= 22.0.0"
88
},
99
"scripts": {
10+
"build": "tsc && cp -r src/scripts dist/scripts",
11+
"coverage": "nyc report --reporter=text-lcov > coverage.lcov",
12+
"deploy:init": "cross-env NODE_ENV=production node setup.js",
13+
"deploy:start": "npx pm2 start pm2.config.json",
1014
"dev": "concurrently \"npm run dev:app\" \"npm run dev:ws\" \"npm run dev:updater\" \"npm run dev:worker\"",
1115
"dev:app": "cross-env NODE_ENV=development nodemon -e ts,js --exec ts-node src/app.ts",
12-
"dev:ws": "cross-env NODE_ENV=development nodemon -e ts,js --exec ts-node src/ws.ts",
1316
"dev:updater": "cross-env NODE_ENV=development nodemon -e ts,js --exec ts-node src/jobs/updater.ts",
1417
"dev:worker": "cross-env NODE_ENV=development nodemon -e ts,js --exec ts-node src/jobs/worker.ts",
15-
"build": "tsc && cp -r src/scripts dist/scripts",
16-
"start": "cross-env NODE_ENV=production node dist/app.js",
17-
"deploy:init": "cross-env NODE_ENV=production node setup.js",
18-
"deploy:start": "npx pm2 start pm2.config.json",
19-
"pretest": "cross-env NODE_ENV=test ts-node test/pretest.ts",
20-
"test": "cross-env-shell NODE_ENV=test \"ts-node test/pretest.ts && nyc ava && ts-node test/posttest.ts\"",
21-
"report": "nyc report --reporter=html",
22-
"posttest": "cross-env NODE_ENV=test ts-node test/posttest.ts",
18+
"dev:ws": "cross-env NODE_ENV=development nodemon -e ts,js --exec ts-node src/ws.ts",
2319
"lint": "eslint .",
2420
"lint:fix": "eslint . --fix",
25-
"coverage": "nyc report --reporter=text-lcov > coverage.lcov"
21+
"posttest": "cross-env NODE_ENV=test ts-node test/posttest.ts",
22+
"pretest": "cross-env NODE_ENV=test ts-node test/pretest.ts",
23+
"report": "nyc report --reporter=html",
24+
"start": "cross-env NODE_ENV=production node dist/app.js",
25+
"test": "cross-env-shell NODE_ENV=test \"ts-node test/pretest.ts && nyc ava && ts-node test/posttest.ts\"",
26+
"typecheck": "tsc --noEmit --skipLibCheck"
2627
},
2728
"dependencies": {
2829
"@koa/router": "^15.3.0",

backend/src/config/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { OAuthClientConfig } from '../services/oauth'
33
import { randomBytes } from 'node:crypto'
44
import { env } from 'node:process'
55
import dotenvFlow from 'dotenv-flow'
6-
import constants from '../utils/constants'
76

87
dotenvFlow.config()
98

@@ -151,11 +150,4 @@ export const globalConfig: GlobalConfig = {
151150
submissionHeatmapTimezone: stringEnv('PTOJ_SUBMISSION_HEATMAP_TIMEZONE', 'Asia/Shanghai'),
152151
}
153152

154-
const config = {
155-
globalConfig,
156-
...globalConfig,
157-
/** @deprecated */
158-
...constants,
159-
} as const
160-
161-
export default config
153+
export default globalConfig

backend/src/config/setup.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import path from 'node:path'
2+
import { UserPrivilege } from '@putongoj/shared'
23
import fse from 'fs-extra'
3-
import config from '.'
44
import ID from '../models/ID'
55
import Problem from '../models/Problem'
66
import User from '../models/User'
77
import { passwordHash } from '../utils'
8-
import { privilege } from '../utils/constants'
8+
import { deploy } from '../utils/constants'
99

1010
export async function databaseSetup () {
1111
const tasks: Promise<any>[] = []
@@ -36,8 +36,8 @@ export async function databaseSetup () {
3636
tasks.push(
3737
new User({
3838
uid: 'admin',
39-
pwd: passwordHash(config.deploy.adminInitPwd),
40-
privilege: privilege.Root,
39+
pwd: passwordHash(deploy.adminInitPwd),
40+
privilege: UserPrivilege.Root,
4141
}).save(),
4242
)
4343
}

backend/src/controllers/news.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Context } from 'koa'
2-
import config from '../config'
32
import { loadProfile } from '../middlewares/authn'
43
import News from '../models/News'
54
import { only } from '../utils'
5+
import { status } from '../utils/constants'
66

77
/**
88
* 预加载通知信息
@@ -30,7 +30,7 @@ const find = async (ctx: Context) => {
3030

3131
const filter: Record<string, any> = {}
3232
if (!ctx.state.profile?.isAdmin) {
33-
filter.status = config.status.Available
33+
filter.status = status.Available
3434
}
3535

3636
const list = await News.paginate(filter, {

backend/src/models/News.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Document, PaginateModel, Types } from 'mongoose'
22
import type { NewsEntity } from '../types/entity'
33
import mongoosePaginate from 'mongoose-paginate-v2'
4-
import config from '../config'
54
import mongoose from '../config/db'
5+
import { status } from '../utils/constants'
66
import ids from './ID'
77

88
export interface NewsDocument extends Document<Types.ObjectId>, NewsEntity {}
@@ -39,7 +39,7 @@ const newsSchema = new mongoose.Schema({
3939
},
4040
status: {
4141
type: Number,
42-
default: config.status.Available, // 默认新建的消息显示
42+
default: status.Available, // 默认新建的消息显示
4343
},
4444
create: {
4545
type: Number,

backend/src/models/Problem.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { Document, PaginateModel, Types } from 'mongoose'
22
import type { ProblemEntity } from '../types/entity'
33
import type { TagDocument } from './Tag'
44
import mongoosePaginate from 'mongoose-paginate-v2'
5-
import config from '../config'
65
import mongoose from '../config/db'
6+
import { limitation, problemType, status } from '../utils/constants'
77
import ID from './ID'
88

99
export type ProblemDocument = Document<Types.ObjectId> & ProblemEntity
@@ -39,13 +39,13 @@ const problemSchema = new mongoose.Schema({
3939
type: Number,
4040
default: 1000,
4141
min: 100,
42-
max: config.limitation.time,
42+
max: limitation.time,
4343
},
4444
memory: {
4545
type: Number,
4646
default: 32768,
4747
min: 32768,
48-
max: config.limitation.memory,
48+
max: limitation.memory,
4949
},
5050
description: {
5151
type: String,
@@ -73,13 +73,13 @@ const problemSchema = new mongoose.Schema({
7373
},
7474
status: {
7575
type: Number,
76-
enum: Object.values(config.status),
77-
default: config.status.Reserve,
76+
enum: Object.values(status),
77+
default: status.Reserve,
7878
},
7979
type: {
8080
type: Number,
81-
enum: Object.values(config.problemType),
82-
default: config.problemType.Traditional,
81+
enum: Object.values(problemType),
82+
default: problemType.Traditional,
8383
},
8484
code: {
8585
type: String,

backend/src/models/User.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { UserModel } from '@putongoj/shared'
22
import type { Document, PaginateModel, Schema, Types } from 'mongoose'
3-
import { UserModelSchema } from '@putongoj/shared'
3+
import { UserModelSchema, UserPrivilege, UserPrivilegeValues } from '@putongoj/shared'
44
import mongoosePaginate from 'mongoose-paginate-v2'
55
import mongoose from '../config/db'
6-
import { privilege } from '../utils/constants'
76

87
export interface UserEntity extends UserModel { }
98

@@ -36,8 +35,8 @@ const userSchema: Schema = new mongoose.Schema({
3635
},
3736
privilege: {
3837
type: Number,
39-
enum: Object.values(privilege),
40-
default: privilege.User,
38+
enum: UserPrivilegeValues,
39+
default: UserPrivilege.User,
4140
},
4241
nick: {
4342
type: String,
@@ -119,13 +118,13 @@ const userSchema: Schema = new mongoose.Schema({
119118
userSchema.plugin(mongoosePaginate)
120119

121120
userSchema.virtual('isBanned').get(function (this: UserDocument): boolean {
122-
return this.privilege === privilege.Banned
121+
return this.privilege === UserPrivilege.Banned
123122
})
124123
userSchema.virtual('isAdmin').get(function (this: UserDocument): boolean {
125-
return this.privilege >= privilege.Admin
124+
return this.privilege >= UserPrivilege.Admin
126125
})
127126
userSchema.virtual('isRoot').get(function (this: UserDocument): boolean {
128-
return this.privilege >= privilege.Root
127+
return this.privilege >= UserPrivilege.Root
129128
})
130129

131130
const User = mongoose.model<UserDocument, PaginateModel<UserDocument>>(

backend/src/utils/constants.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import type { CourseRole } from '../types'
22
import { ErrorCode } from '@putongoj/shared'
33

4-
export const privilege = Object.freeze({
5-
Banned: 0,
6-
User: 1,
7-
Admin: 2,
8-
Root: 3,
9-
})
10-
114
export const limitation = Object.freeze({
125
time: 10 * 1000,
136
memory: 256 * 1024,
@@ -88,7 +81,6 @@ export default Object.freeze({
8881
deploy,
8982
encrypt,
9083
limitation,
91-
privilege,
9284
problemType,
9385
status,
9486
courseRoleNone,

backend/test/controllers/contest/admin.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import test from 'ava'
22
import supertest from 'supertest'
33
import app from '../../../src/app'
4-
import config from '../../../src/config'
54
import { encryptData } from '../../../src/services/crypto'
5+
import { deploy } from '../../../src/utils/constants'
66

77
const server = app.listen()
88
const request = supertest.agent(server)
@@ -25,7 +25,7 @@ test.before('Login as admin', async (t) => {
2525
.post('/api/account/login')
2626
.send({
2727
username: 'admin',
28-
password: await encryptData(config.deploy.adminInitPwd),
28+
password: await encryptData(deploy.adminInitPwd),
2929
})
3030

3131
t.is(login.status, 200)

backend/test/controllers/contest/user.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { ParticipationStatus } from '@putongoj/shared'
22
import test from 'ava'
33
import supertest from 'supertest'
44
import app from '../../../src/app'
5-
import config from '../../../src/config'
65
import { encryptData } from '../../../src/services/crypto'
6+
import { deploy } from '../../../src/utils/constants'
77
import { userSeeds } from '../../seeds/user'
88

99
const server = app.listen()
@@ -35,7 +35,7 @@ test.before('Setup: admin creates test contests and user logs in', async (t) =>
3535
.post('/api/account/login')
3636
.send({
3737
username: 'admin',
38-
password: await encryptData(config.deploy.adminInitPwd),
38+
password: await encryptData(deploy.adminInitPwd),
3939
})
4040
t.is(adminLogin.status, 200)
4141
t.true(adminLogin.body.success)

0 commit comments

Comments
 (0)