chore: fix when no payments or invoices

This commit is contained in:
AP 2019-12-17 08:11:37 +01:00
parent b51dca146d
commit c58302da2a
2 changed files with 25 additions and 18 deletions

View file

@ -52,7 +52,7 @@
}, },
"husky": { "husky": {
"hooks": { "hooks": {
"pre-commit": "pretty-quick" "pre-commit": "pretty-quick --staged"
} }
} }
} }

View file

@ -61,6 +61,7 @@ export const getResume = {
let lastInvoiceDate = ''; let lastInvoiceDate = '';
let firstInvoiceDate = ''; let firstInvoiceDate = '';
let token = ''; let token = '';
let withInvoices = true;
try { try {
const invoiceList: InvoicesProps = await getInvoices({ const invoiceList: InvoicesProps = await getInvoices({
@ -76,28 +77,34 @@ export const getResume = {
}; };
}); });
const { date } = invoices[invoices.length - 1]; if (invoices.length <= 0) {
firstInvoiceDate = invoices[0].date; withInvoices = false;
lastInvoiceDate = date; } else {
token = invoiceList.next; const { date } = invoices[invoices.length - 1];
firstInvoiceDate = invoices[0].date;
lastInvoiceDate = date;
token = invoiceList.next;
}
} catch (error) { } catch (error) {
logger.error('Error getting invoices: %o', error); logger.error('Error getting invoices: %o', error);
throw new Error(getErrorMsg(error)); throw new Error(getErrorMsg(error));
} }
const filteredPayments = payments.filter(payment => { const filteredPayments = withInvoices
const last = ? payments.filter(payment => {
compareDesc( const last =
new Date(lastInvoiceDate), compareDesc(
new Date(payment.date), new Date(lastInvoiceDate),
) === 1; new Date(payment.date),
const first = ) === 1;
compareDesc( const first =
new Date(payment.date), compareDesc(
new Date(firstInvoiceDate), new Date(payment.date),
) === 1; new Date(firstInvoiceDate),
return last && first; ) === 1;
}); return last && first;
})
: payments;
const resumeArray = sortBy( const resumeArray = sortBy(
[...invoices, ...filteredPayments], [...invoices, ...filteredPayments],