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 76717d7
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 134 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 searchResults = await (new OfferService()).get(
{
...req.query,
showHidden: req?.query?.showHidden && req.hasAdminPrivileges,
showAdminReason: req.hasAdminPrivileges
}
);

return res.json(offers);
return res.json(searchResults);
} catch (err) {
console.error(err);
return next(err);
Expand Down
36 changes: 18 additions & 18 deletions src/services/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ class OfferService {
score: lastOfferScore
} = queryToken ? this.decodeQueryToken(queryToken) : {};

let offers;
let results;
if (lastOfferId && value) {
offers = Offer.aggregate([
results = Offer.aggregate([
{ $match: { $text: { $search: value } } },
{ $addFields: {
score: { $meta: "textScore" },
Expand All @@ -231,32 +231,35 @@ class OfferService {
} else {
if (lastOfferId) {

offers = Offer.find({ "$and": [
results = Offer.find({ "$and": [
this._buildFilterQuery(filters),
{ _id: { "$gt": ObjectId(lastOfferId) } }
] });

} else {

offers = (value ? Offer.find({ "$and": [
results = (value ? Offer.find({ "$and": [
this._buildFilterQuery(filters),
{ "$text": { "$search": value } }
] }, { score: { "$meta": "textScore" } }

) : Offer.find(this._buildFilterQuery(filters)));
}

offers.current();
if (!showHidden) offers.withoutHidden();
if (!showAdminReason) offers.select("-adminReason");
results.current();
if (!showHidden) results.withoutHidden();
if (!showAdminReason) results.select("-adminReason");
}

const results = await offers
const offers = await results
.sort(value ? { score: { "$meta": "textScore" }, _id: 1 } : { _id: 1 })
.limit(limit)
;

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

_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 76717d7

Please sign in to comment.