Project.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import mongoose from '../infrastructure/Mongoose.mjs'
  2. import _ from 'lodash'
  3. import { FolderSchema } from './Folder.mjs'
  4. import Errors from '../Features/Errors/Errors.js'
  5. const ConcreteObjectId = mongoose.Types.ObjectId
  6. const { Schema } = mongoose
  7. const { ObjectId } = Schema
  8. const DeletedDocSchema = new Schema({
  9. name: String,
  10. deletedAt: { type: Date },
  11. })
  12. export const ProjectSchema = new Schema(
  13. {
  14. name: { type: String, default: 'new project' },
  15. lastUpdated: {
  16. type: Date,
  17. default() {
  18. return new Date()
  19. },
  20. },
  21. lastUpdatedBy: { type: ObjectId, ref: 'User' },
  22. lastOpened: { type: Date },
  23. active: { type: Boolean, default: true },
  24. readOnly: { type: Boolean, default: false },
  25. owner_ref: { type: ObjectId, ref: 'User' },
  26. collaberator_refs: [{ type: ObjectId, ref: 'User' }],
  27. reviewer_refs: [{ type: ObjectId, ref: 'User' }],
  28. readOnly_refs: [{ type: ObjectId, ref: 'User' }],
  29. pendingEditor_refs: [{ type: ObjectId, ref: 'User' }],
  30. pendingReviewer_refs: [{ type: ObjectId, ref: 'User' }],
  31. rootDoc_id: { type: ObjectId },
  32. rootFolder: [FolderSchema],
  33. mainBibliographyDoc_id: { type: ObjectId },
  34. version: { type: Number }, // incremented for every change in the project structure (folders and filenames)
  35. publicAccesLevel: { type: String, default: 'private' },
  36. compiler: { type: String, default: 'pdflatex' },
  37. spellCheckLanguage: { type: String, default: 'en' },
  38. deletedByExternalDataSource: { type: Boolean, default: false },
  39. description: { type: String, default: '' },
  40. archived: { type: Schema.Types.Mixed },
  41. trashed: [{ type: ObjectId, ref: 'User' }],
  42. deletedDocs: [DeletedDocSchema],
  43. imageName: { type: String },
  44. brandVariationId: { type: String },
  45. track_changes: { type: Object },
  46. tokens: {
  47. readOnly: {
  48. type: String,
  49. index: {
  50. unique: true,
  51. partialFilterExpression: { 'tokens.readOnly': { $exists: true } },
  52. },
  53. },
  54. readAndWrite: {
  55. type: String,
  56. index: {
  57. unique: true,
  58. partialFilterExpression: { 'tokens.readAndWrite': { $exists: true } },
  59. },
  60. },
  61. readAndWritePrefix: {
  62. type: String,
  63. index: {
  64. unique: true,
  65. partialFilterExpression: {
  66. 'tokens.readAndWritePrefix': { $exists: true },
  67. },
  68. },
  69. },
  70. },
  71. tokenAccessReadOnly_refs: [{ type: ObjectId, ref: 'User' }],
  72. tokenAccessReadAndWrite_refs: [{ type: ObjectId, ref: 'User' }],
  73. fromV1TemplateId: { type: Number },
  74. fromV1TemplateVersionId: { type: Number },
  75. overleaf: {
  76. id: { type: Number },
  77. imported_at_ver_id: { type: Number },
  78. token: { type: String },
  79. read_token: { type: String },
  80. history: {
  81. id: { type: Schema.Types.Mixed },
  82. display: { type: Boolean },
  83. upgradedAt: { type: Date },
  84. allowDowngrade: { type: Boolean },
  85. zipFileArchivedInProject: { type: Boolean },
  86. rangesSupportEnabled: { type: Boolean },
  87. otMigrationStage: { type: Number },
  88. },
  89. isDebugCopyOf: { type: ObjectId },
  90. },
  91. collabratecUsers: [
  92. {
  93. user_id: { type: ObjectId, ref: 'User' },
  94. collabratec_document_id: { type: String },
  95. collabratec_privategroup_id: { type: String },
  96. added_at: {
  97. type: Date,
  98. default() {
  99. return new Date()
  100. },
  101. },
  102. },
  103. ],
  104. deferredTpdsFlushCounter: { type: Number },
  105. },
  106. { minimize: false }
  107. )
  108. ProjectSchema.statics.getProject = function (projectOrId, fields, callback) {
  109. if (projectOrId._id != null) {
  110. callback(null, projectOrId)
  111. } else {
  112. try {
  113. // eslint-disable-next-line no-new
  114. new ConcreteObjectId(projectOrId.toString())
  115. } catch (e) {
  116. return callback(new Errors.NotFoundError(e.message))
  117. }
  118. this.findById(projectOrId, fields, callback)
  119. }
  120. }
  121. function applyToAllFilesRecursivly(folder, fun) {
  122. _.forEach(folder.fileRefs, file => fun(file))
  123. _.forEach(folder.folders, folder => applyToAllFilesRecursivly(folder, fun))
  124. }
  125. ProjectSchema.statics.applyToAllFilesRecursivly = applyToAllFilesRecursivly
  126. export const Project = mongoose.model('Project', ProjectSchema)