team.model.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // api/team.model.js
  2. import {
  3. default as localDatabase,
  4. default as mongoose,
  5. } from "../../common/localDatabase.js";
  6. /*const schema = new localDatabase.Schema(
  7. {
  8. name: String,
  9. code: String,
  10. users: [
  11. {
  12. _id: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
  13. email: { type: String },
  14. },
  15. ],
  16. },
  17. { timestamps: true, toJSON: { virtuals: true } }
  18. );*/
  19. // In team.model.js
  20. const schema = new localDatabase.Schema(
  21. {
  22. name: String,
  23. code: String,
  24. accountId: { // Add this field
  25. type: mongoose.Schema.Types.ObjectId,
  26. ref: "Account",
  27. required: true // Make it required
  28. },
  29. users: [
  30. {
  31. _id: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
  32. email: { type: String },
  33. },
  34. ],
  35. },
  36. { timestamps: true, toJSON: { virtuals: true } }
  37. );
  38. schema.virtual("id").get(function () {
  39. return this._id;
  40. });
  41. schema.statics.maxTeamsPerPlan = function (plan) {
  42. if (plan == "starter") {
  43. return 5;
  44. } else if (plan == "basic") {
  45. return 10;
  46. } else if (plan == "pro") {
  47. return 20;
  48. } else {
  49. return 0;
  50. }
  51. };
  52. const Team = localDatabase.model("Team", schema, "team");
  53. export default Team;