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} */ 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} */ 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} */ 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} */ 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 };