refs #5203 refactor: se le pasan los argumentos directamente y se corrige la transaccion
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Vicent Llopis 2023-03-21 08:51:30 +01:00
parent 40ccc4f409
commit 46a6a5fc63
4 changed files with 42 additions and 44 deletions

View File

@ -299,7 +299,8 @@ INSERT INTO `vn`.`payMethod`(`id`,`code`, `name`, `graceDays`, `outstandingDebt
INSERT INTO `vn`.`payDem`(`id`, `payDem`)
VALUES
(1, 10),
(2, 20);
(2, 20),
(7, 0);
INSERT INTO `vn`.`autonomy`(`id`, `name`, `countryFk`)
VALUES

View File

@ -1,11 +1,10 @@
module.exports = Self => {
Self.remoteMethod('newSupplier', {
Self.remoteMethodCtx('newSupplier', {
description: 'Creates a new supplier and returns it',
accessType: 'WRITE',
accepts: [{
arg: 'params',
type: 'object',
http: {source: 'body'}
arg: 'name',
type: 'string'
}],
returns: {
type: 'string',
@ -17,29 +16,18 @@ module.exports = Self => {
}
});
Self.newSupplier = async params => {
Self.newSupplier = async(ctx, options) => {
const models = Self.app.models;
const args = ctx.args;
const myOptions = {};
if (typeof(params) == 'string')
params = JSON.parse(params);
if (typeof options == 'object')
Object.assign(myOptions, options);
params.nickname = params.name;
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const supplier = await models.Supplier.create(params, myOptions);
if (tx) await tx.commit();
delete args.ctx;
const data = {...args, ...{nickname: args.name}};
const supplier = await models.Supplier.create(data, myOptions);
return supplier;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

@ -1,31 +1,39 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('Supplier newSupplier()', () => {
const newSupp = {
name: 'TestSupplier-1'
};
const administrativeId = 5;
it('should create a new supplier containing only the name', async() => {
pending('https://redmine.verdnatura.es/issues/5203');
const activeCtx = {
accessToken: {userId: administrativeId},
http: {
req: {
headers: {origin: 'http://localhost'}
}
}
};
const ctx = {req: activeCtx};
beforeEach(() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
let result = await app.models.Supplier.newSupplier(JSON.stringify(newSupp));
it('should create a new supplier containing only the name', async() => {
const tx = await models.Supplier.beginTransaction({});
expect(result.name).toEqual('TestSupplier-1');
expect(result.id).toEqual(443);
try {
const options = {transaction: tx};
ctx.args = {
name: 'newSupplier'
};
const createdSupplier = await app.models.Supplier.findById(result.id);
const result = await models.Supplier.newSupplier(ctx, options);
expect(createdSupplier.id).toEqual(result.id);
expect(createdSupplier.name).toEqual(result.name);
expect(createdSupplier.payDemFk).toEqual(7);
expect(createdSupplier.nickname).toEqual(result.name);
expect(result.name).toEqual('newSupplier');
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -1,7 +1,8 @@
<vn-watcher
vn-id="watcher"
url="suppliers/newSupplier"
url="Suppliers/newSupplier"
data="$ctrl.supplier"
params="$ctrl.supplier"
insert-mode="true"
form="form">
</vn-watcher>