feat: refs #6276 test machineWorker_update
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2024-01-19 09:52:49 +01:00
parent 34420375cd
commit 5c0425b942
8 changed files with 136 additions and 7 deletions

View File

@ -0,0 +1,116 @@
const {models} = require('vn-loopback/server/server');
fdescribe('machineWorker updateInTime()', () => {
const itBoss = 104;
const davidCharles = 1106;
beforeAll(async() => {
ctx = {
req: {
accessToken: {},
headers: {origin: 'http://localhost'},
__: value => value
}
};
});
it('should throw an error if the plate does not exist', async() => {
const tx = await models.MachineWorker.beginTransaction({});
const options = {transaction: tx};
const plate = 'RE-123';
ctx.req.accessToken.userId = 1106;
try {
await models.MachineWorker.updateInTime(ctx, plate, options);
await tx.rollback();
} catch (e) {
const error = e;
expect(error.message).toContain('the plate does not exist');
await tx.rollback();
}
});
it('should grab a machine where is not in use', async() => {
const tx = await models.MachineWorker.beginTransaction({});
const options = {transaction: tx};
const plate = 'RE-003';
ctx.req.accessToken.userId = 1107;
try {
const totalBefore = await models.MachineWorker.find(null, options);
await models.MachineWorker.updateInTime(ctx, plate, options);
const totalAfter = await models.MachineWorker.find(null, options);
expect(totalAfter.length).toEqual(totalBefore.length + 1);
await tx.rollback();
} catch (e) {
await tx.rollback();
}
});
describe('less than 12h', () => {
const plate = 'RE-001';
it('should trow an error if it is not himself', async() => {
const tx = await models.MachineWorker.beginTransaction({});
const options = {transaction: tx};
ctx.req.accessToken.userId = davidCharles;
try {
await models.MachineWorker.updateInTime(ctx, plate, options);
await tx.rollback();
} catch (e) {
const error = e;
expect(error.message).toContain('This machine is already in use');
await tx.rollback();
}
});
it('should set the out time if it is himself', async() => {
const tx = await models.MachineWorker.beginTransaction({});
const options = {transaction: tx};
ctx.req.accessToken.userId = itBoss;
try {
const isNotParked = await models.MachineWorker.findOne({
where: {workerFk: itBoss}
}, options);
await models.MachineWorker.updateInTime(ctx, plate, options);
const isParked = await models.MachineWorker.findOne({
where: {workerFk: itBoss}
}, options);
expect(isNotParked.outTime).toBeNull();
expect(isParked.outTime).toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
}
});
});
describe('equal or more than 12h', () => {
const plate = 'RE-002';
it('should set the out time and grab the machine', async() => {
const tx = await models.MachineWorker.beginTransaction({});
const options = {transaction: tx};
ctx.req.accessToken.userId = davidCharles;
const filter = {
where: {workerFk: davidCharles, machineFk: 2}
};
try {
const isNotParked = await models.MachineWorker.findOne(filter, options);
const totalBefore = await models.MachineWorker.find(null, options);
await models.MachineWorker.updateInTime(ctx, plate, options);
const isParked = await models.MachineWorker.findOne(filter, options);
const totalAfter = await models.MachineWorker.find(null, options);
expect(isNotParked.outTime).toBeNull();
expect(isParked.outTime).toBeDefined();
expect(totalAfter.length).toEqual(totalBefore.length + 1);
await tx.rollback();
} catch (e) {
await tx.rollback();
}
});
});
});

View File

@ -18,6 +18,7 @@ module.exports = Self => {
Self.updateInTime = async(ctx, plate, options) => {
const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
const $t = ctx.req.__;
let tx;
const myOptions = {};
@ -35,11 +36,13 @@ module.exports = Self => {
fields: ['id', 'plate'],
where: {plate}
}, myOptions);
if (!machine) throw new Error(`plate ${plate} does not exist.`);
if (!machine)
throw new Error($t('the plate does not exist', {plate}));
const machineWorker = await Self.findOne({
where: {
machineFk: machine.id,
or: [{machineFk: machine.id}, {workerFk: userId}],
outTime: null,
}
}, myOptions);
@ -49,7 +52,8 @@ module.exports = Self => {
const hoursDifference = (Date.vnNow() - machineWorker.inTime.getTime()) / (60 * 60 * 1000);
const isHimSelf = userId == machineWorker.workerFk;
if (maxHours > hoursDifference && !isHimSelf) throw new UserError('This machine is already in use.');
if (maxHours > hoursDifference && !isHimSelf)
throw new UserError($t('This machine is already in use.'));
await machineWorker.updateAttributes({
outTime: Date.vnNew()

View File

@ -3700,4 +3700,12 @@ UPDATE vn.sale
INSERT INTO machineWorkerConfig(maxHours)
VALUES(12);
INSERT INTO workerAppTester(workerFk) VALUES(66)
INSERT INTO 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');

View File

@ -205,6 +205,7 @@
"It was not able to remove the next expeditions:": "It was not able to remove the next expeditions: {{expeditions}}",
"Incorrect pin": "Incorrect pin.",
"This machine is already in use.": "This machine is already in use.",
"the plate does not exist": "The plate {{plate}} does not exist",
"This pallet does not exist": "This pallet does not exist",
"We do not have availability for the selected item": "We do not have availability for the selected item"
}

View File

@ -341,6 +341,8 @@
"No hay tickets para sacar": "No hay tickets para sacar",
"There is no zone for these parameters 999999": "There is no zone for these parameters 999999",
"This machine is already in use.": "Esta máquina ya está en uso.",
"the plate does not exist": "La máquina {{plate}} no existe",
"This pallet does not exist": "Este palet no existe",
"We do not have availability for the selected item": "No tenemos disponible el item seleccionado",
"Esta máquina ya está en uso.": "Esta máquina ya está en uso."
}

View File

@ -3,7 +3,6 @@ const {models} = require('vn-loopback/server/server');
describe('itemShelving return()', () => {
beforeAll(async() => {
ctx = {
accessToken: {userId: 9},
req: {
headers: {origin: 'http://localhost'},
}

View File

@ -1,6 +1,6 @@
module.exports = Self => {
Self.remoteMethod('card', {
description: 'Idk',
description: 'Get the data from an item',
accessType: 'READ',
http: {
path: `/card`,

View File

@ -3,7 +3,6 @@ const {models} = require('vn-loopback/server/server');
describe('expeditonPallet getPallet()', () => {
beforeAll(async() => {
ctx = {
accessToken: {userId: 9},
req: {
headers: {origin: 'http://localhost'},
__: value => value