Skip to content

Commit 9ae1d30

Browse files
committed
fix(user): filter out devices with unknown models
1 parent 6ba216d commit 9ae1d30

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lambda/listUserDevices.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { corsOPTIONS } from '@hello.nrfcloud.com/lambda-helpers/corsOPTIONS'
77
import { problemResponse } from '@hello.nrfcloud.com/lambda-helpers/problemResponse'
88
import { requestLogger } from '@hello.nrfcloud.com/lambda-helpers/requestLogger'
99
import { Context, type UserDevices } from '@hello.nrfcloud.com/proto-map/api'
10+
import { models } from '@hello.nrfcloud.com/proto-map/models'
1011
import middy from '@middy/core'
1112
import { type Static } from '@sinclair/typebox'
1213
import type {
@@ -51,12 +52,15 @@ const h = async (
5152

5253
const res: Static<typeof UserDevices> = {
5354
'@context': Context.userDevices.toString(),
54-
devices: devices.map((d) => ({
55-
id: d.id,
56-
deviceId: d.deviceId,
57-
model: d.model,
58-
expires: new Date(d.ttl * 1000).toISOString(),
59-
})),
55+
devices: devices
56+
// Filter out devices with unknown models
57+
.filter((d) => isModel(d.model))
58+
.map((d) => ({
59+
id: d.id,
60+
deviceId: d.deviceId,
61+
model: d.model,
62+
expires: new Date(d.ttl * 1000).toISOString(),
63+
})),
6064
}
6165

6266
return aResponse(
@@ -82,3 +86,6 @@ export const handler = middy()
8286
)
8387
.use(problemResponse())
8488
.handler(h)
89+
90+
const isModel = (model: string): model is string =>
91+
Object.keys(models).includes(model)

0 commit comments

Comments
 (0)