Source: models/registrationModel.js

/**
 * @file Defines the Mongoose schema and model for an Event Registration.
 * @module models/registrationModel
 * @requires mongoose
 */

const mongoose = require("mongoose");

/**
 * @typedef Registration
 * @property {mongoose.Types.ObjectId} user - The user who registered for the event (ref: User, required).
 * @property {mongoose.Types.ObjectId} event - The event for which the user registered (ref: Event, required).
 * @property {Date} registrationDate - The date when the registration was made (defaults to Date.now).
 * @property {Date} createdAt - Timestamp when the registration was created (auto-generated).
 * @property {Date} updatedAt - Timestamp when the registration was last updated (auto-generated).
 */

/**
 * Mongoose schema for the Registration collection.
 * @constant
 * @type {mongoose.Schema<Registration>}
 */
const registrationSchema = new mongoose.Schema(
  {
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
    event: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Event",
      required: true,
    },
    registrationDate: {
      type: Date,
      default: Date.now,
    },
  },
  {
    timestamps: true, // adds createdAt and updatedAt
  }
);

/**
 * Compound index to ensure a user can only register for an event once.
 * @name user_event_unique_index
 * @memberof module:models/registrationModel~registrationSchema
 */
registrationSchema.index({ user: 1, event: 1 }, { unique: true });

/**
 * The Mongoose model for a Registration, compiled from the registrationSchema.
 * @type {mongoose.Model<Registration>}
 */
const Registration = mongoose.model("Registration", registrationSchema);

module.exports = Registration;