2018-01-29 11:37:54 +00:00
|
|
|
module.exports = function(Self) {
|
|
|
|
Self.remoteMethod('createWithUser', {
|
|
|
|
description: 'Creates both client and its web account',
|
|
|
|
accepts: {
|
|
|
|
arg: 'data',
|
|
|
|
type: 'object',
|
|
|
|
http: {source: 'body'}
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
root: true,
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
verb: 'post',
|
|
|
|
path: '/createWithUser'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-12 12:09:52 +00:00
|
|
|
Self.createWithUser = async data => {
|
2018-01-29 11:37:54 +00:00
|
|
|
let firstEmail = data.email ? data.email.split(',')[0] : null;
|
|
|
|
let user = {
|
|
|
|
name: data.userName,
|
|
|
|
email: firstEmail,
|
2018-03-12 11:07:55 +00:00
|
|
|
password: parseInt(Math.random() * 100000000000000)
|
2018-01-29 11:37:54 +00:00
|
|
|
};
|
|
|
|
let Account = Self.app.models.Account;
|
|
|
|
|
2018-03-12 12:09:52 +00:00
|
|
|
let transaction = await Account.beginTransaction({});
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2018-03-12 12:09:52 +00:00
|
|
|
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,
|
2018-03-22 17:02:48 +00:00
|
|
|
salesPersonFk: data.salesPersonFk,
|
|
|
|
postcode: data.postcode,
|
|
|
|
street: data.street,
|
|
|
|
city: data.city,
|
|
|
|
provinceFk: data.provinceFk,
|
|
|
|
isEqualizated: data.isEqualizated
|
2018-03-12 12:09:52 +00:00
|
|
|
};
|
2018-04-04 17:59:03 +00:00
|
|
|
newClient = await Self.create(client);
|
2018-03-12 12:09:52 +00:00
|
|
|
await transaction.commit();
|
|
|
|
return newClient;
|
|
|
|
} catch (e) {
|
2018-04-04 17:59:03 +00:00
|
|
|
await transaction.rollback();
|
2018-03-12 12:09:52 +00:00
|
|
|
throw e;
|
|
|
|
}
|
2018-01-29 11:37:54 +00:00
|
|
|
};
|
|
|
|
};
|