Project.js 4.0 KB

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