Skip to content

Commit

Permalink
Merge pull request #4987 from kiva/add-loanIds-filter-to-live-loan-MA…
Browse files Browse the repository at this point in the history
…RS-487

feat: add loanId filter
  • Loading branch information
christian14b authored Sep 27, 2023
2 parents fcd3677 + bd268a0 commit 4f29961
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
21 changes: 21 additions & 0 deletions server/live-loan-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ module.exports = function liveLoanRouter(cache) {
});
});

// Loan Id URL Router
router.use('/lid/:id([0-9]{0,})/url', async (req, res) => {
await tracer.trace('live-loan.loanid.redirectToUrl', { resource: req.path }, async () => {
await redirectToUrl('loanid', cache, req, res);
});
});

// Loan Id IMG Router (Legacy)
router.use('/lid/:id([0-9]{0,})/img', async (req, res) => {
await tracer.trace('live-loan.loanid.serveImg', { resource: req.path }, async () => {
await serveImg('loanid', 'legacy', cache, req, res);
});
});

// Loan Id IMG Router (Kiva Classic)
router.use('/lid/:id([0-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
30 changes: 29 additions & 1 deletion 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 @@ -369,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 @@ -380,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 4f29961

Please sign in to comment.