From f21a91f9390aafc045363c212c5bd5c8e028c901 Mon Sep 17 00:00:00 2001 From: Han Date: Tue, 12 Nov 2024 16:06:14 +0000 Subject: [PATCH] update structure --- .../v1/services/routes/get-services.test.js | 19 +++++++++++++++---- .../v1/services/routes/get-services.js | 12 +++++++++--- src/routes/parameters.yml | 2 +- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/__tests__/unit/v1/services/routes/get-services.test.js b/__tests__/unit/v1/services/routes/get-services.test.js index 3dc3aa6..d71ef70 100644 --- a/__tests__/unit/v1/services/routes/get-services.test.js +++ b/__tests__/unit/v1/services/routes/get-services.test.js @@ -430,9 +430,17 @@ 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" }]) @@ -440,9 +448,12 @@ describe("get-services", () => { }) 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" }, diff --git a/src/controllers/v1/services/routes/get-services.js b/src/controllers/v1/services/routes/get-services.js index 99da3d7..9dd327f 100644 --- a/src/controllers/v1/services/routes/get-services.js +++ b/src/controllers/v1/services/routes/get-services.js @@ -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 @@ -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(","))] diff --git a/src/routes/parameters.yml b/src/routes/parameters.yml index cbf17aa..6f8c662 100644 --- a/src/routes/parameters.yml +++ b/src/routes/parameters.yml @@ -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