diff --git a/database/contract/contract.go b/database/contract/contract.go index 3d7f1e7..6f5c56c 100644 --- a/database/contract/contract.go +++ b/database/contract/contract.go @@ -120,8 +120,9 @@ func UpdateContract(contract models.Contract) (models.Contract, int, error) { var devices []models.Device var status int var err error - if contract.DeviceIDs != nil { - devices, status, err = device.GetDevicesByID(contract.DeviceIDs) + if contract.DevicesImeis != nil { + log.Printf("Found devices imes %v", contract.DevicesImeis) + devices, status, err = device.GetDevicesByImei(contract.DevicesImeis) if err != nil { return contract, status, err } diff --git a/database/device/device.go b/database/device/device.go index adf0c8f..a6658be 100644 --- a/database/device/device.go +++ b/database/device/device.go @@ -59,6 +59,35 @@ func GetDevicesByID(deviceIDs []int64) ([]models.Device, int, error) { return devices, http.StatusOK, nil } +func GetDevicesByImei(imeiIDs []string) ([]models.Device, int, error) { + // Create a slice to hold the devices + var devices []models.Device + + // Quote each IMEI ID + for i, imei := range imeiIDs { + imeiIDs[i] = "'" + imei + "'" + } + + // Join the quoted IMEI IDs into a comma-separated string + idStr := strings.Join(imeiIDs, ",") + + // Construct the SQL query manually + sqlQuery := fmt.Sprintf("SELECT * FROM devices WHERE imei IN (%s)", idStr) + + // Fetch devices from the database based on DeviceIDs in the contract + // Execute the query + if err := shared.GetDb().Raw(sqlQuery).Scan(&devices).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + log.Printf("GetDevicesByContract Error: No devices found: %v", err) + return devices, http.StatusNotFound, err + } else { + log.Printf("GetDevicesByContract Error: Database error: %v", err) + return devices, http.StatusInternalServerError, err + } + } + return devices, http.StatusOK, nil +} + func GetDeviceInfoForContract(deviceID uint64, contract models.Contract) (models.GeoJSONFeatureCollection, int, error) { var deviceInfos []models.DeviceInfo diff --git a/models/contract.go b/models/contract.go index 7055cfa..210d2ca 100644 --- a/models/contract.go +++ b/models/contract.go @@ -36,6 +36,7 @@ type Contract struct { SellerName string `json:"sellerName" gorm:"-"` ProductName string `json:"productName" gorm:"-"` NumberOfDevices int `json:"numberOfDevices" gorm:"-"` + DevicesImeis []string `json:"devicesImeis" gorm:"-"` } type DashboardContractResponse struct { @@ -266,8 +267,8 @@ type Status struct { } type ActiveContractsResponse struct { - ActiveCount int64 `json:"active"` - ExecutedCount int64 `json:"executed"` + ActiveCount int64 `json:"active"` + ExecutedCount int64 `json:"executed"` MonthlyContracts map[string]map[string]int64 `json:"monthly"` } diff --git a/postman/Backend.postman_collection.json b/postman/Backend.postman_collection.json index 3ac8494..18f9c52 100644 --- a/postman/Backend.postman_collection.json +++ b/postman/Backend.postman_collection.json @@ -1,10 +1,8 @@ { "info": { - "_postman_id": "cae9b5db-2e4f-46c8-9f86-51c530d51c36", + "_postman_id": "001ed695-e2e5-49c0-8fab-39516921370b", "name": "Backend", - "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json", - "_exporter_id": "29923405", - "_collection_link": "https://pactual-toptal.postman.co/workspace/pactual-toptal-Workspace~afe12f79-75cd-428c-8cbd-e47c5f2cad3c/collection/29923405-cae9b5db-2e4f-46c8-9f86-51c530d51c36?action=share&source=collection_link&creator=29923405" + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ { @@ -24,7 +22,16 @@ } } }, - "url": "{{URL}}/contracts/create" + "url": { + "raw": "{{URL}}/contracts/create", + "host": [ + "{{URL}}" + ], + "path": [ + "contracts", + "create" + ] + } }, "response": [ { @@ -41,7 +48,16 @@ } } }, - "url": "{{URL}}/contracts/create" + "url": { + "raw": "{{URL}}/contracts/create", + "host": [ + "{{URL}}" + ], + "path": [ + "contracts", + "create" + ] + } }, "status": "OK", "code": 200, @@ -88,14 +104,23 @@ "header": [], "body": { "mode": "raw", - "raw": "{\n \"deviceIds\": [1,2],\n \"status\": \"active\"\n}", + "raw": "{\n \"devicesImeis\": [\"352656107000002\",\"352656107000001\"],\n \"status\": \"active\"\n}", "options": { "raw": { "language": "json" } } }, - "url": "{{URL}}/contracts/2" + "url": { + "raw": "{{URL}}/contracts/1", + "host": [ + "{{URL}}" + ], + "path": [ + "contracts", + "1" + ] + } }, "response": [ { @@ -112,7 +137,16 @@ } } }, - "url": "{{URL}}/contracts/create" + "url": { + "raw": "{{URL}}/contracts/create", + "host": [ + "{{URL}}" + ], + "path": [ + "contracts", + "create" + ] + } }, "status": "OK", "code": 200, @@ -157,7 +191,15 @@ "request": { "method": "GET", "header": [], - "url": "{{URL}}/contracts" + "url": { + "raw": "{{URL}}/contracts", + "host": [ + "{{URL}}" + ], + "path": [ + "contracts" + ] + } }, "response": [ { @@ -165,7 +207,15 @@ "originalRequest": { "method": "GET", "header": [], - "url": "{{URL}}/contracts" + "url": { + "raw": "{{URL}}/contracts", + "host": [ + "{{URL}}" + ], + "path": [ + "contracts" + ] + } }, "status": "OK", "code": 200, @@ -210,7 +260,16 @@ "request": { "method": "GET", "header": [], - "url": "{{URL}}/contracts/1" + "url": { + "raw": "{{URL}}/contracts/1", + "host": [ + "{{URL}}" + ], + "path": [ + "contracts", + "1" + ] + } }, "response": [ { @@ -218,7 +277,15 @@ "originalRequest": { "method": "GET", "header": [], - "url": "{{URL}}/contracts" + "url": { + "raw": "{{URL}}/contracts", + "host": [ + "{{URL}}" + ], + "path": [ + "contracts" + ] + } }, "status": "OK", "code": 200, @@ -263,7 +330,16 @@ "request": { "method": "GET", "header": [], - "url": "{{URL}}/contracts/statuses" + "url": { + "raw": "{{URL}}/contracts/statuses", + "host": [ + "{{URL}}" + ], + "path": [ + "contracts", + "statuses" + ] + } }, "response": [ { @@ -271,7 +347,16 @@ "originalRequest": { "method": "GET", "header": [], - "url": "{{URL}}/contracts/statuses" + "url": { + "raw": "{{URL}}/contracts/statuses", + "host": [ + "{{URL}}" + ], + "path": [ + "contracts", + "statuses" + ] + } }, "status": "OK", "code": 200, @@ -508,7 +593,16 @@ "request": { "method": "GET", "header": [], - "url": "{{URL}}/product_templates/1" + "url": { + "raw": "{{URL}}/product_templates/1", + "host": [ + "{{URL}}" + ], + "path": [ + "product_templates", + "1" + ] + } }, "response": [ {