87 lines
2.4 KiB
JavaScript
87 lines
2.4 KiB
JavaScript
var mongoose = require('mongoose');
|
|
var uniqueValidator = require('mongoose-unique-validator');
|
|
var slug = require('slug');
|
|
|
|
var JumpSchema = new mongoose.Schema({
|
|
slug: { type: String, lowercase: true, unique: true },
|
|
date: { type: Date, default: Date.now },
|
|
numero: { type: Number, unique: true },
|
|
lieu: String,
|
|
oaci: String,
|
|
aeronef: String,
|
|
imat: String,
|
|
hauteur: Number,
|
|
deploiement: Number,
|
|
voile: String,
|
|
taille: Number,
|
|
categorie: String,
|
|
module: String,
|
|
participants: Number,
|
|
sautants: [String],
|
|
programme: String,
|
|
accessoires: String,
|
|
zone: String,
|
|
dossier: String,
|
|
video: String,
|
|
files: [{ type: mongoose.Schema.Types.ObjectId, ref: 'JumpFile' }],
|
|
x2data: { type: mongoose.Schema.Types.ObjectId, ref: 'X2DataLog' },
|
|
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
|
|
}, {timestamps: true, toJSON: {virtuals: true}});
|
|
|
|
JumpSchema.plugin(uniqueValidator, { message: 'is already taken' });
|
|
|
|
JumpSchema.pre('validate', function (next) {
|
|
if (!this.slug) {
|
|
this.slugify();
|
|
}
|
|
next();
|
|
});
|
|
|
|
JumpSchema.methods.slugify = function () {
|
|
this.slug = slug(`${this.numero} ${this.lieu}`) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36);
|
|
};
|
|
|
|
JumpSchema.methods.addFile = function (id) {
|
|
if (this.files.indexOf(id) === -1) {
|
|
this.files.push(id);
|
|
}
|
|
return this.save();
|
|
};
|
|
|
|
JumpSchema.methods.removeFile = function (id) {
|
|
this.files.remove(id);
|
|
return this.save();
|
|
};
|
|
|
|
JumpSchema.methods.toJSONFor = function(user) {
|
|
return {
|
|
slug: this.slug,
|
|
date: this.date,
|
|
numero: this.numero,
|
|
lieu: this.lieu,
|
|
oaci: this.oaci,
|
|
aeronef: this.aeronef,
|
|
imat: this.imat,
|
|
hauteur: this.hauteur,
|
|
deploiement: this.deploiement,
|
|
voile: this.voile,
|
|
taille: this.taille,
|
|
categorie: this.categorie,
|
|
module: this.module,
|
|
participants: this.participants,
|
|
sautants: this.sautants,
|
|
programme: this.programme,
|
|
accessoires: this.accessoires,
|
|
zone: this.zone,
|
|
dossier: this.dossier,
|
|
video: this.video,
|
|
files: this.files.map(file => file ? file.toJSONForJump() : null),
|
|
x2data: this.x2data ? this.x2data.toJSONFor() : null,
|
|
createdAt: this.createdAt,
|
|
updatedAt: this.updatedAt,
|
|
author: this.author.toProfileJSONFor(user)
|
|
};
|
|
};
|
|
|
|
mongoose.model('Jump', JumpSchema);
|