From cf38c503bc6f26a4b964911baf84a7855f5d2954 Mon Sep 17 00:00:00 2001 From: Gilles FRANCOIS Date: Tue, 17 Sep 2024 10:52:05 +0200 Subject: [PATCH] Store the location where the presence was detected both i the devices collections and in the users collection --- lib/api.js | 3 ++- lib/models/device.js | 8 +++++--- lib/models/member.js | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/api.js b/lib/api.js index 6f77de4..9a984d5 100644 --- a/lib/api.js +++ b/lib/api.js @@ -112,7 +112,8 @@ export async function forceWordpressSync(req, res) { export async function heartbeat(req, res) { const macAddresses = req.body.macAddresses.split(',') - await Device.heartbeatDevicesByMacAddresses(macAddresses) + const {location} = req.body + await Device.heartbeatDevicesByMacAddresses(macAddresses, location) res.sendStatus(200) } diff --git a/lib/models/device.js b/lib/models/device.js index c22d031..e6fe737 100644 --- a/lib/models/device.js +++ b/lib/models/device.js @@ -46,20 +46,22 @@ export async function getAssignedDevices() { return mongo.db.collection('devices').find({member: {$ne: null}}).toArray() } -export async function heartbeatDevicesByMacAddresses(macAddresses) { +export async function heartbeatDevicesByMacAddresses(macAddresses, location = null) { const now = new Date() const cleanedMacAddresses = checkMacAddresseList(macAddresses) + const set = {heartbeat: now, location: location || ''} + await mongo.db.collection('devices').updateMany( {macAddress: {$in: cleanedMacAddresses}}, - {$set: {heartbeat: now}} + {$set: set} ) const memberIds = await mongo.db.collection('devices').distinct('member', { macAddress: {$in: cleanedMacAddresses} }) - await Member.heartbeatMembers(memberIds, now) + await Member.heartbeatMembers(memberIds, now, location) } function isMacAddress(value) { diff --git a/lib/models/member.js b/lib/models/member.js index b4c99b1..da9dfb5 100644 --- a/lib/models/member.js +++ b/lib/models/member.js @@ -209,15 +209,17 @@ export async function createUser({wpUserId, firstName, lastName, birthDate, emai return user } -export async function heartbeatMembers(memberIds, referenceDate) { +export async function heartbeatMembers(memberIds, referenceDate, location = null) { if (!referenceDate) { throw new Error('Missing referenceDate') } + const set = {'profile.heartbeat': referenceDate.toISOString(), 'profile.heartbeatLocation': location || ''} + await Promise.all(memberIds.map(async memberId => { const userBeforeUpdate = await mongo.db.collection('users').findOneAndUpdate( {_id: memberId}, - {$set: {'profile.heartbeat': referenceDate.toISOString()}} + {$set: set} ) if (userBeforeUpdate.value) {