Skip to content

Commit

Permalink
chore(api): refactor device store
Browse files Browse the repository at this point in the history
* DeviceSetOnline: ensures that incomplete device documents are not inserted
* DeviceUpdateStatus: avoid registering a device as online when changing
  its status to accepted.
  • Loading branch information
gustavosbarreto committed Nov 10, 2023
1 parent f709d34 commit 704c010
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions api/store/mongo/device_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (s *Store) DeviceSetOnline(ctx context.Context, uid models.UID, timestamp t
}

collOptions := writeconcern.W1()
updateOptions := options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.Before)
updateOptions := options.FindOneAndUpdate().SetUpsert(false).SetReturnDocument(options.Before)

result := s.db.Collection("devices", options.Collection().SetWriteConcern(collOptions)).
FindOneAndUpdate(ctx, bson.M{"uid": uid},
Expand Down Expand Up @@ -401,8 +401,7 @@ func (s *Store) DeviceUpdateLastSeen(ctx context.Context, uid models.UID, ts tim
return nil
}

// DeviceUpdateStatus updates the status of a specific device in the devices collection and records the device's
// connection status in the connected_devices collection.
// DeviceUpdateStatus updates the status of a specific device in the devices collection
func (s *Store) DeviceUpdateStatus(ctx context.Context, uid models.UID, status models.DeviceStatus) error {
updateOptions := options.FindOneAndUpdate().SetReturnDocument(options.After)
result := s.db.Collection("devices", options.Collection()).
Expand All @@ -417,17 +416,6 @@ func (s *Store) DeviceUpdateStatus(ctx context.Context, uid models.UID, status m
return FromMongoError(err)
}

cd := &models.ConnectedDevice{
UID: device.UID,
TenantID: device.TenantID,
LastSeen: clock.Now(),
Status: string(status),
}

if _, err := s.db.Collection("connected_devices").InsertOne(ctx, &cd); err != nil {
return FromMongoError(err)
}

return nil
}

Expand Down

0 comments on commit 704c010

Please sign in to comment.