salix/services/loopback/common/methods/client/createWithUser.js

54 lines
1.6 KiB
JavaScript
Raw Normal View History

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,
2018-03-12 11:07:55 +00:00
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,
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-04-04 17:59:03 +00:00
newClient = await Self.create(client);
await transaction.commit();
return newClient;
} catch (e) {
2018-04-04 17:59:03 +00:00
await transaction.rollback();
throw e;
}
};
};