refactor(模型关系): 重构模型关联关系定义位置,从模型文件移至路由文件

This commit is contained in:
zhang1106
2026-03-12 11:19:56 +08:00
parent 2e87654c98
commit 668ea073e6
17 changed files with 1478 additions and 553 deletions
+53 -176
View File
@@ -1,168 +1,61 @@
const Joi = require('joi');
const DeviceField = require('../models/DeviceField');
const DEVICE_TYPES = ['server', 'switch', 'router', 'storage', 'other'];
const DEVICE_STATUS = ['running', 'maintenance', 'offline', 'fault'];
const baseFieldSchemas = {
deviceId: Joi.string()
.max(50)
.pattern(/^[a-zA-Z0-9_-]+$/)
.allow('', null)
.messages({
'string.max': '设备ID不能超过50个字符',
'string.pattern.base': '设备ID只能包含字母、数字、下划线和横线'
}),
name: Joi.string()
.max(100)
.messages({
'string.empty': '设备名称不能为空',
'string.max': '设备名称不能超过100个字符'
}),
type: Joi.string()
.valid(...DEVICE_TYPES)
.messages({
'string.empty': '设备类型不能为空',
'any.only': `设备类型必须是以下之一: ${DEVICE_TYPES.join(', ')}`
}),
model: Joi.string()
.max(100)
.allow('', null)
.messages({
'string.max': '型号不能超过100个字符'
}),
serialNumber: Joi.string()
.max(100)
.messages({
'string.empty': '序列号不能为空',
'string.max': '序列号不能超过100个字符'
}),
rackId: Joi.string()
.max(50)
.messages({
'string.empty': '机柜ID不能为空',
'string.max': '机柜ID不能超过50个字符'
}),
position: Joi.number()
.integer()
.min(1)
.max(100)
.messages({
'number.base': '位置必须是数字',
'number.integer': '位置必须是整数',
'number.min': '位置不能小于1',
'number.max': '位置不能大于100'
}),
height: Joi.number()
.integer()
.min(1)
.max(50)
.messages({
'number.base': '高度必须是数字',
'number.integer': '高度必须是整数',
'number.min': '高度不能小于1',
'number.max': '高度不能大于50'
}),
powerConsumption: Joi.number()
.min(0)
.max(100000)
.messages({
'number.base': '功率必须是数字',
'number.min': '功率不能小于0',
'number.max': '功率不能超过100000'
}),
ipAddress: Joi.string()
.ip({ version: ['ipv4', 'ipv6'] })
.allow('', null)
.messages({
'string.ip': 'IP地址格式无效'
}),
status: Joi.string()
.valid(...DEVICE_STATUS)
.default('offline')
.messages({
'any.only': `状态必须是以下之一: ${DEVICE_STATUS.join(', ')}`
}),
purchaseDate: Joi.date()
.allow(null)
.messages({
'date.base': '购买日期格式无效'
}),
warrantyExpiry: Joi.date()
.allow(null)
.messages({
'date.base': '保修到期日期格式无效'
}),
description: Joi.string()
.max(500)
.allow('', null)
.messages({
'string.max': '描述不能超过500个字符'
}),
const createDeviceSchema = Joi.object({
name: Joi.string().required().max(100).messages({
'string.empty': '设备名称不能为空',
'string.max': '设备名称不能超过100个字符',
'any.required': '设备名称是必填字段'
}),
type: Joi.string().required().valid(...DEVICE_TYPES).messages({
'any.only': `设备类型必须是以下之一: ${DEVICE_TYPES.join(', ')}`,
'any.required': '设备类型是必填字段'
}),
model: Joi.string().allow('', null).max(100),
serialNumber: Joi.string().required().max(100).messages({
'string.empty': '序列号不能为空',
'string.max': '序列号不能超过100个字符',
'any.required': '序列号是必填字段'
}),
rackId: Joi.string().allow('', null).max(50),
position: Joi.number().integer().min(1).max(100).allow(null),
height: Joi.number().integer().min(1).max(50).allow(null),
powerConsumption: Joi.number().min(0).max(100000).allow(null),
ipAddress: Joi.string().allow('', null).max(50),
status: Joi.string().valid(...DEVICE_STATUS).default('offline'),
purchaseDate: Joi.date().allow(null),
warrantyExpiry: Joi.date().allow(null),
description: Joi.string().allow('', null).max(500),
customFields: Joi.object().allow(null)
};
});
async function buildDynamicSchema(isCreate = true) {
const fields = await DeviceField.findAll({
where: { isSystem: true },
order: [['order', 'ASC']]
});
const schemaObj = {};
fields.forEach(field => {
const baseSchema = baseFieldSchemas[field.fieldName];
if (baseSchema) {
let fieldSchema = baseSchema.clone();
if (field.required && isCreate) {
fieldSchema = fieldSchema.required();
}
schemaObj[field.fieldName] = fieldSchema;
}
});
schemaObj.customFields = baseFieldSchemas.customFields;
return Joi.object(schemaObj).custom((value, helpers) => {
if (value.purchaseDate && value.warrantyExpiry) {
const purchase = new Date(value.purchaseDate);
const warranty = new Date(value.warrantyExpiry);
if (warranty <= purchase) {
return helpers.error('date.warrantyAfterPurchase');
}
}
return value;
}).messages({
'date.warrantyAfterPurchase': '保修到期日期必须晚于购买日期'
});
}
async function getCreateDeviceSchema() {
return buildDynamicSchema(true);
}
async function getUpdateDeviceSchema() {
const schema = await buildDynamicSchema(false);
return schema.min(1).messages({
'object.min': '至少需要提供一个字段进行更新'
});
}
const updateDeviceSchema = Joi.object({
name: Joi.string().max(100).messages({
'string.empty': '设备名称不能为空',
'string.max': '设备名称不能超过100个字符'
}),
type: Joi.string().valid(...DEVICE_TYPES).messages({
'any.only': `设备类型必须是以下之一: ${DEVICE_TYPES.join(', ')}`
}),
model: Joi.string().allow('', null).max(100),
serialNumber: Joi.string().max(100).messages({
'string.max': '序列号不能超过100个字符'
}),
rackId: Joi.string().allow('', null).max(50),
position: Joi.number().integer().min(1).max(100).allow(null),
height: Joi.number().integer().min(1).max(50).allow(null),
powerConsumption: Joi.number().min(0).max(100000).allow(null),
ipAddress: Joi.string().allow('', null).max(50),
status: Joi.string().valid(...DEVICE_STATUS),
purchaseDate: Joi.date().allow(null),
warrantyExpiry: Joi.date().allow(null),
description: Joi.string().allow('', null).max(500),
customFields: Joi.object().allow(null)
}).min(1).messages({
'object.min': '至少需要提供一个字段进行更新'
});
const batchDeviceIdsSchema = Joi.object({
deviceIds: Joi.array()
@@ -233,24 +126,10 @@ const queryDeviceSchema = Joi.object({
pageSize: Joi.number()
.integer()
.min(1)
.max(100)
.max(10000)
.default(10)
});
const createDeviceSchema = {
validate: async (data, options = {}) => {
const schema = await getCreateDeviceSchema();
return schema.validate(data, options);
}
};
const updateDeviceSchema = {
validate: async (data, options = {}) => {
const schema = await getUpdateDeviceSchema();
return schema.validate(data, options);
}
};
module.exports = {
createDeviceSchema,
updateDeviceSchema,
@@ -259,7 +138,5 @@ module.exports = {
batchMoveSchema,
queryDeviceSchema,
DEVICE_TYPES,
DEVICE_STATUS,
getCreateDeviceSchema,
getUpdateDeviceSchema
DEVICE_STATUS
};