From 9bd69baaf166aa575bf396c3f15b6569fa4acb78 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez <=> Date: Tue, 7 Aug 2018 11:06:40 +0200 Subject: [PATCH] #569 ticket tracking back unit tests plus tiny refactor --- .../models/specs/ticket-tracking.spec.js | 24 +++++++++++++++++++ .../ticket/common/models/ticket-tracking.js | 6 ++--- 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 services/ticket/common/models/specs/ticket-tracking.spec.js diff --git a/services/ticket/common/models/specs/ticket-tracking.spec.js b/services/ticket/common/models/specs/ticket-tracking.spec.js new file mode 100644 index 000000000..961d1732c --- /dev/null +++ b/services/ticket/common/models/specs/ticket-tracking.spec.js @@ -0,0 +1,24 @@ +const app = require(`${servicesDir}/ticket/server/server`); + +describe('ticket model TicketTracking', () => { + let ticketTrackingId; + + afterAll(async() => { + await app.models.TicketTracking.destroyById(ticketTrackingId); + }); + + it('should store the worker id into the instance', async() => { + let originalTrackings = await app.models.TicketTracking.find({where: {ticketFk: 1}}); + + expect(originalTrackings.length).toEqual(1); + + await app.models.TicketTracking.create({ticketFk: 1, stateFk: 20}); + let result = await app.models.TicketTracking.find({where: {ticketFk: 1}}); + + let workerFk = result[1].workerFk; + ticketTrackingId = result[1].id; + + expect(result.length).toEqual(2); + expect(workerFk).toEqual(1); + }); +}); diff --git a/services/ticket/common/models/ticket-tracking.js b/services/ticket/common/models/ticket-tracking.js index 5875a7816..45acf7f90 100644 --- a/services/ticket/common/models/ticket-tracking.js +++ b/services/ticket/common/models/ticket-tracking.js @@ -6,9 +6,9 @@ module.exports = function(Self) { Self.observe('before save', async function(ctx) { let models = Self.app.models; let token = ctx.options.accessToken; - let userId = token && token.userId; + let currentUserId = token && token.userId; - let user = await models.Worker.findOne({where: {userFk: userId}}); - ctx.instance.workerFk = user.id; + let worker = await models.Worker.findOne({where: {userFk: currentUserId}}); + ctx.instance.workerFk = worker.id; }); };