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) {
|
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) => {
|
try {
|
||||||
if (error) {
|
let account = await Account.create(user, {transaction});
|
||||||
transaction.rollback();
|
let client = {
|
||||||
return callback(error);
|
id: account.id,
|
||||||
}
|
name: data.name,
|
||||||
|
fi: data.fi,
|
||||||
let client = {
|
socialName: data.socialName,
|
||||||
name: data.name,
|
email: data.email,
|
||||||
fi: data.fi,
|
salesPersonFk: data.salesPersonFk
|
||||||
socialName: data.socialName,
|
};
|
||||||
id: account.id,
|
newClient = await Self.create(client, {transaction});
|
||||||
email: data.email,
|
await transaction.commit();
|
||||||
salesPersonFk: data.salesPersonFk
|
return newClient;
|
||||||
};
|
} catch (e) {
|
||||||
|
transaction.rollback();
|
||||||
Self.create(client, {transaction}, (error, newClient) => {
|
throw e;
|
||||||
if (error) {
|
}
|
||||||
transaction.rollback();
|
|
||||||
return callback(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
transaction.commit();
|
|
||||||
callback(null, newClient);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -48,45 +48,39 @@ 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 = {
|
|
||||||
name: client.name,
|
|
||||||
userName: account.name,
|
|
||||||
email: client.email,
|
|
||||||
fi: client.fi,
|
|
||||||
socialName: client.socialName
|
|
||||||
};
|
|
||||||
|
|
||||||
app.models.Client.createWithUser(formerAccountData, (err, client) => {
|
let formerAccountData = {
|
||||||
expect(err.details.codes.name[0]).toEqual('uniqueness');
|
name: client.name,
|
||||||
done();
|
userName: account.name,
|
||||||
});
|
email: client.email,
|
||||||
});
|
fi: client.fi,
|
||||||
})
|
socialName: client.socialName
|
||||||
.catch(catchErrors(done));
|
};
|
||||||
|
|
||||||
|
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 => {
|
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}})
|
client = await app.models.Client.findOne({where: {name: newAccountData.name}});
|
||||||
.then(client => {
|
|
||||||
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));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue