Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions backend/src/queryBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ const escapeRegex = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
// Filter values come in as "full_sun", the database has "full sun"
const normalize = (s: string) => escapeRegex(s.replace(/_/g, " ").trim());

// Order from low to high.
const SORT_ORDERS = {
// The values each filter can have, from low to high. Must match PLANT_FILTERS in
// frontend/src/components/plantSearchForm/plantFilters.ts
export const FILTER_OPTIONS: Record<FilterParam, string[]> = {
watering: ["minimum", "average", "frequent"],
sunlight: ["full_sun", "part_shade", "filtered_shade"],
careLevel: ["low", "medium", "high"],
};

Expand Down Expand Up @@ -61,7 +63,9 @@ export const buildSortStages = (
return [
{
$addFields: {
sortRank: rankOf(SORT_ORDERS.watering, { $toLower: "$watering" }),
sortRank: rankOf(FILTER_OPTIONS.watering, {
$toLower: "$watering",
}),
},
},
{ $sort: { sortRank: 1, commonName: 1, _id: 1 } },
Expand All @@ -71,7 +75,7 @@ export const buildSortStages = (
return [
{
$addFields: {
sortRank: rankOf(SORT_ORDERS.careLevel, {
sortRank: rankOf(FILTER_OPTIONS.careLevel, {
$let: {
vars: { c: { $toLower: { $ifNull: ["$careLevel", ""] } } },
in: {
Expand Down Expand Up @@ -149,14 +153,6 @@ export const buildPlantFilter = (args: PlantFilterArgs): Filter<PlantDoc> => {
return filter;
};

// The values each filter can have. Must match PLANT_FILTERS in
// frontend/src/components/plantSearchForm/plantFilters.ts
export const FILTER_OPTIONS: Record<FilterParam, string[]> = {
watering: ["minimum", "average", "frequent"],
sunlight: ["full_sun", "part_shade", "filtered_shade"],
careLevel: ["low", "medium", "high"],
};

// The name of one count in the $facet result, e.g. "watering:minimum"
const facetKey = (param: string, value: string) => `${param}:${value}`;

Expand Down Expand Up @@ -222,8 +218,8 @@ const sunlightIncludes = (text: string): Document => ({
});

// How each preference id maps to the plant attributes. Must match
// frontend/src/components/plantPreferenceChips/PlantPreferenceChips.tsx
// and the preference ids in frontend/src/pages/profile/ProfilePage.tsx.
// PREFERENCE_IDS in frontend/src/types/Preference.ts and getPlantPreferences
// in frontend/src/components/plantCard/plantTraits.ts.
// The data uses "Low" for easy care and both "Medium" and "Moderate".
export const PREFERENCE_MATCHERS: Record<string, Document> = {
minimumWater: { $eq: [lower("$watering"), "minimum"] },
Expand Down
Loading