refactor(data-access): 组 C 模块迁移至 cacheFn(lesson-preparation/elective/announcements/messaging/notifications/audit/onboarding/invitation-codes/scheduling/settings)

This commit is contained in:
SpecialX
2026-07-05 21:49:18 +08:00
parent cb5b92160c
commit 6dee6b6299
9 changed files with 520 additions and 336 deletions

View File

@@ -1,10 +1,10 @@
import "server-only"
import { cache } from "react"
import { eq, and } from "drizzle-orm"
import { db } from "@/shared/db"
import { systemSettings } from "@/shared/db/schema"
import { cacheFn } from "@/shared/lib/cache"
/**
* 选课模块配置化设置P2-4 新增)。
@@ -55,23 +55,29 @@ async function readSettingValue(key: string): Promise<string | null> {
*
* @param gradeId 学生所在年级 ID可选
*/
export const getElectiveCreditLimit = cache(
async (gradeId?: string | null): Promise<number> => {
if (gradeId) {
const gradeValue = await readSettingValue(`creditLimit:grade:${gradeId}`)
if (gradeValue !== null) {
const parsed = Number(gradeValue)
if (!Number.isNaN(parsed) && parsed > 0) return parsed
}
}
const defaultValue = await readSettingValue("creditLimit:default")
if (defaultValue !== null) {
const parsed = Number(defaultValue)
export const getElectiveCreditLimitRaw = async (
gradeId?: string | null,
): Promise<number> => {
if (gradeId) {
const gradeValue = await readSettingValue(`creditLimit:grade:${gradeId}`)
if (gradeValue !== null) {
const parsed = Number(gradeValue)
if (!Number.isNaN(parsed) && parsed > 0) return parsed
}
return DEFAULT_MAX_CREDIT_PER_TERM
}
)
const defaultValue = await readSettingValue("creditLimit:default")
if (defaultValue !== null) {
const parsed = Number(defaultValue)
if (!Number.isNaN(parsed) && parsed > 0) return parsed
}
return DEFAULT_MAX_CREDIT_PER_TERM
}
export const getElectiveCreditLimit = cacheFn(getElectiveCreditLimitRaw, {
tags: ["elective"],
ttl: 300,
keyParts: ["elective", "getElectiveCreditLimit"],
})
/**
* 获取容量阈值通知比例P2-4 新增)。
@@ -79,16 +85,20 @@ export const getElectiveCreditLimit = cache(
* 当课程 `enrolledCount >= capacity * threshold` 时触发管理员通知。
* 默认 0.990%)。
*/
export const getCapacityNotifyThreshold = cache(
async (): Promise<number> => {
const value = await readSettingValue("capacityNotifyThreshold")
if (value !== null) {
const parsed = Number(value)
if (!Number.isNaN(parsed) && parsed > 0 && parsed <= 1) return parsed
}
return DEFAULT_CAPACITY_NOTIFY_THRESHOLD
export const getCapacityNotifyThresholdRaw = async (): Promise<number> => {
const value = await readSettingValue("capacityNotifyThreshold")
if (value !== null) {
const parsed = Number(value)
if (!Number.isNaN(parsed) && parsed > 0 && parsed <= 1) return parsed
}
)
return DEFAULT_CAPACITY_NOTIFY_THRESHOLD
}
export const getCapacityNotifyThreshold = cacheFn(getCapacityNotifyThresholdRaw, {
tags: ["elective"],
ttl: 300,
keyParts: ["elective", "getCapacityNotifyThreshold"],
})
/** 导出默认值常量(供测试与文档引用) */
export const ELECTIVE_DEFAULTS = {