diff --git a/server/live-loan-router.js b/server/live-loan-router.js index 6b00265e1c..11ca518854 100644 --- a/server/live-loan-router.js +++ b/server/live-loan-router.js @@ -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); diff --git a/server/util/live-loan/live-loan-fetch.js b/server/util/live-loan/live-loan-fetch.js index e0c3b4e579..a1b6ae8c22 100644 --- a/server/util/live-loan/live-loan-fetch.js +++ b/server/util/live-loan/live-loan-fetch.js @@ -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 { @@ -36,6 +36,10 @@ const loanValues = `values { ... on LoanPartner { themes } +`; + +const loanValues = `values { + ${loanData} }`; // Make a graphql query and return the results found at @@ -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 }); @@ -248,7 +255,6 @@ const supportedFilterLegacy = name => { case 'sort': case 'theme': case 'tag': - case 'loanids': return true; default: warn(`Unsupported legacy filter "${name}"`); @@ -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); @@ -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') { @@ -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'); };