salix/modules/client/back/methods/address/createDefaultAddress.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

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);
2019-10-14 09:50:45 +00:00
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;
}
};
};