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)
|
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 {
|
try {
|
||||||
const province = await models.Province.findOne({
|
const province = await models.Province.findOne({
|
||||||
where: {id: data.provinceFk},
|
where: {id: data.provinceFk},
|
||||||
|
|
|
@ -48,11 +48,11 @@ describe('Client Create', () => {
|
||||||
expect(error.message).toEqual('An email is necessary');
|
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 = {
|
const newAccount = {
|
||||||
userName: 'deadpool',
|
userName: 'deadpool',
|
||||||
email: 'deadpool@marvel.com',
|
email: 'deadpool@marvel.com',
|
||||||
fi: '16195279J',
|
fi: '07972486L',
|
||||||
name: 'Wade',
|
name: 'Wade',
|
||||||
socialName: 'DEADPOOL MARVEL',
|
socialName: 'DEADPOOL MARVEL',
|
||||||
street: 'WALL STREET',
|
street: 'WALL STREET',
|
||||||
|
@ -61,7 +61,6 @@ describe('Client Create', () => {
|
||||||
provinceFk: 1
|
provinceFk: 1
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
|
||||||
const province = await models.Province.findById(newAccount.provinceFk, {
|
const province = await models.Province.findById(newAccount.provinceFk, {
|
||||||
fields: ['id', 'name', 'autonomyFk'],
|
fields: ['id', 'name', 'autonomyFk'],
|
||||||
include: {
|
include: {
|
||||||
|
@ -71,7 +70,9 @@ describe('Client Create', () => {
|
||||||
|
|
||||||
const client = await models.Client.createWithUser(newAccount, options);
|
const client = await models.Client.createWithUser(newAccount, options);
|
||||||
const account = await models.VnUser.findOne({where: {name: newAccount.userName}}, options);
|
const account = await models.VnUser.findOne({where: {name: newAccount.userName}}, options);
|
||||||
|
const supplierRole = await models.VnRole.findOne({where: {name: 'supplier'}}, options);
|
||||||
|
|||||||
|
|
||||||
|
expect(account.roleFk).toEqual(supplierRole.id);
|
||||||
expect(province.autonomy().hasDailyInvoice).toBeTruthy();
|
expect(province.autonomy().hasDailyInvoice).toBeTruthy();
|
||||||
expect(account.name).toEqual(newAccount.userName);
|
expect(account.name).toEqual(newAccount.userName);
|
||||||
expect(client.id).toEqual(account.id);
|
expect(client.id).toEqual(account.id);
|
||||||
|
@ -81,13 +82,9 @@ describe('Client Create', () => {
|
||||||
expect(client.socialName).toEqual(newAccount.socialName);
|
expect(client.socialName).toEqual(newAccount.socialName);
|
||||||
expect(client.businessTypeFk).toEqual(newAccount.businessTypeFk);
|
expect(client.businessTypeFk).toEqual(newAccount.businessTypeFk);
|
||||||
expect(client.hasDailyInvoice).toBeTruthy();
|
expect(client.hasDailyInvoice).toBeTruthy();
|
||||||
} catch (e) {
|
|
||||||
await tx.rollback();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a new account without dailyInvoice', async() => {
|
it('should create a new account without dailyInvoice and role customer', async() => {
|
||||||
const newAccount = {
|
const newAccount = {
|
||||||
userName: 'deadpool',
|
userName: 'deadpool',
|
||||||
email: 'deadpool@marvel.com',
|
email: 'deadpool@marvel.com',
|
||||||
|
@ -100,7 +97,6 @@ describe('Client Create', () => {
|
||||||
provinceFk: 3
|
provinceFk: 3
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
|
||||||
const province = await models.Province.findById(newAccount.provinceFk, {
|
const province = await models.Province.findById(newAccount.provinceFk, {
|
||||||
fields: ['id', 'name', 'autonomyFk'],
|
fields: ['id', 'name', 'autonomyFk'],
|
||||||
include: {
|
include: {
|
||||||
|
@ -109,13 +105,12 @@ describe('Client Create', () => {
|
||||||
}, options);
|
}, 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(vnUser.roleFk).toEqual(customerRole.id);
|
||||||
expect(province.autonomy.hasDailyInvoice).toBeFalsy();
|
expect(province.autonomy.hasDailyInvoice).toBeFalsy();
|
||||||
expect(client.hasDailyInvoice).toBeFalsy();
|
expect(client.hasDailyInvoice).toBeFalsy();
|
||||||
} catch (e) {
|
|
||||||
await tx.rollback();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not be able to create a user if exists', async() => {
|
it('should not be able to create a user if exists', async() => {
|
||||||
|
|
Loading…
Reference in New Issue
options?