Skip to content

Commit

Permalink
Merge pull request #114 from coworking-metz/location-in-heartbeat
Browse files Browse the repository at this point in the history
Heartbeat location
  • Loading branch information
gfra54 authored Sep 17, 2024
2 parents c122344 + cf38c50 commit b35a28f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
8 changes: 5 additions & 3 deletions lib/models/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions lib/models/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b35a28f

Please sign in to comment.