45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
module.exports = function(Self) {
|
|
Self.remoteMethod('createDefaultAddress', {
|
|
description: 'Creates both client and its web account',
|
|
accepts: {
|
|
arg: 'data',
|
|
type: 'object',
|
|
http: {source: 'body'}
|
|
},
|
|
returns: {
|
|
root: true,
|
|
type: 'Object'
|
|
},
|
|
http: {
|
|
verb: 'post',
|
|
path: '/createDefaultAddress'
|
|
}
|
|
});
|
|
|
|
Self.createDefaultAddress = async data => {
|
|
const Address = Self.app.models.Address;
|
|
const Client = Self.app.models.Client;
|
|
const tx = await Address.beginTransaction({});
|
|
|
|
try {
|
|
let options = {transaction: tx};
|
|
|
|
let address = data.address;
|
|
let newAddress = await Address.create(address, options);
|
|
let client = await Client.findById(address.clientFk, null, options);
|
|
|
|
if (data.isDefaultAddress) {
|
|
await client.updateAttributes({
|
|
defaultAddressFk: newAddress.id
|
|
}, options);
|
|
}
|
|
|
|
await tx.commit();
|
|
return newAddress;
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|