Skip to content

Commit 959e396

Browse files
committed
fix(core): stale update of non updated sensors
1 parent da2ecf6 commit 959e396

3 files changed

Lines changed: 157 additions & 44 deletions

File tree

services/core/devices/application.go

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ import (
1414

1515
type DeviceStore interface {
1616
List(context.Context, DeviceFilter, pagination.Request) (*pagination.Page[Device], error)
17-
ListInBoundingBox(context.Context, DeviceFilter, pagination.Request) (*pagination.Page[Device], error)
17+
ListInBoundingBox(
18+
context.Context,
19+
DeviceFilter,
20+
pagination.Request,
21+
) (*pagination.Page[Device], error)
1822
ListInRange(context.Context, DeviceFilter, pagination.Request) (*pagination.Page[Device], error)
1923
ListSensors(context.Context, pagination.Request) (*pagination.Page[Sensor], error)
2024
Find(ctx context.Context, id int64) (*Device, error)
2125
Save(ctx context.Context, dev *Device) error
26+
UpdateSensor(ctx context.Context, id int64, opts UpdateSensorOpts) error
2227
Delete(ctx context.Context, dev *Device) error
2328
GetSensor(ctx context.Context, id int64) (*Sensor, error)
2429
}
@@ -36,7 +41,11 @@ type Service struct {
3641
featureOfInterestService *featuresofinterest.Service
3742
}
3843

39-
func New(store DeviceStore, sensorGroupStore SensorGroupStore, featureOfInterestService *featuresofinterest.Service) *Service {
44+
func New(
45+
store DeviceStore,
46+
sensorGroupStore SensorGroupStore,
47+
featureOfInterestService *featuresofinterest.Service,
48+
) *Service {
4049
return &Service{
4150
store: store,
4251
sensorGroupStore: sensorGroupStore,
@@ -73,7 +82,11 @@ func (f DeviceFilter) HasRange() bool {
7382
return f.Latitude != nil && f.Longitude != nil && f.Distance != nil
7483
}
7584

76-
func (s *Service) ListDevices(ctx context.Context, filter DeviceFilter, p pagination.Request) (*pagination.Page[Device], error) {
85+
func (s *Service) ListDevices(
86+
ctx context.Context,
87+
filter DeviceFilter,
88+
p pagination.Request,
89+
) (*pagination.Page[Device], error) {
7790
if err := auth.MustHavePermissions(ctx, auth.Permissions{auth.READ_DEVICES}); err != nil {
7891
return nil, err
7992
}
@@ -144,7 +157,10 @@ func (s *Service) AddSensor(ctx context.Context, dev *Device, dto NewSensorDTO)
144157
IsFallback: dto.IsFallback,
145158
}
146159
if dto.FeatureOfInterestID > 0 {
147-
feature, err := s.featureOfInterestService.GetFeatureOfInterest(ctx, dto.FeatureOfInterestID)
160+
feature, err := s.featureOfInterestService.GetFeatureOfInterest(
161+
ctx,
162+
dto.FeatureOfInterestID,
163+
)
148164
if err != nil {
149165
return fmt.Errorf("in AddSensor: could not get feature of interest: %w", err)
150166
}
@@ -228,7 +244,10 @@ func (s *Service) DeleteDevice(ctx context.Context, dev *Device) error {
228244
return nil
229245
}
230246

231-
func (s *Service) ListSensors(ctx context.Context, p pagination.Request) (*pagination.Page[Sensor], error) {
247+
func (s *Service) ListSensors(
248+
ctx context.Context,
249+
p pagination.Request,
250+
) (*pagination.Page[Sensor], error) {
232251
if err := auth.MustHavePermissions(ctx, auth.Permissions{auth.READ_DEVICES}); err != nil {
233252
return nil, err
234253
}
@@ -254,57 +273,42 @@ type UpdateSensorOpts struct {
254273
FeatureOfInterestID *int64 `json:"feature_of_interest_id"`
255274
}
256275

257-
func (s *Service) UpdateSensor(ctx context.Context, device *Device, sensor *Sensor, opt UpdateSensorOpts) error {
276+
func (s *Service) UpdateSensor(
277+
ctx context.Context,
278+
device *Device,
279+
sensor *Sensor,
280+
opt UpdateSensorOpts,
281+
) error {
258282
if err := auth.MustHavePermissions(ctx, auth.Permissions{auth.WRITE_DEVICES}); err != nil {
259283
return err
260284
}
261285

262-
if opt.Description != nil {
263-
sensor.Description = *opt.Description
264-
}
265-
if opt.Brand != nil {
266-
sensor.Brand = *opt.Brand
267-
}
268-
if opt.ArchiveTime != nil {
269-
if *opt.ArchiveTime == 0 {
270-
sensor.ArchiveTime = nil
271-
} else {
272-
sensor.ArchiveTime = opt.ArchiveTime
273-
}
274-
}
275-
if opt.ExternalID != nil {
276-
sensor.ExternalID = *opt.ExternalID
277-
}
278-
if opt.IsFallback != nil {
279-
sensor.IsFallback = *opt.IsFallback
280-
}
281-
if opt.Properties != nil {
282-
sensor.Properties = opt.Properties
283-
}
284-
if opt.FeatureOfInterestID != nil {
285-
if *opt.FeatureOfInterestID == 0 {
286-
sensor.FeatureOfInterest = nil
287-
} else {
288-
feature, err := s.featureOfInterestService.GetFeatureOfInterest(ctx, *opt.FeatureOfInterestID)
289-
if err != nil {
290-
return fmt.Errorf("in UpdateSensor: could not get feature of interest: %w", err)
291-
}
292-
sensor.FeatureOfInterest = feature
293-
}
294-
}
295-
296286
if err := device.UpdateSensor(sensor); err != nil {
297287
return err
298288
}
299289

300-
if err := s.store.Save(ctx, device); err != nil {
290+
// Ensure that the FoI exists
291+
if opt.FeatureOfInterestID != nil && *opt.FeatureOfInterestID > 0 {
292+
_, err := s.featureOfInterestService.GetFeatureOfInterest(
293+
ctx,
294+
*opt.FeatureOfInterestID,
295+
)
296+
if err != nil {
297+
return fmt.Errorf("in UpdateSensor: could not get feature of interest: %w", err)
298+
}
299+
}
300+
301+
if err := s.store.UpdateSensor(ctx, sensor.ID, opt); err != nil {
301302
return err
302303
}
303304

304305
return nil
305306
}
306307

307-
func (s *Service) CreateSensorGroup(ctx context.Context, name, description string) (*SensorGroup, error) {
308+
func (s *Service) CreateSensorGroup(
309+
ctx context.Context,
310+
name, description string,
311+
) (*SensorGroup, error) {
308312
if err := auth.MustHavePermissions(ctx, auth.Permissions{auth.WRITE_DEVICES}); err != nil {
309313
return nil, err
310314
}
@@ -323,7 +327,10 @@ func (s *Service) CreateSensorGroup(ctx context.Context, name, description strin
323327
return group, nil
324328
}
325329

326-
func (s *Service) ListSensorGroups(ctx context.Context, p pagination.Request) (*pagination.Page[SensorGroup], error) {
330+
func (s *Service) ListSensorGroups(
331+
ctx context.Context,
332+
p pagination.Request,
333+
) (*pagination.Page[SensorGroup], error) {
327334
if err := auth.MustHavePermissions(ctx, auth.Permissions{auth.READ_DEVICES}); err != nil {
328335
return nil, err
329336
}
@@ -410,7 +417,11 @@ type UpdateSensorGroupOpts struct {
410417
Description *string `json:"description"`
411418
}
412419

413-
func (s *Service) UpdateSensorGroup(ctx context.Context, group *SensorGroup, opts UpdateSensorGroupOpts) error {
420+
func (s *Service) UpdateSensorGroup(
421+
ctx context.Context,
422+
group *SensorGroup,
423+
opts UpdateSensorGroupOpts,
424+
) error {
414425
if err := auth.MustHavePermissions(ctx, auth.Permissions{auth.WRITE_DEVICES}); err != nil {
415426
return err
416427
}

services/core/devices/infra/store_psql.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,3 +603,49 @@ func (s *PSQLStore) Save(ctx context.Context, dev *devices.Device) error {
603603

604604
return nil
605605
}
606+
607+
func (store *PSQLStore) UpdateSensor(
608+
ctx context.Context,
609+
id int64,
610+
opts devices.UpdateSensorOpts,
611+
) error {
612+
q := pq.Update("sensors").Where(sq.Eq{"id": id})
613+
614+
if opts.Description != nil {
615+
q = q.Set("description", *opts.Description)
616+
}
617+
if opts.Brand != nil {
618+
q = q.Set("description", *opts.Description)
619+
}
620+
if opts.ArchiveTime != nil {
621+
if *opts.ArchiveTime == 0 {
622+
q = q.Set("archive_time", nil)
623+
} else {
624+
q = q.Set("archive_time", *opts.ArchiveTime)
625+
}
626+
}
627+
if opts.ExternalID != nil {
628+
q = q.Set("external_id", *opts.ExternalID)
629+
}
630+
if opts.IsFallback != nil {
631+
q = q.Set("is_fallback", *opts.IsFallback)
632+
}
633+
if opts.Properties != nil {
634+
q = q.Set("properties", opts.Properties)
635+
}
636+
if opts.FeatureOfInterestID != nil {
637+
if *opts.FeatureOfInterestID == 0 {
638+
q = q.Set("feature_of_interest_id", nil)
639+
} else {
640+
q = q.Set("feature_of_interest_id", *opts.FeatureOfInterestID)
641+
}
642+
}
643+
644+
q = auth.ProtectedQuery(ctx, "tenant_id", q)
645+
_, err := q.RunWith(store.db).Exec()
646+
if err != nil {
647+
return fmt.Errorf("in UpdateSensor, Query error: %w", err)
648+
}
649+
650+
return nil
651+
}

services/core/devices/mock_test.go

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)