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

This commit is contained in:
Jorge Penadés 2024-01-19 14:35:10 +01:00
parent b7ce6cac9c
commit 817a6b1289
4 changed files with 51 additions and 6 deletions

View File

@ -208,6 +208,6 @@
"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",
"You are already using a machine": "You are already using a machine"
"You are already using a machine": "You are already using a machine",
"This worker does not exist": "This worker does not exist"
}

View File

@ -345,5 +345,6 @@
"This pallet does not exist": "Este palet no existe",
"We do not have availability for the selected item": "No tenemos disponible el item seleccionado",
"You are already using a machine": "Ya estás usando una máquina.",
"Ya estás usando una máquina.": "Ya estás usando una máquina."
"This worker does not exist": "Este trabajador no existe",
"Este trabajador no existe": "Este trabajador no existe"
}

View File

@ -1,3 +1,4 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('add', {
description: 'Add a new operator',
@ -8,13 +9,28 @@ module.exports = Self => {
}
});
Self.add = async ctx => {
Self.add = async(ctx, options) => {
const userId = ctx.req.accessToken.userId;
const user = await Self.findById(userId);
if (!user) {
const myOptions = {};
const $t = ctx.req.__;
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
await Self.create({
workerFk: userId
});
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw new UserError($t('This worker does not exist'));
}
};
};

View File

@ -0,0 +1,28 @@
const {models} = require('vn-loopback/server/server');
describe('operator add()', () => {
beforeAll(async() => {
ctx = {
req: {
accessToken: {userId: 100000},
headers: {origin: 'http://localhost'},
__: value => value
}
};
});
it('should throw an error if the worker does not exist', async() => {
const tx = await models.Operator.beginTransaction({});
const options = {transaction: tx};
try {
await models.Operator.add(ctx, options);
await tx.rollback();
} catch (e) {
const error = e;
expect(error.message).toEqual('This worker does not exist');
await tx.rollback();
}
});
});