43 lines
1.2 KiB
JavaScript
43 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 transaction = await Address.beginTransaction({});
|
|
|
|
try {
|
|
let address = data.address;
|
|
let newAddress = await Address.create(address, {transaction});
|
|
let client = await Client.findById(address.clientFk, {transaction});
|
|
|
|
if (data.isDefaultAddress) {
|
|
await client.updateAttributes({
|
|
defaultAddressFk: newAddress.id
|
|
}, {transaction});
|
|
}
|
|
|
|
await transaction.commit();
|
|
return newAddress;
|
|
} catch (e) {
|
|
await transaction.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|