diff --git a/modules/claim/back/methods/claim/regularizeClaim.js b/modules/claim/back/methods/claim/regularizeClaim.js index 16ef1adfe..2106ab210 100644 --- a/modules/claim/back/methods/claim/regularizeClaim.js +++ b/modules/claim/back/methods/claim/regularizeClaim.js @@ -77,12 +77,13 @@ module.exports = Self => { }, myOptions); if (!ticketFk) { - ticketFk = await createTicket(ctx, { + ctx.args = { clientId: address.clientFk, warehouseId: sale.ticket().warehouseFk, companyId: sale.ticket().companyFk, addressId: addressId - }, myOptions); + }; + ticketFk = await createTicket(ctx, myOptions); } await models.Sale.create({ @@ -153,22 +154,13 @@ module.exports = Self => { return ticket && ticket.id; } - async function createTicket(ctx, params, options) { - params.shipped = new Date(); - params.landed = new Date(); - params.agencyModeId = null; - params.routeId = null; + async function createTicket(ctx, options) { + ctx.args.shipped = new Date(); + ctx.args.landed = new Date(); + ctx.args.agencyModeId = null; + ctx.args.routeId = null; - const ticket = await Self.app.models.Ticket.new(ctx, - params.clientId, - params.shipped, - params.landed, - params.warehouseId, - params.companyId, - params.addressId, - params.agencyModeId, - params.routeId, - options); + const ticket = await Self.app.models.Ticket.new(ctx, options); return ticket.id; } diff --git a/modules/claim/back/methods/claim/specs/regularizeClaim.spec.js b/modules/claim/back/methods/claim/specs/regularizeClaim.spec.js index 543001922..8ea310772 100644 --- a/modules/claim/back/methods/claim/specs/regularizeClaim.spec.js +++ b/modules/claim/back/methods/claim/specs/regularizeClaim.spec.js @@ -1,6 +1,6 @@ const app = require('vn-loopback/server/server'); -describe('regularizeClaim()', () => { +describe('claim regularizeClaim()', () => { const ctx = { req: { accessToken: {userId: 18}, diff --git a/modules/client/back/methods/client/specs/canBeInvoiced.spec.js b/modules/client/back/methods/client/specs/canBeInvoiced.spec.js index c070eba37..7b0640d2a 100644 --- a/modules/client/back/methods/client/specs/canBeInvoiced.spec.js +++ b/modules/client/back/methods/client/specs/canBeInvoiced.spec.js @@ -32,6 +32,7 @@ describe('client canBeInvoiced()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -51,6 +52,7 @@ describe('client canBeInvoiced()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -67,6 +69,7 @@ describe('client canBeInvoiced()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/canCreateTicket.spec.js b/modules/client/back/methods/client/specs/canCreateTicket.spec.js index 2a2797899..eb3bdec50 100644 --- a/modules/client/back/methods/client/specs/canCreateTicket.spec.js +++ b/modules/client/back/methods/client/specs/canCreateTicket.spec.js @@ -15,6 +15,7 @@ describe('Client canCreateTicket', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -32,6 +33,7 @@ describe('Client canCreateTicket', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/confirmTransaction.spec.js b/modules/client/back/methods/client/specs/confirmTransaction.spec.js index ce1034887..50e6806b5 100644 --- a/modules/client/back/methods/client/specs/confirmTransaction.spec.js +++ b/modules/client/back/methods/client/specs/confirmTransaction.spec.js @@ -21,6 +21,7 @@ describe('Client confirmTransaction', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/consumption.spec.js b/modules/client/back/methods/client/specs/consumption.spec.js index 06a121e03..47a495d79 100644 --- a/modules/client/back/methods/client/specs/consumption.spec.js +++ b/modules/client/back/methods/client/specs/consumption.spec.js @@ -14,13 +14,14 @@ describe('client consumption() filter', () => { }, order: 'itemTypeFk, itemName, itemSize' }; - const result = await app.models.Client.consumption(ctx, filter, options); + const result = await models.Client.consumption(ctx, filter, options); expect(result.length).toEqual(10); await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -41,7 +42,7 @@ describe('client consumption() filter', () => { }, order: 'itemFk' }; - const result = await app.models.Client.consumption(ctx, filter, options); + const result = await models.Client.consumption(ctx, filter, options); const firstRow = result[0]; const secondRow = result[1]; @@ -55,6 +56,7 @@ describe('client consumption() filter', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -75,7 +77,7 @@ describe('client consumption() filter', () => { }, order: 'itemTypeFk, itemName, itemSize' }; - const result = await app.models.Client.consumption(ctx, filter, options); + const result = await models.Client.consumption(ctx, filter, options); const expectedItemId = 4; const firstRow = result[0]; @@ -85,6 +87,7 @@ describe('client consumption() filter', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/createAddress.spec.js b/modules/client/back/methods/client/specs/createAddress.spec.js index d934ae1c8..d6178ae0d 100644 --- a/modules/client/back/methods/client/specs/createAddress.spec.js +++ b/modules/client/back/methods/client/specs/createAddress.spec.js @@ -99,6 +99,7 @@ describe('Address createAddress', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -132,6 +133,7 @@ describe('Address createAddress', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/createReceipt.spec.js b/modules/client/back/methods/client/specs/createReceipt.spec.js index 929e43e49..915836a38 100644 --- a/modules/client/back/methods/client/specs/createReceipt.spec.js +++ b/modules/client/back/methods/client/specs/createReceipt.spec.js @@ -50,6 +50,7 @@ describe('Client createReceipt', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -147,6 +148,7 @@ describe('Client createReceipt', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -187,6 +189,7 @@ describe('Client createReceipt', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/createWithUser.spec.js b/modules/client/back/methods/client/specs/createWithUser.spec.js index 307a2be48..f85ad1929 100644 --- a/modules/client/back/methods/client/specs/createWithUser.spec.js +++ b/modules/client/back/methods/client/specs/createWithUser.spec.js @@ -26,6 +26,7 @@ describe('Client Create', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -48,6 +49,7 @@ describe('Client Create', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); diff --git a/modules/client/back/methods/client/specs/getAverageInvoiced.spec.js b/modules/client/back/methods/client/specs/getAverageInvoiced.spec.js index 3a4458736..74a761389 100644 --- a/modules/client/back/methods/client/specs/getAverageInvoiced.spec.js +++ b/modules/client/back/methods/client/specs/getAverageInvoiced.spec.js @@ -14,6 +14,7 @@ describe('client getAverageInvoiced()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/getCard.spec.js b/modules/client/back/methods/client/specs/getCard.spec.js index a7e985311..e713c9883 100644 --- a/modules/client/back/methods/client/specs/getCard.spec.js +++ b/modules/client/back/methods/client/specs/getCard.spec.js @@ -17,6 +17,7 @@ describe('Client getCard()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/getDebt.spec.js b/modules/client/back/methods/client/specs/getDebt.spec.js index 93b80dad4..471d45a6d 100644 --- a/modules/client/back/methods/client/specs/getDebt.spec.js +++ b/modules/client/back/methods/client/specs/getDebt.spec.js @@ -14,6 +14,7 @@ describe('client getDebt()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/getMana.spec.js b/modules/client/back/methods/client/specs/getMana.spec.js index 00170eac9..093e491a3 100644 --- a/modules/client/back/methods/client/specs/getMana.spec.js +++ b/modules/client/back/methods/client/specs/getMana.spec.js @@ -14,6 +14,7 @@ describe('client getMana()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/getTransactions.spec.js b/modules/client/back/methods/client/specs/getTransactions.spec.js index 992355668..0387eb59a 100644 --- a/modules/client/back/methods/client/specs/getTransactions.spec.js +++ b/modules/client/back/methods/client/specs/getTransactions.spec.js @@ -15,6 +15,7 @@ describe('Client getTransations', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/hasCustomerRole.spec.js b/modules/client/back/methods/client/specs/hasCustomerRole.spec.js index 6cd4f63a3..08b13a88e 100644 --- a/modules/client/back/methods/client/specs/hasCustomerRole.spec.js +++ b/modules/client/back/methods/client/specs/hasCustomerRole.spec.js @@ -16,6 +16,7 @@ describe('Client hasCustomerRole', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -33,6 +34,7 @@ describe('Client hasCustomerRole', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -51,6 +53,7 @@ describe('Client hasCustomerRole', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -69,6 +72,7 @@ describe('Client hasCustomerRole', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/isValidClient.spec.js b/modules/client/back/methods/client/specs/isValidClient.spec.js index 34bab4646..e7bf8fa8a 100644 --- a/modules/client/back/methods/client/specs/isValidClient.spec.js +++ b/modules/client/back/methods/client/specs/isValidClient.spec.js @@ -15,6 +15,7 @@ describe('Client isValidClient', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -32,6 +33,7 @@ describe('Client isValidClient', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -49,6 +51,7 @@ describe('Client isValidClient', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -66,6 +69,7 @@ describe('Client isValidClient', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -83,6 +87,7 @@ describe('Client isValidClient', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -100,6 +105,7 @@ describe('Client isValidClient', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/lastActiveTickets.spec.js b/modules/client/back/methods/client/specs/lastActiveTickets.spec.js index 55d2ec0f4..7bddb55ee 100644 --- a/modules/client/back/methods/client/specs/lastActiveTickets.spec.js +++ b/modules/client/back/methods/client/specs/lastActiveTickets.spec.js @@ -22,6 +22,7 @@ describe('Client last active tickets', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/sendSms.spec.js b/modules/client/back/methods/client/specs/sendSms.spec.js index 290fab103..121d427ce 100644 --- a/modules/client/back/methods/client/specs/sendSms.spec.js +++ b/modules/client/back/methods/client/specs/sendSms.spec.js @@ -15,7 +15,7 @@ describe('client sendSms()', () => { const sms = await models.Client.sendSms(ctx, id, destination, message, options); - const createdLog = await models.ClientLog.findById(sms.logId); + const createdLog = await models.ClientLog.findById(sms.logId, null, options); const json = JSON.parse(JSON.stringify(createdLog.newInstance)); expect(json.message).toEqual(message); @@ -23,6 +23,7 @@ describe('client sendSms()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/summary.spec.js b/modules/client/back/methods/client/specs/summary.spec.js index dedc1f8ea..1eef35024 100644 --- a/modules/client/back/methods/client/specs/summary.spec.js +++ b/modules/client/back/methods/client/specs/summary.spec.js @@ -16,6 +16,7 @@ describe('client summary()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -33,6 +34,7 @@ describe('client summary()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -50,6 +52,7 @@ describe('client summary()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -67,6 +70,7 @@ describe('client summary()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -84,6 +88,7 @@ describe('client summary()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -101,6 +106,7 @@ describe('client summary()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -118,6 +124,7 @@ describe('client summary()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/updateAddress.spec.js b/modules/client/back/methods/client/specs/updateAddress.spec.js index e3e65f604..efaa1b93c 100644 --- a/modules/client/back/methods/client/specs/updateAddress.spec.js +++ b/modules/client/back/methods/client/specs/updateAddress.spec.js @@ -64,6 +64,8 @@ describe('Address updateAddress', () => { try { const options = {transaction: tx}; + + const expectedResult = 'My edited address'; const ctx = { args: { provinceFk: provinceId, @@ -72,7 +74,6 @@ describe('Address updateAddress', () => { customsAgentFk: customAgentOneId } }; - const expectedResult = 'My edited address'; await models.Client.updateAddress(ctx, clientId, addressId, options); @@ -83,6 +84,7 @@ describe('Address updateAddress', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -91,12 +93,13 @@ describe('Address updateAddress', () => { try { const options = {transaction: tx}; + + const expectedResult = 'My second time edited address'; const ctx = { args: { nickname: expectedResult } }; - const expectedResult = 'My second time edited address'; await models.Client.updateAddress(ctx, clientId, addressId, options); @@ -107,6 +110,7 @@ describe('Address updateAddress', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/client/specs/updateFiscalData.spec.js b/modules/client/back/methods/client/specs/updateFiscalData.spec.js index a0a46c610..75273a39f 100644 --- a/modules/client/back/methods/client/specs/updateFiscalData.spec.js +++ b/modules/client/back/methods/client/specs/updateFiscalData.spec.js @@ -71,6 +71,7 @@ describe('Client updateFiscalData', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/credit-classification/createWithInsurance.js b/modules/client/back/methods/credit-classification/createWithInsurance.js index 853f68389..fb0a7222b 100644 --- a/modules/client/back/methods/credit-classification/createWithInsurance.js +++ b/modules/client/back/methods/credit-classification/createWithInsurance.js @@ -20,8 +20,8 @@ module.exports = Self => { Self.createWithInsurance = async(data, options) => { const models = Self.app.models; - let tx; const myOptions = {}; + let tx; if (typeof options == 'object') Object.assign(myOptions, options); diff --git a/modules/client/back/methods/credit-classification/createWithInsurance.spec.js b/modules/client/back/methods/credit-classification/createWithInsurance.spec.js index fdc14a953..95ff5025f 100644 --- a/modules/client/back/methods/credit-classification/createWithInsurance.spec.js +++ b/modules/client/back/methods/credit-classification/createWithInsurance.spec.js @@ -1,6 +1,23 @@ const models = require('vn-loopback/server/server').models; +const LoopBackContext = require('loopback-context'); describe('Client createWithInsurance', () => { + const activeCtx = { + accessToken: {userId: 19}, + http: { + req: { + headers: {origin: 'http://localhost/'}, + __: () => {} + } + } + }; + + beforeAll(async() => { + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx, + }); + }); + it('should create a new client credit classification with insurance', async() => { const tx = await models.Client.beginTransaction({}); @@ -12,8 +29,8 @@ describe('Client createWithInsurance', () => { expect(result.client).toEqual(1101); - const classifications = await models.CreditClassification.find(); - const insurances = await models.CreditInsurance.find(); + const classifications = await models.CreditClassification.find(null, options); + const insurances = await models.CreditInsurance.find(null, options); expect(classifications.length).toEqual(6); expect(insurances.length).toEqual(4); @@ -21,6 +38,7 @@ describe('Client createWithInsurance', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/greuge/specs/sumAmount.spec.js b/modules/client/back/methods/greuge/specs/sumAmount.spec.js index 25b9e44a4..a4a746742 100644 --- a/modules/client/back/methods/greuge/specs/sumAmount.spec.js +++ b/modules/client/back/methods/greuge/specs/sumAmount.spec.js @@ -7,15 +7,16 @@ describe('Greuge totalGreuge()', () => { try { const options = {transaction: tx}; - const clientId = 1; + const clientId = 1101; - const result = await models.Client.sumAmount(clientId, options); + const result = await models.Greuge.sumAmount(clientId, options); - expect(result).toBe(6000); + expect(result).toBe(203.71); await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/receipt/specs/filter.spec.js b/modules/client/back/methods/receipt/specs/filter.spec.js index 0ddc9b3a7..c28961d0c 100644 --- a/modules/client/back/methods/receipt/specs/filter.spec.js +++ b/modules/client/back/methods/receipt/specs/filter.spec.js @@ -17,6 +17,7 @@ describe('receipt filter()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/back/methods/recovery/hasActiveRecovery.spec.js b/modules/client/back/methods/recovery/hasActiveRecovery.spec.js index 1a3d5887e..8ff4d66cc 100644 --- a/modules/client/back/methods/recovery/hasActiveRecovery.spec.js +++ b/modules/client/back/methods/recovery/hasActiveRecovery.spec.js @@ -14,6 +14,7 @@ describe('client hasActiveRecovery', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -30,6 +31,7 @@ describe('client hasActiveRecovery', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/client/front/descriptor/index.html b/modules/client/front/descriptor/index.html index dc79d224b..d5beca282 100644 --- a/modules/client/front/descriptor/index.html +++ b/modules/client/front/descriptor/index.html @@ -94,6 +94,8 @@
diff --git a/modules/item/back/methods/item/regularize.js b/modules/item/back/methods/item/regularize.js index 8aec2fa02..0878e3242 100644 --- a/modules/item/back/methods/item/regularize.js +++ b/modules/item/back/methods/item/regularize.js @@ -65,16 +65,17 @@ module.exports = Self => { }, myOptions); if (!ticketId) { - ticketId = await createTicket(ctx, { + ctx.args = { clientId: itemDestination.address().clientFk, warehouseId: warehouseFk, addressId: itemDestination.addressFk - }, myOptions); + }; + ticketId = await createTicket(ctx, myOptions); } - res = await models.Item.getVisibleAvailable(itemFk, warehouseFk); + const res = await models.Item.getVisibleAvailable(itemFk, warehouseFk, null, myOptions); - let newQuantity = res.visible - quantity; + const newQuantity = res.visible - quantity; await models.Sale.create({ ticketFk: ticketId, @@ -92,23 +93,14 @@ module.exports = Self => { throw e; } - async function createTicket(ctx, params, options) { - params.shipped = new Date(); - params.landed = new Date(); - params.companyId = null; - params.agencyModeId = null; - params.routeId = null; + async function createTicket(ctx, options) { + ctx.args.shipped = new Date(); + ctx.args.landed = new Date(); + ctx.args.companyId = null; + ctx.args.agencyModeId = null; + ctx.args.routeId = null; - const ticket = await Self.app.models.Ticket.new(ctx, - params.clientId, - params.shipped, - params.landed, - params.warehouseId, - params.companyId, - params.addressId, - params.agencyModeId, - params.routeId, - options); + const ticket = await Self.app.models.Ticket.new(ctx, options); return ticket.id; } diff --git a/modules/ticket/back/methods/ticket/addSale.js b/modules/ticket/back/methods/ticket/addSale.js index dc45e5de9..527b861a1 100644 --- a/modules/ticket/back/methods/ticket/addSale.js +++ b/modules/ticket/back/methods/ticket/addSale.js @@ -7,23 +7,23 @@ module.exports = Self => { accessType: 'WRITE', accepts: [{ arg: 'id', - type: 'Number', + type: 'number', required: true, description: 'The ticket id', http: {source: 'path'} }, { arg: 'itemId', - type: 'Number', + type: 'number', required: true }, { arg: 'quantity', - type: 'Number', + type: 'number', required: true }], returns: { - type: 'Object', + type: 'object', root: true }, http: { @@ -32,64 +32,81 @@ module.exports = Self => { } }); - Self.addSale = async(ctx, id, itemId, quantity) => { + Self.addSale = async(ctx, id, itemId, quantity, options) => { const $t = ctx.req.__; // $translate const models = Self.app.models; + const myOptions = {}; + let tx; - const isEditable = await models.Ticket.isEditable(ctx, id); - if (!isEditable) - throw new UserError(`The sales of this ticket can't be modified`); + if (typeof options == 'object') + Object.assign(myOptions, options); - const item = await models.Item.findById(itemId); - const ticket = await models.Ticket.findById(id, { - include: { - relation: 'client', - scope: { - include: { - relation: 'salesPersonUser', - scope: { - fields: ['id', 'name'] + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const isEditable = await models.Ticket.isEditable(ctx, id, myOptions); + if (!isEditable) + throw new UserError(`The sales of this ticket can't be modified`); + + const item = await models.Item.findById(itemId, null, myOptions); + const ticket = await models.Ticket.findById(id, { + include: { + relation: 'client', + scope: { + include: { + relation: 'salesPersonUser', + scope: { + fields: ['id', 'name'] + } } } } + }, myOptions); + + const itemInfo = await models.Item.getVisibleAvailable(itemId, ticket.warehouseFk, ticket.shipped, myOptions); + + const isPackaging = item.family == 'EMB'; + if (!isPackaging && itemInfo.available < quantity) + throw new UserError(`This item is not available`); + + const newSale = await models.Sale.create({ + ticketFk: id, + itemFk: item.id, + concept: item.name, + quantity: quantity + }, myOptions); + + await Self.rawSql('CALL vn.sale_calculateComponent(?, NULL)', [newSale.id], myOptions); + + const sale = await models.Sale.findById(newSale.id, { + include: { + relation: 'item' + } + }, myOptions); + + const addition = `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity})`; + + const salesPerson = ticket.client().salesPersonUser(); + if (salesPerson) { + const origin = ctx.req.headers.origin; + + const message = $t('Added sale to ticket', { + ticketId: id, + ticketUrl: `${origin}/#!/ticket/${id}/sale`, + addition: addition + }); + await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); } - }); - const res = await models.Item.getVisibleAvailable(itemId, ticket.warehouseFk, ticket.shipped); + if (tx) await tx.commit(); - const isPackaging = item.family == 'EMB'; - if (!isPackaging && res.available < quantity) - throw new UserError(`This item is not available`); - - const newSale = await models.Sale.create({ - ticketFk: id, - itemFk: item.id, - concept: item.name, - quantity: quantity - }); - - await Self.rawSql('CALL vn.sale_calculateComponent(?, NULL)', [newSale.id]); - - const sale = await models.Sale.findById(newSale.id, { - include: { - relation: 'item' - } - }); - - const addition = `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity})`; - - const salesPerson = ticket.client().salesPersonUser(); - if (salesPerson) { - const origin = ctx.req.headers.origin; - - const message = $t('Added sale to ticket', { - ticketId: id, - ticketUrl: `${origin}/#!/ticket/${id}/sale`, - addition: addition - }); - await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); + return sale; + } catch (e) { + if (tx) await tx.rollback(); + throw e; } - - return sale; }; }; diff --git a/modules/ticket/back/methods/ticket/canBeInvoiced.js b/modules/ticket/back/methods/ticket/canBeInvoiced.js index facb7b945..a009d63cf 100644 --- a/modules/ticket/back/methods/ticket/canBeInvoiced.js +++ b/modules/ticket/back/methods/ticket/canBeInvoiced.js @@ -22,7 +22,7 @@ module.exports = function(Self) { }); Self.canBeInvoiced = async(ticketsIds, options) => { - let myOptions = {}; + const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); diff --git a/modules/ticket/back/methods/ticket/canHaveStowaway.js b/modules/ticket/back/methods/ticket/canHaveStowaway.js index 69be212d1..9246d0308 100644 --- a/modules/ticket/back/methods/ticket/canHaveStowaway.js +++ b/modules/ticket/back/methods/ticket/canHaveStowaway.js @@ -19,8 +19,13 @@ module.exports = Self => { } }); - Self.canHaveStowaway = async id => { + Self.canHaveStowaway = async(id, options) => { const models = Self.app.models; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + const ticket = await models.Ticket.findById(id, { include: { relation: 'ship', @@ -28,8 +33,10 @@ module.exports = Self => { fields: ['id'] } } - }); - const warehouse = await models.Warehouse.findById(ticket.warehouseFk); + }, myOptions); + + const warehouse = await models.Warehouse.findById(ticket.warehouseFk, null, myOptions); + const hasStowaway = ticket.ship() ? true : false; const validStowaway = warehouse && warehouse.hasStowaway && !hasStowaway; diff --git a/modules/ticket/back/methods/ticket/changeWorker.js b/modules/ticket/back/methods/ticket/changeWorker.js deleted file mode 100644 index 896c8a7f9..000000000 --- a/modules/ticket/back/methods/ticket/changeWorker.js +++ /dev/null @@ -1,47 +0,0 @@ -module.exports = function(Self) { - Self.remoteMethodCtx('changeWorker', { - description: 'Change worker of tickets state', - accepts: [ - { - arg: 'worker', - type: 'number', - required: true, - description: 'worker id', - http: {source: 'path'} - } - ], - returns: { - arg: 'response', - type: 'boolean' - }, - http: { - verb: 'put', - path: '/:worker/changeWorker' - } - }); - - Self.changeWorker = function(ctx, worker, cb) { - let tickets = ctx.req.body.tickets; - changeWorker(worker, tickets, cb); - }; - - function changeWorker(worker, tickets, cb) { - let ticketTracking = Self.app.models.ticketTracking; - let toSave = []; - for (let ticket of tickets) { - toSave.push( - { - stateFk: 20, - ticketFk: ticket, - workerFk: worker - } - ); - } - ticketTracking.create(toSave, function(err, info) { - if (err) - cb(err, null); - else - cb(null, info); - }); - } -}; diff --git a/modules/ticket/back/methods/ticket/componentUpdate.js b/modules/ticket/back/methods/ticket/componentUpdate.js index 9ebd51bf4..f1256771c 100644 --- a/modules/ticket/back/methods/ticket/componentUpdate.js +++ b/modules/ticket/back/methods/ticket/componentUpdate.js @@ -90,8 +90,8 @@ module.exports = Self => { Self.componentUpdate = async(ctx, options) => { const args = ctx.args; + const myOptions = {}; let tx; - let myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); diff --git a/modules/ticket/back/methods/ticket/deleteStowaway.js b/modules/ticket/back/methods/ticket/deleteStowaway.js index b86cdc464..c3e5e0db1 100644 --- a/modules/ticket/back/methods/ticket/deleteStowaway.js +++ b/modules/ticket/back/methods/ticket/deleteStowaway.js @@ -19,69 +19,87 @@ module.exports = Self => { } }); - Self.deleteStowaway = async(ctx, id) => { + Self.deleteStowaway = async(ctx, id, options) => { const models = Self.app.models; const $t = ctx.req.__; // $translate - const ticket = await Self.findById(id, { - include: [{ - relation: 'ship' - }, { - relation: 'stowaway' - }, { - relation: 'client', - scope: { - include: { - relation: 'salesPersonUser', - scope: { - fields: ['id', 'name'] + const myOptions = {}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const ticket = await Self.findById(id, { + include: [{ + relation: 'ship' + }, { + relation: 'stowaway' + }, { + relation: 'client', + scope: { + include: { + relation: 'salesPersonUser', + scope: { + fields: ['id', 'name'] + } } } + }] + }, myOptions); + + let stowawayFk; + let shipFk; + if (ticket.stowaway()) { + shipFk = ticket.stowaway().shipFk; + stowawayFk = ticket.stowaway().id; + } else if (ticket.ship()) { + shipFk = ticket.ship().shipFk; + stowawayFk = ticket.ship().id; + } + + const stowaway = await models.Stowaway.findOne({ + where: { + id: stowawayFk, + shipFk: shipFk } - }] - }); + }, myOptions); + const result = await stowaway.destroy(myOptions); - let stowawayFk; - let shipFk; - if (ticket.stowaway()) { - shipFk = ticket.stowaway().shipFk; - stowawayFk = ticket.stowaway().id; - } else if (ticket.ship()) { - shipFk = ticket.ship().shipFk; - stowawayFk = ticket.ship().id; + const state = await models.State.findOne({ + where: { + code: 'BOARDING' + } + }, myOptions); + const ticketTracking = await models.TicketTracking.findOne({ + where: { + ticketFk: shipFk, + stateFk: state.id + } + }, myOptions); + + await ticketTracking.destroy(myOptions); + + const salesPerson = ticket.client().salesPersonUser(); + if (salesPerson) { + const origin = ctx.req.headers.origin; + const message = $t('This ticket is not an stowaway anymore', { + ticketId: stowawayFk, + ticketUrl: `${origin}/#!/ticket/${stowawayFk}/sale` + }); + await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); + } + + if (tx) await tx.commit(); + + return result; + } catch (e) { + if (tx) await tx.rollback(); + throw e; } - - const stowaway = await models.Stowaway.findOne({ - where: { - id: stowawayFk, - shipFk: shipFk - } - }); - const result = await stowaway.destroy(); - - const state = await models.State.findOne({ - where: { - code: 'BOARDING' - } - }); - const ticketTracking = await models.TicketTracking.findOne({ - where: { - ticketFk: shipFk, - stateFk: state.id - } - }); - - await ticketTracking.destroy(); - - const salesPerson = ticket.client().salesPersonUser(); - if (salesPerson) { - const origin = ctx.req.headers.origin; - const message = $t('This ticket is not an stowaway anymore', { - ticketId: stowawayFk, - ticketUrl: `${origin}/#!/ticket/${stowawayFk}/sale` - }); - await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); - } - - return result; }; }; diff --git a/modules/ticket/back/methods/ticket/filter.js b/modules/ticket/back/methods/ticket/filter.js index 52a918314..9038b0886 100644 --- a/modules/ticket/back/methods/ticket/filter.js +++ b/modules/ticket/back/methods/ticket/filter.js @@ -119,12 +119,17 @@ module.exports = Self => { } }); - Self.filter = async(ctx, filter) => { + Self.filter = async(ctx, filter, options) => { const userId = ctx.req.accessToken.userId; const conn = Self.dataSource.connector; const models = Self.app.models; const args = ctx.args; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + // Apply filter by team const teamMembersId = []; if (args.myTeam != null) { @@ -132,7 +137,8 @@ module.exports = Self => { include: { relation: 'collegues' } - }); + }, myOptions); + const collegues = worker.collegues() || []; collegues.forEach(collegue => { teamMembersId.push(collegue.collegueFk); @@ -204,7 +210,7 @@ module.exports = Self => { filter = mergeFilters(filter, {where}); - let stmts = []; + const stmts = []; let stmt; stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.filter'); @@ -306,7 +312,7 @@ module.exports = Self => { break; } - let problems = {[condition]: [ + const problems = {[condition]: [ {'tp.isFreezed': hasProblem}, {'tp.risk': hasProblem}, {'tp.hasTicketRequest': hasProblem}, @@ -318,15 +324,15 @@ module.exports = Self => { stmt.merge(conn.makeOrderBy(filter.order)); stmt.merge(conn.makeLimit(filter)); - let ticketsIndex = stmts.push(stmt) - 1; + const ticketsIndex = stmts.push(stmt) - 1; stmts.push( `DROP TEMPORARY TABLE tmp.filter, tmp.ticket_problems`); - let sql = ParameterizedSQL.join(stmts, ';'); - let result = await conn.executeStmt(sql); + const sql = ParameterizedSQL.join(stmts, ';'); + const result = await conn.executeStmt(sql, myOptions); return result[ticketsIndex]; }; diff --git a/modules/ticket/back/methods/ticket/freightCost.js b/modules/ticket/back/methods/ticket/freightCost.js index 008121f8f..85dc99bae 100644 --- a/modules/ticket/back/methods/ticket/freightCost.js +++ b/modules/ticket/back/methods/ticket/freightCost.js @@ -10,7 +10,7 @@ module.exports = Self => { http: {source: 'path'} }, returns: { - type: 'Number', + type: 'number', root: true }, http: { @@ -19,8 +19,14 @@ module.exports = Self => { } }); - Self.freightCost = async ticketFk => { - const [freightCost] = await Self.rawSql(`SELECT vn.ticket_getFreightCost(?) total`, [ticketFk]); + Self.freightCost = async(ticketFk, options) => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const [freightCost] = await Self.rawSql(`SELECT vn.ticket_getFreightCost(?) total`, [ticketFk], myOptions); + return freightCost.total; }; }; diff --git a/modules/ticket/back/methods/ticket/getComponentsSum.js b/modules/ticket/back/methods/ticket/getComponentsSum.js index c611913d8..276f96414 100644 --- a/modules/ticket/back/methods/ticket/getComponentsSum.js +++ b/modules/ticket/back/methods/ticket/getComponentsSum.js @@ -10,7 +10,7 @@ module.exports = Self => { http: {source: 'path'} }, returns: { - type: 'Number', + type: 'number', root: true }, http: { @@ -18,10 +18,15 @@ module.exports = Self => { verb: 'GET' } }); - Self.getComponentsSum = async id => { + Self.getComponentsSum = async(id, options) => { const models = Self.app.models; - let componentsSum = []; - let sales = await models.Sale.find({ + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const componentsSum = []; + const sales = await models.Sale.find({ include: { relation: 'components', scope: {fields: ['value', 'componentFk'], @@ -31,10 +36,10 @@ module.exports = Self => { } }, where: {ticketFk: id} - }); + }, myOptions); for (let sale of sales) { for (let component of sale.components()) { - let componentId = componentsSum[component.componentFk]; + const componentId = componentsSum[component.componentFk]; if (!componentId) { componentsSum[component.componentFk] = { componentFk: component.componentFk, diff --git a/modules/ticket/back/methods/ticket/getPossibleStowaways.js b/modules/ticket/back/methods/ticket/getPossibleStowaways.js index 675270840..c97e3de89 100644 --- a/modules/ticket/back/methods/ticket/getPossibleStowaways.js +++ b/modules/ticket/back/methods/ticket/getPossibleStowaways.js @@ -20,25 +20,30 @@ module.exports = Self => { } }); - Self.getPossibleStowaways = async ticketFk => { + Self.getPossibleStowaways = async(ticketFk, options) => { const models = Self.app.models; - const canHaveStowaway = await models.Ticket.canHaveStowaway(ticketFk); + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const canHaveStowaway = await models.Ticket.canHaveStowaway(ticketFk, myOptions); if (!canHaveStowaway) throw new UserError(`Can't create stowaway for this ticket`); - let ship = await models.Ticket.findById(ticketFk); + const ship = await models.Ticket.findById(ticketFk, null, myOptions); if (!ship || !ship.shipped) return []; - let lowestDate = new Date(ship.shipped.getTime()); + const lowestDate = new Date(ship.shipped.getTime()); lowestDate.setHours(0, 0, -1, 0); - let highestDate = new Date(ship.shipped.getTime()); + const highestDate = new Date(ship.shipped.getTime()); highestDate.setHours(23, 59, 59); - let possibleStowaways = await models.Ticket.find({ + const possibleStowaways = await models.Ticket.find({ where: { id: {neq: ticketFk}, clientFk: ship.clientFk, @@ -62,7 +67,7 @@ module.exports = Self => { }, }, ] - }); + }, myOptions); return possibleStowaways; }; diff --git a/modules/ticket/back/methods/ticket/getSales.js b/modules/ticket/back/methods/ticket/getSales.js index 155f23362..c3c7fd860 100644 --- a/modules/ticket/back/methods/ticket/getSales.js +++ b/modules/ticket/back/methods/ticket/getSales.js @@ -23,7 +23,7 @@ module.exports = Self => { Self.getSales = async(id, options) => { const models = Self.app.models; - let myOptions = {}; + const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); diff --git a/modules/ticket/back/methods/ticket/getSalesPersonMana.js b/modules/ticket/back/methods/ticket/getSalesPersonMana.js index 42faec61b..c200f869a 100644 --- a/modules/ticket/back/methods/ticket/getSalesPersonMana.js +++ b/modules/ticket/back/methods/ticket/getSalesPersonMana.js @@ -18,8 +18,13 @@ module.exports = Self => { } }); - Self.getSalesPersonMana = async ticketId => { + Self.getSalesPersonMana = async(ticketId, options) => { + const myOptions = {}; const models = Self.app.models; + + if (typeof options == 'object') + Object.assign(myOptions, options); + const ticket = await models.Ticket.findById(ticketId, { include: [{ relation: 'client', @@ -28,14 +33,16 @@ module.exports = Self => { } }], fields: ['id', 'clientFk'] - }); + }, myOptions); if (!ticket) return 0; const mana = await models.WorkerMana.findOne({ where: { workerFk: ticket.client().salesPersonFk - }, fields: 'amount'}); + }, + fields: 'amount' + }, myOptions); return mana ? mana.amount : 0; }; diff --git a/modules/ticket/back/methods/ticket/getTotalVolume.js b/modules/ticket/back/methods/ticket/getTotalVolume.js index 117c3c62f..81927aae5 100644 --- a/modules/ticket/back/methods/ticket/getTotalVolume.js +++ b/modules/ticket/back/methods/ticket/getTotalVolume.js @@ -18,7 +18,16 @@ module.exports = Self => { } }); - Self.getTotalVolume = async ticketFk => { - return (await Self.rawSql(`SELECT vn.ticketTotalVolume(?) totalVolume, vn.ticketTotalVolumeBoxes(?) totalBoxes`, [ticketFk, ticketFk]))[0]; + Self.getTotalVolume = async(ticketFk, options) => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const volumeData = await Self.rawSql(` + SELECT vn.ticketTotalVolume(?) totalVolume, vn.ticketTotalVolumeBoxes(?) totalBoxes + `, [ticketFk, ticketFk], myOptions); + + return volumeData[0]; }; }; diff --git a/modules/ticket/back/methods/ticket/getVolume.js b/modules/ticket/back/methods/ticket/getVolume.js index 149f650ec..376c9ee4e 100644 --- a/modules/ticket/back/methods/ticket/getVolume.js +++ b/modules/ticket/back/methods/ticket/getVolume.js @@ -19,8 +19,13 @@ module.exports = Self => { } }); - Self.getVolume = async ticketFk => { + Self.getVolume = async(ticketFk, options) => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + return Self.rawSql(`SELECT * FROM vn.saleVolume - WHERE ticketFk = ?`, [ticketFk]); + WHERE ticketFk = ?`, [ticketFk], myOptions); }; }; diff --git a/modules/ticket/back/methods/ticket/isEmpty.js b/modules/ticket/back/methods/ticket/isEmpty.js index 0465589e6..315687894 100644 --- a/modules/ticket/back/methods/ticket/isEmpty.js +++ b/modules/ticket/back/methods/ticket/isEmpty.js @@ -12,7 +12,7 @@ module.exports = function(Self) { } ], returns: { - type: 'Boolean', + type: 'boolean', root: true }, http: { @@ -24,25 +24,27 @@ module.exports = function(Self) { Self.isEmpty = async(id, options) => { const models = Self.app.models; - if ((typeof options) != 'object') - options = {}; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); const hasSales = await models.Sale.count({ ticketFk: id - }, options); + }, myOptions); const hasPackages = await models.TicketPackaging.count({ ticketFk: id - }, options); + }, myOptions); const hasServices = await models.TicketService.count({ ticketFk: id - }, options); + }, myOptions); const hasPurchaseRequests = await models.TicketRequest.count({ ticketFk: id, isOk: true - }, options); + }, myOptions); const isEmpty = !hasSales && !hasPackages && !hasServices && !hasPurchaseRequests; diff --git a/modules/ticket/back/methods/ticket/isLocked.js b/modules/ticket/back/methods/ticket/isLocked.js index a6c3cc036..947c93811 100644 --- a/modules/ticket/back/methods/ticket/isLocked.js +++ b/modules/ticket/back/methods/ticket/isLocked.js @@ -25,7 +25,7 @@ module.exports = Self => { if (typeof options == 'object') Object.assign(myOptions, options); - const ticket = await Self.app.models.Ticket.findById(id, { + const ticket = await Self.findById(id, { fields: ['isDeleted', 'refFk'] }, myOptions); diff --git a/modules/ticket/back/methods/ticket/makeInvoice.js b/modules/ticket/back/methods/ticket/makeInvoice.js index ce7380568..0f0e5b62a 100644 --- a/modules/ticket/back/methods/ticket/makeInvoice.js +++ b/modules/ticket/back/methods/ticket/makeInvoice.js @@ -27,8 +27,8 @@ module.exports = function(Self) { const userId = ctx.req.accessToken.userId; const models = Self.app.models; + const myOptions = {}; let tx; - let myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); diff --git a/modules/ticket/back/methods/ticket/new.js b/modules/ticket/back/methods/ticket/new.js index cec7857bd..a72ea6c45 100644 --- a/modules/ticket/back/methods/ticket/new.js +++ b/modules/ticket/back/methods/ticket/new.js @@ -57,77 +57,78 @@ module.exports = Self => { } }); - Self.new = async(ctx, clientId, shipped, landed, warehouseId, - companyId, addressId, agencyModeId, routeId, options) => { + Self.new = async(ctx, options) => { + const args = ctx.args; const myUserId = ctx.req.accessToken.userId; const models = Self.app.models; - const address = await models.Address.findOne({ - where: {id: addressId}, - fields: ['id', 'clientFk'], - include: { - relation: 'client', - scope: { - include: { - relation: 'type' - } - } - } - }); - - if (!address) - throw new UserError(`This address doesn't exist`); - - let agencyMode; - if (agencyModeId) - agencyMode = await models.AgencyMode.findById(agencyModeId); - - if (address.client().type().code === 'normal' && (!agencyMode || agencyMode.code != 'refund')) { - const canCreateTicket = await models.Client.canCreateTicket(clientId); - if (!canCreateTicket) - throw new UserError(`You can't create a ticket for a inactive client`); - } - + const myOptions = {}; let tx; - if ((typeof options) != 'object') - options = {}; + if (typeof options == 'object') + Object.assign(myOptions, options); - if (!options.transaction) { + if (!myOptions.transaction) { tx = await Self.beginTransaction({}); - options.transaction = tx; + myOptions.transaction = tx; } try { - if (!shipped && landed) { - const shippedResult = await models.Agency.getShipped(landed, - address.id, agencyModeId, warehouseId); - shipped = (shippedResult && shippedResult.shipped) || landed; + const address = await models.Address.findOne({ + where: {id: args.addressId}, + fields: ['id', 'clientFk'], + include: { + relation: 'client', + scope: { + include: { + relation: 'type' + } + } + } + }, myOptions); + + if (!address) + throw new UserError(`This address doesn't exist`); + + let agencyMode; + if (args.agencyModeId) + agencyMode = await models.AgencyMode.findById(args.agencyModeId, null, myOptions); + + if (address.client().type().code === 'normal' && (!agencyMode || agencyMode.code != 'refund')) { + const canCreateTicket = await models.Client.canCreateTicket(args.clientId, myOptions); + if (!canCreateTicket) + throw new UserError(`You can't create a ticket for a inactive client`); } - if (shipped && !landed) { - const landedResult = await models.Agency.getLanded(shipped, - address.id, agencyModeId, warehouseId, false); - landed = landedResult && landedResult.landed; + if (!args.shipped && args.landed) { + const shippedResult = await models.Agency.getShipped(args.landed, + address.id, args.agencyModeId, args.warehouseId, myOptions); + args.shipped = (shippedResult && shippedResult.shipped) || args.landed; + } + + if (args.shipped && !args.landed) { + const landedResult = await models.Agency.getLanded(args.shipped, + address.id, args.agencyModeId, args.warehouseId, false, myOptions); + args.landed = landedResult && landedResult.landed; } query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result); SELECT @result newTicketId;`; - let result = await Self.rawSql(query, [ - clientId, - shipped, - warehouseId, - companyId || 442, - addressId, - agencyModeId || null, - routeId || null, - landed, + const result = await Self.rawSql(query, [ + args.clientId, + args.shipped, + args.warehouseId, + args.companyId || 442, + args.addressId, + args.agencyModeId || null, + args.routeId || null, + args.landed, myUserId - ], options); + ], myOptions); - let ticket = await models.Ticket.findById(result[1][0].newTicketId, null, options); - let cleanInstance = JSON.parse(JSON.stringify(ticket)); + const ticket = await models.Ticket.findById(result[1][0].newTicketId, null, myOptions); + const cleanInstance = JSON.parse(JSON.stringify(ticket)); - let logRecord = { + const logRecord = { originFk: cleanInstance.id, userFk: myUserId, action: 'insert', @@ -137,9 +138,10 @@ module.exports = Self => { newInstance: cleanInstance }; - await models.TicketLog.create(logRecord, options); + await models.TicketLog.create(logRecord, myOptions); if (tx) await tx.commit(); + return await ticket; } catch (e) { if (tx) await tx.rollback(); diff --git a/modules/ticket/back/methods/ticket/priceDifference.js b/modules/ticket/back/methods/ticket/priceDifference.js index 2631481c6..202a619a5 100644 --- a/modules/ticket/back/methods/ticket/priceDifference.js +++ b/modules/ticket/back/methods/ticket/priceDifference.js @@ -6,43 +6,43 @@ module.exports = Self => { accessType: 'READ', accepts: [{ arg: 'id', - type: 'Number', + type: 'number', required: true, description: 'The ticket id', http: {source: 'path'} }, { arg: 'landed', - type: 'Date', + type: 'date', description: 'The landing date', required: true }, { arg: 'addressId', - type: 'Number', + type: 'number', description: 'The address id', required: true }, { arg: 'agencyModeId', - type: 'Number', + type: 'number', description: 'The agencyMode id', required: true }, { arg: 'zoneId', - type: 'Number', + type: 'number', description: 'The zone id', required: true }, { arg: 'warehouseId', - type: 'Number', + type: 'number', description: 'The warehouse id', required: true }], returns: { - type: ['Object'], + type: ['object'], root: true }, http: { @@ -51,38 +51,57 @@ module.exports = Self => { } }); - Self.priceDifference = async(ctx, id, landed, addressId, agencyModeId, zoneId, warehouseId) => { + Self.priceDifference = async(ctx, options) => { + const args = ctx.args; const models = Self.app.models; - const isEditable = await Self.isEditable(ctx, id); const userId = ctx.req.accessToken.userId; + const myOptions = {}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + const isEditable = await Self.isEditable(ctx, args.id, myOptions); if (!isEditable) throw new UserError(`The sales of this ticket can't be modified`); - const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss'); + const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss', myOptions); if (!isProductionBoss) { - const zoneShipped = await models.Agency.getShipped(landed, addressId, agencyModeId, warehouseId); + const zoneShipped = await models.Agency.getShipped( + args.landed, + args.addressId, + args.agencyModeId, + args.warehouseId, + myOptions); - if (!zoneShipped || zoneShipped.zoneFk != zoneId) + if (!zoneShipped || zoneShipped.zoneFk != args.zoneId) throw new UserError(`You don't have privileges to change the zone`); } - let salesObj = { - items: await models.Sale.find({ - where: { - ticketFk: id - }, - order: 'concept ASC', - include: 'item' - }), + const items = await models.Sale.find({ + where: { + ticketFk: args.id + }, + order: 'concept ASC', + include: 'item' + }, myOptions); + + const salesObj = { + items: items, totalUnitPrice: 0.00, totalNewPrice: 0.00, totalDifference: 0.00, }; const query = `CALL vn.ticket_priceDifference(?, ?, ?, ?, ?)`; - const args = [id, landed, addressId, zoneId, warehouseId]; - const [difComponents] = await Self.rawSql(query, args); + const params = [args.id, args.landed, args.addressId, args.zoneId, args.warehouseId]; + const [difComponents] = await Self.rawSql(query, params, myOptions); const map = new Map(); diff --git a/modules/ticket/back/methods/ticket/specs/addSale.spec.js b/modules/ticket/back/methods/ticket/specs/addSale.spec.js index 9f6da7ed2..cfd149511 100644 --- a/modules/ticket/back/methods/ticket/specs/addSale.spec.js +++ b/modules/ticket/back/methods/ticket/specs/addSale.spec.js @@ -1,70 +1,88 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket addSale()', () => { const ticketId = 13; - let newSale; - - afterAll(async done => { - const sale = await app.models.Sale.findById(newSale.id); - await sale.destroy(); - - done(); - }); it('should create a new sale for the ticket with id 13', async() => { - const ctx = { - req: { - accessToken: {userId: 9}, - headers: {origin: 'localhost:5000'}, - __: () => {} - } - }; - const itemId = 4; - const quantity = 10; - newSale = await app.models.Ticket.addSale(ctx, ticketId, itemId, quantity); + const tx = await models.Ticket.beginTransaction({}); - expect(newSale.itemFk).toEqual(4); + try { + const options = {transaction: tx}; + + const ctx = { + req: { + accessToken: {userId: 9}, + headers: {origin: 'localhost:5000'}, + __: () => {} + } + }; + const itemId = 4; + const quantity = 10; + const newSale = await models.Ticket.addSale(ctx, ticketId, itemId, quantity, options); + + expect(newSale.itemFk).toEqual(4); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should not be able to add a sale if the item quantity is not available', async() => { - const ctx = { - req: { - accessToken: {userId: 9}, - headers: {origin: 'localhost:5000'}, - __: () => {} - } - }; - const itemId = 11; - const quantity = 10; + const tx = await models.Ticket.beginTransaction({}); let error; - await app.models.Ticket.addSale(ctx, ticketId, itemId, quantity).catch(e => { - error = e; - }).finally(() => { - expect(error.message).toEqual(`This item is not available`); - }); - expect(error).toBeDefined(); + try { + const options = {transaction: tx}; + + const ctx = { + req: { + accessToken: {userId: 9}, + headers: {origin: 'localhost:5000'}, + __: () => {} + } + }; + const itemId = 11; + const quantity = 10; + + await models.Ticket.addSale(ctx, ticketId, itemId, quantity, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + error = e; + } + + expect(error.message).toEqual(`This item is not available`); }); it('should not be able to add a sale if the ticket is not editable', async() => { - const ctx = { - req: { - accessToken: {userId: 9}, - headers: {origin: 'localhost:5000'}, - __: () => {} - } - }; - const notEditableTicketId = 1; - const itemId = 4; - const quantity = 10; - let error; - await app.models.Ticket.addSale(ctx, notEditableTicketId, itemId, quantity).catch(e => { - error = e; - }).finally(() => { - expect(error.message).toEqual(`The sales of this ticket can't be modified`); - }); + const tx = await models.Ticket.beginTransaction({}); - expect(error).toBeDefined(); + let error; + + try { + const options = {transaction: tx}; + const ctx = { + req: { + accessToken: {userId: 9}, + headers: {origin: 'localhost:5000'}, + __: () => {} + } + }; + const notEditableTicketId = 1; + const itemId = 4; + const quantity = 10; + await models.Ticket.addSale(ctx, notEditableTicketId, itemId, quantity, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + error = e; + } + + expect(error.message).toEqual(`The sales of this ticket can't be modified`); }); }); diff --git a/modules/ticket/back/methods/ticket/specs/canBeInvoiced.spec.js b/modules/ticket/back/methods/ticket/specs/canBeInvoiced.spec.js index 77da98b26..05853acba 100644 --- a/modules/ticket/back/methods/ticket/specs/canBeInvoiced.spec.js +++ b/modules/ticket/back/methods/ticket/specs/canBeInvoiced.spec.js @@ -1,6 +1,5 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); -const models = app.models; describe('ticket canBeInvoiced()', () => { const userId = 19; @@ -24,7 +23,7 @@ describe('ticket canBeInvoiced()', () => { const options = {transaction: tx}; const ticket = await models.Ticket.findById(ticketId, null, options); - await ticket.updateAttribute('refFk', 'T1234567', options); + await ticket.updateAttribute('refFk', 'T1111111', options); const canBeInvoiced = await models.Ticket.canBeInvoiced([ticketId], options); @@ -33,6 +32,7 @@ describe('ticket canBeInvoiced()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -52,6 +52,7 @@ describe('ticket canBeInvoiced()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); @@ -75,12 +76,24 @@ describe('ticket canBeInvoiced()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); it('should return truthy for an invoiceable ticket', async() => { - const canBeInvoiced = await models.Ticket.canBeInvoiced([ticketId]); + const tx = await models.Ticket.beginTransaction({}); - expect(canBeInvoiced).toEqual(true); + try { + const options = {transaction: tx}; + + const canBeInvoiced = await models.Ticket.canBeInvoiced([ticketId], options); + + expect(canBeInvoiced).toEqual(true); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/canHaveStowaway.spec.js b/modules/ticket/back/methods/ticket/specs/canHaveStowaway.spec.js index 231f2581b..2bd25c2d2 100644 --- a/modules/ticket/back/methods/ticket/specs/canHaveStowaway.spec.js +++ b/modules/ticket/back/methods/ticket/specs/canHaveStowaway.spec.js @@ -1,17 +1,39 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket canHaveStowaway()', () => { it('should return true if the ticket warehouse have hasStowaway equal 1', async() => { - const ticketId = 16; - let canStowaway = await app.models.Ticket.canHaveStowaway(ticketId); + const tx = await models.Ticket.beginTransaction({}); - expect(canStowaway).toBeTruthy(); + try { + const options = {transaction: tx}; + + const ticketId = 16; + const canStowaway = await models.Ticket.canHaveStowaway(ticketId, options); + + expect(canStowaway).toBeTruthy(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return false if the ticket warehouse dont have hasStowaway equal 0', async() => { - const ticketId = 10; - let canStowaway = await app.models.Ticket.canHaveStowaway(ticketId); + const tx = await models.Ticket.beginTransaction({}); - expect(canStowaway).toBeFalsy(); + try { + const options = {transaction: tx}; + + const ticketId = 10; + const canStowaway = await models.Ticket.canHaveStowaway(ticketId, options); + + expect(canStowaway).toBeFalsy(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js index f8aa70dcc..9fa69b595 100644 --- a/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js +++ b/modules/ticket/back/methods/ticket/specs/componentUpdate.spec.js @@ -1,4 +1,4 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket componentUpdate()', () => { const userID = 1101; @@ -16,20 +16,20 @@ describe('ticket componentUpdate()', () => { let componentValue; beforeAll(async() => { - const deliveryComponenet = await app.models.Component.findOne({where: {code: 'delivery'}}); + const deliveryComponenet = await 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); + [componentValue] = await models.SaleComponent.rawSql(componentOfSaleSeven); firstvalueBeforeChange = componentValue.value; - [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight); + [componentValue] = await 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({}); + const tx = await models.SaleComponent.beginTransaction({}); try { const options = {transaction: tx}; @@ -59,12 +59,12 @@ describe('ticket componentUpdate()', () => { } }; - await app.models.Ticket.componentUpdate(ctx, options); + await models.Ticket.componentUpdate(ctx, options); - [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleSeven, null, options); + [componentValue] = await models.SaleComponent.rawSql(componentOfSaleSeven, null, options); let firstvalueAfterChange = componentValue.value; - [componentValue] = await app.models.SaleComponent.rawSql(componentOfSaleEight, null, options); + [componentValue] = await models.SaleComponent.rawSql(componentOfSaleEight, null, options); let secondvalueAfterChange = componentValue.value; expect(firstvalueBeforeChange).not.toEqual(firstvalueAfterChange); @@ -78,7 +78,7 @@ describe('ticket componentUpdate()', () => { }); it('should change the addressFk and check that delivery observations have been changed', async() => { - const tx = await app.models.SaleComponent.beginTransaction({}); + const tx = await models.SaleComponent.beginTransaction({}); try { const options = {transaction: tx}; @@ -107,10 +107,10 @@ describe('ticket componentUpdate()', () => { } } }; - const observationTypeDelivery = await app.models.ObservationType.findOne({ + const observationTypeDelivery = await models.ObservationType.findOne({ where: {code: 'delivery'} }, options); - const originalTicketObservation = await app.models.TicketObservation.findOne({ + const originalTicketObservation = await models.TicketObservation.findOne({ where: { ticketFk: args.id, observationTypeFk: observationTypeDelivery.id} @@ -118,9 +118,9 @@ describe('ticket componentUpdate()', () => { expect(originalTicketObservation).toBeDefined(); - await app.models.Ticket.componentUpdate(ctx, options); + await models.Ticket.componentUpdate(ctx, options); - const removedTicketObservation = await app.models.TicketObservation.findOne({ + const removedTicketObservation = await models.TicketObservation.findOne({ where: { ticketFk: ticketID, observationTypeFk: observationTypeDelivery.id} diff --git a/modules/ticket/back/methods/ticket/specs/deleteStowaway.spec.js b/modules/ticket/back/methods/ticket/specs/deleteStowaway.spec.js index 563c1334d..62bfc71be 100644 --- a/modules/ticket/back/methods/ticket/specs/deleteStowaway.spec.js +++ b/modules/ticket/back/methods/ticket/specs/deleteStowaway.spec.js @@ -1,4 +1,4 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket deleteStowaway()', () => { const shipId = 16; @@ -14,51 +14,62 @@ describe('ticket deleteStowaway()', () => { }; it(`should create an stowaway, delete it and see the states of both stowaway and ship go back to the last states`, async() => { - await app.models.Stowaway.rawSql(` - INSERT INTO stowaway (id, shipFk) VALUES (?, ?) - `, [stowawayId, shipId]); - await app.models.Stowaway.rawSql( - `CALL ticketStateUpdate(?, ?)`, [shipId, 'BOARDING']); - await app.models.Stowaway.rawSql( - `CALL ticketStateUpdate(?, ?)`, [stowawayId, 'BOARDING']); + const tx = await models.Ticket.beginTransaction({}); - let createdStowaways = await app.models.Stowaway.count({id: stowawayId, shipFk: shipId}); + try { + const options = {transaction: tx}; - expect(createdStowaways).toEqual(1); + await models.Stowaway.rawSql(` + INSERT INTO stowaway (id, shipFk) VALUES (?, ?) + `, [stowawayId, shipId], options); + await models.Stowaway.rawSql( + `CALL ticketStateUpdate(?, ?)`, [shipId, 'BOARDING'], options); + await models.Stowaway.rawSql( + `CALL ticketStateUpdate(?, ?)`, [stowawayId, 'BOARDING'], options); - let shipState = await app.models.TicketLastState.findOne({ - where: { - ticketFk: shipId - } - }); - let stowawayState = await app.models.TicketLastState.findOne({ - where: { - ticketFk: stowawayId - } - }); + let createdStowaways = await models.Stowaway.count({id: stowawayId, shipFk: shipId}, options); - expect(shipState.name).toEqual('Embarcando'); - expect(stowawayState.name).toEqual('Embarcando'); + expect(createdStowaways).toEqual(1); - await app.models.Ticket.deleteStowaway(ctx, shipId); - await app.models.Ticket.deleteStowaway(ctx, stowawayId); + let shipState = await models.TicketLastState.findOne({ + where: { + ticketFk: shipId + } + }, options); + let stowawayState = await models.TicketLastState.findOne({ + where: { + ticketFk: stowawayId + } + }, options); - createdStowaways = await app.models.Stowaway.count({id: stowawayId, shipFk: shipId}); + expect(shipState.name).toEqual('Embarcando'); + expect(stowawayState.name).toEqual('Embarcando'); - expect(createdStowaways).toEqual(0); + await models.Ticket.deleteStowaway(ctx, shipId, options); + await models.Ticket.deleteStowaway(ctx, stowawayId, options); - shipState = await app.models.TicketLastState.findOne({ - where: { - ticketFk: shipId - } - }); - stowawayState = await app.models.TicketLastState.findOne({ - where: { - ticketFk: stowawayId - } - }); + createdStowaways = await models.Stowaway.count({id: stowawayId, shipFk: shipId}, options); - expect(shipState.name).toEqual('OK'); - expect(stowawayState.name).toEqual('Libre'); + expect(createdStowaways).toEqual(0); + + shipState = await models.TicketLastState.findOne({ + where: { + ticketFk: shipId + } + }, options); + stowawayState = await models.TicketLastState.findOne({ + where: { + ticketFk: stowawayId + } + }, options); + + expect(shipState.name).toEqual('OK'); + expect(stowawayState.name).toEqual('Libre'); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/filter.spec.js b/modules/ticket/back/methods/ticket/specs/filter.spec.js index c742de41d..743a3ba81 100644 --- a/modules/ticket/back/methods/ticket/specs/filter.spec.js +++ b/modules/ticket/back/methods/ticket/specs/filter.spec.js @@ -1,106 +1,205 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket filter()', () => { it('should return the tickets matching the filter', async() => { - const ctx = {req: {accessToken: {userId: 9}}, args: {}}; - const filter = {order: 'id DESC'}; - const result = await app.models.Ticket.filter(ctx, filter); + const tx = await models.Ticket.beginTransaction({}); - expect(result.length).toEqual(24); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 9}}, args: {}}; + const filter = {order: 'id DESC'}; + const result = await models.Ticket.filter(ctx, filter, options); + + expect(result.length).toEqual(24); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return the tickets matching the problems on true', async() => { - const yesterday = new Date(); - yesterday.setHours(0, 0, 0, 0); - const today = new Date(); - today.setHours(23, 59, 59, 59); + const tx = await models.Ticket.beginTransaction({}); - const ctx = {req: {accessToken: {userId: 9}}, args: { - problems: true, - from: yesterday, - to: today - }}; - const filter = {}; - const result = await app.models.Ticket.filter(ctx, filter); + try { + const options = {transaction: tx}; - expect(result.length).toEqual(4); + const yesterday = new Date(); + yesterday.setHours(0, 0, 0, 0); + const today = new Date(); + today.setHours(23, 59, 59, 59); + + const ctx = {req: {accessToken: {userId: 9}}, args: { + problems: true, + from: yesterday, + to: today + }}; + const filter = {}; + const result = await models.Ticket.filter(ctx, filter, options); + + expect(result.length).toEqual(4); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return the tickets matching the problems on false', async() => { - const yesterday = new Date(); - yesterday.setDate(yesterday.getDate() - 1); - yesterday.setHours(0, 0, 0, 0); - const today = new Date(); - today.setHours(23, 59, 59, 59); + const tx = await models.Ticket.beginTransaction({}); - const ctx = {req: {accessToken: {userId: 9}}, args: { - problems: false, - from: yesterday, - to: today - }}; - const filter = {}; - const result = await app.models.Ticket.filter(ctx, filter); + try { + const options = {transaction: tx}; - expect(result.length).toEqual(6); + const yesterday = new Date(); + yesterday.setDate(yesterday.getDate() - 1); + yesterday.setHours(0, 0, 0, 0); + const today = new Date(); + today.setHours(23, 59, 59, 59); + + const ctx = {req: {accessToken: {userId: 9}}, args: { + problems: false, + from: yesterday, + to: today + }}; + const filter = {}; + const result = await models.Ticket.filter(ctx, filter, options); + + expect(result.length).toEqual(6); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return the tickets matching the problems on null', async() => { - const ctx = {req: {accessToken: {userId: 9}}, args: {problems: null}}; - const filter = {}; - const result = await app.models.Ticket.filter(ctx, filter); + const tx = await models.Ticket.beginTransaction({}); - expect(result.length).toEqual(24); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 9}}, args: {problems: null}}; + const filter = {}; + const result = await models.Ticket.filter(ctx, filter, options); + + expect(result.length).toEqual(24); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return the tickets matching the orderId 11', async() => { - const ctx = {req: {accessToken: {userId: 9}}, args: {orderFk: 11}}; - const filter = {}; - const result = await app.models.Ticket.filter(ctx, filter); - const firstRow = result[0]; + const tx = await models.Ticket.beginTransaction({}); - expect(result.length).toEqual(1); - expect(firstRow.id).toEqual(11); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 9}}, args: {orderFk: 11}}; + const filter = {}; + const result = await models.Ticket.filter(ctx, filter, options); + const firstRow = result[0]; + + expect(result.length).toEqual(1); + expect(firstRow.id).toEqual(11); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return the tickets with grouped state "Pending" and not "Ok" nor "BOARDING"', async() => { - const ctx = {req: {accessToken: {userId: 9}}, args: {pending: true}}; - const filter = {}; - const result = await app.models.Ticket.filter(ctx, filter); + const tx = await models.Ticket.beginTransaction({}); - const length = result.length; - const anyResult = result[Math.floor(Math.random() * Math.floor(length))]; + try { + const options = {transaction: tx}; - expect(length).toEqual(7); - expect(anyResult.state).toMatch(/(Libre|Arreglar)/); + const ctx = {req: {accessToken: {userId: 9}}, args: {pending: true}}; + const filter = {}; + const result = await models.Ticket.filter(ctx, filter, options); + + const length = result.length; + const anyResult = result[Math.floor(Math.random() * Math.floor(length))]; + + expect(length).toEqual(7); + expect(anyResult.state).toMatch(/(Libre|Arreglar)/); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return the tickets that are not pending', async() => { - const ctx = {req: {accessToken: {userId: 9}}, args: {pending: false}}; - const filter = {}; - const result = await app.models.Ticket.filter(ctx, filter); - const firstRow = result[0]; - const secondRow = result[1]; - const thirdRow = result[2]; + const tx = await models.Ticket.beginTransaction({}); - expect(result.length).toEqual(12); - expect(firstRow.state).toEqual('Entregado'); - expect(secondRow.state).toEqual('Entregado'); - expect(thirdRow.state).toEqual('Entregado'); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 9}}, args: {pending: false}}; + const filter = {}; + const result = await models.Ticket.filter(ctx, filter, options); + const firstRow = result[0]; + const secondRow = result[1]; + const thirdRow = result[2]; + + expect(result.length).toEqual(12); + expect(firstRow.state).toEqual('Entregado'); + expect(secondRow.state).toEqual('Entregado'); + expect(thirdRow.state).toEqual('Entregado'); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return the tickets from the worker team', async() => { - const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: true}}; - const filter = {}; - const result = await app.models.Ticket.filter(ctx, filter); + const tx = await models.Ticket.beginTransaction({}); - expect(result.length).toEqual(20); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: true}}; + const filter = {}; + const result = await models.Ticket.filter(ctx, filter, options); + + expect(result.length).toEqual(20); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return the tickets that are not from the worker team', async() => { - const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: false}}; - const filter = {}; - const result = await app.models.Ticket.filter(ctx, filter); + const tx = await models.Ticket.beginTransaction({}); - expect(result.length).toEqual(4); + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 18}}, args: {myTeam: false}}; + const filter = {}; + const result = await models.Ticket.filter(ctx, filter, options); + + expect(result.length).toEqual(4); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/freightCost.spec.js b/modules/ticket/back/methods/ticket/specs/freightCost.spec.js index cb8f5a562..6edb4cebb 100644 --- a/modules/ticket/back/methods/ticket/specs/freightCost.spec.js +++ b/modules/ticket/back/methods/ticket/specs/freightCost.spec.js @@ -1,17 +1,39 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket freightCost()', () => { it('should return the freight cost of a given ticket', async() => { - let ticketId = 7; - let freightCost = await app.models.Ticket.freightCost(ticketId); + const tx = await models.Ticket.beginTransaction({}); - expect(freightCost).toBe(4); + try { + const options = {transaction: tx}; + + const ticketId = 7; + const freightCost = await models.Ticket.freightCost(ticketId, options); + + expect(freightCost).toBe(4); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return null if the ticket does not exist', async() => { - let ticketId = 99; - let freightCost = await app.models.Ticket.freightCost(ticketId); + const tx = await models.Ticket.beginTransaction({}); - expect(freightCost).toBeNull(); + try { + const options = {transaction: tx}; + + const ticketId = 99; + const freightCost = await models.Ticket.freightCost(ticketId, options); + + expect(freightCost).toBeNull(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/getComponentsSum.spec.js b/modules/ticket/back/methods/ticket/specs/getComponentsSum.spec.js index 4be8de3f0..5c4de4463 100644 --- a/modules/ticket/back/methods/ticket/specs/getComponentsSum.spec.js +++ b/modules/ticket/back/methods/ticket/specs/getComponentsSum.spec.js @@ -1,20 +1,42 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket getComponentsSum()', () => { it('should get the list of component for the ticket sales', async() => { - const ticketId = 7; - const components = await app.models.Ticket.getComponentsSum(ticketId); - const length = components.length; - const anyComponent = components[Math.floor(Math.random() * Math.floor(length))]; + const tx = await models.Ticket.beginTransaction({}); - expect(components.length).toBeGreaterThan(0); - expect(anyComponent.componentFk).toBeDefined(); + try { + const options = {transaction: tx}; + + const ticketId = 7; + const components = await models.Ticket.getComponentsSum(ticketId, options); + const length = components.length; + const anyComponent = components[Math.floor(Math.random() * Math.floor(length))]; + + expect(components.length).toBeGreaterThan(0); + expect(anyComponent.componentFk).toBeDefined(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return 0 if the given ticket does not have sales', async() => { - const ticketWithoutSales = 21; - const components = await app.models.Ticket.getComponentsSum(ticketWithoutSales); + const tx = await models.Ticket.beginTransaction({}); - expect(components.length).toEqual(0); + try { + const options = {transaction: tx}; + + const ticketWithoutSales = 21; + const components = await models.Ticket.getComponentsSum(ticketWithoutSales, options); + + expect(components.length).toEqual(0); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/getPossibleStowaways.spec.js b/modules/ticket/back/methods/ticket/specs/getPossibleStowaways.spec.js index 7e667ad36..ed1a327d4 100644 --- a/modules/ticket/back/methods/ticket/specs/getPossibleStowaways.spec.js +++ b/modules/ticket/back/methods/ticket/specs/getPossibleStowaways.spec.js @@ -1,30 +1,60 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; let UserError = require('vn-loopback/util/user-error'); describe('ticket getPossibleStowaways()', () => { it(`should throw an error if Can't create stowaway for this ticket`, async() => { - let error; - let ticketId = 10; + const tx = await models.Ticket.beginTransaction({}); - await app.models.Ticket.getPossibleStowaways(ticketId) - .catch(e => { - error = e; - }); + let error; + + try { + const options = {transaction: tx}; + + const ticketId = 10; + await models.Ticket.getPossibleStowaways(ticketId, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + error = e; + } expect(error).toEqual(new UserError(`Can't create stowaway for this ticket`)); }); it('should return an empty list of tickets for a valid ticket', async() => { - let ticketId = 12; - let possibleStowaways = await app.models.Ticket.getPossibleStowaways(ticketId); + const tx = await models.Ticket.beginTransaction({}); - expect(possibleStowaways.length).toEqual(0); + try { + const options = {transaction: tx}; + + const ticketId = 12; + const possibleStowaways = await models.Ticket.getPossibleStowaways(ticketId, options); + + expect(possibleStowaways.length).toEqual(0); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return allowed list of tickets for a valid ticket', async() => { - let ticketId = 16; - let possibleStowaways = await app.models.Ticket.getPossibleStowaways(ticketId); + const tx = await models.Ticket.beginTransaction({}); - expect(possibleStowaways.length).toEqual(1); + try { + const options = {transaction: tx}; + + const ticketId = 16; + const possibleStowaways = await models.Ticket.getPossibleStowaways(ticketId, options); + + expect(possibleStowaways.length).toEqual(1); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/getSales.spec.js b/modules/ticket/back/methods/ticket/specs/getSales.spec.js index 4e57f570f..b9f78e40b 100644 --- a/modules/ticket/back/methods/ticket/specs/getSales.spec.js +++ b/modules/ticket/back/methods/ticket/specs/getSales.spec.js @@ -1,14 +1,25 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket getSales()', () => { it('should return the sales of a ticket', async() => { - let sales = await app.models.Ticket.getSales(16); + const tx = await models.Ticket.beginTransaction({}); - expect(sales.length).toEqual(4); - expect(sales[0].item).toBeDefined(); - expect(sales[1].item).toBeDefined(); - expect(sales[2].item).toBeDefined(); - expect(sales[3].item).toBeDefined(); - expect(sales[0].claim).toBeDefined(); + try { + const options = {transaction: tx}; + + const sales = await models.Ticket.getSales(16, options); + + expect(sales.length).toEqual(4); + expect(sales[0].item).toBeDefined(); + expect(sales[1].item).toBeDefined(); + expect(sales[2].item).toBeDefined(); + expect(sales[3].item).toBeDefined(); + expect(sales[0].claim).toBeDefined(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/getSalespersonMana.spec.js b/modules/ticket/back/methods/ticket/specs/getSalespersonMana.spec.js index 84e958dfa..6029ca4a7 100644 --- a/modules/ticket/back/methods/ticket/specs/getSalespersonMana.spec.js +++ b/modules/ticket/back/methods/ticket/specs/getSalespersonMana.spec.js @@ -1,15 +1,37 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket getSalesPersonMana()', () => { it('should get the mana of a salesperson of a given ticket', async() => { - let mana = await app.models.Ticket.getSalesPersonMana(1); + const tx = await models.Ticket.beginTransaction({}); - expect(mana).toEqual(124); + try { + const options = {transaction: tx}; + + const mana = await models.Ticket.getSalesPersonMana(1, options); + + expect(mana).toEqual(124); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return 0 if the given ticket does not exist', async() => { - let mana = await app.models.Ticket.getSalesPersonMana(99); + const tx = await models.Ticket.beginTransaction({}); - expect(mana).toEqual(0); + try { + const options = {transaction: tx}; + + const mana = await models.Ticket.getSalesPersonMana(99, options); + + expect(mana).toEqual(0); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/getTotalVolume.spec.js b/modules/ticket/back/methods/ticket/specs/getTotalVolume.spec.js index 1901e2cb2..d11e830c5 100644 --- a/modules/ticket/back/methods/ticket/specs/getTotalVolume.spec.js +++ b/modules/ticket/back/methods/ticket/specs/getTotalVolume.spec.js @@ -1,13 +1,23 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket getTotalVolume()', () => { it('should return the total volume of a ticket', async() => { - let ticketFk = 1; + const tx = await models.Ticket.beginTransaction({}); - let expectedResult = 1.568; + try { + const options = {transaction: tx}; - let result = await app.models.Ticket.getTotalVolume(ticketFk); + const ticketFk = 1; + const expectedResult = 1.568; - expect(result.totalVolume).toEqual(expectedResult); + const result = await models.Ticket.getTotalVolume(ticketFk, options); + + expect(result.totalVolume).toEqual(expectedResult); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/getVolume.spec.js b/modules/ticket/back/methods/ticket/specs/getVolume.spec.js index 680724661..35134e4d0 100644 --- a/modules/ticket/back/methods/ticket/specs/getVolume.spec.js +++ b/modules/ticket/back/methods/ticket/specs/getVolume.spec.js @@ -1,11 +1,21 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket getVolume()', () => { it('should call the getVolume method', async() => { - let ticketFk = 1; - await app.models.Ticket.getVolume(ticketFk) - .then(response => { - expect(response[0].volume).toEqual(1.09); - }); + const tx = await models.Ticket.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const ticketId = 1; + const result = await models.Ticket.getVolume(ticketId, options); + + expect(result[0].volume).toEqual(1.09); + + 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 5a6bf7d19..adc2acdee 100644 --- a/modules/ticket/back/methods/ticket/specs/isEditable.spec.js +++ b/modules/ticket/back/methods/ticket/specs/isEditable.spec.js @@ -1,8 +1,8 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket isEditable()', () => { it('should return false if the given ticket does not exist', async() => { - const tx = await app.models.Ticket.beginTransaction({}); + const tx = await models.Ticket.beginTransaction({}); let result; try { @@ -11,7 +11,7 @@ describe('ticket isEditable()', () => { req: {accessToken: {userId: 9}} }; - result = await app.models.Ticket.isEditable(ctx, 9999, options); + result = await models.Ticket.isEditable(ctx, 9999, options); await tx.rollback(); } catch (e) { @@ -23,12 +23,12 @@ describe('ticket isEditable()', () => { }); it(`should return false if the given ticket isn't invoiced but isDeleted`, async() => { - const tx = await app.models.Ticket.beginTransaction({}); + const tx = await models.Ticket.beginTransaction({}); let result; try { const options = {transaction: tx}; - const deletedTicket = await app.models.Ticket.findOne({ + const deletedTicket = await models.Ticket.findOne({ where: { invoiceOut: null, isDeleted: true @@ -40,7 +40,7 @@ describe('ticket isEditable()', () => { req: {accessToken: {userId: 9}} }; - result = await app.models.Ticket.isEditable(ctx, deletedTicket.id, options); + result = await models.Ticket.isEditable(ctx, deletedTicket.id, options); await tx.rollback(); } catch (e) { @@ -52,7 +52,7 @@ describe('ticket isEditable()', () => { }); it('should return true if the given ticket is editable', async() => { - const tx = await app.models.Ticket.beginTransaction({}); + const tx = await models.Ticket.beginTransaction({}); let result; try { @@ -61,7 +61,7 @@ describe('ticket isEditable()', () => { req: {accessToken: {userId: 9}} }; - result = await app.models.Ticket.isEditable(ctx, 16, options); + result = await models.Ticket.isEditable(ctx, 16, options); await tx.rollback(); } catch (e) { @@ -73,7 +73,7 @@ describe('ticket isEditable()', () => { }); it('should not be able to edit a deleted or invoiced ticket even for salesAssistant', async() => { - const tx = await app.models.Ticket.beginTransaction({}); + const tx = await models.Ticket.beginTransaction({}); let result; try { @@ -82,7 +82,7 @@ describe('ticket isEditable()', () => { req: {accessToken: {userId: 21}} }; - result = await app.models.Ticket.isEditable(ctx, 19, options); + result = await models.Ticket.isEditable(ctx, 19, options); await tx.rollback(); } catch (e) { @@ -94,7 +94,7 @@ describe('ticket isEditable()', () => { }); it('should not be able to edit a deleted or invoiced ticket even for productionBoss', async() => { - const tx = await app.models.Ticket.beginTransaction({}); + const tx = await models.Ticket.beginTransaction({}); let result; try { @@ -103,7 +103,7 @@ describe('ticket isEditable()', () => { req: {accessToken: {userId: 50}} }; - result = await app.models.Ticket.isEditable(ctx, 19, options); + result = await models.Ticket.isEditable(ctx, 19, options); await tx.rollback(); } catch (e) { @@ -115,7 +115,7 @@ describe('ticket isEditable()', () => { }); it('should not be able to edit a deleted or invoiced ticket even for salesPerson', async() => { - const tx = await app.models.Ticket.beginTransaction({}); + const tx = await models.Ticket.beginTransaction({}); let result; try { @@ -124,7 +124,7 @@ describe('ticket isEditable()', () => { req: {accessToken: {userId: 18}} }; - result = await app.models.Ticket.isEditable(ctx, 19, options); + result = await models.Ticket.isEditable(ctx, 19, options); await tx.rollback(); } catch (e) { diff --git a/modules/ticket/back/methods/ticket/specs/isEmpty.spec.js b/modules/ticket/back/methods/ticket/specs/isEmpty.spec.js index df8818261..ffa71f523 100644 --- a/modules/ticket/back/methods/ticket/specs/isEmpty.spec.js +++ b/modules/ticket/back/methods/ticket/specs/isEmpty.spec.js @@ -1,27 +1,71 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket isEmpty()', () => { it('should return false if the ticket contains any packages', async() => { - let result = await app.models.Ticket.isEmpty(3); + const tx = await models.Ticket.beginTransaction({}); - expect(result).toBeFalsy(); + try { + const options = {transaction: tx}; + + const result = await models.Ticket.isEmpty(3, options); + + expect(result).toBeFalsy(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return false if the ticket contains any services', async() => { - let result = await app.models.Ticket.isEmpty(8); + const tx = await models.Ticket.beginTransaction({}); - expect(result).toBeFalsy(); + try { + const options = {transaction: tx}; + + const result = await models.Ticket.isEmpty(8, options); + + expect(result).toBeFalsy(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return false if the ticket contains any purchase request', async() => { - let result = await app.models.Ticket.isEmpty(11); + const tx = await models.Ticket.beginTransaction({}); - expect(result).toBeFalsy(); + try { + const options = {transaction: tx}; + + const result = await models.Ticket.isEmpty(11, options); + + expect(result).toBeFalsy(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return false if the ticket contains any sale', async() => { - let result = await app.models.Ticket.isEmpty(4); + const tx = await models.Ticket.beginTransaction({}); - expect(result).toBeFalsy(); + try { + const options = {transaction: tx}; + + const result = await models.Ticket.isEmpty(4, options); + + expect(result).toBeFalsy(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/isLocked.spec.js b/modules/ticket/back/methods/ticket/specs/isLocked.spec.js index 192c80f10..7d161ad82 100644 --- a/modules/ticket/back/methods/ticket/specs/isLocked.spec.js +++ b/modules/ticket/back/methods/ticket/specs/isLocked.spec.js @@ -1,34 +1,67 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; describe('ticket isLocked()', () => { it('should return true if the given ticket does not exist', async() => { - let result = await app.models.Ticket.isLocked(99999); + const tx = await models.Ticket.beginTransaction({}); - expect(result).toEqual(true); + try { + const options = {transaction: tx}; + + const result = await models.Ticket.isLocked(99999, options); + + expect(result).toEqual(true); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return true if the given ticket is invoiced', async() => { - let invoicedTicket = await app.models.Ticket.findOne({ - where: {invoiceOut: {neq: null}}, - fields: ['id'] - }); + const tx = await models.Ticket.beginTransaction({}); - let result = await app.models.Ticket.isLocked(invoicedTicket.id); + try { + const options = {transaction: tx}; - expect(result).toEqual(true); + const invoicedTicket = await models.Ticket.findOne({ + where: {invoiceOut: {neq: null}}, + fields: ['id'] + }, options); + + const result = await models.Ticket.isLocked(invoicedTicket.id, options); + + expect(result).toEqual(true); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it(`should return true if the given ticket isn't invoiced but deleted`, async() => { - let deletedTicket = await app.models.Ticket.findOne({ - where: { - invoiceOut: null, - isDeleted: true - }, - fields: ['id'] - }); + const tx = await models.Ticket.beginTransaction({}); - let result = await app.models.Ticket.isLocked(deletedTicket.id); + try { + const options = {transaction: tx}; - expect(result).toEqual(true); + const deletedTicket = await models.Ticket.findOne({ + where: { + invoiceOut: null, + isDeleted: true + }, + fields: ['id'] + }, options); + + const result = await models.Ticket.isLocked(deletedTicket.id, options); + + expect(result).toEqual(true); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/makeInvoice.spec.js b/modules/ticket/back/methods/ticket/specs/makeInvoice.spec.js index 55c5bccd7..36fc6732b 100644 --- a/modules/ticket/back/methods/ticket/specs/makeInvoice.spec.js +++ b/modules/ticket/back/methods/ticket/specs/makeInvoice.spec.js @@ -1,6 +1,5 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); -const models = app.models; describe('ticket makeInvoice()', () => { const userId = 19; @@ -107,6 +106,7 @@ describe('ticket makeInvoice()', () => { await tx.rollback(); } catch (e) { await tx.rollback(); + throw e; } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/new.spec.js b/modules/ticket/back/methods/ticket/specs/new.spec.js index d7d226295..fce7cdceb 100644 --- a/modules/ticket/back/methods/ticket/specs/new.spec.js +++ b/modules/ticket/back/methods/ticket/specs/new.spec.js @@ -1,118 +1,119 @@ -const app = require('vn-loopback/server/server'); +const models = require('vn-loopback/server/server').models; let UserError = require('vn-loopback/util/user-error'); describe('ticket new()', () => { - let ticketIdsToDelete = []; - let today = new Date(); - let ctx = {req: {accessToken: {userId: 1}}}; - - afterAll(async done => { - for (id of ticketIdsToDelete) - await app.models.Ticket.destroyById(id); - - done(); - }); + const today = new Date(); + const ctx = {req: {accessToken: {userId: 1}}}; it('should throw an error if the client isnt frozen and isnt active', async() => { - let error; - let params = { - clientId: 1106, - shipped: today, - landed: null, - warehouseId: 1, - companyId: 442, - addressId: 6 - }; + const tx = await models.Ticket.beginTransaction({}); - await app.models.Ticket.new(ctx, - params.clientId, - params.shipped, - params.landed, - params.warehouseId, - params.companyId, - params.addressId - ).catch(e => { + let error; + + try { + const options = {transaction: tx}; + + ctx.args = { + clientId: 1106, + shipped: today, + landed: null, + warehouseId: 1, + companyId: 442, + addressId: 6 + }; + + await models.Ticket.new(ctx, options); + + await tx.rollback(); + } catch (e) { error = e; - }); + await tx.rollback(); + } expect(error).toEqual(new UserError(`You can't create a ticket for a inactive client`)); }); it('should throw an error if the address doesnt exist', async() => { + const tx = await models.Ticket.beginTransaction({}); + let error; - let params = { - clientId: 1104, - shipped: today, - landed: null, - warehouseId: 1, - companyId: 442, - addressId: 'invalid address' - }; - await app.models.Ticket.new(ctx, - params.clientId, - params.shipped, - params.landed, - params.warehouseId, - params.companyId, - params.addressId - ).catch(response => { - expect(response.message).toEqual(`This address doesn't exist`); - error = response; - }); + try { + const options = {transaction: tx}; - expect(error).toBeDefined(); + ctx.args = { + clientId: 1104, + shipped: today, + landed: null, + warehouseId: 1, + companyId: 442, + addressId: 'invalid address' + }; + + await models.Ticket.new(ctx, options); + + await tx.rollback(); + } catch (e) { + error = e; + await tx.rollback(); + } + + expect(error.message).toEqual(`This address doesn't exist`); }); it('should return the id of the created ticket', async() => { - let params = { - clientId: 1104, - shipped: today, - landed: today, - warehouseId: 2, - companyId: 442, - addressId: 4, - agencyModeId: 1 - }; + const tx = await models.Ticket.beginTransaction({}); - const ticket = await app.models.Ticket.new(ctx, - params.clientId, - params.shipped, - params.landed, - params.warehouseId, - params.companyId, - params.addressId, - params.agencyModeId); + try { + const options = {transaction: tx}; - let newestTicketIdInFixtures = 21; + ctx.args = { + clientId: 1104, + shipped: today, + landed: today, + warehouseId: 2, + companyId: 442, + addressId: 4, + agencyModeId: 1 + }; - ticketIdsToDelete.push(ticket.id); + const ticket = await models.Ticket.new(ctx, options); - expect(ticket.id).toBeGreaterThan(newestTicketIdInFixtures); + const newestTicketIdInFixtures = 21; + + expect(ticket.id).toBeGreaterThan(newestTicketIdInFixtures); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return the set a shipped when the agency is not especified', async() => { - let params = { - clientId: 1104, - landed: today, - shipped: null, - warehouseId: 2, - companyId: 442, - addressId: 4, - agencyModeId: null - }; + const tx = await models.Ticket.beginTransaction({}); - const ticket = await app.models.Ticket.new(ctx, - params.clientId, - params.shipped, - params.landed, - params.warehouseId, - params.companyId, - params.addressId, - params.agencyModeId); + try { + const options = {transaction: tx}; - ticketIdsToDelete.push(ticket.id); + ctx.args = { + clientId: 1104, + landed: today, + shipped: null, + warehouseId: 2, + companyId: 442, + addressId: 4, + agencyModeId: null + }; - expect(ticket.shipped).toEqual(jasmine.any(Date)); + const ticket = await models.Ticket.new(ctx, options); + + expect(ticket.shipped).toEqual(jasmine.any(Date)); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/ticket/back/methods/ticket/specs/priceDifference.spec.js b/modules/ticket/back/methods/ticket/specs/priceDifference.spec.js index ffdee2a5a..fed899d77 100644 --- a/modules/ticket/back/methods/ticket/specs/priceDifference.spec.js +++ b/modules/ticket/back/methods/ticket/specs/priceDifference.spec.js @@ -1,38 +1,61 @@ -const app = require('vn-loopback/server/server'); -let UserError = require('vn-loopback/util/user-error'); +const models = require('vn-loopback/server/server').models; +const UserError = require('vn-loopback/util/user-error'); describe('sale priceDifference()', () => { it('should return ticket price differences', async() => { - let tomorrow = new Date(); - tomorrow.setDate(tomorrow.getDate() + 1); + const tx = await models.Ticket.beginTransaction({}); - const ticketId = 16; - const landed = tomorrow; - const addressId = 126; - const agencyModeId = 7; - const zoneId = 3; - const warehouseId = 1; + try { + const options = {transaction: tx}; - const httpCtx = {req: {accessToken: {userId: 1106}}}; - let result = await app.models.Ticket.priceDifference(httpCtx, ticketId, landed, - addressId, agencyModeId, zoneId, warehouseId); + const tomorrow = new Date(); + tomorrow.setDate(tomorrow.getDate() + 1); - expect(result.totalUnitPrice).toEqual(result.totalNewPrice); - expect(result.totalDifference).toEqual(0); + const ctx = {req: {accessToken: {userId: 1106}}}; + ctx.args = { + id: 16, + landed: tomorrow, + addressId: 126, + agencyModeId: 7, + zoneId: 3, + warehouseId: 1 + }; + + const result = await models.Ticket.priceDifference(ctx, options); + + expect(result.totalUnitPrice).toEqual(result.totalNewPrice); + expect(result.totalDifference).toEqual(0); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should return an error if the ticket is not editable', async() => { - const ticketId = 1; - const landed = new Date(); - const addressId = 121; - const zoneId = 3; - const warehouseId = 1; + const tx = await models.Ticket.beginTransaction({}); + let error; - const httpCtx = {req: {accessToken: {userId: 1106}}}; - await app.models.Ticket.priceDifference(httpCtx, ticketId, landed, addressId, zoneId, warehouseId) - .catch(e => { - error = e; - }); + + try { + const options = {transaction: tx}; + + const ctx = {req: {accessToken: {userId: 1106}}}; + ctx.args = { + id: 1, + landed: new Date(), + addressId: 121, + zoneId: 3, + warehouseId: 1 + }; + await models.Ticket.priceDifference(ctx, options); + + await tx.rollback(); + } catch (e) { + error = e; + await tx.rollback(); + } expect(error).toEqual(new UserError(`The sales of this ticket can't be modified`)); }); diff --git a/modules/ticket/back/models/ticket.js b/modules/ticket/back/models/ticket.js index 8159d254d..b217eae4d 100644 --- a/modules/ticket/back/models/ticket.js +++ b/modules/ticket/back/models/ticket.js @@ -2,7 +2,6 @@ const LoopBackContext = require('loopback-context'); module.exports = Self => { - require('../methods/ticket/changeWorker')(Self); require('../methods/ticket/getVolume')(Self); require('../methods/ticket/getTotalVolume')(Self); require('../methods/ticket/summary')(Self); diff --git a/modules/worker/front/descriptor/index.html b/modules/worker/front/descriptor/index.html index ad0b9e5c3..036b70eab 100644 --- a/modules/worker/front/descriptor/index.html +++ b/modules/worker/front/descriptor/index.html @@ -46,6 +46,8 @@
diff --git a/modules/zone/back/methods/agency/getShipped.js b/modules/zone/back/methods/agency/getShipped.js index a0cc64338..2889a2557 100644 --- a/modules/zone/back/methods/agency/getShipped.js +++ b/modules/zone/back/methods/agency/getShipped.js @@ -35,7 +35,7 @@ module.exports = Self => { }); Self.getShipped = async(landed, addressFk, agencyModeFk, warehouseFk, options) => { - let myOptions = {}; + const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options);