Merge pull request 'refs #5203 refactor: se le pasan los argumentos directamente y se corrige la transaccion' (!1398) from 5203-supplier.create_refactor into dev
gitea/salix/pipeline/head There was a failure building this commit Details

Reviewed-on: #1398
Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
Joan Sanchez 2023-03-21 09:07:07 +00:00
commit 5e61cbabc7
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;
delete args.ctx;
const data = {...args, ...{nickname: args.name}};
const supplier = await models.Supplier.create(data, myOptions);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const supplier = await models.Supplier.create(params, myOptions);
if (tx) await tx.commit();
return supplier;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
return supplier;
};
};

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;
const activeCtx = {
accessToken: {userId: administrativeId},
http: {
req: {
headers: {origin: 'http://localhost'}
}
}
};
const ctx = {req: activeCtx};
it('should create a new supplier containing only the name', async() => {
pending('https://redmine.verdnatura.es/issues/5203');
const activeCtx = {
accessToken: {userId: administrativeId},
};
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>
@ -9,8 +10,8 @@
<vn-card class="vn-pa-lg">
<vn-horizontal>
<vn-textfield
label="Supplier name"
ng-model="$ctrl.supplier.name"
label="Supplier name"
ng-model="$ctrl.supplier.name"
vn-focus>
</vn-textfield>
</vn-horizontal>