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 = { let user = {
name: data.userName, name: data.userName,
email: data.email, email: data.email,
password: parseInt(Math.random() * 100000000000000) password: parseInt(Math.random() * 100000000000000)
}; };
app.models.Account.create (user, (err, obj) => {
if (err) { app.models.Account.beginTransaction('READ COMMITTED', (err, transaction) => {
cb(err); app.models.Account.create (user, {transaction}, (err, account) => {
} else { if (err) {
return callback(err);
}
let client = { let client = {
name: data.name, name: data.name,
fi: data.fi, fi: data.fi,
socialName: data.socialName, socialName: data.socialName,
id: obj.id id: account.id
}; };
Client.create (client, (err, obj) => {
if (err) Client.create (client, (err, client) => {
cb(err); if (err) {
else transaction.rollback();
cb(null, true); return callback(err);
}
transaction.commit();
callback(null, true);
}); });
}; });
}); });
}; };
}; };