@@ -29,6 +29,7 @@ import { aResponse } from '@hello.nrfcloud.com/lambda-helpers/aResponse'
2929import { aProblem } from '@hello.nrfcloud.com/lambda-helpers/aProblem'
3030import { addVersionHeader } from '@hello.nrfcloud.com/lambda-helpers/addVersionHeader'
3131import { shadowToObjects } from '@hello.nrfcloud.com/proto-map/lwm2m/aws'
32+ import type { LwM2MObjectInstance } from '@hello.nrfcloud.com/proto-map/lwm2m'
3233
3334const {
3435 publicDevicesTableName,
@@ -57,7 +58,7 @@ const h = async (
5758) : Promise < APIGatewayProxyResultV2 > => {
5859 console . log ( JSON . stringify ( { event } ) )
5960
60- const devicesToFetch : { id : string ; model : string } [ ] = [ ]
61+ const devicesToFetch : { id : string ; deviceId : string ; model : string } [ ] = [ ]
6162 const minConfirmTime = Date . now ( ) - consentDurationMS
6263
6364 const qs : Record < string , any > = event . queryStringParameters ?? { }
@@ -80,6 +81,7 @@ const h = async (
8081 '#model = :model AND #ownerConfirmed > :minConfirmTime' ,
8182 ExpressionAttributeNames : {
8283 '#id' : 'id' ,
84+ '#deviceId' : 'secret__deviceId' ,
8385 '#model' : 'model' ,
8486 '#ownerConfirmed' : 'ownerConfirmed' ,
8587 } ,
@@ -89,7 +91,7 @@ const h = async (
8991 S : new Date ( minConfirmTime ) . toISOString ( ) ,
9092 } ,
9193 } ,
92- ProjectionExpression : '#id' ,
94+ ProjectionExpression : '#id, #deviceId ' ,
9395 }
9496
9597 if ( maybeValidQuery . value . ids !== undefined ) {
@@ -105,22 +107,34 @@ const h = async (
105107 console . log ( JSON . stringify ( { queryInput } ) )
106108
107109 const { Items } = await db . send ( new QueryCommand ( queryInput ) )
110+
108111 devicesToFetch . push (
109112 ...( Items ?? [ ] )
110- . map ( ( item ) => unmarshall ( item ) as { id : string } )
111- . map ( ( { id } ) => ( { id, model } ) ) ,
113+ . map (
114+ ( item ) =>
115+ unmarshall ( item ) as { id : string ; secret__deviceId : string } ,
116+ )
117+ . map ( ( { id, secret__deviceId } ) => ( {
118+ id,
119+ deviceId : secret__deviceId ,
120+ model,
121+ } ) ) ,
112122 )
113123 }
114124
115125 console . log ( JSON . stringify ( { devicesToFetch } ) )
116126
117- const devices = (
127+ const devices : Array < {
128+ id : string
129+ model : string
130+ state ?: Array < LwM2MObjectInstance >
131+ } > = (
118132 await Promise . all (
119- devicesToFetch . map ( async ( { id : id , model } ) => {
133+ devicesToFetch . map ( async ( { id, deviceId , model } ) => {
120134 try {
121135 const shadow = await iotData . send (
122136 new GetThingShadowCommand ( {
123- thingName : id ,
137+ thingName : deviceId ,
124138 shadowName : 'lwm2m' ,
125139 } ) ,
126140 )
@@ -136,7 +150,7 @@ const h = async (
136150 }
137151 } catch ( err ) {
138152 if ( err instanceof ResourceNotFoundException ) {
139- console . debug ( `[${ id } ]: no shadow found.` )
153+ console . debug ( `[${ id } ]: no shadow found for ${ deviceId } .` )
140154 } else {
141155 console . error ( err )
142156 }
@@ -163,5 +177,5 @@ const h = async (
163177
164178export const handler = middy ( )
165179 . use ( addVersionHeader ( version ) )
166- . use ( corsOPTIONS ( 'POST ' ) )
180+ . use ( corsOPTIONS ( 'GET ' ) )
167181 . handler ( h )
0 commit comments