Merge pull request 'refactor: refs #7764 add role assignment for supplier during user creation' (!3481) from 7764-createClienteAsSupplier into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #3481
Reviewed-by: Javier Segarra <jsegarra@verdnatura.es>
This commit is contained in:
Javi Gallego 2025-02-21 13:00:59 +00:00
commit 4dcd3f0d76
2 changed files with 45 additions and 39 deletions

View File

@ -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},

View File

@ -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'}}, options);
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'}}, options);
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() => {