diff --git a/back/methods/chat/sendCheckingPresence.js b/back/methods/chat/sendCheckingPresence.js index 16fa01dc9..c1bc565eb 100644 --- a/back/methods/chat/sendCheckingPresence.js +++ b/back/methods/chat/sendCheckingPresence.js @@ -24,11 +24,16 @@ module.exports = Self => { } }); - Self.sendCheckingPresence = async(ctx, recipientId, message) => { + Self.sendCheckingPresence = async(ctx, recipientId, message, options) => { if (!recipientId) return false; + let myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + const models = Self.app.models; - const account = await models.Account.findById(recipientId); + const account = await models.Account.findById(recipientId, null, myOptions); const userId = ctx.req.accessToken.userId; if (recipientId == userId) return false; @@ -37,14 +42,14 @@ module.exports = Self => { throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`); const query = `SELECT worker_isWorking(?) isWorking`; - const [result] = await Self.rawSql(query, [recipientId]); + const [result] = await Self.rawSql(query, [recipientId], myOptions); if (!result.isWorking) { const workerDepartment = await models.WorkerDepartment.findById(recipientId, { include: { relation: 'department' } - }); + }, myOptions); const department = workerDepartment && workerDepartment.department(); const channelName = department && department.chatName; diff --git a/modules/route/back/methods/route/getTickets.js b/modules/route/back/methods/route/getTickets.js index 7d9f3f254..3dfc5d73a 100644 --- a/modules/route/back/methods/route/getTickets.js +++ b/modules/route/back/methods/route/getTickets.js @@ -26,6 +26,10 @@ module.exports = Self => { Self.getTickets = async(filter, options) => { const conn = Self.dataSource.connector; + let myOptions = {}; + if (typeof options == 'object') + Object.assign(myOptions, options); + const stmt = new ParameterizedSQL( `SELECT t.id, @@ -66,7 +70,9 @@ module.exports = Self => { stmt.merge(conn.makeSuffix(filter)); - const tickets = await conn.executeStmt(stmt, options); + const tickets = await conn.executeStmt(stmt, myOptions); + + if (tickets.length === 1 && !tickets[0].id) return; return tickets; }; diff --git a/modules/route/front/tickets/index.html b/modules/route/front/tickets/index.html index 621a9b3c5..6a5a4ce7c 100644 --- a/modules/route/front/tickets/index.html +++ b/modules/route/front/tickets/index.html @@ -113,7 +113,7 @@ + on-accept="$ctrl.removeTicketFromRoute($index)"> { - addresses = addresses + '+to:' + line.address.postalCode + ' ' + line.address.city + ' ' + line.address.street; + addresses = addresses + '+to:' + line.postalCode + ' ' + line.city + ' ' + line.street; }); window.open(url + addresses, '_blank'); @@ -78,10 +78,11 @@ class Controller extends Section { this.$.confirm.show(); } - removeTicketFromRoute() { + removeTicketFromRoute($index) { let params = {routeFk: null}; let query = `Tickets/${this.selectedTicket}/`; this.$http.patch(query, params).then(() => { + this.$.model.remove($index); this.vnApp.showSuccess(this.$t('Ticket removed from route')); this.updateVolume(); }); diff --git a/modules/ticket/back/methods/ticket-request/filter.js b/modules/ticket/back/methods/ticket-request/filter.js index 5778b1db2..9b864f632 100644 --- a/modules/ticket/back/methods/ticket-request/filter.js +++ b/modules/ticket/back/methods/ticket-request/filter.js @@ -9,48 +9,57 @@ module.exports = Self => { accepts: [ { arg: 'ctx', - type: 'Object', + type: 'object', http: {source: 'context'} - }, { + }, + { arg: 'filter', - type: 'Object', + type: 'object', description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string` - }, { + }, + { arg: 'search', - type: 'String', + type: 'string', description: `If it's and integer searchs by id, otherwise it searchs by nickname` - }, { + }, + { arg: 'ticketFk', - type: 'Number', + type: 'number', description: `Searchs by ticketFk` - }, { + }, + { arg: 'warehouseFk', - type: 'Number', + type: 'number', description: `Search by warehouse` - }, { + }, + { arg: 'attenderFk', - type: 'Number', + type: 'number', description: `Search requests attended by a given worker id` - }, { + }, + { arg: 'mine', - type: 'Boolean', + type: 'boolean', description: `Search requests attended by the current user` - }, { + }, + { arg: 'from', - type: 'Date', + type: 'date', description: `Date from` - }, { + }, + { arg: 'to', - type: 'Date', + type: 'date', description: `Date to` - }, { + }, + { arg: 'state', - type: 'String', + type: 'string', description: `Search request by request state` } ], returns: { - type: ['Object'], + type: ['object'], root: true }, http: { diff --git a/modules/ticket/back/methods/ticket/componentUpdate.js b/modules/ticket/back/methods/ticket/componentUpdate.js index fe395e562..c4d1baed1 100644 --- a/modules/ticket/back/methods/ticket/componentUpdate.js +++ b/modules/ticket/back/methods/ticket/componentUpdate.js @@ -5,65 +5,76 @@ module.exports = Self => { Self.remoteMethodCtx('componentUpdate', { description: 'Save ticket sale components', accessType: 'WRITE', - accepts: [{ - arg: 'id', - type: 'Number', - required: true, - description: 'The ticket id', - http: {source: 'path'} - }, { - arg: 'clientFk', - type: 'Number', - description: 'The client id', - required: true - }, { - arg: 'agencyModeFk', - type: 'Number', - description: 'The agencyMode id', - required: true - }, { - arg: 'addressFk', - type: 'Number', - description: 'The address id', - required: true - }, { - arg: 'zoneFk', - type: 'Number', - description: 'The zone id', - required: true - }, { - arg: 'warehouseFk', - type: 'Number', - description: 'The warehouse id', - required: true - }, { - arg: 'companyFk', - type: 'Number', - description: 'The company id', - required: true - }, { - arg: 'shipped', - type: 'Date', - description: 'The shipped date', - required: true - }, { - arg: 'landed', - type: 'Date', - description: 'The landing date', - required: true - }, { - arg: 'isDeleted', - type: 'Boolean', - description: 'Ticket is deleted', - required: true - }, { - arg: 'option', - type: 'Number', - description: 'Action id', - required: true - }], + accepts: [ + { + arg: 'id', + type: 'number', + required: true, + description: 'The ticket id', + http: {source: 'path'} + }, + { + arg: 'clientFk', + type: 'number', + description: 'The client id', + required: true + }, + { + arg: 'agencyModeFk', + type: 'number', + description: 'The agencyMode id', + required: true + }, + { + arg: 'addressFk', + type: 'number', + description: 'The address id', + required: true + }, + { + arg: 'zoneFk', + type: 'number', + description: 'The zone id', + required: true + }, + { + arg: 'warehouseFk', + type: 'number', + description: 'The warehouse id', + required: true + }, + { + arg: 'companyFk', + type: 'number', + description: 'The company id', + required: true + }, + { + arg: 'shipped', + type: 'date', + description: 'The shipped date', + required: true + }, + { + arg: 'landed', + type: 'date', + description: 'The landing date', + required: true + }, + { + arg: 'isDeleted', + type: 'boolean', + description: 'Ticket is deleted', + required: true + }, + { + arg: 'option', + type: 'number', + description: 'Action id', + required: true + }], returns: { - type: ['Object'], + type: ['object'], root: true }, http: { @@ -72,128 +83,156 @@ module.exports = Self => { } }); - Self.componentUpdate = async(ctx, id, clientFk, agencyModeFk, addressFk, zoneFk, warehouseFk, - companyFk, shipped, landed, isDeleted, option) => { - const userId = ctx.req.accessToken.userId; - const models = Self.app.models; - const $t = ctx.req.__; // $translate - const isEditable = await models.Ticket.isEditable(ctx, id); + Self.componentUpdate = async(ctx, options) => { + const args = ctx.args; + let tx; + let myOptions = {}; - if (!isEditable) - throw new UserError(`The sales of this ticket can't be modified`); + if (typeof options == 'object') + Object.assign(myOptions, options); - const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss'); - if (!isProductionBoss) { - const zoneShipped = await models.Agency.getShipped(landed, addressFk, agencyModeFk, warehouseFk); - - if (!zoneShipped || zoneShipped.zoneFk != zoneFk) - throw new UserError(`You don't have privileges to change the zone`); + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; } - const observationTypeDelivery = await models.ObservationType.findOne({ - where: {code: 'delivery'} - }); - const originalTicket = await models.Ticket.findOne({ - where: {id: id}, - fields: ['id', 'clientFk', 'agencyModeFk', 'addressFk', 'zoneFk', - 'warehouseFk', 'companyFk', 'shipped', 'landed', 'isDeleted'], - include: [ - { - relation: 'client', - scope: { - fields: 'salesPersonFk' - } - }] - }); - const updatedTicket = Object.assign({}, ctx.args); - delete updatedTicket.ctx; - delete updatedTicket.option; + try { + const userId = ctx.req.accessToken.userId; + const models = Self.app.models; + const $t = ctx.req.__; // $translate + const isEditable = await models.Ticket.isEditable(ctx, args.id, myOptions); - // Force to unroute ticket - const hasToBeUnrouted = true; - const query = 'CALL vn.ticket_componentMakeUpdate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; - const res = await Self.rawSql(query, [ - id, - clientFk, - agencyModeFk, - addressFk, - zoneFk, - warehouseFk, - companyFk, - shipped, - landed, - isDeleted, - hasToBeUnrouted, - option - ]); + if (!isEditable) + throw new UserError(`The sales of this ticket can't be modified`); - if (originalTicket.addressFk != updatedTicket.addressFk) { - const ticketObservation = await models.TicketObservation.findOne({ - where: { - ticketFk: id, - observationTypeFk: observationTypeDelivery.id} - }); + const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss', myOptions); + if (!isProductionBoss) { + const zoneShipped = await models.Agency.getShipped(args.landed, args.addressFk, args.agencyModeFk, args.warehouseFk, myOptions); - if (ticketObservation) - await ticketObservation.destroy(); + if (!zoneShipped || zoneShipped.zoneFk != args.zoneFk) + throw new UserError(`You don't have privileges to change the zone`); + } + const observationTypeDelivery = await models.ObservationType.findOne({ + where: {code: 'delivery'} + }, myOptions); - const address = await models.Address.findOne({ - where: {id: addressFk}, - include: { - relation: 'observations', - scope: { - where: {observationTypeFk: observationTypeDelivery.id}, - include: { - relation: 'observationType' + const originalTicket = await models.Ticket.findOne({ + where: {id: args.id}, + fields: [ + 'id', + 'clientFk', + 'agencyModeFk', + 'addressFk', + 'zoneFk', + 'warehouseFk', + 'companyFk', + 'shipped', + 'landed', + 'isDeleted' + ], + include: [ + { + relation: 'client', + scope: { + fields: 'salesPersonFk' + } + }] + }, myOptions); + const updatedTicket = Object.assign({}, args); + delete updatedTicket.ctx; + delete updatedTicket.option; + + // Force to unroute ticket + const hasToBeUnrouted = true; + const query = 'CALL vn.ticket_componentMakeUpdate(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; + const res = await Self.rawSql(query, [ + args.id, + args.clientFk, + args.agencyModeFk, + args.addressFk, + args.zoneFk, + args.warehouseFk, + args.companyFk, + args.shipped, + args.landed, + args.isDeleted, + hasToBeUnrouted, + args.option + ], myOptions); + + if (originalTicket.addressFk != updatedTicket.addressFk) { + const ticketObservation = await models.TicketObservation.findOne({ + where: { + ticketFk: args.id, + observationTypeFk: observationTypeDelivery.id} + }, myOptions); + + if (ticketObservation) + await ticketObservation.destroy(myOptions); + + const address = await models.Address.findOne({ + where: {id: args.addressFk}, + include: { + relation: 'observations', + scope: { + where: {observationTypeFk: observationTypeDelivery.id}, + include: { + relation: 'observationType' + } } } + }, myOptions); + const [observation] = address.observations(); + if (observation) { + await models.TicketObservation.create({ + ticketFk: args.id, + observationTypeFk: observation.observationTypeFk, + description: observation.description + }, myOptions); } - }); - const [observation] = address.observations(); - if (observation) { - await models.TicketObservation.create({ - ticketFk: id, - observationTypeFk: observation.observationTypeFk, - description: observation.description + } + + const changes = loggable.getChanges(originalTicket, updatedTicket); + const oldProperties = await loggable.translateValues(Self, changes.old); + const newProperties = await loggable.translateValues(Self, changes.new); + + await models.TicketLog.create({ + originFk: args.id, + userFk: userId, + action: 'update', + changedModel: 'Ticket', + changedModelId: args.id, + oldInstance: oldProperties, + newInstance: newProperties + }, myOptions); + + const salesPersonId = originalTicket.client().salesPersonFk; + if (salesPersonId) { + const origin = ctx.req.headers.origin; + + let changesMade = ''; + for (let change in newProperties) { + let value = newProperties[change]; + let oldValue = oldProperties[change]; + + changesMade += `\r\n~${$t(change)}: ${oldValue}~ ➔ *${$t(change)}: ${value}*`; + } + + const message = $t('Changed this data from the ticket', { + ticketId: args.id, + ticketUrl: `${origin}/#!/ticket/${args.id}/summary`, + changes: changesMade }); - } - } - - const changes = loggable.getChanges(originalTicket, updatedTicket); - const oldProperties = await loggable.translateValues(Self, changes.old); - const newProperties = await loggable.translateValues(Self, changes.new); - - await models.TicketLog.create({ - originFk: id, - userFk: userId, - action: 'update', - changedModel: 'Ticket', - changedModelId: id, - oldInstance: oldProperties, - newInstance: newProperties - }); - - const salesPersonId = originalTicket.client().salesPersonFk; - if (salesPersonId) { - const origin = ctx.req.headers.origin; - - let changesMade = ''; - for (let change in newProperties) { - let value = newProperties[change]; - let oldValue = oldProperties[change]; - - changesMade += `\r\n~${$t(change)}: ${oldValue}~ ➔ *${$t(change)}: ${value}*`; + await models.Chat.sendCheckingPresence(ctx, salesPersonId, message, myOptions); } - const message = $t('Changed this data from the ticket', { - ticketId: id, - ticketUrl: `${origin}/#!/ticket/${id}/summary`, - changes: changesMade - }); - await models.Chat.sendCheckingPresence(ctx, salesPersonId, message); - } + if (tx) await tx.commit(); - return res; + return res; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; }; diff --git a/modules/ticket/back/methods/ticket/isEditable.js b/modules/ticket/back/methods/ticket/isEditable.js index 317cfac96..1cdca9f11 100644 --- a/modules/ticket/back/methods/ticket/isEditable.js +++ b/modules/ticket/back/methods/ticket/isEditable.js @@ -19,15 +19,19 @@ module.exports = Self => { } }); - Self.isEditable = async(ctx, id) => { + Self.isEditable = async(ctx, id, options) => { const userId = ctx.req.accessToken.userId; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); let state = await Self.app.models.TicketState.findOne({ where: {ticketFk: id} - }); + }, myOptions); - const isSalesAssistant = await Self.app.models.Account.hasRole(userId, 'salesAssistant'); - const isProductionBoss = await Self.app.models.Account.hasRole(userId, 'productionBoss'); + const isSalesAssistant = await Self.app.models.Account.hasRole(userId, 'salesAssistant', myOptions); + const isProductionBoss = await Self.app.models.Account.hasRole(userId, 'productionBoss', myOptions); const isValidRole = isSalesAssistant || isProductionBoss; let alertLevel = state ? state.alertLevel : null; @@ -41,8 +45,8 @@ module.exports = Self => { } } }] - }); - const isLocked = await Self.app.models.Ticket.isLocked(id); + }, myOptions); + const isLocked = await Self.app.models.Ticket.isLocked(id, myOptions); const alertLevelGreaterThanZero = (alertLevel && alertLevel > 0); const isNormalClient = ticket && ticket.client().type().code == 'normal'; diff --git a/modules/ticket/back/methods/ticket/isLocked.js b/modules/ticket/back/methods/ticket/isLocked.js index 7cf7b807e..a6c3cc036 100644 --- a/modules/ticket/back/methods/ticket/isLocked.js +++ b/modules/ticket/back/methods/ticket/isLocked.js @@ -19,10 +19,15 @@ module.exports = Self => { } }); - Self.isLocked = async id => { + Self.isLocked = async(id, options) => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + const ticket = await Self.app.models.Ticket.findById(id, { fields: ['isDeleted', 'refFk'] - }); + }, myOptions); const isDeleted = ticket && ticket.isDeleted; const isInvoiced = ticket && ticket.refFk; diff --git a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js index 76733f2c6..1347af888 100644 --- a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js +++ b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js @@ -13,147 +13,125 @@ describe('ticket componentUpdate()', () => { let secondvalueBeforeChange; let componentOfSaleSeven; let componentOfSaleEight; + let componentValue; + + beforeAll(async() => { + const deliveryComponenet = await app.models.Component.findOne({where: {code: 'delivery'}}); + deliveryComponentId = deliveryComponenet.id; + componentOfSaleSeven = `SELECT value FROM vn.saleComponent WHERE saleFk = 7 AND componentFk = ${deliveryComponentId}`; + componentOfSaleEight = `SELECT value FROM vn.saleComponent WHERE saleFk = 8 AND componentFk = ${deliveryComponentId}`; + + [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven); + firstvalueBeforeChange = componentValue.value; + + [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight); + secondvalueBeforeChange = componentValue.value; + }); + + it('should change the agencyMode to modify the sale components value', async() => { + const tx = await app.models.SaleComponent.beginTransaction({}); - beforeAll(async done => { try { - let deliveryComponenet = await app.models.Component.findOne({where: {code: 'delivery'}}); - deliveryComponentId = deliveryComponenet.id; - componentOfSaleSeven = `SELECT value FROM vn.saleComponent WHERE saleFk = 7 AND componentFk = ${deliveryComponentId}`; - componentOfSaleEight = `SELECT value FROM vn.saleComponent WHERE saleFk = 8 AND componentFk = ${deliveryComponentId}`; + const options = {transaction: tx}; - [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven); - firstvalueBeforeChange = componentValue.value; + const args = { + id: ticketID, + clientFk: 102, + agencyModeFk: 8, + addressFk: 122, + zoneFk: 5, + warehouseFk: 1, + companyFk: 442, + shipped: today, + landed: tomorrow, + isDeleted: false, + option: 1 + }; - [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight); - secondvalueBeforeChange = componentValue.value; - } catch (error) { - console.error(error); + let ctx = { + args: args, + req: { + accessToken: {userId: userID}, + headers: {origin: 'http://localhost'}, + __: value => { + return value; + } + } + }; + + await app.models.Ticket.componentUpdate(ctx, options); + + [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven, null, options); + let firstvalueAfterChange = componentValue.value; + + [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight, null, options); + let secondvalueAfterChange = componentValue.value; + + expect(firstvalueBeforeChange).not.toEqual(firstvalueAfterChange); + expect(secondvalueBeforeChange).not.toEqual(secondvalueAfterChange); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; } - - done(); }); - it('should change the agencyMode to modify the sale components value and then undo the changes', async() => { - const clientID = 102; - const addressID = 122; - const agencyModeID = 8; - const warehouseID = 1; - const zoneID = 5; - const shipped = today; - const companyID = 442; - const isDeleted = false; - const landed = tomorrow; - const option = 1; + it('should change the addressFk and check that delivery observations have been changed', async() => { + const tx = await app.models.SaleComponent.beginTransaction({}); - let ctx = { - args: {clientFk: clientID, - agencyModeFk: agencyModeID}, - req: { - accessToken: {userId: userID}, - headers: {origin: 'http://localhost'}, - __: value => { - return value; + try { + const options = {transaction: tx}; + + const args = { + id: ticketID, + clientFk: 102, + agencyModeFk: 8, + addressFk: 2, + zoneFk: 5, + warehouseFk: 1, + companyFk: 442, + shipped: today, + landed: tomorrow, + isDeleted: false, + option: 1 + }; + + const ctx = { + args: args, + req: { + accessToken: {userId: userID}, + headers: {origin: 'http://localhost'}, + __: value => { + return value; + } } - } - }; + }; + const observationTypeDelivery = await app.models.ObservationType.findOne({ + where: {code: 'delivery'} + }, options); + const originalTicketObservation = await app.models.TicketObservation.findOne({ + where: { + ticketFk: args.id, + observationTypeFk: observationTypeDelivery.id} + }, options); - await app.models.Ticket.componentUpdate(ctx, ticketID, clientID, agencyModeID, addressID, - zoneID, warehouseID, companyID, shipped, landed, isDeleted, option); + expect(originalTicketObservation).toBeDefined(); - [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven); - let firstvalueAfterChange = componentValue.value; + await app.models.Ticket.componentUpdate(ctx, options); - [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight); - let secondvalueAfterChange = componentValue.value; + const removedTicketObservation = await app.models.TicketObservation.findOne({ + where: { + ticketFk: ticketID, + observationTypeFk: observationTypeDelivery.id} + }, options); - expect(firstvalueBeforeChange).not.toEqual(firstvalueAfterChange); - expect(secondvalueBeforeChange).not.toEqual(secondvalueAfterChange); + expect(removedTicketObservation).toBeNull(); - // restores - const restores = { - clientID: 102, - addressID: 122, - agencyModeID: 7, - warehouseID: 1, - zoneID: 3, - shipped: today, - companyID: 442, - isDeleted: false, - landed: tomorrow, - option: 1, - }; - - ctx.clientFk = restores.clientID; - ctx.agencyModeFk = restores.agencyModeID; - - await app.models.Ticket.componentUpdate(ctx, ticketID, restores.clientID, restores.agencyModeID, restores.addressID, - restores.zoneID, restores.warehouseID, restores.companyID, restores.shipped, restores.landed, restores.isDeleted, restores.option); - - [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven); - firstvalueAfterChange = componentValue.value; - - [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight); - secondvalueAfterChange = componentValue.value; - - expect(firstvalueBeforeChange).toEqual(firstvalueAfterChange); - expect(secondvalueBeforeChange).toEqual(secondvalueAfterChange); - }); - - it('should change the addressFk and check that delivery observations have been changed and then undo the changes', async() => { - const clientID = 102; - const addressID = 122; - const newAddressID = 2; - const agencyModeID = 8; - const warehouseID = 1; - const zoneID = 5; - const shipped = today; - const companyID = 442; - const isDeleted = false; - const landed = tomorrow; - const option = 1; - const ctx = { - args: {clientFk: clientID, - agencyModeFk: agencyModeID}, - req: { - accessToken: {userId: userID}, - headers: {origin: 'http://localhost'}, - __: value => { - return value; - } - } - }; - const observationTypeDelivery = await app.models.ObservationType.findOne({ - where: {code: 'delivery'} - }); - const originalTicketObservation = await app.models.TicketObservation.findOne({ - where: { - ticketFk: ticketID, - observationTypeFk: observationTypeDelivery.id} - }); - - expect(originalTicketObservation).toBeDefined(); - - await app.models.Ticket.componentUpdate(ctx, ticketID, clientID, agencyModeID, newAddressID, - zoneID, warehouseID, companyID, shipped, landed, isDeleted, option); - - const removedTicketObservation = await app.models.TicketObservation.findOne({ - where: { - ticketFk: ticketID, - observationTypeFk: observationTypeDelivery.id} - }); - - expect(removedTicketObservation).toBeNull(); - - // restores - await app.models.Ticket.componentUpdate(ctx, ticketID, clientID, agencyModeID, addressID, - zoneID, warehouseID, companyID, shipped, landed, isDeleted, option); - - const restoredTicketObservation = await app.models.TicketObservation.findOne({ - where: { - ticketFk: ticketID, - observationTypeFk: observationTypeDelivery.id} - }); - - expect(restoredTicketObservation.description).toEqual(originalTicketObservation.description); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/isEditable.spec.js b/modules/ticket/back/methods/ticket/specs/isEditable.spec.js index 276aeacf1..5a6bf7d19 100644 --- a/modules/ticket/back/methods/ticket/specs/isEditable.spec.js +++ b/modules/ticket/back/methods/ticket/specs/isEditable.spec.js @@ -2,52 +2,135 @@ const app = require('vn-loopback/server/server'); describe('ticket isEditable()', () => { it('should return false if the given ticket does not exist', async() => { - let ctx = {req: {accessToken: {userId: 9}}}; - let result = await app.models.Ticket.isEditable(ctx, 99999); + const tx = await app.models.Ticket.beginTransaction({}); + let result; + + try { + const options = {transaction: tx}; + const ctx = { + req: {accessToken: {userId: 9}} + }; + + result = await app.models.Ticket.isEditable(ctx, 9999, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } expect(result).toEqual(false); }); it(`should return false if the given ticket isn't invoiced but isDeleted`, async() => { - let ctx = {req: {accessToken: {userId: 9}}}; - let deletedTicket = await app.models.Ticket.findOne({ - where: { - invoiceOut: null, - isDeleted: true - }, - fields: ['id'] - }); + const tx = await app.models.Ticket.beginTransaction({}); + let result; - let result = await app.models.Ticket.isEditable(ctx, deletedTicket.id); + try { + const options = {transaction: tx}; + const deletedTicket = await app.models.Ticket.findOne({ + where: { + invoiceOut: null, + isDeleted: true + }, + fields: ['id'] + }); + + const ctx = { + req: {accessToken: {userId: 9}} + }; + + result = await app.models.Ticket.isEditable(ctx, deletedTicket.id, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } expect(result).toEqual(false); }); it('should return true if the given ticket is editable', async() => { - let ctx = {req: {accessToken: {userId: 9}}}; + const tx = await app.models.Ticket.beginTransaction({}); + let result; - let result = await app.models.Ticket.isEditable(ctx, 16); + try { + const options = {transaction: tx}; + const ctx = { + req: {accessToken: {userId: 9}} + }; + + result = await app.models.Ticket.isEditable(ctx, 16, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } expect(result).toEqual(true); }); it('should not be able to edit a deleted or invoiced ticket even for salesAssistant', async() => { - let ctx = {req: {accessToken: {userId: 21}}}; - let result = await app.models.Ticket.isEditable(ctx, 19); + const tx = await app.models.Ticket.beginTransaction({}); + let result; + + try { + const options = {transaction: tx}; + const ctx = { + req: {accessToken: {userId: 21}} + }; + + result = await app.models.Ticket.isEditable(ctx, 19, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } expect(result).toEqual(false); }); it('should not be able to edit a deleted or invoiced ticket even for productionBoss', async() => { - let ctx = {req: {accessToken: {userId: 50}}}; - let result = await app.models.Ticket.isEditable(ctx, 19); + const tx = await app.models.Ticket.beginTransaction({}); + let result; + + try { + const options = {transaction: tx}; + const ctx = { + req: {accessToken: {userId: 50}} + }; + + result = await app.models.Ticket.isEditable(ctx, 19, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } expect(result).toEqual(false); }); it('should not be able to edit a deleted or invoiced ticket even for salesPerson', async() => { - let ctx = {req: {accessToken: {userId: 18}}}; - let result = await app.models.Ticket.isEditable(ctx, 19); + const tx = await app.models.Ticket.beginTransaction({}); + let result; + + try { + const options = {transaction: tx}; + const ctx = { + req: {accessToken: {userId: 18}} + }; + + result = await app.models.Ticket.isEditable(ctx, 19, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } expect(result).toEqual(false); }); diff --git a/modules/zone/back/methods/agency/getShipped.js b/modules/zone/back/methods/agency/getShipped.js index 529d7478e..b54056a5f 100644 --- a/modules/zone/back/methods/agency/getShipped.js +++ b/modules/zone/back/methods/agency/getShipped.js @@ -34,7 +34,12 @@ module.exports = Self => { } }); - Self.getShipped = async(landed, addressFk, agencyModeFk, warehouseFk) => { + Self.getShipped = async(landed, addressFk, agencyModeFk, warehouseFk, options) => { + let myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + let stmts = []; stmts.push(new ParameterizedSQL( `CALL vn.zone_getShippedWarehouse(?, ?, ?)`, [ @@ -44,14 +49,14 @@ module.exports = Self => { ] )); - let rsIndex = stmts.push(new ParameterizedSQL( + const rsIndex = stmts.push(new ParameterizedSQL( `SELECT * FROM tmp.zoneGetShipped WHERE warehouseFk = ?`, [ warehouseFk ] )) - 1; - let sql = ParameterizedSQL.join(stmts, ';'); - let shipped = await Self.rawStmt(sql); + const sql = ParameterizedSQL.join(stmts, ';'); + const shipped = await Self.rawStmt(sql, myOptions); return shipped[rsIndex][0]; }; diff --git a/modules/zone/back/methods/agency/specs/getShipped.spec.js b/modules/zone/back/methods/agency/specs/getShipped.spec.js index 8f79dab3b..2d65c8bc0 100644 --- a/modules/zone/back/methods/agency/specs/getShipped.spec.js +++ b/modules/zone/back/methods/agency/specs/getShipped.spec.js @@ -2,27 +2,49 @@ const app = require('vn-loopback/server/server'); describe('agency getShipped()', () => { it('should return a shipment date', async() => { - const landed = new Date(); - landed.setDate(landed.getDate() + 1); - const addressFk = 121; - const agencyModeFk = 7; - const warehouseFk = 1; + const tx = await app.models.Agency.beginTransaction({}); + let result; - let result = await app.models.Agency.getShipped(landed, addressFk, agencyModeFk, warehouseFk); + try { + const options = {transaction: tx}; + + const landed = new Date(); + landed.setDate(landed.getDate() + 1); + const addressFk = 121; + const agencyModeFk = 7; + const warehouseFk = 1; + + result = await app.models.Agency.getShipped(landed, addressFk, agencyModeFk, warehouseFk, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } expect(result).toBeDefined(); }); it('should not return a shipment date', async() => { - let newDate = new Date(); - newDate.setMonth(newDate.getMonth() - 1); + const tx = await app.models.Agency.beginTransaction({}); + let result; - const landed = newDate; - const addressFk = 121; - const agencyModeFk = 7; - const warehouseFk = 1; + try { + const options = {transaction: tx}; + let newDate = new Date(); + newDate.setMonth(newDate.getMonth() - 1); + const landed = newDate; + const addressFk = 121; + const agencyModeFk = 7; + const warehouseFk = 1; - let result = await app.models.Agency.getShipped(landed, addressFk, agencyModeFk, warehouseFk); + result = await app.models.Agency.getShipped(landed, addressFk, agencyModeFk, warehouseFk, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } expect(result).toBeUndefined(); });