55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
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'
|
|
}
|
|
});
|
|
|
|
Self.createWithUser = async data => {
|
|
let firstEmail = data.email ? data.email.split(',')[0] : null;
|
|
let user = {
|
|
name: data.userName,
|
|
email: firstEmail,
|
|
password: parseInt(Math.random() * 100000000000000)
|
|
};
|
|
let Account = Self.app.models.Account;
|
|
|
|
let transaction = await Account.beginTransaction({});
|
|
|
|
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,
|
|
salesPersonFk: data.salesPersonFk,
|
|
postcode: data.postcode,
|
|
street: data.street,
|
|
city: data.city,
|
|
provinceFk: data.provinceFk,
|
|
countryFk: data.countryFk,
|
|
isEqualizated: data.isEqualizated
|
|
};
|
|
newClient = await Self.create(client, {transaction});
|
|
await transaction.commit();
|
|
return newClient;
|
|
} catch (e) {
|
|
await transaction.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|