Files
headup_api/models/mongo/Dropzone.js
T
2023-09-12 21:43:57 +02:00

35 lines
949 B
JavaScript

var mongoose = require('mongoose');
var User = mongoose.model('User');
var slug = require('slug');
var DropzoneSchema = new mongoose.Schema({
slug: { type: String, lowercase: true, unique: true },
lieu: String,
oaci: String,
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
}, {timestamps: true, toJSON: {virtuals: true}});
DropzoneSchema.pre('validate', function (next) {
if (!this.slug) {
this.slugify();
}
next();
});
DropzoneSchema.methods.slugify = function () {
this.slug = slug(`${this.lieu} ${this.oaci}`) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36);
};
DropzoneSchema.methods.toJSONFor = function(user){
return {
slug: this.slug,
lieu: this.lieu,
oaci: this.oaci,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
author: this.author.toProfileJSONFor(user)
};
};
mongoose.model('Dropzone', DropzoneSchema);