Skip to content

Commit

Permalink
Merge pull request #68 from wearefuturegov/TOP-251-meta-filter
Browse files Browse the repository at this point in the history
update structure
  • Loading branch information
apricot13 authored Nov 12, 2024
2 parents 7561d86 + f21a91f commit cb80cac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
19 changes: 15 additions & 4 deletions __tests__/unit/v1/services/routes/get-services.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,19 +430,30 @@ describe("get-services", () => {
const { meta } = await parseRequestParameters({})
expect(meta).toEqual([])
})
it("should return a unique array multiple targets are passed through", async () => {
it("should cope with multiple dividers", 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 if one target is passed through", async () => {
const { meta } = await parseRequestParameters({
"meta-service-meta-key": "service-meta-value",
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",
meta: [
"service-meta-key:service-meta-value",
"service-meta-key-2:service-meta-value-2",
],
})
console.log(meta)
expect(new Set(meta)).toEqual(
new Set([
{ key: "service-meta-key", value: "service-meta-value" },
Expand Down
12 changes: 9 additions & 3 deletions src/controllers/v1/services/routes/get-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ 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 }))
let meta = queryParams?.meta ? [].concat(queryParams.meta) : []

const minAge = parseInt(queryParams.min_age) || undefined
const maxAge = parseInt(queryParams.max_age) || undefined
Expand Down Expand Up @@ -76,6 +74,14 @@ module.exports = {
]
daysDeprecated = [...new Set(daysDeprecated.flatMap(str => str.split(",")))]
only = [...new Set(only.flatMap(str => str.split(",")))]
meta = [...new Set(meta.flatMap(str => str.split(",")))]

// split meta into key values
meta = meta.map(str => {
const [key, ...valueParts] = str.split(":")
const value = valueParts.join(":") // Join the rest of the parts to handle cases where the value contains colons
return { key, value }
})

// we dont de-dupe these as they are used in pairs
startTime = [...startTime.flatMap(str => str.split(","))]
Expand Down
2 changes: 1 addition & 1 deletion src/routes/parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ parameters:
meta:
name: meta
in: query
description: Prefix the key with meta- to filter by meta data. eg/ "meta-foo=bar"
description: Filter by meta data. eg/ "meta=foo:bar"
required: false
schema:
type: string
Expand Down

0 comments on commit cb80cac

Please sign in to comment.