Skip to content

Commit

Permalink
feat: create new route to just fetch loan data
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Bedon committed Sep 26, 2023
1 parent 1db4450 commit a2057f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
7 changes: 7 additions & 0 deletions server/live-loan-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ module.exports = function liveLoanRouter(cache) {
});
});

// Loan Id IMG Router (Kiva Classic)
router.use('/lid/:id([a-zA-Z0-9.%,_-]{0,})/img2', async (req, res) => {
await tracer.trace('live-loan.loanid.serveImg', { resource: req.path }, async () => {
await serveImg('loanid', 'classic', cache, req, res);
});
});

// 404 any /live-loan/* routes that don't match above
router.use((req, res) => {
res.sendStatus(404);
Expand Down
35 changes: 30 additions & 5 deletions server/util/live-loan/live-loan-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { warn, error } = require('../log');
const loanCount = 4;

// Which loan properties to fetch
const loanValues = `values {
const loanData = `
name
id
activity {
Expand Down Expand Up @@ -36,6 +36,10 @@ const loanValues = `values {
... on LoanPartner {
themes
}
`;

const loanValues = `values {
${loanData}
}`;

// Make a graphql query <request> and return the results found at <resultPath>
Expand All @@ -46,6 +50,9 @@ async function fetchLoansFromGraphQL(request, resultPath) {
// Ensure no falsy values are included in the returned array
return data.filter(x => x);
}
if (typeof data === 'object' && data !== null) {
return [data];
}
return [];
} catch (err) {
error(`Error fetching loans: ${err}`, { error: err });
Expand Down Expand Up @@ -248,7 +255,6 @@ const supportedFilterLegacy = name => {
case 'sort':
case 'theme':
case 'tag':
case 'loanids':
return true;
default:
warn(`Unsupported legacy filter "${name}"`);
Expand Down Expand Up @@ -292,9 +298,7 @@ async function parseFilterStringLegacy(filterString) {
.filter(([name]) => supportedFilterLegacy(name))
// Add each filter to the filter object
.forEach(([name, value]) => {
if (name === 'loanids') {
addArrayFilterValue('loanIds', Number(value));
} else if (name === 'country') {
if (name === 'country') {
addArrayFilterValue(name, value);
} else if (name === 'gender') {
setFilterValue(name, value);
Expand Down Expand Up @@ -372,6 +376,24 @@ async function fetchRecommendationsByLegacyFilter(filterString) {
);
}

// Get loans from legacy lend search matching a set of filters
async function fetchLoanById(loanId) {
return fetchLoansFromGraphQL(
{
query: `query($loanId: Int!) {
lend {
loan(id: $loanId) {
${loanData}
}
}
}`,
variables: {
loanId: Number(loanId),
}
},
'data.lend.loan'
);
}
// Export a function that will fetch loans by live-loan type and id
module.exports = async function fetchLoansByType(type, id) {
if (type === 'user') {
Expand All @@ -383,5 +405,8 @@ module.exports = async function fetchLoansByType(type, id) {
// return fetchRecommendationsByFilter(id);
return fetchRecommendationsByLegacyFilter(id);
}
if (type === 'loanid') {
return fetchLoanById(id);
}
throw new Error('Type must be user, loan, or filter');
};

0 comments on commit a2057f5

Please sign in to comment.