Client.createWithUser refactored to async/await
This commit is contained in:
parent
b952ab502a
commit
3dbd7e6bc0
|
@ -1,5 +1,3 @@
|
|||
let md5 = require('md5');
|
||||
|
||||
module.exports = function(Self) {
|
||||
Self.remoteMethod('createWithUser', {
|
||||
description: 'Creates both client and its web account',
|
||||
|
@ -18,7 +16,7 @@ module.exports = function(Self) {
|
|||
}
|
||||
});
|
||||
|
||||
Self.createWithUser = (data, callback) => {
|
||||
Self.createWithUser = async data => {
|
||||
let firstEmail = data.email ? data.email.split(',')[0] : null;
|
||||
let user = {
|
||||
name: data.userName,
|
||||
|
@ -27,34 +25,24 @@ module.exports = function(Self) {
|
|||
};
|
||||
let Account = Self.app.models.Account;
|
||||
|
||||
Account.beginTransaction({}, (error, transaction) => {
|
||||
if (error) return callback(error);
|
||||
let transaction = await Account.beginTransaction({});
|
||||
|
||||
Account.create(user, {transaction}, (error, account) => {
|
||||
if (error) {
|
||||
transaction.rollback();
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
let client = {
|
||||
name: data.name,
|
||||
fi: data.fi,
|
||||
socialName: data.socialName,
|
||||
id: account.id,
|
||||
email: data.email,
|
||||
salesPersonFk: data.salesPersonFk
|
||||
};
|
||||
|
||||
Self.create(client, {transaction}, (error, newClient) => {
|
||||
if (error) {
|
||||
transaction.rollback();
|
||||
return callback(error);
|
||||
}
|
||||
|
||||
transaction.commit();
|
||||
callback(null, newClient);
|
||||
});
|
||||
});
|
||||
});
|
||||
try {
|
||||
let account = await Account.create(user, {transaction});
|
||||
let client = {
|
||||
id: account.id,
|
||||
name: data.name,
|
||||
fi: data.fi,
|
||||
socialName: data.socialName,
|
||||
email: data.email,
|
||||
salesPersonFk: data.salesPersonFk
|
||||
};
|
||||
newClient = await Self.create(client, {transaction});
|
||||
await transaction.commit();
|
||||
return newClient;
|
||||
} catch (e) {
|
||||
transaction.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -48,45 +48,39 @@ describe('Client Create', () => {
|
|||
.catch(catchErrors(done));
|
||||
});
|
||||
|
||||
it('should not be able to create a user if exists', done => {
|
||||
app.models.Client.findOne({where: {name: 'Charles Xavier'}})
|
||||
.then(client => {
|
||||
app.models.Account.findOne({where: {id: client.id}})
|
||||
.then(account => {
|
||||
let formerAccountData = {
|
||||
name: client.name,
|
||||
userName: account.name,
|
||||
email: client.email,
|
||||
fi: client.fi,
|
||||
socialName: client.socialName
|
||||
};
|
||||
it('should not be able to create a user if exists', async() => {
|
||||
let client = await app.models.Client.findOne({where: {name: 'Charles Xavier'}});
|
||||
let account = await app.models.Account.findOne({where: {id: client.id}});
|
||||
|
||||
app.models.Client.createWithUser(formerAccountData, (err, client) => {
|
||||
expect(err.details.codes.name[0]).toEqual('uniqueness');
|
||||
done();
|
||||
});
|
||||
});
|
||||
})
|
||||
.catch(catchErrors(done));
|
||||
let formerAccountData = {
|
||||
name: client.name,
|
||||
userName: account.name,
|
||||
email: client.email,
|
||||
fi: client.fi,
|
||||
socialName: client.socialName
|
||||
};
|
||||
|
||||
try {
|
||||
let client = await app.models.Client.createWithUser(formerAccountData);
|
||||
|
||||
expect(client).toBeNull();
|
||||
} catch (err) {
|
||||
expect(err.details.codes.name[0]).toEqual('uniqueness');
|
||||
}
|
||||
});
|
||||
|
||||
it('should create a new account', done => {
|
||||
app.models.Client.createWithUser(newAccountData, (error, client) => {
|
||||
if (error) return catchErrors(done)(error);
|
||||
app.models.Account.findOne({where: {name: newAccountData.userName}})
|
||||
.then(account => {
|
||||
expect(account.name).toEqual(newAccountData.userName);
|
||||
app.models.Client.findOne({where: {name: newAccountData.name}})
|
||||
.then(client => {
|
||||
expect(client.id).toEqual(account.id);
|
||||
expect(client.name).toEqual(newAccountData.name);
|
||||
expect(client.email).toEqual(newAccountData.email);
|
||||
expect(client.fi).toEqual(newAccountData.fi);
|
||||
expect(client.socialName).toEqual(newAccountData.socialName);
|
||||
done();
|
||||
});
|
||||
})
|
||||
.catch(catchErrors(done));
|
||||
});
|
||||
it('should create a new account', async() => {
|
||||
let client = await app.models.Client.createWithUser(newAccountData);
|
||||
let account = await app.models.Account.findOne({where: {name: newAccountData.userName}});
|
||||
|
||||
expect(account.name).toEqual(newAccountData.userName);
|
||||
|
||||
client = await app.models.Client.findOne({where: {name: newAccountData.name}});
|
||||
|
||||
expect(client.id).toEqual(account.id);
|
||||
expect(client.name).toEqual(newAccountData.name);
|
||||
expect(client.email).toEqual(newAccountData.email);
|
||||
expect(client.fi).toEqual(newAccountData.fi);
|
||||
expect(client.socialName).toEqual(newAccountData.socialName);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue