Skip to content

Commit

Permalink
Changed queryToken from once per offer to once per search
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoRosendo committed Mar 20, 2022
1 parent abb04e7 commit 91accbb
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 126 deletions.
9 changes: 5 additions & 4 deletions src/api/routes/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ export default (app) => {
router.use(offerMiddleware.setTargetOwner);

/**
* Gets all currently active offers (without filtering, for now)
* supports offset and limit as query params
* Gets active offers based on passed filters and full-text search.
* Returns the offers found and a queryToken used for pagination.
* Also takes queryToken and limit as query params.
*/
router.get("/", validators.get, async (req, res, next) => {
try {
const offers = await (new OfferService()).get(
const resultsAndQueryToken = await (new OfferService()).get(
{
...req.query,
showHidden: req?.query?.showHidden && req.hasAdminPrivileges,
showAdminReason: req.hasAdminPrivileges
}
);

return res.json(offers);
return res.json(resultsAndQueryToken);
} catch (err) {
console.error(err);
return next(err);
Expand Down
20 changes: 10 additions & 10 deletions src/services/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ class OfferService {
.limit(limit)
;

return results.map(this.buildQueryToken);
return results.length > 0 ?
{ results, queryToken: this.encodeQueryToken(results[results.length - 1]) }
:
{ results };
}

_buildFilterQuery(filters) {
Expand Down Expand Up @@ -307,17 +310,14 @@ class OfferService {
}

/**
* Builds a query token, by taking the offer's id and FTS score (if present) and decoding in safe base64
* Encodes a query token, by taking the offer's id and FTS score (if present) and decoding in safe url base64
* @param {*} offer
*/
buildQueryToken(offer) {
return {
...(offer.toJSON ? offer.toJSON() : offer), // Aggregation differs from query
queryToken: base64url.encode(JSON.stringify({
id: offer._id,
score: offer.score || offer._doc?.score
})),
};
encodeQueryToken(offer) {
return base64url.encode(JSON.stringify({
id: offer._id,
score: offer.score || offer._doc?.score
}));
}

/**
Expand Down
Loading

0 comments on commit 91accbb

Please sign in to comment.