diff --git a/controllers/contracts_controller.go b/controllers/contracts_controller.go index 8719aaa..c215b3e 100644 --- a/controllers/contracts_controller.go +++ b/controllers/contracts_controller.go @@ -16,7 +16,7 @@ func GetLatestContracts(c *gin.Context) { // Get limit and offset from query parameter with defaults limitStr := c.DefaultQuery("limit", "99999999999999999999999999999999") offsetStr := c.DefaultQuery("offset", "0") - status := c.DefaultQuery("status", "active") + status := c.DefaultQuery("status", models.ContractStatusActive) // Convert limit and offset to int limit, err := strconv.Atoi(limitStr) diff --git a/controllers/devices_controller.go b/controllers/devices_controller.go index 3d2ba64..99091cc 100644 --- a/controllers/devices_controller.go +++ b/controllers/devices_controller.go @@ -115,9 +115,22 @@ func GetDeviceData(c *gin.Context) { return } + deviceConnectedToContract := false + for _, contractDeviceID := range contract.DeviceIDs { + if deviceID == uint64(contractDeviceID) { + deviceConnectedToContract = true + } + } + + if !deviceConnectedToContract { + log.Printf("Device %v is not connected to contract %v", deviceID, contractID ) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Device is not present int his contract"}) + return + } + var deviceInfos []models.DeviceInfo // Modify your query to include filtering based on the contract's creation date - query := shared.GetDb().Where("device_id = ?", deviceID).Where("start_time >= ? AND end_time <= ?", contract.StartTime,contract.EndTime) + query := shared.GetDb().Where("device_id = ?", deviceID).Where("created_at >= ? AND created_at <= ?", contract.StartTime,contract.EndTime) if err := query.Find(&deviceInfos).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) {