Created
July 13, 2021 16:57
-
-
Save jamesseanwright/e4ea2aef142cb4904f4b287929d9fb4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const { getDBConnection, createRes } = require('../common'); | |
exports.handler = async ({ queryStringParameters }) => { | |
const connection = await getDBConnection(); | |
const { deviceID, date, eventType } = queryStringParameters; | |
try { | |
const events = await connection | |
.db('events') | |
.collection('events') | |
.aggregate([ | |
{ | |
$match: { | |
deviceID, | |
date: new Date(date), | |
eventType, | |
}, | |
}, | |
{ | |
$limit: 1, | |
}, | |
{ | |
$unwind: '$events', | |
}, | |
{ | |
$project: { | |
_id: 0, | |
date: '$events.date', | |
value: '$events.value', | |
}, | |
}, | |
]) | |
.toArray(); | |
return createRes(200, events); | |
} catch ({ message }) { | |
return createRes(500, { | |
message, | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment