124 lines
3.4 KiB
JavaScript
124 lines
3.4 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = function(Self) {
|
|
Self.remoteMethod('updateAddress', {
|
|
description: 'Updates a client address updating default address',
|
|
accepts: [
|
|
{
|
|
arg: 'ctx',
|
|
type: 'object',
|
|
http: {source: 'context'}
|
|
},
|
|
{
|
|
arg: 'clientId',
|
|
type: 'number',
|
|
description: 'The client id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'addressId',
|
|
type: 'number',
|
|
description: 'The address id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'nickname',
|
|
type: 'string'
|
|
},
|
|
{
|
|
arg: 'city',
|
|
type: 'string'
|
|
},
|
|
{
|
|
arg: 'street',
|
|
type: 'string'
|
|
},
|
|
{
|
|
arg: 'phone',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'mobile',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'postalCode',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'provinceFk',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'agencyModeFk',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'incotermsFk',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'customsAgentFk',
|
|
type: 'any'
|
|
},
|
|
{
|
|
arg: 'isActive',
|
|
type: 'boolean'
|
|
},
|
|
{
|
|
arg: 'isEqualizated',
|
|
type: 'boolean'
|
|
}
|
|
],
|
|
returns: {
|
|
root: true,
|
|
type: 'object'
|
|
},
|
|
http: {
|
|
verb: 'patch',
|
|
path: '/:clientId/updateAddress/:addressId'
|
|
}
|
|
});
|
|
|
|
Self.updateAddress = async(ctx, clientId, addressId, options) => {
|
|
const models = Self.app.models;
|
|
const args = ctx.args;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
const address = await models.Address.findOne({
|
|
where: {
|
|
id: addressId,
|
|
clientFk: clientId
|
|
}
|
|
}, myOptions);
|
|
|
|
const provinceId = args.provinceFk || address.provinceFk;
|
|
if (provinceId) {
|
|
const province = await models.Province.findById(provinceId, {
|
|
include: {
|
|
relation: 'country'
|
|
}
|
|
}, myOptions);
|
|
|
|
const isUeeMember = province.country().isUeeMember;
|
|
const incotermsId = args.incotermsFk || address.incotermsFk;
|
|
|
|
if (!isUeeMember && !incotermsId)
|
|
throw new UserError(`Incoterms is required for a non UEE member`);
|
|
|
|
const customsAgentId = args.customsAgentFk || address.customsAgentFk;
|
|
if (!isUeeMember && !customsAgentId)
|
|
throw new UserError(`Customs agent is required for a non UEE member`);
|
|
}
|
|
|
|
delete args.ctx; // Remove unwanted properties
|
|
|
|
const updatedAddress = await address.updateAttributes(ctx.args, myOptions);
|
|
|
|
return updatedAddress;
|
|
};
|
|
};
|