Merge branch 'dev' into 5241-virtual_pos
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Joan Sanchez 2023-03-21 09:10:34 +00:00
commit 3a922b453f
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`) INSERT INTO `vn`.`payDem`(`id`, `payDem`)
VALUES VALUES
(1, 10), (1, 10),
(2, 20); (2, 20),
(7, 0);
INSERT INTO `vn`.`autonomy`(`id`, `name`, `countryFk`) INSERT INTO `vn`.`autonomy`(`id`, `name`, `countryFk`)
VALUES VALUES

View File

@ -1,11 +1,10 @@
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('newSupplier', { Self.remoteMethodCtx('newSupplier', {
description: 'Creates a new supplier and returns it', description: 'Creates a new supplier and returns it',
accessType: 'WRITE', accessType: 'WRITE',
accepts: [{ accepts: [{
arg: 'params', arg: 'name',
type: 'object', type: 'string'
http: {source: 'body'}
}], }],
returns: { returns: {
type: 'string', type: 'string',
@ -17,29 +16,18 @@ module.exports = Self => {
} }
}); });
Self.newSupplier = async params => { Self.newSupplier = async(ctx, options) => {
const models = Self.app.models; const models = Self.app.models;
const args = ctx.args;
const myOptions = {}; const myOptions = {};
if (typeof(params) == 'string') if (typeof options == 'object')
params = JSON.parse(params); 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) { return supplier;
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;
}
}; };
}; };

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'); const LoopBackContext = require('loopback-context');
describe('Supplier newSupplier()', () => { describe('Supplier newSupplier()', () => {
const newSupp = {
name: 'TestSupplier-1'
};
const administrativeId = 5; 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() => { beforeEach(() => {
pending('https://redmine.verdnatura.es/issues/5203');
const activeCtx = {
accessToken: {userId: administrativeId},
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx 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'); try {
expect(result.id).toEqual(443); 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(result.name).toEqual('newSupplier');
expect(createdSupplier.name).toEqual(result.name); } catch (e) {
expect(createdSupplier.payDemFk).toEqual(7); await tx.rollback();
expect(createdSupplier.nickname).toEqual(result.name); throw e;
}
}); });
}); });

View File

@ -1,7 +1,8 @@
<vn-watcher <vn-watcher
vn-id="watcher" vn-id="watcher"
url="suppliers/newSupplier" url="Suppliers/newSupplier"
data="$ctrl.supplier" data="$ctrl.supplier"
params="$ctrl.supplier"
insert-mode="true" insert-mode="true"
form="form"> form="form">
</vn-watcher> </vn-watcher>
@ -9,8 +10,8 @@
<vn-card class="vn-pa-lg"> <vn-card class="vn-pa-lg">
<vn-horizontal> <vn-horizontal>
<vn-textfield <vn-textfield
label="Supplier name" label="Supplier name"
ng-model="$ctrl.supplier.name" ng-model="$ctrl.supplier.name"
vn-focus> vn-focus>
</vn-textfield> </vn-textfield>
</vn-horizontal> </vn-horizontal>