You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
5.9 KiB
225 lines
5.9 KiB
const prisma = require("../utils/prisma");
|
|
|
|
/**
|
|
* @typedef {Object} DeptDocument
|
|
* @property {number} id
|
|
* @property {number} deptId
|
|
* @property {string} realDocId
|
|
* @property {string} filename
|
|
* @property {string} docpath
|
|
* @property {string} [metadata]
|
|
* @property {string} [tag]
|
|
* @property {string} realFilename
|
|
* @property {boolean} public
|
|
* @property {Date} createdAt
|
|
* @property {Date} lastUpdatedAt
|
|
*/
|
|
|
|
const DeptDocument = {
|
|
writable: [
|
|
"deptId",
|
|
"parsedFileName",
|
|
"parsedFilePath",
|
|
"realFileName",
|
|
"realFileAlias",
|
|
"realFilePath",
|
|
"isPublic",
|
|
"tags",
|
|
"delTag",
|
|
],
|
|
validations: {
|
|
filename: (newValue = "") => {
|
|
if (typeof newValue !== "string" || newValue.length > 255) {
|
|
throw new Error(
|
|
"Filename must be a string and cannot be longer than 255 characters"
|
|
);
|
|
}
|
|
return newValue;
|
|
},
|
|
docpath: (newValue = "") => {
|
|
if (typeof newValue !== "string" || newValue.length > 255) {
|
|
throw new Error(
|
|
"Document path must be a string and cannot be longer than 255 characters"
|
|
);
|
|
}
|
|
return newValue;
|
|
},
|
|
public: (newValue = false) => {
|
|
if (typeof newValue !== "boolean") {
|
|
throw new Error("Public must be a boolean");
|
|
}
|
|
return newValue;
|
|
},
|
|
},
|
|
castColumnValue: function (key, value) {
|
|
switch (key) {
|
|
case "public":
|
|
return Boolean(value);
|
|
default:
|
|
return value;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 创建组织机构文档
|
|
* @param {Object} data - 组织机构文档数据
|
|
* @returns {Promise<{ deptDocument: DeptDocument | null, error: string | null }>}
|
|
*/
|
|
create: async function (data) {
|
|
try {
|
|
const validatedData = {};
|
|
for (const key of this.writable) {
|
|
if (data[key] !== undefined) {
|
|
if (this.validations[key]) {
|
|
validatedData[key] = this.validations[key](data[key]);
|
|
} else {
|
|
validatedData[key] = this.castColumnValue(key, data[key]);
|
|
}
|
|
}
|
|
}
|
|
|
|
const deptDocument = await prisma.dept_document.create({
|
|
data: {
|
|
...validatedData,
|
|
createdAt: new Date(),
|
|
lastUpdatedAt: new Date(),
|
|
},
|
|
});
|
|
|
|
return { deptDocument, error: null };
|
|
} catch (error) {
|
|
console.error("FAILED TO CREATE DEPT DOCUMENT.", error.message);
|
|
return { deptDocument: null, error: error.message };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 更新组织机构文档
|
|
* @param {number} id - 文档 ID
|
|
* @param {Object} updates - 更新的字段
|
|
* @returns {Promise<{ success: boolean, error: string | null, deptDocument: DeptDocument | null }>}
|
|
*/
|
|
update: async function (id, updates = {}) {
|
|
try {
|
|
if (!id) throw new Error("No document id provided for update");
|
|
|
|
const currentDocument = await prisma.dept_document.findUnique({
|
|
where: { id },
|
|
});
|
|
if (!currentDocument) throw new Error("Document not found");
|
|
|
|
const validatedUpdates = {};
|
|
for (const key of this.writable) {
|
|
if (updates[key] !== undefined) {
|
|
if (this.validations[key]) {
|
|
validatedUpdates[key] = this.validations[key](updates[key]);
|
|
} else {
|
|
validatedUpdates[key] = this.castColumnValue(key, updates[key]);
|
|
}
|
|
}
|
|
}
|
|
|
|
validatedUpdates.lastUpdatedAt = new Date();
|
|
|
|
const updatedDocument = await prisma.dept_document.update({
|
|
where: { id },
|
|
data: validatedUpdates,
|
|
});
|
|
|
|
return { success: true, error: null, deptDocument: updatedDocument };
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return { success: false, error: error.message, deptDocument: null };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 获取组织机构文档
|
|
* @param {Object} clause - 查询条件
|
|
* @returns {Promise<{ deptDocument: DeptDocument | null }>}
|
|
*/
|
|
get: async function (clause = {}) {
|
|
try {
|
|
const deptDocument = await prisma.dept_document.findFirst({
|
|
where: clause,
|
|
});
|
|
return deptDocument ? { deptDocument } : null;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return null;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 删除组织机构文档
|
|
* @param {Object} clause - 删除条件
|
|
* @returns {Promise<boolean>}
|
|
*/
|
|
delete: async function (clause = {}) {
|
|
try {
|
|
const affectedRows = await prisma.dept_document.deleteMany({
|
|
where: clause,
|
|
});
|
|
return affectedRows.count > 0;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return false;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 查询组织机构文档列表
|
|
* @param {Object} clause - 查询条件
|
|
* @param {number} limit - 限制数量
|
|
* @returns {Promise<DeptDocument[]>}
|
|
*/
|
|
where: async function (clause = {}, limit = null) {
|
|
try {
|
|
const deptDocuments = await prisma.dept_document.findMany({
|
|
where: clause,
|
|
take: limit !== null ? limit : undefined,
|
|
});
|
|
return deptDocuments;
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
return [];
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 检查文档路径是否唯一
|
|
* @param {string} docpath - 文档路径
|
|
* @returns {Promise<boolean>}
|
|
*/
|
|
checkDocpathUnique: async function (docpath) {
|
|
try {
|
|
const existingDocument = await prisma.dept_document.findFirst({
|
|
where: { docpath },
|
|
});
|
|
return !existingDocument;
|
|
} catch (error) {
|
|
console.error("检查文档路径唯一性失败:", error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 检查文档是否属于指定组织机构
|
|
* @param {number} id - 文档 ID
|
|
* @param {number} deptId - 组织机构 ID
|
|
* @returns {Promise<boolean>}
|
|
*/
|
|
checkDocumentBelongsToDept: async function (id, deptId) {
|
|
try {
|
|
const document = await prisma.dept_document.findFirst({
|
|
where: { id, deptId },
|
|
});
|
|
return !!document;
|
|
} catch (error) {
|
|
console.error("检查文档所属组织机构失败:", error);
|
|
throw error;
|
|
}
|
|
},
|
|
};
|
|
|
|
module.exports = { DeptDocument };
|