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

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. const prisma = require("../utils/prisma");
  2. /**
  3. * @typedef {Object} DeptDocument
  4. * @property {number} id
  5. * @property {number} deptId
  6. * @property {string} realDocId
  7. * @property {string} filename
  8. * @property {string} docpath
  9. * @property {string} [metadata]
  10. * @property {string} [tag]
  11. * @property {string} realFilename
  12. * @property {boolean} public
  13. * @property {Date} createdAt
  14. * @property {Date} lastUpdatedAt
  15. */
  16. const DeptDocument = {
  17. writable: [
  18. "deptId",
  19. "parsedFileName",
  20. "parsedFilePath",
  21. "realFileName",
  22. "realFileAlias",
  23. "realFilePath",
  24. "isPublic",
  25. "tags",
  26. "delTag",
  27. ],
  28. validations: {
  29. filename: (newValue = "") => {
  30. if (typeof newValue !== "string" || newValue.length > 255) {
  31. throw new Error(
  32. "Filename must be a string and cannot be longer than 255 characters"
  33. );
  34. }
  35. return newValue;
  36. },
  37. docpath: (newValue = "") => {
  38. if (typeof newValue !== "string" || newValue.length > 255) {
  39. throw new Error(
  40. "Document path must be a string and cannot be longer than 255 characters"
  41. );
  42. }
  43. return newValue;
  44. },
  45. public: (newValue = false) => {
  46. if (typeof newValue !== "boolean") {
  47. throw new Error("Public must be a boolean");
  48. }
  49. return newValue;
  50. },
  51. },
  52. castColumnValue: function (key, value) {
  53. switch (key) {
  54. case "public":
  55. return Boolean(value);
  56. default:
  57. return value;
  58. }
  59. },
  60. /**
  61. * 创建组织机构文档
  62. * @param {Object} data - 组织机构文档数据
  63. * @returns {Promise<{ deptDocument: DeptDocument | null, error: string | null }>}
  64. */
  65. create: async function (data) {
  66. try {
  67. const validatedData = {};
  68. for (const key of this.writable) {
  69. if (data[key] !== undefined) {
  70. if (this.validations[key]) {
  71. validatedData[key] = this.validations[key](data[key]);
  72. } else {
  73. validatedData[key] = this.castColumnValue(key, data[key]);
  74. }
  75. }
  76. }
  77. const deptDocument = await prisma.dept_document.create({
  78. data: {
  79. ...validatedData,
  80. createdAt: new Date(),
  81. lastUpdatedAt: new Date(),
  82. },
  83. });
  84. return { deptDocument, error: null };
  85. } catch (error) {
  86. console.error("FAILED TO CREATE DEPT DOCUMENT.", error.message);
  87. return { deptDocument: null, error: error.message };
  88. }
  89. },
  90. /**
  91. * 更新组织机构文档
  92. * @param {number} id - 文档 ID
  93. * @param {Object} updates - 更新的字段
  94. * @returns {Promise<{ success: boolean, error: string | null, deptDocument: DeptDocument | null }>}
  95. */
  96. update: async function (id, updates = {}) {
  97. try {
  98. if (!id) throw new Error("No document id provided for update");
  99. const currentDocument = await prisma.dept_document.findUnique({
  100. where: { id },
  101. });
  102. if (!currentDocument) throw new Error("Document not found");
  103. const validatedUpdates = {};
  104. for (const key of this.writable) {
  105. if (updates[key] !== undefined) {
  106. if (this.validations[key]) {
  107. validatedUpdates[key] = this.validations[key](updates[key]);
  108. } else {
  109. validatedUpdates[key] = this.castColumnValue(key, updates[key]);
  110. }
  111. }
  112. }
  113. validatedUpdates.lastUpdatedAt = new Date();
  114. const updatedDocument = await prisma.dept_document.update({
  115. where: { id },
  116. data: validatedUpdates,
  117. });
  118. return { success: true, error: null, deptDocument: updatedDocument };
  119. } catch (error) {
  120. console.error(error.message);
  121. return { success: false, error: error.message, deptDocument: null };
  122. }
  123. },
  124. /**
  125. * 获取组织机构文档
  126. * @param {Object} clause - 查询条件
  127. * @returns {Promise<{ deptDocument: DeptDocument | null }>}
  128. */
  129. get: async function (clause = {}) {
  130. try {
  131. const deptDocument = await prisma.dept_document.findFirst({
  132. where: clause,
  133. });
  134. return deptDocument ? { deptDocument } : null;
  135. } catch (error) {
  136. console.error(error.message);
  137. return null;
  138. }
  139. },
  140. /**
  141. * 删除组织机构文档
  142. * @param {Object} clause - 删除条件
  143. * @returns {Promise<boolean>}
  144. */
  145. delete: async function (clause = {}) {
  146. try {
  147. const affectedRows = await prisma.dept_document.deleteMany({
  148. where: clause,
  149. });
  150. return affectedRows.count > 0;
  151. } catch (error) {
  152. console.error(error.message);
  153. return false;
  154. }
  155. },
  156. /**
  157. * 查询组织机构文档列表
  158. * @param {Object} clause - 查询条件
  159. * @param {number} limit - 限制数量
  160. * @returns {Promise<DeptDocument[]>}
  161. */
  162. where: async function (clause = {}, limit = null) {
  163. try {
  164. const deptDocuments = await prisma.dept_document.findMany({
  165. where: clause,
  166. take: limit !== null ? limit : undefined,
  167. });
  168. return deptDocuments;
  169. } catch (error) {
  170. console.error(error.message);
  171. return [];
  172. }
  173. },
  174. /**
  175. * 检查文档路径是否唯一
  176. * @param {string} docpath - 文档路径
  177. * @returns {Promise<boolean>}
  178. */
  179. checkDocpathUnique: async function (docpath) {
  180. try {
  181. const existingDocument = await prisma.dept_document.findFirst({
  182. where: { docpath },
  183. });
  184. return !existingDocument;
  185. } catch (error) {
  186. console.error("检查文档路径唯一性失败:", error);
  187. throw error;
  188. }
  189. },
  190. /**
  191. * 检查文档是否属于指定组织机构
  192. * @param {number} id - 文档 ID
  193. * @param {number} deptId - 组织机构 ID
  194. * @returns {Promise<boolean>}
  195. */
  196. checkDocumentBelongsToDept: async function (id, deptId) {
  197. try {
  198. const document = await prisma.dept_document.findFirst({
  199. where: { id, deptId },
  200. });
  201. return !!document;
  202. } catch (error) {
  203. console.error("检查文档所属组织机构失败:", error);
  204. throw error;
  205. }
  206. },
  207. };
  208. module.exports = { DeptDocument };