123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- // api/workspaces/workspace.model.js
- import mongoose from "mongoose";
- import localDatabase from "../../common/localDatabase.js";
- const schema = new localDatabase.Schema(
- {
- name: {
- type: String,
- required: true,
- trim: true
- },
- slug: {
- type: String,
- trim: true
- },
- description: {
- type: String,
- default: ""
- },
- timezone: {
- type: String,
- default: "UTC"
- },
- filePath: {
- type: String,
- default: null
- },
- fileType: {
- type: String,
- default: null
- },
- fileSize: {
- type: Number,
- default: 0
- },
- metadata: {
- type: Object,
- default: {}
- },
- isPublic: {
- type: Boolean,
- default: false
- },
- accountId: {
- type: mongoose.Schema.Types.ObjectId,
- ref: "Account",
- required: true
- },
- ownerId: {
- type: mongoose.Schema.Types.ObjectId,
- ref: "User",
- required: true
- },
-
- // Keeping original userId field for backward compatibility
- userId: {
- type: mongoose.Schema.Types.ObjectId,
- ref: "User",
- required: true
- },
-
- // Nested structures
- members: [{
- userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
- name: { type: String },
- email: { type: String },
- role: { type: String, enum: ["admin", "user", "guest"], default: "user" },
- joinedAt: { type: Date, default: Date.now }
- }],
-
- contacts: [{
- contactId: { type: mongoose.Schema.Types.ObjectId },
- name: { type: String },
- email: { type: String },
- phone: { type: String },
- company: { type: String },
- tags: [{ type: String }],
- status: { type: String },
- assignedTo: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
- createdAt: { type: Date, default: Date.now }
- }],
-
- projects: [{
- projectId: { type: mongoose.Schema.Types.ObjectId },
- name: { type: String },
- description: { type: String },
- status: { type: String },
- contacts: [{ type: mongoose.Schema.Types.ObjectId }],
- assignedTo: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
- dueDate: { type: Date },
- createdAt: { type: Date, default: Date.now }
- }],
-
- meetings: [{
- meetingId: { type: mongoose.Schema.Types.ObjectId },
- title: { type: String },
- description: { type: String },
- contactIds: [{ type: mongoose.Schema.Types.ObjectId }],
- participantIds: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
- projectId: { type: mongoose.Schema.Types.ObjectId },
- start: { type: Date },
- end: { type: Date },
- location: { type: String },
- type: { type: String },
- createdBy: { type: mongoose.Schema.Types.ObjectId, ref: "User" }
- }],
-
- notes: [{
- noteId: { type: mongoose.Schema.Types.ObjectId },
- content: { type: String },
- createdBy: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
- linkedTo: {
- type: { type: String },
- id: { type: mongoose.Schema.Types.ObjectId }
- },
- createdAt: { type: Date, default: Date.now }
- }],
-
- tasks: [{
- taskId: { type: mongoose.Schema.Types.ObjectId },
- title: { type: String },
- description: { type: String },
- dueDate: { type: Date },
- assignedTo: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
- status: { type: String },
- projectId: { type: mongoose.Schema.Types.ObjectId },
- contactId: { type: mongoose.Schema.Types.ObjectId },
- createdAt: { type: Date, default: Date.now }
- }],
-
- teams: [{
- _id: { type: mongoose.Schema.Types.ObjectId, ref: "Team" },
- code: { type: String },
- name: { type: String }
- }],
-
- lastAccessed: {
- type: Date,
- default: null
- },
-
- // MySQL connection fields (kept from our previous changes)
- mysqlHost: {
- type: String,
- default: null
- },
- mysqlPort: {
- type: Number,
- default: 3306
- },
- mysqlUser: {
- type: String,
- default: null
- },
- mysqlPassword: {
- type: String,
- default: null
- },
- mysqlDatabase: {
- type: String,
- default: null
- },
- mysqlTable: {
- type: String,
- default: null
- },
- mysqlConnectionType: {
- type: String,
- enum: ['direct', 'ssh', null],
- default: null
- }
- },
- { timestamps: true, toJSON: { virtuals: true } }
- );
- schema.virtual("id").get(function () {
- return this._id;
- });
- // Pre-save hook to generate slug if not provided
- schema.pre('save', function(next) {
- if (!this.slug && this.name) {
- this.slug = this.name.toLowerCase().replace(/\s+/g, '-').replace(/[^\w\-]+/g, '');
- }
-
- // If userId is provided but ownerId is not, use userId as ownerId
- if (this.userId && !this.ownerId) {
- this.ownerId = this.userId;
- }
-
- next();
- });
- const Workspace = localDatabase.model("Workspace", schema, "workspace");
- export default Workspace;
|