sequelize-middleware/libs/Middleware.js

213 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-02-10 16:13:42 +01:00
import QueryBuilder from "./QueryBuilder";
import ErrorBuilder from "./ErrorBuilder";
class Middleware extends QueryBuilder {
/**
* Initialisation de la classe
* @param {Object} params
* @param {Object} models
*/
constructor(params, models) {
super(params, models);
this.params = params;
this.models = models;
2020-02-10 16:13:42 +01:00
this.includes = [];
}
/**
* Fonction permettant de créer un nouvel élément
* @param {Object} req
* @param {Function} callback
* @return {Boolean}
*/
createOne(req, callback) {
// On test les droits
if (!this._haveRight(req)) {
callback(new ErrorBuilder(401.1, "You're not allowed"));
return false;
}
// On teste le body
this._checkCreateOneValues(req, (err, value) => {
// Il manque un paramètre dans le body
if (err) {
callback(err, value);
return false;
}
this._insertItem(req, value, callback);
return true;
});
return true;
}
/**
* Fonction permettant de retourner une liste d'items
* @param {Object} req
* @param {Function} callback
*/
getAll(req, callback) {
// On spécifie le type de requête (list ou item)
req.getType = "list";
this._createQuery(req, (err, query, limit) => {
if (err) {
callback(err, query);
return false;
}
this.models[this.params.model]
.findAndCountAll(query)
.then(res => {
if (!res || res.count === 0) {
callback();
return false;
}
const rows = [];
for (let i = 0; i < res.rows.length; i += 1) {
const current = res.rows[i];
let found = false;
for (let j = 0; j < rows.length; j += 1) {
if (rows[j].id === current.id) {
found = true;
rows[j].assign({}, rows[j], current);
break;
}
}
if (!found || rows.length === 0) {
rows.push(current);
}
}
const total = rows.length;
const values = rows.slice(limit.start, limit.start + limit.limit);
callback(null, {
data: this._formatItems(req, values),
...QueryBuilder._createPagination(
req,
total,
limit.limit,
limit.start
)
});
return true;
})
.catch(callback);
return true;
});
}
/**
* Fonction permettant de retourner un item par son Id
* @param {Object} req
* @param {Function} callback
*/
getOne(req, callback) {
// On spécifie le type de requête (list ou item)
req.getType = "item";
this._createQuery(req, (err, query) => {
if (err) {
callback(err, query);
return false;
}
this.models[this.params.model]
.findOne(query)
.then(item => {
if (!item) {
callback(new ErrorBuilder(404, "Item not found"));
return false;
}
let formatedItem = item;
if (this.params.format) {
const formatRules = this.params.format[req.user.role];
if (formatRules) {
formatedItem = this._formatItem(item, formatRules);
}
}
callback(null, formatedItem);
return true;
})
.catch(callback);
return true;
});
}
/**
* Fonction permettant de mettre à jour un item
* @param {Object} req
* @param {Function} callback
* @return {Boolean}
*/
patchOne(req, callback) {
// On test les droits
if (!this._haveRight(req)) {
callback(new ErrorBuilder(401.1, "You're not allowed"));
return false;
}
// On teste les params
const { error, value } = this.params.validate.update.validate(req.body, {
abortEarly: false
});
// Un paramètre n'est pas bon dans les params
if (error) {
callback(new ErrorBuilder(406.1, error));
2020-02-10 16:13:42 +01:00
return false;
}
this.getOne(req, (err, item) => {
if (err) {
callback(err, item);
return false;
}
item
.update(value)
.then(updated => {
callback(null, updated);
})
.catch(callback);
return true;
});
return true;
}
/**
* Fonction permettant de supprimer un item
* @param {Object} req
* @param {Function} callback
* @return {Boolean}
*/
deleteOne(req, callback) {
// On test les droits
if (!this._haveRight(req)) {
callback(new ErrorBuilder(401.1, "You're not allowed"));
return false;
}
this.getOne(req, (err, item) => {
if (err) {
callback(err, item);
return false;
}
item
.destroy()
.then(() => {
callback(null, {});
})
.catch(callback);
return true;
});
return true;
}
}
export default Middleware;