{"size": 1258, "ext": "js", "max_stars_repo_path": "models/schedule.js", "max_stars_repo_name": "harish32/scheduler", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "models/schedule.js", "max_issues_repo_name": "harish32/scheduler", "max_issues_repo_issues_event_min_datetime": "2022-03-26T05:51:36.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-26T05:51:36.000Z", "max_forks_repo_path": "models/schedule.js", "max_forks_repo_name": "harish32/scheduler", "max_forks_repo_forks_event_min_datetime": "2020-03-30T14:50:56.000Z", "max_forks_repo_forks_event_max_datetime": "2020-03-30T14:50:56.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 19.9682539683, "max_line_length": 60, "alphanum_fraction": 0.5055643879}
const mongoose = require("mongoose");
const User = require("./user");

const scheduleSchema = mongoose.Schema({
  title: {
    type: String,
    required: [true, "please enter a title"]
  },
  date: String,
  slots: [
    {
      date: {
        type: Date,
        default: Date.now()
      },
      timeSlots: [
        {
          count: {
            type: Number,
            required: [true, "please add the count"]
          },
          from: {
            type: String,
            required: ["true", "please add the from timing"]
          },
          end: {
            type: String,
            required: ["true", "please add the end timing"]
          },
          total: Number
        }
      ]
    }
  ],
  batchId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Batch"
  },
  users: [
    {
      user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
      }
    }
  ]
});

// scheduleSchema.pre("save", function() {
//   this.total = this.count;
// });

scheduleSchema.pre("remove", async function() {
  await User.updateMany(
    { batchId: this.batchId },
    { $pull: { schedules: { scheduleId: this._id } } }
  );
});

const Schedule = mongoose.model("Schedule", scheduleSchema);

module.exports = Schedule;
