workspace.model.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // api/workspaces/workspace.model.js
  2. import mongoose from "mongoose";
  3. import localDatabase from "../../common/localDatabase.js";
  4. const schema = new localDatabase.Schema(
  5. {
  6. name: {
  7. type: String,
  8. required: true,
  9. trim: true
  10. },
  11. slug: {
  12. type: String,
  13. trim: true
  14. },
  15. description: {
  16. type: String,
  17. default: ""
  18. },
  19. timezone: {
  20. type: String,
  21. default: "UTC"
  22. },
  23. filePath: {
  24. type: String,
  25. default: null
  26. },
  27. fileType: {
  28. type: String,
  29. default: null
  30. },
  31. fileSize: {
  32. type: Number,
  33. default: 0
  34. },
  35. metadata: {
  36. type: Object,
  37. default: {}
  38. },
  39. isPublic: {
  40. type: Boolean,
  41. default: false
  42. },
  43. accountId: {
  44. type: mongoose.Schema.Types.ObjectId,
  45. ref: "Account",
  46. required: true
  47. },
  48. ownerId: {
  49. type: mongoose.Schema.Types.ObjectId,
  50. ref: "User",
  51. required: true
  52. },
  53. // Keeping original userId field for backward compatibility
  54. userId: {
  55. type: mongoose.Schema.Types.ObjectId,
  56. ref: "User",
  57. required: true
  58. },
  59. // Nested structures
  60. members: [{
  61. userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
  62. name: { type: String },
  63. email: { type: String },
  64. role: { type: String, enum: ["admin", "user", "guest"], default: "user" },
  65. joinedAt: { type: Date, default: Date.now }
  66. }],
  67. contacts: [{
  68. contactId: { type: mongoose.Schema.Types.ObjectId },
  69. name: { type: String },
  70. email: { type: String },
  71. phone: { type: String },
  72. company: { type: String },
  73. tags: [{ type: String }],
  74. status: { type: String },
  75. assignedTo: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
  76. createdAt: { type: Date, default: Date.now }
  77. }],
  78. projects: [{
  79. projectId: { type: mongoose.Schema.Types.ObjectId },
  80. name: { type: String },
  81. description: { type: String },
  82. status: { type: String },
  83. contacts: [{ type: mongoose.Schema.Types.ObjectId }],
  84. assignedTo: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
  85. dueDate: { type: Date },
  86. createdAt: { type: Date, default: Date.now }
  87. }],
  88. meetings: [{
  89. meetingId: { type: mongoose.Schema.Types.ObjectId },
  90. title: { type: String },
  91. description: { type: String },
  92. contactIds: [{ type: mongoose.Schema.Types.ObjectId }],
  93. participantIds: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
  94. projectId: { type: mongoose.Schema.Types.ObjectId },
  95. start: { type: Date },
  96. end: { type: Date },
  97. location: { type: String },
  98. type: { type: String },
  99. createdBy: { type: mongoose.Schema.Types.ObjectId, ref: "User" }
  100. }],
  101. notes: [{
  102. noteId: { type: mongoose.Schema.Types.ObjectId },
  103. content: { type: String },
  104. createdBy: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
  105. linkedTo: {
  106. type: { type: String },
  107. id: { type: mongoose.Schema.Types.ObjectId }
  108. },
  109. createdAt: { type: Date, default: Date.now }
  110. }],
  111. tasks: [{
  112. taskId: { type: mongoose.Schema.Types.ObjectId },
  113. title: { type: String },
  114. description: { type: String },
  115. dueDate: { type: Date },
  116. assignedTo: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
  117. status: { type: String },
  118. projectId: { type: mongoose.Schema.Types.ObjectId },
  119. contactId: { type: mongoose.Schema.Types.ObjectId },
  120. createdAt: { type: Date, default: Date.now }
  121. }],
  122. teams: [{
  123. _id: { type: mongoose.Schema.Types.ObjectId, ref: "Team" },
  124. code: { type: String },
  125. name: { type: String }
  126. }],
  127. lastAccessed: {
  128. type: Date,
  129. default: null
  130. },
  131. // MySQL connection fields (kept from our previous changes)
  132. mysqlHost: {
  133. type: String,
  134. default: null
  135. },
  136. mysqlPort: {
  137. type: Number,
  138. default: 3306
  139. },
  140. mysqlUser: {
  141. type: String,
  142. default: null
  143. },
  144. mysqlPassword: {
  145. type: String,
  146. default: null
  147. },
  148. mysqlDatabase: {
  149. type: String,
  150. default: null
  151. },
  152. mysqlTable: {
  153. type: String,
  154. default: null
  155. },
  156. mysqlConnectionType: {
  157. type: String,
  158. enum: ['direct', 'ssh', null],
  159. default: null
  160. }
  161. },
  162. { timestamps: true, toJSON: { virtuals: true } }
  163. );
  164. schema.virtual("id").get(function () {
  165. return this._id;
  166. });
  167. // Pre-save hook to generate slug if not provided
  168. schema.pre('save', function(next) {
  169. if (!this.slug && this.name) {
  170. this.slug = this.name.toLowerCase().replace(/\s+/g, '-').replace(/[^\w\-]+/g, '');
  171. }
  172. // If userId is provided but ownerId is not, use userId as ownerId
  173. if (this.userId && !this.ownerId) {
  174. this.ownerId = this.userId;
  175. }
  176. next();
  177. });
  178. const Workspace = localDatabase.model("Workspace", schema, "workspace");
  179. export default Workspace;