salix/services/loopback/common/helpers.js

47 lines
1.4 KiB
JavaScript

exports.UserError = class UserError extends Error {
constructor(message, ...translateArgs) {
super(message);
this.name = 'UserError';
this.statusCode = 400;
this.translateArgs = translateArgs;
}
};
exports.getFinalState = function(ctx) {
if (ctx.isNewInstance)
return ctx.instance;
if (ctx.currentInstance) {
return Object.assign({},
ctx.currentInstance.__data,
ctx.data || ctx.instance
);
}
return null;
};
exports.isMultiple = function(ctx) {
return !ctx.isNewInstance && !ctx.currentInstance;
};
/* 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;
};
*/