diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 9fc4d4fb1..04846435f 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -3697,15 +3697,19 @@ UPDATE vn.collection UPDATE vn.sale SET isPicked =FALSE; -INSERT INTO machineWorkerConfig(maxHours) +INSERT INTO vn.machineWorkerConfig(maxHours) VALUES(12); -INSERT INTO workerAppTester(workerFk) VALUES(66); +INSERT INTO vn.workerAppTester(workerFk) VALUES(66); INSERT INTO `vn`.`machine` (`plate`, `maker`, `model`, `warehouseFk`, `departmentFk`, `type`, `use`, `productionYear`, `workerFk`, `companyFk`) VALUES ('RE-003', 'IRON', 'JPH-24', 60, 23, 'ELECTRIC TOW', 'Drag cars', 2020, 103, 442); -INSERT INTO machineWorker(workerFk,machineFk,inTimed) - VALUES (104,1,'2001-01-01 10:00:00.00.000'); \ No newline at end of file +INSERT INTO vn.machineWorker(workerFk,machineFk,inTimed) + VALUES (104,1,'2001-01-01 10:00:00.00.000'); + +UPDATE vn.buy + SET itemOriginalFk = 1 + WHERE id = 1; \ No newline at end of file diff --git a/modules/ticket/back/methods/sale-tracking/specs/updateTracking.spec.js b/modules/ticket/back/methods/sale-tracking/specs/updateTracking.spec.js new file mode 100644 index 000000000..2756bc09e --- /dev/null +++ b/modules/ticket/back/methods/sale-tracking/specs/updateTracking.spec.js @@ -0,0 +1,78 @@ +const {models} = require('vn-loopback/server/server'); + +fdescribe('saleTracking updateTracking()', () => { + const saleFk = 1; + const originalQuantity = 10; + const code = 'PREPARED'; + const isChecked = true; + const buyFk = 1; + const isScanned = false; + + beforeAll(async() => { + ctx = { + req: { + accessToken: {userId: 104}, + headers: {origin: 'http://localhost'}, + } + }; + }); + + it('should add a new saleTracking and saleBuy', async() => { + const tx = await models.SaleTracking.beginTransaction({}); + const options = {transaction: tx}; + + try { + const saleTrackingBefore = await models.SaleTracking.find(null, options); + const saleBuyBefore = await models.SaleBuy.find(null, options); + await models.SaleTracking.updateTracking( + ctx, + saleFk, + originalQuantity, + code, + isChecked, + buyFk, + isScanned, + options + ); + + const saleTrackingAfter = await models.SaleTracking.find(null, options); + const saleBuyAfter = await models.SaleBuy.find(null, options); + + expect(saleTrackingAfter.length).toEqual(saleTrackingBefore.length + 1); + expect(saleBuyAfter.length).toEqual(saleBuyBefore.length + 1); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should only update a saleTracking', async() => { + const tx = await models.SaleTracking.beginTransaction({}); + const options = {transaction: tx}; + const saleFk = 2; + + try { + const saleTrackingBefore = await models.SaleTracking.find(null, options); + await models.SaleTracking.updateTracking( + ctx, + saleFk, + originalQuantity, + code, + isChecked, + buyFk, + isScanned, + options + ); + const saleTrackingAfter = await models.SaleTracking.find(null, options); + + expect(saleTrackingAfter.length).toEqual(saleTrackingBefore.length + 1); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/ticket/back/methods/sale-tracking/updateTracking.js b/modules/ticket/back/methods/sale-tracking/updateTracking.js index 453b0b0c6..7cb2b4a58 100644 --- a/modules/ticket/back/methods/sale-tracking/updateTracking.js +++ b/modules/ticket/back/methods/sale-tracking/updateTracking.js @@ -39,7 +39,7 @@ module.exports = Self => { } }); - Self.updateTracking = async(ctx, saleFk, originalQuantity, code, isChecked, buyFk, isScanned, options) => { + Self.updateTracking = async(ctx, saleFk, originalQuantity, code, isChecked, buyFk, isScanned = null, options) => { const userId = ctx.req.accessToken.userId; const models = Self.app.models; const myOptions = {}; @@ -58,24 +58,31 @@ module.exports = Self => { where: {code}, }, myOptions); - const attributes = { + const uniqueAttributes = { saleFk, + workerFk: userId, + stateFk: state?.id, + }; + const attributes = { isChecked, originalQuantity, - workerFk: userId, - stateFk: state.id, - isScanned: isScanned === undefined ? null : isScanned, + isScanned }; const saleTracking = await models.SaleTracking.findOne({ - where: attributes, + where: uniqueAttributes, }, myOptions); - if (!saleTracking) - await models.SaleTracking.create(attributes, myOptions); - - else - await saleTracking.updateAttributes(attributes, myOptions); + if (!saleTracking) { + await models.SaleTracking.create({ + ...uniqueAttributes, + ...attributes + }, myOptions); + } else { + await saleTracking.updateAttributes({ + ...attributes + }, myOptions); + } let isBuy; if (buyFk) { diff --git a/modules/ticket/back/models/sale-buy.json b/modules/ticket/back/models/sale-buy.json index 5279e6787..60ff2bef4 100644 --- a/modules/ticket/back/models/sale-buy.json +++ b/modules/ticket/back/models/sale-buy.json @@ -17,9 +17,6 @@ }, "created": { "type": "date" - }, - "isChecked": { - "type": "number" } }, "relations": { @@ -27,6 +24,11 @@ "type": "belongsTo", "model": "Sale", "foreignKey": "saleFk" + }, + "worker": { + "type": "belongsTo", + "model": "Worker", + "foreignKey": "workerFk" } } }