salix/modules/client/back/methods/receipt/balanceCompensationEmail.js

53 lines
1.7 KiB
JavaScript

const {Email} = require('vn-print');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('balanceCompensationEmail', {
description: 'Sends the debit balances compensation email with an attached PDF',
accessType: 'WRITE',
accepts: [
{
arg: 'id',
type: 'Number',
required: true,
description: 'The receipt id',
http: {source: 'path'}
}
],
returns: {
type: ['object'],
root: true
},
http: {
path: '/:id/balance-compensation-email',
verb: 'POST'
}
});
Self.balanceCompensationEmail = async(ctx, id) => {
const models = Self.app.models;
const receipt = await models.Receipt.findById(id, {fields: ['clientFk', 'bankFk']});
const bank = await models.Accounting.findById(receipt.bankFk);
if (!bank)
throw new UserError(`Receipt's bank was not found`);
const accountingType = await models.AccountingType.findById(bank.accountingTypeFk);
if (!(accountingType && accountingType.code == 'compensation'))
throw new UserError(`This receipt was not compensated`);
const client = await models.Client.findById(receipt.clientFk, {fields: ['email']});
if (!client.email)
throw new UserError(`Client's email was not found`);
else {
const email = new Email('balance-compensation', {
lang: ctx.req.getLocale(),
recipient: client.email + ',administracion@verdnatura.es',
id
});
return email.send();
}
};
};