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);
|
2020-02-11 15:38:28 +01:00
|
|
|
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
|
2020-02-13 22:36:14 +01:00
|
|
|
if (!this.haveRight(req)) {
|
2020-02-10 16:13:42 +01:00
|
|
|
callback(new ErrorBuilder(401.1, "You're not allowed"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// On teste le body
|
2020-02-13 22:36:14 +01:00
|
|
|
this.checkCreateOneValues(req, (err, value) => {
|
2020-02-10 16:13:42 +01:00
|
|
|
// Il manque un paramètre dans le body
|
|
|
|
if (err) {
|
|
|
|
callback(err, value);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:36:14 +01:00
|
|
|
this.insertItem(req, value, callback);
|
2020-02-10 16:13:42 +01:00
|
|
|
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";
|
|
|
|
|
2020-02-13 22:36:14 +01:00
|
|
|
this.createQuery(req, (err, query, limit) => {
|
2020-02-10 16:13:42 +01:00
|
|
|
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, {
|
2020-02-13 22:36:14 +01:00
|
|
|
data: this.formatItems(req, values),
|
|
|
|
...QueryBuilder.createPagination(
|
2020-02-10 16:13:42 +01:00
|
|
|
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";
|
|
|
|
|
2020-02-13 22:36:14 +01:00
|
|
|
this.createQuery(req, (err, query) => {
|
2020-02-10 16:13:42 +01:00
|
|
|
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;
|
2020-02-13 22:36:14 +01:00
|
|
|
if (req.method === "GET") {
|
|
|
|
if (this.params.format) {
|
|
|
|
const formatRules = this.params.format[req.user.role];
|
2020-02-10 16:13:42 +01:00
|
|
|
|
2020-02-13 22:36:14 +01:00
|
|
|
if (formatRules) {
|
|
|
|
formatedItem = this.formatItem(item, formatRules);
|
|
|
|
}
|
2020-02-10 16:13:42 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-13 22:36:14 +01:00
|
|
|
|
2020-02-10 16:13:42 +01:00
|
|
|
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
|
2020-02-13 22:36:14 +01:00
|
|
|
if (!this.haveRight(req)) {
|
2020-02-10 16:13:42 +01:00
|
|
|
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) {
|
2020-02-11 15:38:28 +01:00
|
|
|
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 => {
|
2020-02-13 22:36:14 +01:00
|
|
|
if (this.params.belongsToMany) {
|
|
|
|
const needCreate = [];
|
|
|
|
let created = 0;
|
|
|
|
const _next = errNext => {
|
|
|
|
if (errNext) {
|
|
|
|
// TODO: break and revert
|
|
|
|
} else {
|
|
|
|
created += 1;
|
|
|
|
|
|
|
|
if (created === needCreate.length) {
|
|
|
|
callback(null, updated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Object.keys(this.params.belongsToMany).map(key => {
|
|
|
|
if (value[key]) {
|
|
|
|
needCreate.push(key);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (needCreate.length > 0) {
|
|
|
|
for (let i = 0; i < needCreate.length; i += 1) {
|
|
|
|
const currentKey = needCreate[i];
|
|
|
|
const currentValue = value[currentKey];
|
|
|
|
this.createBelonsTo(
|
|
|
|
updated,
|
|
|
|
currentKey,
|
|
|
|
currentValue,
|
|
|
|
"add",
|
|
|
|
_next
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callback(null, updated);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callback(null, updated);
|
|
|
|
}
|
2020-02-10 16:13:42 +01:00
|
|
|
})
|
|
|
|
.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
|
2020-02-13 22:36:14 +01:00
|
|
|
if (!this.haveRight(req)) {
|
2020-02-10 16:13:42 +01:00
|
|
|
callback(new ErrorBuilder(401.1, "You're not allowed"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getOne(req, (err, item) => {
|
|
|
|
if (err) {
|
|
|
|
callback(err, item);
|
2020-02-13 22:36:14 +01:00
|
|
|
} else {
|
|
|
|
let deletedBelongs = 0;
|
|
|
|
const needToDeleteBelongs = [];
|
2020-02-10 16:13:42 +01:00
|
|
|
|
2020-02-13 22:36:14 +01:00
|
|
|
const _deleteItem = () => {
|
|
|
|
item
|
|
|
|
.destroy()
|
|
|
|
.then(() => {
|
|
|
|
callback(null, {});
|
|
|
|
})
|
|
|
|
.catch(callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
const _next = () => {
|
|
|
|
deletedBelongs += 1;
|
|
|
|
|
|
|
|
if (deletedBelongs === needToDeleteBelongs.length) {
|
|
|
|
_deleteItem();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.params.belongsToMany) {
|
|
|
|
Object.keys(this.params.belongsToMany).map(key => {
|
|
|
|
const collection = this.params.belongsToMany[key];
|
|
|
|
if (item[collection].length) {
|
|
|
|
needToDeleteBelongs.push(key);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (needToDeleteBelongs.length > 0) {
|
|
|
|
for (let i = 0; i < needToDeleteBelongs.length; i += 1) {
|
|
|
|
const currentKey = needToDeleteBelongs[i];
|
|
|
|
const collection = this.params.belongsToMany[currentKey];
|
|
|
|
const valuesToDelete = [];
|
|
|
|
for (let j = 0; j < item[collection].length; j += 1) {
|
|
|
|
valuesToDelete.push(item[collection][j].id);
|
|
|
|
}
|
|
|
|
this.deleteBelongsTo(item, currentKey, valuesToDelete, _next);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_deleteItem();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_deleteItem();
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 16:13:42 +01:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Middleware;
|