refactor(data-access): 组 C 模块迁移至 cacheFn(lesson-preparation/elective/announcements/messaging/notifications/audit/onboarding/invitation-codes/scheduling/settings)
This commit is contained in:
@@ -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.9(90%)。
|
||||
*/
|
||||
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 = {
|
||||
|
||||
Reference in New Issue
Block a user