diff --git a/modules/ticket/back/methods/ticket-collection/hasUncheckedTicket.js b/modules/ticket/back/methods/ticket-collection/hasUncheckedTicket.js new file mode 100644 index 000000000..1ea522de6 --- /dev/null +++ b/modules/ticket/back/methods/ticket-collection/hasUncheckedTicket.js @@ -0,0 +1,40 @@ +module.exports = Self => { + Self.remoteMethod('hasUncheckedTicket', { + description: + 'Get boolean if the collection of ticket has a ticket not checked', + accessType: 'READ', + accepts: [{ + arg: 'ticketFk', + type: 'integer', + required: true, + description: 'Ticket id' + }], + returns: { + type: 'boolean', + root: true + }, + http: { + path: `/hasUncheckedTicket`, + verb: 'GET' + } + }); + + Self.hasUncheckedTicket = async(ticketFk, options) => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const result = await Self.rawSql(` + SELECT tc2.ticketFk + FROM vn.ticketCollection tc + JOIN vn.ticketCollection tc2 ON tc2.collectionFk = tc.collectionFk + LEFT JOIN vn.ticketState ts ON ts.ticketFk = tc2.ticketFk + JOIN vn.state s ON s.id = ts.stateFk + JOIN vn.state s2 ON s2.code = 'CHECKED' + WHERE tc.ticketFk = ? AND s.order < s2.id + LIMIT 1;`, + [ticketFk], myOptions); + return result.length > 0 && result[0]['ticketFk'] > 0; + }; +}; diff --git a/modules/ticket/back/methods/ticket-collection/spec/hasUncheckedTicket.spec.js b/modules/ticket/back/methods/ticket-collection/spec/hasUncheckedTicket.spec.js new file mode 100644 index 000000000..ae7a8e6ee --- /dev/null +++ b/modules/ticket/back/methods/ticket-collection/spec/hasUncheckedTicket.spec.js @@ -0,0 +1,38 @@ + +const {models} = require('vn-loopback/server/server'); + +describe('ticketCollection hasUncheckedTicket()', () => { + it('should return false because there are not tickets not checked', async() => { + const ticketFk = 1; + const result = await models.TicketCollection.hasUncheckedTicket(ticketFk); + + expect(result).toBe(false); + }); + + it('should return true because there is a ticket not checked', async() => { + const ticketFk = 1; + const stateFkCurrent = 16; + const stateFkUpdated = 7; + + const tx = await models.TicketTracking.beginTransaction({}); + const myOptions = {transaction: tx}; + const filter = {where: { + ticketFk: 1, + stateFk: stateFkCurrent} + }; + try { + const ticketTracking = await models.TicketTracking.findOne(filter, myOptions); + await ticketTracking.updateAttributes({ + stateFk: stateFkUpdated + }, myOptions); + const result = await models.TicketCollection.hasUncheckedTicket(ticketFk, myOptions); + + expect(result).toBe(true); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); + diff --git a/modules/ticket/back/models/ticket-collection.js b/modules/ticket/back/models/ticket-collection.js new file mode 100644 index 000000000..c5cd75a94 --- /dev/null +++ b/modules/ticket/back/models/ticket-collection.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/ticket-collection/hasUncheckedTicket')(Self); +}; diff --git a/modules/ticket/back/models/ticket-collection.json b/modules/ticket/back/models/ticket-collection.json index 4bd34f08e..bc1c2dd18 100644 --- a/modules/ticket/back/models/ticket-collection.json +++ b/modules/ticket/back/models/ticket-collection.json @@ -13,6 +13,9 @@ }, "usedShelves": { "type": "number" + }, + "collectionFk": { + "type": "number" } }, "relations": { @@ -20,6 +23,11 @@ "type": "belongsTo", "model": "Ticket", "foreignKey": "ticketFk" + }, + "collection": { + "type": "belongsTo", + "model": "Collection", + "foreignKey": "collectionFk" } } } \ No newline at end of file