refs #5866 addBackTransferClient #1682

Merged
jorgep merged 4 commits from 5866-addTicketTransfer into dev 2023-07-20 13:28:04 +00:00
6 changed files with 105 additions and 23 deletions

View File

@ -0,0 +1,2 @@
INSERT INTO `salix`.`ACL` (`model`,`property`,`accessType`,`permission`,`principalType`,`principalId`)
VALUES ('Ticket','transferClient','WRITE','ALLOW','ROLE','administrative');

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

@ -0,0 +1,49 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('transferClient', {
description: 'Transfering ticket to another client',
accessType: 'WRITE',
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'the ticket id',
http: {source: 'path'}
},
{
arg: 'clientId',
type: 'number',
required: true,
},
],
http: {
path: `/:id/transferClient`,
verb: 'PATCH'
}
});
Self.transferClient = async(ctx, ticketId, clientId, options) => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const isEditable = await Self.isEditable(ctx, ticketId, myOptions);
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

@ -19,6 +19,7 @@ module.exports = function(Self) {
require('../methods/ticket/uploadFile')(Self); require('../methods/ticket/uploadFile')(Self);
require('../methods/ticket/addSale')(Self); require('../methods/ticket/addSale')(Self);
require('../methods/ticket/transferSales')(Self); require('../methods/ticket/transferSales')(Self);
require('../methods/ticket/transferClient')(Self);
require('../methods/ticket/recalculateComponents')(Self); require('../methods/ticket/recalculateComponents')(Self);
require('../methods/ticket/sendSms')(Self); require('../methods/ticket/sendSms')(Self);
require('../methods/ticket/isLocked')(Self); require('../methods/ticket/isLocked')(Self);

View File

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

View File

@ -326,17 +326,4 @@ describe('Ticket Component vnTicketDescriptorMenu', () => {
expect(controller.vnApp.showSuccess).toHaveBeenCalled(); 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();
});
});
}); });