#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 UserError extends Error {
exports.UserError = class extends Error { constructor(message, ...translateArgs) {
constructor(message) {
super(message); super(message);
this.name = 'UserError'; this.name = 'UserError';
this.statusCode = 400; this.statusCode = 400;
this.translateArgs = translateArgs;
} }
}; };
exports.getFinalState = function(ctx) { exports.getFinalState = function(ctx) {
if (ctx.isNewInstance) if (ctx.isNewInstance)
return ctx.instance; return ctx.instance;
if (ctx.currentInstance) if (ctx.currentInstance) {
return Object.assign({}, return Object.assign({},
ctx.currentInstance.__data, ctx.currentInstance.__data,
ctx.data || ctx.instance ctx.data || ctx.instance
); );
}
return null; return null;
}; };
@ -22,3 +23,24 @@ exports.getFinalState = function(ctx) {
exports.isMultiple = function(ctx) { exports.isMultiple = function(ctx) {
return !ctx.isNewInstance && !ctx.currentInstance; 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", "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 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 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 => { module.exports = Self => {
Self.remoteMethodCtx('deleted', { Self.remoteMethodCtx('deleted', {
description: 'Sets the isDeleted value of a ticket to 1', description: 'Sets the isDeleted value of a ticket to 1',
@ -19,10 +21,14 @@ module.exports = Self => {
} }
}); });
Self.deleted = async(ctx, params) => { Self.deleted = async (ctx, params) => {
let currentTicket = await Self.app.models.Ticket.findById(params.id); let currentTicket = await Self.app.models.Ticket.findById(params.id);
await currentTicket.updateAttributes({isDeleted: '1'}); 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) { if (ctx.req.accessToken) {
let token = ctx.req.accessToken; let token = ctx.req.accessToken;
let currentUserId = token && token.userId; let currentUserId = token && token.userId;

View File

@ -3,21 +3,21 @@ const app = require(`${servicesDir}/ticket/server/server`);
describe('ticket deleted()', () => { describe('ticket deleted()', () => {
let ticket; let ticket;
beforeAll(async() => { beforeAll(async () => {
let originalTicket = await app.models.Ticket.findOne({where: {id: 16}}); let originalTicket = await app.models.Ticket.findOne({where: {id: 16}});
originalTicket.id = null; originalTicket.id = null;
ticket = await app.models.Ticket.create(originalTicket); ticket = await app.models.Ticket.create(originalTicket);
}); });
afterAll(async() => { afterAll(async () => {
await app.models.Ticket.destroyById(ticket.id); await app.models.Ticket.destroyById(ticket.id);
}); });
it('should make sure the ticket is not deleted yet', async() => { it('should make sure the ticket is not deleted yet', async () => {
expect(ticket.isDeleted).toEqual(false); expect(ticket.isDeleted).toEqual(false);
}); });
it('should set a ticket to deleted and log the change on TicketState table', async() => { it('should set a ticket to deleted and log the change on TicketState table', async () => {
let ctx = {req: {accessToken: {userId: 9}}}; let ctx = {req: {accessToken: {userId: 9}}};
let params = {id: ticket.id}; let params = {id: ticket.id};
await app.models.Ticket.deleted(ctx, params); await app.models.Ticket.deleted(ctx, params);
@ -28,4 +28,19 @@ describe('ticket deleted()', () => {
expect(deletedTicket.isDeleted).toEqual(true); expect(deletedTicket.isDeleted).toEqual(true);
expect(changedState.stateFk).toEqual(17); 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');
});
}); });