6921_addFromDelivery #2304
|
@ -527,7 +527,8 @@ INSERT INTO `vn`.`observationType`(`id`,`description`, `code`)
|
||||||
(4, 'SalesPerson', 'salesPerson'),
|
(4, 'SalesPerson', 'salesPerson'),
|
||||||
(5, 'Administrative', 'administrative'),
|
(5, 'Administrative', 'administrative'),
|
||||||
(6, 'Weight', 'weight'),
|
(6, 'Weight', 'weight'),
|
||||||
(7, 'InvoiceOut', 'invoiceOut');
|
(7, 'InvoiceOut', 'invoiceOut'),
|
||||||
|
(8, 'DropOff', 'dropOff');
|
||||||
|
|
||||||
INSERT INTO `vn`.`addressObservation`(`id`,`addressFk`,`observationTypeFk`,`description`)
|
INSERT INTO `vn`.`addressObservation`(`id`,`addressFk`,`observationTypeFk`,`description`)
|
||||||
VALUES
|
VALUES
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
-- Place your SQL code here
|
-- Place your SQL code here
|
||||||
|
|
||||||
USE vn;
|
USE vn;
|
||||||
INSERT INTO vn.observationType (description,code) VALUES ('Entrega','dropOff')
|
INSERT INTO vn.observationType (description,code) VALUES ('Entrega','dropOff');
|
|
@ -0,0 +1,54 @@
|
||||||
|
|
||||||
|
module.exports = Self => {
|
||||||
|
Self.remoteMethod('addDropOff', {
|
||||||
|
description: 'Add a dropOff note in a ticket',
|
||||||
|
accessType: 'WRITE',
|
||||||
|
accepts: [{
|
||||||
|
arg: 'ticketFk',
|
||||||
|
type: 'number',
|
||||||
|
required: true,
|
||||||
|
description: 'ticket ID'
|
||||||
|
}, {
|
||||||
|
arg: 'note',
|
||||||
|
type: 'string',
|
||||||
|
required: true,
|
||||||
|
description: 'note text'
|
||||||
|
}],
|
||||||
|
|
||||||
|
http: {
|
||||||
|
path: `/addDropOff`,
|
||||||
|
verb: 'post'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Self.addDropOff = async(ticketFk, note, options) => {
|
||||||
|
const models = Self.app.models;
|
||||||
|
const myOptions = {};
|
||||||
|
let tx;
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
|
if (!myOptions.transaction) {
|
||||||
sergiodt marked this conversation as resolved
Outdated
|
|||||||
|
tx = await Self.beginTransaction({});
|
||||||
|
myOptions.transaction = tx;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const observationTypeDropOff = await models.ObservationType.findOne({
|
||||||
|
where: {code: 'dropOff'}
|
||||||
|
}, myOptions);
|
||||||
|
|
||||||
|
await models.TicketObservation.create({
|
||||||
|
ticketFk: ticketFk,
|
||||||
|
observationTypeFk: observationTypeDropOff.id,
|
||||||
|
description: note
|
||||||
|
|
||||||
|
}, myOptions);
|
||||||
|
|
||||||
|
if (tx) await tx.commit();
|
||||||
|
} catch (error) {
|
||||||
|
if (tx) await tx.rollback();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
|
@ -0,0 +1,37 @@
|
||||||
|
const {models} = require('vn-loopback/server/server');
|
||||||
|
|
||||||
|
describe('ticketObservation addDropOff()', () => {
|
||||||
|
const ticketFk = 5;
|
||||||
|
const note = 'DropOff note';
|
||||||
|
const code = 'dropOff';
|
||||||
|
|
||||||
|
it('should return a dropOff note', async() => {
|
||||||
|
const myOptions = {};
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
|
if (!myOptions.transaction) {
|
||||||
|
tx = await models.TicketObservation.beginTransaction({});
|
||||||
|
myOptions.transaction = tx;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await models.TicketObservation.addDropOff(
|
||||||
|
ticketFk, note, myOptions);
|
||||||
|
|
||||||
|
const observationTypeDropOff = await models.TicketObservation.find({
|
||||||
|
where: {
|
||||||
|
ticketFk,
|
||||||
|
code
|
||||||
|
}
|
||||||
|
}, myOptions);
|
||||||
|
|
||||||
|
expect(observationTypeDropOff.length).toEqual(1);
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,6 +1,7 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
|
require('../methods/ticket-observation/addDropOff')(Self);
|
||||||
Self.rewriteDbError(function(err) {
|
Self.rewriteDbError(function(err) {
|
||||||
if (err.code === 'ER_DUP_ENTRY')
|
if (err.code === 'ER_DUP_ENTRY')
|
||||||
return new UserError(`The observation type can't be repeated`);
|
return new UserError(`The observation type can't be repeated`);
|
||||||
|
|
Loading…
Reference in New Issue
no cal començar la transaccio sols hi ha un create