2022-04-05 01:15:43 +00:00
|
|
|
import mongoose from "mongoose";
|
|
|
|
import ModelNameRegister from "./modelNameRegister.js";
|
|
|
|
|
|
|
|
const Types = mongoose.Schema.Types; // Some types require defining from this object.
|
|
|
|
|
|
|
|
const matchSchema = new mongoose.Schema({
|
|
|
|
title: { type: String, required: true, trim: true },
|
|
|
|
when: { type: Date, required: true },
|
|
|
|
publicity: { type: Number, required: true, default: 2 },
|
|
|
|
location: {
|
|
|
|
type: [Number],
|
|
|
|
required: true,
|
|
|
|
validate: {
|
|
|
|
validator: function (v) {
|
|
|
|
return v.length === 2;
|
|
|
|
},
|
|
|
|
message: "Invalid coordinate format (array not length of 2)."
|
|
|
|
}
|
|
|
|
},
|
|
|
|
creator: { type: Types.ObjectId, ref: ModelNameRegister.User },
|
|
|
|
participants: { type: [{ type: Types.ObjectId, ref: ModelNameRegister.User }], required: true, default: [] },
|
|
|
|
difficulty: { type: Number, required: true },
|
|
|
|
sport: { type: Types.ObjectId, ref: ModelNameRegister.Sport },
|
2022-04-08 01:23:32 +00:00
|
|
|
createDate: { type: Date, required: true, default: Date.now() }
|
2022-04-05 01:15:43 +00:00
|
|
|
});
|
|
|
|
|
2022-04-05 03:50:26 +00:00
|
|
|
matchSchema.pre("remove", function (next) {
|
|
|
|
const match = this;
|
|
|
|
match.populate("creator").populate("participants");
|
|
|
|
|
|
|
|
match.participants.forEach(participant => {
|
|
|
|
const index = participant.participatingMatches.indexOf(match._id);
|
|
|
|
participant.participatingMatches.splice(index, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
match.creator.createdMatches.splice(match.creator.createdMatches.indexOf(match._id), 1);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2022-04-05 01:15:43 +00:00
|
|
|
export default mongoose.model(ModelNameRegister.Match, matchSchema);
|