salix/modules/client/back/methods/client/getClientOrSupplierReferenc...

58 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-02-14 11:41:36 +00:00
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('getClientOrSupplierReference', {
description: 'Returns the reference of a compensation providing a bank account',
accessType: 'READ',
accepts: {
arg: 'bankAccount',
type: 'number',
required: true,
2023-02-15 12:14:52 +00:00
description: 'The bank account of a client or a supplier'
2023-02-14 11:41:36 +00:00
},
returns: {
type: 'string',
root: true
},
http: {
2023-02-15 12:14:52 +00:00
path: `/getClientOrSupplierReference`,
2023-02-14 11:41:36 +00:00
verb: 'GET'
}
});
Self.getClientOrSupplierReference = async(bankAccount, options) => {
const models = Self.app.models;
const myOptions = {};
let reference = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const supplierCompensation = await models.Supplier.findOne({
where: {
account: bankAccount
}
}, myOptions);
reference.supplierId = supplierCompensation?.id;
reference.supplierName = supplierCompensation?.name;
let clientCompensation = {};
if (!supplierCompensation) {
clientCompensation = await models.Client.findOne({
where: {
accountingAccount: bankAccount
}
}, myOptions);
reference.clientId = clientCompensation?.id;
reference.clientName = clientCompensation?.name;
}
if (!supplierCompensation && !clientCompensation)
throw new UserError('Invalid account');
return reference;
};
};