refactor: refs #7764 add role assignment for supplier during user creation #3481
|
@ -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},
|
||||
|
|
|
@ -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);
|
||||
jsegarra
commented
options? 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() => {
|
||||
|
|
Loading…
Reference in New Issue
options?