From 6f5a966c550463365479625ef1037bda78ce365f Mon Sep 17 00:00:00 2001 From: jgallego Date: Fri, 21 Feb 2025 10:25:41 +0100 Subject: [PATCH 1/2] refactor: refs #7764 add role assignment for supplier during user creation --- .../back/methods/client/createWithUser.js | 11 +++ .../client/specs/createWithUser.spec.js | 73 +++++++++---------- 2 files changed, 45 insertions(+), 39 deletions(-) diff --git a/modules/client/back/methods/client/createWithUser.js b/modules/client/back/methods/client/createWithUser.js index 1d5e71fcae..ad20171dc9 100644 --- a/modules/client/back/methods/client/createWithUser.js +++ b/modules/client/back/methods/client/createWithUser.js @@ -43,6 +43,17 @@ module.exports = function(Self) { password: String(Math.random() * 100000000000000) }; + const supplier = await models.Supplier.findOne({ + where: {nif: data.fi} + }); + + const role = supplier ? await models.VnRole.findOne({ + where: {name: 'supplier'} + }) : null; + + if (role) + user.roleFk = role.id; + try { const province = await models.Province.findOne({ where: {id: data.provinceFk}, diff --git a/modules/client/back/methods/client/specs/createWithUser.spec.js b/modules/client/back/methods/client/specs/createWithUser.spec.js index 8cff96ac54..cf68f089ec 100644 --- a/modules/client/back/methods/client/specs/createWithUser.spec.js +++ b/modules/client/back/methods/client/specs/createWithUser.spec.js @@ -48,11 +48,11 @@ describe('Client Create', () => { expect(error.message).toEqual('An email is necessary'); }); - it('should create a new account with dailyInvoice', async() => { + it('should create a new account with dailyInvoice and role supplier', async() => { const newAccount = { userName: 'deadpool', email: 'deadpool@marvel.com', - fi: '16195279J', + fi: '07972486L', name: 'Wade', socialName: 'DEADPOOL MARVEL', street: 'WALL STREET', @@ -61,33 +61,30 @@ describe('Client Create', () => { provinceFk: 1 }; - try { - const province = await models.Province.findById(newAccount.provinceFk, { - fields: ['id', 'name', 'autonomyFk'], - include: { - relation: 'autonomy' - } - }, options); + const province = await models.Province.findById(newAccount.provinceFk, { + fields: ['id', 'name', 'autonomyFk'], + include: { + relation: 'autonomy' + } + }, options); - const client = await models.Client.createWithUser(newAccount, options); - const account = await models.VnUser.findOne({where: {name: newAccount.userName}}, options); + const client = await models.Client.createWithUser(newAccount, options); + const account = await models.VnUser.findOne({where: {name: newAccount.userName}}, options); + const supplierRole = await models.VnRole.findOne({where: {name: 'supplier'}}); - expect(province.autonomy().hasDailyInvoice).toBeTruthy(); - expect(account.name).toEqual(newAccount.userName); - expect(client.id).toEqual(account.id); - expect(client.name).toEqual(newAccount.name); - expect(client.email).toEqual(newAccount.email); - expect(client.fi).toEqual(newAccount.fi); - expect(client.socialName).toEqual(newAccount.socialName); - expect(client.businessTypeFk).toEqual(newAccount.businessTypeFk); - expect(client.hasDailyInvoice).toBeTruthy(); - } catch (e) { - await tx.rollback(); - throw e; - } + expect(account.roleFk).toEqual(supplierRole.id); + expect(province.autonomy().hasDailyInvoice).toBeTruthy(); + expect(account.name).toEqual(newAccount.userName); + expect(client.id).toEqual(account.id); + expect(client.name).toEqual(newAccount.name); + expect(client.email).toEqual(newAccount.email); + expect(client.fi).toEqual(newAccount.fi); + expect(client.socialName).toEqual(newAccount.socialName); + expect(client.businessTypeFk).toEqual(newAccount.businessTypeFk); + expect(client.hasDailyInvoice).toBeTruthy(); }); - it('should create a new account without dailyInvoice', async() => { + it('should create a new account without dailyInvoice and role customer', async() => { const newAccount = { userName: 'deadpool', email: 'deadpool@marvel.com', @@ -100,22 +97,20 @@ describe('Client Create', () => { provinceFk: 3 }; - try { - const province = await models.Province.findById(newAccount.provinceFk, { - fields: ['id', 'name', 'autonomyFk'], - include: { - relation: 'autonomy' - } - }, options); + const province = await models.Province.findById(newAccount.provinceFk, { + fields: ['id', 'name', 'autonomyFk'], + include: { + relation: 'autonomy' + } + }, options); - const client = await models.Client.createWithUser(newAccount, options); + const client = await models.Client.createWithUser(newAccount, options); + const vnUser = await models.VnUser.findOne({where: {name: newAccount.userName}}, options); + const customerRole = await models.VnRole.findOne({where: {name: 'customer'}}); - expect(province.autonomy.hasDailyInvoice).toBeFalsy(); - expect(client.hasDailyInvoice).toBeFalsy(); - } catch (e) { - await tx.rollback(); - throw e; - } + expect(vnUser.roleFk).toEqual(customerRole.id); + expect(province.autonomy.hasDailyInvoice).toBeFalsy(); + expect(client.hasDailyInvoice).toBeFalsy(); }); it('should not be able to create a user if exists', async() => { -- 2.40.1 From a15cf04a4c3d16a08c6e797fd5cd06c43ef482ad Mon Sep 17 00:00:00 2001 From: jgallego Date: Fri, 21 Feb 2025 12:53:08 +0100 Subject: [PATCH 2/2] refactor: refs #7764 include options parameter in role retrieval for user creation tests --- .../client/back/methods/client/specs/createWithUser.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/client/back/methods/client/specs/createWithUser.spec.js b/modules/client/back/methods/client/specs/createWithUser.spec.js index cf68f089ec..fdc0797958 100644 --- a/modules/client/back/methods/client/specs/createWithUser.spec.js +++ b/modules/client/back/methods/client/specs/createWithUser.spec.js @@ -70,7 +70,7 @@ describe('Client Create', () => { const client = await models.Client.createWithUser(newAccount, options); const account = await models.VnUser.findOne({where: {name: newAccount.userName}}, options); - const supplierRole = await models.VnRole.findOne({where: {name: 'supplier'}}); + const supplierRole = await models.VnRole.findOne({where: {name: 'supplier'}}, options); expect(account.roleFk).toEqual(supplierRole.id); expect(province.autonomy().hasDailyInvoice).toBeTruthy(); @@ -106,7 +106,7 @@ describe('Client Create', () => { const client = await models.Client.createWithUser(newAccount, options); const vnUser = await models.VnUser.findOne({where: {name: newAccount.userName}}, options); - const customerRole = await models.VnRole.findOne({where: {name: 'customer'}}); + const customerRole = await models.VnRole.findOne({where: {name: 'customer'}}, options); expect(vnUser.roleFk).toEqual(customerRole.id); expect(province.autonomy.hasDailyInvoice).toBeFalsy(); -- 2.40.1