#803 nueva comprobacion para eliminar un ticket
This commit is contained in:
parent
118ec97106
commit
272ad9fe50
|
@ -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;
|
||||
};
|
||||
*/
|
||||
|
|
|
@ -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"
|
||||
}
|
|
@ -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',
|
||||
|
@ -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);
|
||||
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;
|
||||
|
|
|
@ -3,21 +3,21 @@ const app = require(`${servicesDir}/ticket/server/server`);
|
|||
describe('ticket deleted()', () => {
|
||||
let ticket;
|
||||
|
||||
beforeAll(async() => {
|
||||
beforeAll(async () => {
|
||||
let originalTicket = await app.models.Ticket.findOne({where: {id: 16}});
|
||||
originalTicket.id = null;
|
||||
ticket = await app.models.Ticket.create(originalTicket);
|
||||
});
|
||||
|
||||
afterAll(async() => {
|
||||
afterAll(async () => {
|
||||
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);
|
||||
});
|
||||
|
||||
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 params = {id: ticket.id};
|
||||
await app.models.Ticket.deleted(ctx, params);
|
||||
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue