Client.createWithUser refactored to async/await

This commit is contained in:
Juan 2018-03-12 13:09:52 +01:00
parent b952ab502a
commit 3dbd7e6bc0
2 changed files with 50 additions and 68 deletions

View File

@ -1,5 +1,3 @@
let md5 = require('md5');
module.exports = function(Self) { module.exports = function(Self) {
Self.remoteMethod('createWithUser', { Self.remoteMethod('createWithUser', {
description: 'Creates both client and its web account', 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 firstEmail = data.email ? data.email.split(',')[0] : null;
let user = { let user = {
name: data.userName, name: data.userName,
@ -27,34 +25,24 @@ module.exports = function(Self) {
}; };
let Account = Self.app.models.Account; let Account = Self.app.models.Account;
Account.beginTransaction({}, (error, transaction) => { let transaction = await Account.beginTransaction({});
if (error) return callback(error);
Account.create(user, {transaction}, (error, account) => {
if (error) {
transaction.rollback();
return callback(error);
}
try {
let account = await Account.create(user, {transaction});
let client = { let client = {
id: account.id,
name: data.name, name: data.name,
fi: data.fi, fi: data.fi,
socialName: data.socialName, socialName: data.socialName,
id: account.id,
email: data.email, email: data.email,
salesPersonFk: data.salesPersonFk salesPersonFk: data.salesPersonFk
}; };
newClient = await Self.create(client, {transaction});
Self.create(client, {transaction}, (error, newClient) => { await transaction.commit();
if (error) { return newClient;
} catch (e) {
transaction.rollback(); transaction.rollback();
return callback(error); throw e;
} }
transaction.commit();
callback(null, newClient);
});
});
});
}; };
}; };

View File

@ -48,11 +48,10 @@ describe('Client Create', () => {
.catch(catchErrors(done)); .catch(catchErrors(done));
}); });
it('should not be able to create a user if exists', done => { it('should not be able to create a user if exists', async() => {
app.models.Client.findOne({where: {name: 'Charles Xavier'}}) let client = await app.models.Client.findOne({where: {name: 'Charles Xavier'}});
.then(client => { let account = await app.models.Account.findOne({where: {id: client.id}});
app.models.Account.findOne({where: {id: client.id}})
.then(account => {
let formerAccountData = { let formerAccountData = {
name: client.name, name: client.name,
userName: account.name, userName: account.name,
@ -61,32 +60,27 @@ describe('Client Create', () => {
socialName: client.socialName socialName: client.socialName
}; };
app.models.Client.createWithUser(formerAccountData, (err, client) => { try {
let client = await app.models.Client.createWithUser(formerAccountData);
expect(client).toBeNull();
} catch (err) {
expect(err.details.codes.name[0]).toEqual('uniqueness'); expect(err.details.codes.name[0]).toEqual('uniqueness');
done(); }
});
});
})
.catch(catchErrors(done));
}); });
it('should create a new account', done => { it('should create a new account', async() => {
app.models.Client.createWithUser(newAccountData, (error, client) => { let client = await app.models.Client.createWithUser(newAccountData);
if (error) return catchErrors(done)(error); let account = await app.models.Account.findOne({where: {name: newAccountData.userName}});
app.models.Account.findOne({where: {name: newAccountData.userName}})
.then(account => {
expect(account.name).toEqual(newAccountData.userName); expect(account.name).toEqual(newAccountData.userName);
app.models.Client.findOne({where: {name: newAccountData.name}})
.then(client => { client = await app.models.Client.findOne({where: {name: newAccountData.name}});
expect(client.id).toEqual(account.id); expect(client.id).toEqual(account.id);
expect(client.name).toEqual(newAccountData.name); expect(client.name).toEqual(newAccountData.name);
expect(client.email).toEqual(newAccountData.email); expect(client.email).toEqual(newAccountData.email);
expect(client.fi).toEqual(newAccountData.fi); expect(client.fi).toEqual(newAccountData.fi);
expect(client.socialName).toEqual(newAccountData.socialName); expect(client.socialName).toEqual(newAccountData.socialName);
done();
});
})
.catch(catchErrors(done));
});
}); });
}); });