diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index c4df3e84f..2145f8429 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -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 diff --git a/modules/supplier/back/methods/supplier/newSupplier.js b/modules/supplier/back/methods/supplier/newSupplier.js index ae71380f3..c40e7214f 100644 --- a/modules/supplier/back/methods/supplier/newSupplier.js +++ b/modules/supplier/back/methods/supplier/newSupplier.js @@ -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; }; }; diff --git a/modules/supplier/back/methods/supplier/specs/newSupplier.spec.js b/modules/supplier/back/methods/supplier/specs/newSupplier.spec.js index 44252e71b..d4479d00b 100644 --- a/modules/supplier/back/methods/supplier/specs/newSupplier.spec.js +++ b/modules/supplier/back/methods/supplier/specs/newSupplier.spec.js @@ -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; + } }); }); diff --git a/modules/supplier/front/create/index.html b/modules/supplier/front/create/index.html index 446a16cb6..c3efcf6ae 100644 --- a/modules/supplier/front/create/index.html +++ b/modules/supplier/front/create/index.html @@ -1,7 +1,8 @@ @@ -9,8 +10,8 @@