2020-10-13 13:55:24 +02:00
|
|
|
const Joi = require('joi');
|
2021-08-02 15:39:40 +02:00
|
|
|
const findIndex = require('lodash/findIndex');
|
2020-10-13 13:55:24 +02:00
|
|
|
Joi.objectId = require('joi-objectid')(Joi);
|
|
|
|
|
|
|
|
const schema = Joi.objectId();
|
2021-12-22 20:18:06 +00:00
|
|
|
const duplicatedProjectIds = [
|
|
|
|
'bd7158d8c442eddfaeb5bd18',
|
|
|
|
'587d78af367417b2b2512b03',
|
|
|
|
'587d78af367417b2b2512b04',
|
|
|
|
'587d78b0367417b2b2512b05',
|
|
|
|
'bd7158d8c242eddfaeb5bd13'
|
|
|
|
];
|
2018-01-19 14:03:17 -05:00
|
|
|
|
|
|
|
class MongoIds {
|
|
|
|
constructor() {
|
|
|
|
this.knownIds = [];
|
|
|
|
}
|
|
|
|
check(id, title) {
|
2020-10-13 13:55:24 +02:00
|
|
|
try {
|
2021-05-05 11:11:57 +02:00
|
|
|
schema.validate(id);
|
2020-10-13 13:55:24 +02:00
|
|
|
} catch {
|
2021-12-22 20:18:06 +00:00
|
|
|
throw Error(`Expected a valid ObjectId for ${title}, but got ${id}`);
|
2018-01-19 14:03:17 -05:00
|
|
|
}
|
2020-10-13 13:55:24 +02:00
|
|
|
|
|
|
|
const idIndex = findIndex(this.knownIds, existing => id === existing);
|
2021-12-22 20:18:06 +00:00
|
|
|
if (idIndex !== -1 && !duplicatedProjectIds.includes(id)) {
|
|
|
|
throw Error(`The id for challenge ${title} appears more than once.
|
|
|
|
With the exception of duplicatedProjectIds this should not happen.
|
2018-01-19 14:03:17 -05:00
|
|
|
`);
|
|
|
|
}
|
2019-02-18 19:32:49 +00:00
|
|
|
this.knownIds = [...this.knownIds, id];
|
2018-01-19 14:03:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 16:21:53 +03:00
|
|
|
module.exports = MongoIds;
|