Skip to content

Commit

Permalink
Filter by service meta
Browse files Browse the repository at this point in the history
  • Loading branch information
apricot13 committed Nov 12, 2024
1 parent 6f08305 commit ee39585
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 1 deletion.
47 changes: 47 additions & 0 deletions __tests__/unit/lib/filters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,53 @@ describe("filterOnly", () => {
})
})

describe("filterMeta", () => {
it("should return an empty array if meta is not provided", () => {
expect(filters.filterMeta()).toEqual([])
})
it("should return a query object with key and value if meta includes information", () => {
expect(
filters.filterMeta([
{ key: "service-meta-key", value: "service-meta-value" },
])
).toEqual([
{
meta: {
$elemMatch: {
key: "service-meta-key",
value: "service-meta-value",
},
},
},
])
})
it("should return a query object with each key and value if meta includes information", () => {
expect(
filters.filterMeta([
{ key: "service-meta-key", value: "service-meta-value" },
{ key: "service-meta-key-2", value: "service-meta-value-2" },
])
).toEqual([
{
meta: {
$elemMatch: {
key: "service-meta-key",
value: "service-meta-value",
},
},
},
{
meta: {
$elemMatch: {
key: "service-meta-key-2",
value: "service-meta-value-2",
},
},
},
])
})
})

describe("filterTaxonomies", () => {
it("should return an empty object if taxonomies is not provided", () => {
expect(filters.filterTaxonomies()).toEqual({})
Expand Down
28 changes: 28 additions & 0 deletions __tests__/unit/v1/services/routes/get-services.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe("get-services", () => {
endDate: undefined,
accessibilities: [],
only: [],
meta: [],
minAge: undefined,
maxAge: undefined,
interpreted_location: undefined,
Expand Down Expand Up @@ -424,6 +425,33 @@ describe("get-services", () => {
})
})

describe("meta", () => {
it("should return undefined if only are not provided", async () => {
const { meta } = await parseRequestParameters({})
expect(meta).toEqual([])
})
it("should return a unique array multiple targets are passed through", async () => {
const { meta } = await parseRequestParameters({
"meta-service-meta-key": "service-meta-value",
})
expect(new Set(meta)).toEqual(
new Set([{ key: "service-meta-key", value: "service-meta-value" }])
)
})
it("should return a unique array one target is passed through", async () => {
const { meta } = await parseRequestParameters({
"meta-service-meta-key": "service-meta-value",
"meta-service-meta-key-2": "service-meta-value-2",
})
expect(new Set(meta)).toEqual(
new Set([
{ key: "service-meta-key", value: "service-meta-value" },
{ key: "service-meta-key-2", value: "service-meta-value-2" },
])
)
})
})

describe("minAge", () => {
it("should return undefined if minAge is not provided", async () => {
const { minAge } = await parseRequestParameters({})
Expand Down
5 changes: 5 additions & 0 deletions src/controllers/v1/services/routes/get-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ module.exports = {
// days = days=Monday&days=Tuesday - deprecated
let daysDeprecated = queryParams?.days ? [].concat(queryParams.days) : []
let only = queryParams?.only ? [].concat(queryParams.only) : []
let meta = Object.entries(queryParams)
.filter(([key]) => key.startsWith("meta-"))
.map(([key, value]) => ({ key: key.replace("meta-", ""), value }))

const minAge = parseInt(queryParams.min_age) || undefined
const maxAge = parseInt(queryParams.max_age) || undefined

Expand Down Expand Up @@ -123,6 +127,7 @@ module.exports = {
endDate,
accessibilities,
only,
meta,
minAge,
maxAge,
interpreted_location,
Expand Down
21 changes: 21 additions & 0 deletions src/lib/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ const filters = {
return query
},

// filters by meta
// meta-service-meta-key=service-meta-value
filterMeta: meta => {
let query = []
if (meta) {
meta.forEach(m => {
if (m.key && m.value) {
query.push({
meta: {
$elemMatch: {
key: m.key,
value: m.value,
},
},
})
}
})
}
return query
},

/**
* Specify the taxonomies to search for services
* We are using $in to match any of the taxonomies
Expand Down
4 changes: 4 additions & 0 deletions src/lib/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ module.exports = {
const only = filters.filterOnly(parameters.only)
query.$and.push(...only)

// apply meta filters
const meta = filters.filterMeta(parameters.meta)
query.$and.push(...meta)

// apply visibility filtering
const visibleNow = filters.visibleNow()
query.$and.push(...visibleNow)
Expand Down
10 changes: 9 additions & 1 deletion src/routes/parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ parameters:
only:
name: only
in: query
description: Find services that are only x. Currently only free is supported. eg/ "free"
description: Find services that are only x. Currently only free, needs-referral and local-offer are supported. eg/ "free"
required: false
schema:
type: enum
Expand All @@ -142,6 +142,14 @@ parameters:
- "needs-referral"
- "local-offer"

meta:
name: meta
in: query
description: Prefix the key with meta- to filter by meta data. eg/ "meta-foo=bar"
required: false
schema:
type: string

min_age:
name: min_age
in: query
Expand Down
1 change: 1 addition & 0 deletions src/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports.setup = app => {
* - $ref: '#/parameters/accessibilities'
* - $ref: '#/parameters/days'
* - $ref: '#/parameters/only'
* - $ref: '#/parameters/meta'
* - $ref: '#/parameters/min_age'
* - $ref: '#/parameters/max_age'
* - $ref: '#/parameters/start_time'
Expand Down

0 comments on commit ee39585

Please sign in to comment.