Skip to content

Commit

Permalink
feat: Implement include_if and exclude_if (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Dec 14, 2023
1 parent fad292f commit 4c52f28
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@seamapi/types": "^1.32.0",
"@tsconfig/next": "^2.0.0",
"@types/get-pixels": "^3.3.2",
"@types/lodash": "^4.14.202",
"@types/node": "^20.8.10",
"@types/react": "^18.0.35",
"@types/react-dom": "^18.0.11",
Expand All @@ -93,6 +94,7 @@
"isomorphic-fetch": "^3.0.0",
"landlubber": "^1.0.0",
"liqe": "^3.8.0",
"lodash": "^4.17.21",
"mkdirp": "^3.0.0",
"next": "^12.3.4",
"nextjs-server-modules": "^4.7.0",
Expand Down
26 changes: 26 additions & 0 deletions src/pages/api/v1/device_models/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { routes } from "@seamapi/types/devicedb"
import { get } from "lodash"

import { getBaseUrl } from "lib/get-base-url.ts"
import { withRouteSpec } from "lib/middleware/index.ts"
Expand All @@ -9,8 +10,33 @@ export default withRouteSpec({
methods: ["GET", "POST", "OPTIONS"],
auth: "vercel_protection_bypass_secret",
} as const)(async (req, res) => {
const { include_if, exclude_if } = req.query

if (include_if?.length === 0) {
res.status(200).json({
device_models: [],
})
return
}

let filtered_device_models = [...req.db.device_models]

if (include_if != null) {
filtered_device_models = filtered_device_models.filter((device_model) =>
include_if
.map((path) => get(device_model, path))
.every((v) => v === true),
)
}

if (exclude_if != null) {
filtered_device_models = filtered_device_models.filter((device_model) =>
exclude_if
.map((path) => get(device_model, path))
.every((v) => v !== true),
)
}

if (req.query.main_category) {
filtered_device_models = filtered_device_models.filter(
(device_model) => device_model.main_category === req.query.main_category,
Expand Down
52 changes: 52 additions & 0 deletions test/api/v1/device_models/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,55 @@ test("GET /v1/device_models/list (filter by text_search)", async (t) => {
})
t.is(data.device_models.length, 1)
})

// UPSTREAM: nextlove does not parse this as the empty array
// https://github.com/seamapi/nextlove/issues/77#issuecomment-1786284982
test.failing("GET /v1/device_models/list (empty include_if)", async (t) => {
const { axios } = await getTestServer(t)
const { data } = await axios.get("/v1/device_models/list", {
params: {
include_if: [],
},
})
t.is(data.device_models.length, 0)
})

test("GET /v1/device_models/list (filter by include_if)", async (t) => {
const { axios } = await getTestServer(t)
const { data } = await axios.get("/v1/device_models/list", {
params: {
include_if: ["software_features.can_program_access_codes"],
},
})
t.true(data.device_models.length > 0)
const device_model = data.device_models[0]
if (
device_model != null &&
"software_features" in device_model &&
"can_program_access_codes" in device_model.software_features
) {
t.true(device_model.software_features.can_program_access_codes)
} else {
t.fail("can_program_access_codes not in device_model")
}
})

test("GET /v1/device_models/list (filter by exclude_if)", async (t) => {
const { axios } = await getTestServer(t)
const { data } = await axios.get("/v1/device_models/list", {
params: {
exclude_if: ["physical_properties.has_camera"],
},
})
t.true(data.device_models.length > 0)
const device_model = data.device_models[0]
if (
device_model != null &&
"physical_properties" in device_model &&
"has_camera" in device_model.physical_properties
) {
t.false(device_model.physical_properties.has_camera)
} else {
t.fail("has_camera not in device_model")
}
})

0 comments on commit 4c52f28

Please sign in to comment.