diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index 8660d61c9..30564d8a2 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -527,7 +527,8 @@ INSERT INTO `vn`.`observationType`(`id`,`description`, `code`) (4, 'SalesPerson', 'salesPerson'), (5, 'Administrative', 'administrative'), (6, 'Weight', 'weight'), - (7, 'InvoiceOut', 'invoiceOut'); + (7, 'InvoiceOut', 'invoiceOut'), + (8, 'DropOff', 'dropOff'); INSERT INTO `vn`.`addressObservation`(`id`,`addressFk`,`observationTypeFk`,`description`) VALUES diff --git a/db/versions/10987-tealMonstera/00-firstScript.sql b/db/versions/10987-tealMonstera/00-firstScript.vn.sql similarity index 87% rename from db/versions/10987-tealMonstera/00-firstScript.sql rename to db/versions/10987-tealMonstera/00-firstScript.vn.sql index e78d54fd4..d24ddd5de 100644 --- a/db/versions/10987-tealMonstera/00-firstScript.sql +++ b/db/versions/10987-tealMonstera/00-firstScript.vn.sql @@ -1,4 +1,4 @@ -- Place your SQL code here USE vn; -INSERT INTO vn.observationType (description,code) VALUES ('Entrega','dropOff') \ No newline at end of file +INSERT INTO vn.observationType (description,code) VALUES ('Entrega','dropOff'); \ No newline at end of file diff --git a/modules/ticket/back/methods/ticket-observation/addDropOff.js b/modules/ticket/back/methods/ticket-observation/addDropOff.js new file mode 100644 index 000000000..52a12a58e --- /dev/null +++ b/modules/ticket/back/methods/ticket-observation/addDropOff.js @@ -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) { + 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; + } + }; +}; diff --git a/modules/ticket/back/methods/ticket-observation/specs/addDropOff.spec.js b/modules/ticket/back/methods/ticket-observation/specs/addDropOff.spec.js new file mode 100644 index 000000000..1034dbe67 --- /dev/null +++ b/modules/ticket/back/methods/ticket-observation/specs/addDropOff.spec.js @@ -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; + } + }); +}); diff --git a/modules/ticket/back/models/ticket-observation.js b/modules/ticket/back/models/ticket-observation.js index 77d15d85c..3076484bf 100644 --- a/modules/ticket/back/models/ticket-observation.js +++ b/modules/ticket/back/models/ticket-observation.js @@ -1,6 +1,7 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { + require('../methods/ticket-observation/addDropOff')(Self); Self.rewriteDbError(function(err) { if (err.code === 'ER_DUP_ENTRY') return new UserError(`The observation type can't be repeated`);