refs #5866 addBackTransferClient #1682

Merged
jorgep merged 4 commits from 5866-addTicketTransfer into dev 2023-07-20 13:28:04 +00:00
5 changed files with 71 additions and 69 deletions
Showing only changes of commit 618989052a - Show all commits

View File

@ -0,0 +1,49 @@
const models = require('vn-loopback/server/server').models;
describe('Ticket transferClient()', () => {
const userId = 9;
const activeCtx = {
accessToken: {userId: userId},
};
const ctx = {req: activeCtx};
it('should throw an error as the ticket is not editable', async() => {
const tx = await models.Ticket.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const ticketId = 4;
const clientId = 1;
await models.Ticket.transferClient(ctx, ticketId, clientId, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.message).toEqual(`The current ticket can't be modified`);
});
it('should be assigned a different clientFk', async() => {
const tx = await models.Ticket.beginTransaction({});
let updatedTicket;
const ticketId = 10;
const clientId = 1;
try {
const options = {transaction: tx};
await models.Ticket.transferClient(ctx, ticketId, clientId, options);
updatedTicket = await models.Ticket.findById(ticketId, {fields: ['clientFk']}, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
expect(updatedTicket.clientFk).toEqual(clientId);
});
});

View File

@ -12,58 +12,38 @@ module.exports = Self => {
http: {source: 'path'}
},
{
arg: 'data',
type: 'object',
arg: 'clientId',
type: 'number',
required: true,
description: 'the client id',
http: {source: 'body'}
},
],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/:id/transferClient`,
verb: 'PATCH'
}
});
Self.transferClient = async(ctx, ticketId, params) => {
Self.transferClient = async(ctx, ticketId, clientId, options) => {
const models = Self.app.models;
const args = ctx.args;
const myOptions = {};
try {
const clientId = params.clientId;
const isEditable = await Self.isEditable(ctx, args.id, myOptions);
console.log('es editable?',isEditable) // Revisar
/* if (!isEditable){
console.log('no es editable!')
throw new UserError(`The sales of this ticket can't be modified`);
} */
const ticket = await models.Ticket.findById(ticketId, myOptions);
console.log('ticket',ticket);
if(!ticket) return false;
const nparams = {
clientFk: clientId,
addressFk: ticket.addressFk,
}
const promise = await ticket.updateAttributes(nparams);
console.log('promise', promise);
if(promise) return true;
} catch (error) {
console.log(error);
}
/* if (typeof options == 'object')
if (typeof options == 'object')
Object.assign(myOptions, options);
const ticket = await Self.findById(id, {
fields: ['isDeleted', 'refFk']
}, myOptions); */
const isEditable = await Self.isEditable(ctx, ticketId, myOptions);
return false;
if (!isEditable)
throw new UserError(`The current ticket can't be modified`);
const ticket = await models.Ticket.findById(
ticketId,
{fields: ['id', 'shipped', 'clientFk', 'addressFk']},
myOptions
);
const client = await models.Client.findById(clientId, {fields: ['id', 'defaultAddressFk']}, myOptions);
await ticket.updateAttributes({
clientFk: clientId,
addressFk: client.defaultAddressFk,
});
};
};
};

View File

@ -98,26 +98,12 @@ class Controller extends Section {
transferClient() {
const ticket = this.ticket;
const clientId = ticket.client.id;
console.log('ticketId',ticket.id)
console.log('clientId',clientId);
this.$http.patch(`Tickets/${ticket.id}/transferClient`, {clientId}).then(() =>{
this.vnApp.showSuccess(this.$t('Data saved!'));
this.reload();
})
this.$http.get(`Clients/${this.ticket.client.id}`).then(client => {
const ticket = this.ticket;
const params =
{
clientFk: client.data.id,
addressFk: client.data.defaultAddressFk,
};
this.$http.patch(`Tickets/${ticket.id}`, params).then(() => {
this.$http.patch(`Tickets/${ticket.id}/transferClient`, {clientId})
.then(() => {
this.vnApp.showSuccess(this.$t('Data saved!'));
this.reload();
});
});
}
isTicketEditable() {

View File

@ -326,17 +326,4 @@ describe('Ticket Component vnTicketDescriptorMenu', () => {
expect(controller.vnApp.showSuccess).toHaveBeenCalled();
});
});
describe('transferClient()', () => {
it(`should perform two queries, a get to obtain the clientData and a patch to update the ticket`, () => {
const client =
{
clientFk: 1101,
addressFk: 1,
};
$httpBackend.expect('GET', `Clients/${ticket.client.id}`).respond(client);
$httpBackend.expect('PATCH', `Tickets/${ticket.id}`).respond();
controller.transferClient();
});
});
});