transaction for account and client creation at create.js

This commit is contained in:
Carlos 2017-09-05 23:01:51 +02:00
parent e24a36aa08
commit 9607b2d897
1 changed files with 20 additions and 13 deletions

View File

@ -18,29 +18,36 @@ module.exports = function(Client){
}
});
Client.createNew = (data, cb) => {
Client.createNew = (data, callback) => {
let user = {
name: data.userName,
email: data.email,
password: parseInt(Math.random() * 100000000000000)
};
app.models.Account.create (user, (err, obj) => {
if (err) {
cb(err);
} else {
app.models.Account.beginTransaction('READ COMMITTED', (err, transaction) => {
app.models.Account.create (user, {transaction}, (err, account) => {
if (err) {
return callback(err);
}
let client = {
name: data.name,
fi: data.fi,
socialName: data.socialName,
id: obj.id
id: account.id
};
Client.create (client, (err, obj) => {
if (err)
cb(err);
else
cb(null, true);
Client.create (client, (err, client) => {
if (err) {
transaction.rollback();
return callback(err);
}
transaction.commit();
callback(null, true);
});
};
});
});
};
};
};