#803 nueva comprobacion para eliminar un ticket

This commit is contained in:
Gerard 2018-11-20 10:32:54 +01:00
parent 118ec97106
commit 272ad9fe50
4 changed files with 53 additions and 10 deletions

View File

@ -1,20 +1,21 @@
exports.UserError = class extends Error {
constructor(message) {
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)
if (ctx.currentInstance) {
return Object.assign({},
ctx.currentInstance.__data,
ctx.data || ctx.instance
);
}
return null;
};
@ -22,3 +23,24 @@ exports.getFinalState = function(ctx) {
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;
};
*/

View File

@ -57,5 +57,5 @@
"The sales of this ticket can't be modified": "Los movimientos de este tiquet no pueden ser modificadas",
"You can't create an order for a inactive client": "You can't create an order for a inactive client",
"You can't create an order for a client that doesn't has tax data verified": "You can't create an order for a client that doesn't has tax data verified",
"You don't have enough privileges": "You don't have enough privileges"
"You must delete the claim id %d first": "Antes debes borrar la reclamacion %d"
}

View File

@ -1,3 +1,5 @@
const UserError = require('../../../common/helpers').UserError;
module.exports = Self => {
Self.remoteMethodCtx('deleted', {
description: 'Sets the isDeleted value of a ticket to 1',
@ -23,6 +25,10 @@ module.exports = Self => {
let currentTicket = await Self.app.models.Ticket.findById(params.id);
await currentTicket.updateAttributes({isDeleted: '1'});
let claimOfATicket = await Self.app.models.Claim.findOne({where: {ticketFk: params.id}});
if (claimOfATicket)
throw new UserError('You must delete the claim id %d first', claimOfATicket.id);
if (ctx.req.accessToken) {
let token = ctx.req.accessToken;
let currentUserId = token && token.userId;

View File

@ -28,4 +28,19 @@ describe('ticket deleted()', () => {
expect(deletedTicket.isDeleted).toEqual(true);
expect(changedState.stateFk).toEqual(17);
});
it('should throw an error if the given ticket has a claim', async () => {
let ctx = {req: {accessToken: {userId: 9}}};
let params = {id: 16};
let error;
try {
await app.models.Ticket.deleted(ctx, params);
} catch (e) {
error = e;
}
expect(error.translateArgs[0]).toEqual(2);
expect(error.message).toEqual('You must delete the claim id %d first');
});
});