diff --git a/modules/ticket/back/methods/ticket-tracking/changeState.js b/modules/ticket/back/methods/ticket-tracking/changeState.js
index 065692137..e28413b24 100644
--- a/modules/ticket/back/methods/ticket-tracking/changeState.js
+++ b/modules/ticket/back/methods/ticket-tracking/changeState.js
@@ -14,7 +14,7 @@ module.exports = Self => {
}
],
returns: {
- type: 'string',
+ type: 'Object',
root: true
},
http: {
@@ -53,6 +53,6 @@ module.exports = Self => {
if (!isAllowed)
throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED');
- return await models.TicketTracking.create(params);
+ return models.TicketTracking.create(params);
};
};
diff --git a/modules/ticket/back/methods/ticket-tracking/setDelivered.js b/modules/ticket/back/methods/ticket-tracking/setDelivered.js
new file mode 100644
index 000000000..509f5b446
--- /dev/null
+++ b/modules/ticket/back/methods/ticket-tracking/setDelivered.js
@@ -0,0 +1,50 @@
+module.exports = Self => {
+ Self.remoteMethodCtx('setDelivered', {
+ description: 'Changes the state of the received ticket ids to delivered',
+ accessType: 'WRITE',
+ accepts: [
+ {
+ arg: 'ticketIds',
+ description: 'the array of ticket ids to set as delivered',
+ type: ['Number'],
+ required: true,
+ http: {source: 'body'}
+ }
+ ],
+ returns: {
+ type: 'Object',
+ root: true
+ },
+ http: {
+ path: `/setDelivered`,
+ verb: 'POST'
+ }
+ });
+
+ Self.setDelivered = async(ctx, ticketIds) => {
+ let userId = ctx.req.accessToken.userId;
+ let models = Self.app.models;
+
+ let state = await models.State.findOne({
+ where: {
+ code: 'delivered'
+ },
+ fields: ['id', 'name', 'alertLevel', 'code']
+ });
+
+ let worker = await models.Worker.findOne({where: {userFk: userId}});
+
+ let promises = [];
+ for (let id of ticketIds) {
+ let promise = models.TicketTracking.changeState(ctx, {
+ stateFk: state.id,
+ workerFk: worker.id,
+ ticketFk: id
+ });
+ promises.push(promise);
+ }
+ await Promise.all(promises);
+
+ return state;
+ };
+};
diff --git a/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js b/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js
new file mode 100644
index 000000000..520ccfb03
--- /dev/null
+++ b/modules/ticket/back/methods/ticket-tracking/specs/setDelivered.spec.js
@@ -0,0 +1,36 @@
+const app = require('vn-loopback/server/server');
+
+describe('ticket setDelivered()', () => {
+ let ticketOne;
+ let ticketTwo;
+
+ beforeAll(async done => {
+ let originalTicketOne = await app.models.Ticket.findById(8);
+ let originalTicketTwo = await app.models.Ticket.findById(10);
+
+ originalTicketOne.id = null;
+ originalTicketTwo.id = null;
+ ticketOne = await app.models.Ticket.create(originalTicketOne);
+ ticketTwo = await app.models.Ticket.create(originalTicketTwo);
+
+ done();
+ });
+
+ afterAll(async done => {
+ await app.models.Ticket.destroyById(ticketOne.id);
+ await app.models.Ticket.destroyById(ticketTwo.id);
+
+ done();
+ });
+
+ it('should return the state which has been applied to the given tickets', async() => {
+ let ctx = {req: {accessToken: {userId: 49}}};
+ let delivered = await app.models.State.findOne({where: {code: 'delivered'}, fields: ['id']});
+
+ let params = [ticketOne.id, ticketTwo.id];
+ let state = await app.models.TicketTracking.setDelivered(ctx, params);
+
+ expect(state.id).toEqual(delivered.id);
+ });
+});
+
diff --git a/modules/ticket/back/models/ticket-tracking.js b/modules/ticket/back/models/ticket-tracking.js
index 2b2a93536..2e6d3403e 100644
--- a/modules/ticket/back/models/ticket-tracking.js
+++ b/modules/ticket/back/models/ticket-tracking.js
@@ -1,5 +1,6 @@
module.exports = function(Self) {
require('../methods/ticket-tracking/changeState')(Self);
+ require('../methods/ticket-tracking/setDelivered')(Self);
Self.validatesPresenceOf('stateFk', {message: 'State cannot be blank'});
Self.validatesPresenceOf('workerFk', {message: 'Worker cannot be blank'});
diff --git a/modules/ticket/front/index/index.html b/modules/ticket/front/index/index.html
index f75e9d9f7..abd772229 100644
--- a/modules/ticket/front/index/index.html
+++ b/modules/ticket/front/index/index.html
@@ -89,7 +89,7 @@