feat: refs #6276 test saleTrackingReplace (updateTracking)
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2024-01-22 07:49:21 +01:00
parent 6ee7ca79b7
commit 89d18e29f0
4 changed files with 109 additions and 18 deletions

View File

@ -3697,15 +3697,19 @@ UPDATE vn.collection
UPDATE vn.sale UPDATE vn.sale
SET isPicked =FALSE; SET isPicked =FALSE;
INSERT INTO machineWorkerConfig(maxHours) INSERT INTO vn.machineWorkerConfig(maxHours)
VALUES(12); 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`) INSERT INTO `vn`.`machine` (`plate`, `maker`, `model`, `warehouseFk`, `departmentFk`, `type`, `use`, `productionYear`, `workerFk`, `companyFk`)
VALUES VALUES
('RE-003', 'IRON', 'JPH-24', 60, 23, 'ELECTRIC TOW', 'Drag cars', 2020, 103, 442); ('RE-003', 'IRON', 'JPH-24', 60, 23, 'ELECTRIC TOW', 'Drag cars', 2020, 103, 442);
INSERT INTO machineWorker(workerFk,machineFk,inTimed) INSERT INTO vn.machineWorker(workerFk,machineFk,inTimed)
VALUES (104,1,'2001-01-01 10:00:00.00.000'); VALUES (104,1,'2001-01-01 10:00:00.00.000');
UPDATE vn.buy
SET itemOriginalFk = 1
WHERE id = 1;

View File

@ -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;
}
});
});

View File

@ -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 userId = ctx.req.accessToken.userId;
const models = Self.app.models; const models = Self.app.models;
const myOptions = {}; const myOptions = {};
@ -58,24 +58,31 @@ module.exports = Self => {
where: {code}, where: {code},
}, myOptions); }, myOptions);
const attributes = { const uniqueAttributes = {
saleFk, saleFk,
workerFk: userId,
stateFk: state?.id,
};
const attributes = {
isChecked, isChecked,
originalQuantity, originalQuantity,
workerFk: userId, isScanned
stateFk: state.id,
isScanned: isScanned === undefined ? null : isScanned,
}; };
const saleTracking = await models.SaleTracking.findOne({ const saleTracking = await models.SaleTracking.findOne({
where: attributes, where: uniqueAttributes,
}, myOptions); }, myOptions);
if (!saleTracking) if (!saleTracking) {
await models.SaleTracking.create(attributes, myOptions); await models.SaleTracking.create({
...uniqueAttributes,
else ...attributes
await saleTracking.updateAttributes(attributes, myOptions); }, myOptions);
} else {
await saleTracking.updateAttributes({
...attributes
}, myOptions);
}
let isBuy; let isBuy;
if (buyFk) { if (buyFk) {

View File

@ -17,9 +17,6 @@
}, },
"created": { "created": {
"type": "date" "type": "date"
},
"isChecked": {
"type": "number"
} }
}, },
"relations": { "relations": {
@ -27,6 +24,11 @@
"type": "belongsTo", "type": "belongsTo",
"model": "Sale", "model": "Sale",
"foreignKey": "saleFk" "foreignKey": "saleFk"
},
"worker": {
"type": "belongsTo",
"model": "Worker",
"foreignKey": "workerFk"
} }
} }
} }