Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Add version when storing heartbeats

Acho Arnoldef7f917f14b7
diff --git a/api/pkg/entities/heartbeat.go b/api/pkg/entities/heartbeat.go
index 8ddfdeaa..66b8a3d2 100644
--- a/api/pkg/entities/heartbeat.go
+++ b/api/pkg/entities/heartbeat.go
@@ -10,6 +10,7 @@ import (
type Heartbeat struct {
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;" example:"32343a19-da5e-4b1b-a767-3298a73703cb"`
Owner string `json:"owner" gorm:"index:idx_heartbeats_owner_timestamp" example:"+18005550199"`
+ Version string `json:"version" example:"344c10f"`
UserID UserID `json:"user_id" example:"WB7DRDWrJZRGbYrv2CKGkqbzvqdC"`
Timestamp time.Time `json:"timestamp" gorm:"index:idx_heartbeats_owner_timestamp" example:"2022-06-05T14:26:01.520828+03:00"`
}
diff --git a/api/pkg/handlers/heartbeat_handler.go b/api/pkg/handlers/heartbeat_handler.go
index 7b132885..27bd4b2c 100644
--- a/api/pkg/handlers/heartbeat_handler.go
+++ b/api/pkg/handlers/heartbeat_handler.go
@@ -121,7 +121,7 @@ func (h *HeartbeatHandler) Store(c *fiber.Ctx) error {
return h.responseUnprocessableEntity(c, errors, "validation errors while storing heartbeat")
}
- heartbeat, err := h.service.Store(ctx, request.ToStoreParams(h.userFromContext(c)))
+ heartbeat, err := h.service.Store(ctx, request.ToStoreParams(h.userFromContext(c), c.Get("X-Client-Version")))
if err != nil {
msg := fmt.Sprintf("cannot store heartbeat with params [%+#v]", request)
ctxLogger.Error(stacktrace.Propagate(err, msg))
diff --git a/api/pkg/repositories/gorm_heartbeat_repository.go b/api/pkg/repositories/gorm_heartbeat_repository.go
index 7f106464..ec6408e4 100644
--- a/api/pkg/repositories/gorm_heartbeat_repository.go
+++ b/api/pkg/repositories/gorm_heartbeat_repository.go
@@ -63,7 +63,7 @@ func (repository *gormHeartbeatRepository) Index(ctx context.Context, userID ent
query := repository.db.WithContext(ctx).Where("user_id = ?", userID).Where("owner = ?", owner)
if len(params.Query) > 0 {
queryPattern := "%" + params.Query + "%"
- query.Where("quantity ILIKE ?", queryPattern)
+ query.Where("version LIKE ?", queryPattern)
}
heartbeats := new([]entities.Heartbeat)
diff --git a/api/pkg/requests/heartbeat_store_request.go b/api/pkg/requests/heartbeat_store_request.go
index d3419a38..233f3688 100644
--- a/api/pkg/requests/heartbeat_store_request.go
+++ b/api/pkg/requests/heartbeat_store_request.go
@@ -20,9 +20,10 @@ func (input *HeartbeatStore) Sanitize() HeartbeatStore {
}
// ToStoreParams converts HeartbeatIndex to repositories.IndexParams
-func (input *HeartbeatStore) ToStoreParams(user entities.AuthUser) services.HeartbeatStoreParams {
+func (input *HeartbeatStore) ToStoreParams(user entities.AuthUser, version string) services.HeartbeatStoreParams {
return services.HeartbeatStoreParams{
Owner: input.Owner,
+ Version: version,
Timestamp: time.Now().UTC(),
UserID: user.ID,
}
diff --git a/api/pkg/services/heartbeat_service.go b/api/pkg/services/heartbeat_service.go
index 6a70dd37..3c98e546 100644
--- a/api/pkg/services/heartbeat_service.go
+++ b/api/pkg/services/heartbeat_service.go
@@ -69,6 +69,7 @@ func (service *HeartbeatService) Index(ctx context.Context, userID entities.User
// HeartbeatStoreParams are parameters for creating a new entities.Heartbeat
type HeartbeatStoreParams struct {
Owner string
+ Version string
Timestamp time.Time
UserID entities.UserID
}