Skip to content

Commit

Permalink
fix(live-loans): handles bugs from fewer loans than asked for being r…
Browse files Browse the repository at this point in the history
…eturned
  • Loading branch information
emuvente committed Dec 1, 2021
1 parent e712d7a commit e01eb03
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 5 additions & 1 deletion server/live-loan-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function fetchRecommendedLoans(type, id, cache) {
const loanData = await fetchLoansByType(type, id);

// Set the loan data in memcache, return the loan data
if (loanData.length) {
if (loanData && loanData.length) {
const expires = 10 * 60; // 10 minutes
memJsUtils.setToCache(`recommendations-by-${type}-id-${id}`, JSON.stringify(loanData), expires, cache)
.catch(err => {
Expand All @@ -39,6 +39,10 @@ async function getLoanForRequest(type, cache, req) {
const offset = req.params?.offset || 1;

const loanData = await fetchRecommendedLoans(type, id, cache);
// if there are fewer loan results than the offset, return the last result
if (offset > loanData.length) {
return loanData[loanData.length - 1];
}
return loanData[offset - 1];
}

Expand Down
7 changes: 6 additions & 1 deletion server/util/live-loan/live-loan-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ async function fetchGraphQL(request, resultPath) {
body: JSON.stringify(request),
});
const result = await response.json();
return get(result, resultPath, []);
const data = get(result, resultPath, []);
if (Array.isArray(data)) {
// Ensure no falsy values are included in the returned array
return data.filter(x => x);
}
return [];
} catch (err) {
log(`Error fetching loans: ${err}`, 'error');
}
Expand Down

0 comments on commit e01eb03

Please sign in to comment.