2018-11-20 09:32:54 +00:00
|
|
|
exports.UserError = class UserError extends Error {
|
|
|
|
constructor(message, ...translateArgs) {
|
2018-01-29 11:37:54 +00:00
|
|
|
super(message);
|
2018-08-02 07:49:00 +00:00
|
|
|
this.name = 'UserError';
|
2018-01-29 11:37:54 +00:00
|
|
|
this.statusCode = 400;
|
2018-11-20 09:32:54 +00:00
|
|
|
this.translateArgs = translateArgs;
|
2018-01-29 11:37:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getFinalState = function(ctx) {
|
|
|
|
if (ctx.isNewInstance)
|
|
|
|
return ctx.instance;
|
2018-11-20 09:32:54 +00:00
|
|
|
if (ctx.currentInstance) {
|
2018-01-29 11:37:54 +00:00
|
|
|
return Object.assign({},
|
|
|
|
ctx.currentInstance.__data,
|
|
|
|
ctx.data || ctx.instance
|
|
|
|
);
|
2018-11-20 09:32:54 +00:00
|
|
|
}
|
2018-01-29 11:37:54 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.isMultiple = function(ctx) {
|
|
|
|
return !ctx.isNewInstance && !ctx.currentInstance;
|
|
|
|
};
|
2018-11-20 09:32:54 +00:00
|
|
|
|
|
|
|
/* exports.fkToValue = async function(instance, ctx, model) {
|
|
|
|
let transaction = ctx.options && ctx.options.transaction;
|
|
|
|
let cleanInstance = JSON.parse(JSON.stringify(instance));
|
|
|
|
let result = {};
|
|
|
|
for (let key in cleanInstance) {
|
|
|
|
let val = cleanInstance[key];
|
|
|
|
if (val === undefined || val === null) continue;
|
|
|
|
for (let key1 in model.relations) {
|
|
|
|
let val1 = model.relations[key1];
|
|
|
|
if (val1.keyFrom == key && key != 'id') {
|
|
|
|
let recordSet = await val1.modelTo.findById(val, {}, {transaction});
|
|
|
|
val = recordSet.name || recordSet.id; // FIXME preparar todos los modelos con campo name
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result[key] = val;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
*/
|