Skip to content

Commit

Permalink
Aftral: [premieroctet#153] No duplicate code on resource
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaauvray committed Sep 20, 2024
1 parent b27925d commit 67e14b9
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {schemaOptions} = require('../../../utils/schemas')
const Schema = mongoose.Schema
const {BLOCK_DISCRIMINATOR}=require('../consts')
const { DUMMY_REF } = require('../../../utils/database')
const { BadRequestError } = require('../../../utils/errors')

const ResourceSchema = new Schema({
shortName: {
Expand All @@ -24,4 +25,14 @@ const ResourceSchema = new Schema({
},
}, {...schemaOptions, ...BLOCK_DISCRIMINATOR})

ResourceSchema.pre('validate', async function(next) {
return mongoose.models.resource.exists({_id: {$ne: this._id}, code: this.code})
.then(exists => {
if (exists) {
return next(new BadRequestError(`Une resource de code ${this.code} existe déjà`))
}
return next()
})
})

module.exports = ResourceSchema

0 comments on commit 67e14b9

Please sign in to comment.