Patch contract by ImeiID #21
@@ -120,8 +120,9 @@ func UpdateContract(contract models.Contract) (models.Contract, int, error) {
|
|||||||
var devices []models.Device
|
var devices []models.Device
|
||||||
var status int
|
var status int
|
||||||
var err error
|
var err error
|
||||||
if contract.DeviceIDs != nil {
|
if contract.DevicesImeis != nil {
|
||||||
devices, status, err = device.GetDevicesByID(contract.DeviceIDs)
|
log.Printf("Found devices imes %v", contract.DevicesImeis)
|
||||||
|
devices, status, err = device.GetDevicesByImei(contract.DevicesImeis)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return contract, status, err
|
return contract, status, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,35 @@ func GetDevicesByID(deviceIDs []int64) ([]models.Device, int, error) {
|
|||||||
return devices, http.StatusOK, nil
|
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) {
|
func GetDeviceInfoForContract(deviceID uint64, contract models.Contract) (models.GeoJSONFeatureCollection, int, error) {
|
||||||
|
|
||||||
var deviceInfos []models.DeviceInfo
|
var deviceInfos []models.DeviceInfo
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ type Contract struct {
|
|||||||
SellerName string `json:"sellerName" gorm:"-"`
|
SellerName string `json:"sellerName" gorm:"-"`
|
||||||
ProductName string `json:"productName" gorm:"-"`
|
ProductName string `json:"productName" gorm:"-"`
|
||||||
NumberOfDevices int `json:"numberOfDevices" gorm:"-"`
|
NumberOfDevices int `json:"numberOfDevices" gorm:"-"`
|
||||||
|
DevicesImeis []string `json:"devicesImeis" gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DashboardContractResponse struct {
|
type DashboardContractResponse struct {
|
||||||
@@ -266,8 +267,8 @@ type Status struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ActiveContractsResponse struct {
|
type ActiveContractsResponse struct {
|
||||||
ActiveCount int64 `json:"active"`
|
ActiveCount int64 `json:"active"`
|
||||||
ExecutedCount int64 `json:"executed"`
|
ExecutedCount int64 `json:"executed"`
|
||||||
MonthlyContracts map[string]map[string]int64 `json:"monthly"`
|
MonthlyContracts map[string]map[string]int64 `json:"monthly"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
{
|
{
|
||||||
"info": {
|
"info": {
|
||||||
"_postman_id": "cae9b5db-2e4f-46c8-9f86-51c530d51c36",
|
"_postman_id": "001ed695-e2e5-49c0-8fab-39516921370b",
|
||||||
"name": "Backend",
|
"name": "Backend",
|
||||||
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json",
|
"schema": "https://schema.getpostman.com/json/collection/v2.1.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"
|
|
||||||
},
|
},
|
||||||
"item": [
|
"item": [
|
||||||
{
|
{
|
||||||
@@ -24,7 +22,16 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"url": "{{URL}}/contracts/create"
|
"url": {
|
||||||
|
"raw": "{{URL}}/contracts/create",
|
||||||
|
"host": [
|
||||||
|
"{{URL}}"
|
||||||
|
],
|
||||||
|
"path": [
|
||||||
|
"contracts",
|
||||||
|
"create"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"response": [
|
"response": [
|
||||||
{
|
{
|
||||||
@@ -41,7 +48,16 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"url": "{{URL}}/contracts/create"
|
"url": {
|
||||||
|
"raw": "{{URL}}/contracts/create",
|
||||||
|
"host": [
|
||||||
|
"{{URL}}"
|
||||||
|
],
|
||||||
|
"path": [
|
||||||
|
"contracts",
|
||||||
|
"create"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"status": "OK",
|
"status": "OK",
|
||||||
"code": 200,
|
"code": 200,
|
||||||
@@ -88,14 +104,23 @@
|
|||||||
"header": [],
|
"header": [],
|
||||||
"body": {
|
"body": {
|
||||||
"mode": "raw",
|
"mode": "raw",
|
||||||
"raw": "{\n \"deviceIds\": [1,2],\n \"status\": \"active\"\n}",
|
"raw": "{\n \"devicesImeis\": [\"352656107000002\",\"352656107000001\"],\n \"status\": \"active\"\n}",
|
||||||
"options": {
|
"options": {
|
||||||
"raw": {
|
"raw": {
|
||||||
"language": "json"
|
"language": "json"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"url": "{{URL}}/contracts/2"
|
"url": {
|
||||||
|
"raw": "{{URL}}/contracts/1",
|
||||||
|
"host": [
|
||||||
|
"{{URL}}"
|
||||||
|
],
|
||||||
|
"path": [
|
||||||
|
"contracts",
|
||||||
|
"1"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"response": [
|
"response": [
|
||||||
{
|
{
|
||||||
@@ -112,7 +137,16 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"url": "{{URL}}/contracts/create"
|
"url": {
|
||||||
|
"raw": "{{URL}}/contracts/create",
|
||||||
|
"host": [
|
||||||
|
"{{URL}}"
|
||||||
|
],
|
||||||
|
"path": [
|
||||||
|
"contracts",
|
||||||
|
"create"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"status": "OK",
|
"status": "OK",
|
||||||
"code": 200,
|
"code": 200,
|
||||||
@@ -157,7 +191,15 @@
|
|||||||
"request": {
|
"request": {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"header": [],
|
"header": [],
|
||||||
"url": "{{URL}}/contracts"
|
"url": {
|
||||||
|
"raw": "{{URL}}/contracts",
|
||||||
|
"host": [
|
||||||
|
"{{URL}}"
|
||||||
|
],
|
||||||
|
"path": [
|
||||||
|
"contracts"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"response": [
|
"response": [
|
||||||
{
|
{
|
||||||
@@ -165,7 +207,15 @@
|
|||||||
"originalRequest": {
|
"originalRequest": {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"header": [],
|
"header": [],
|
||||||
"url": "{{URL}}/contracts"
|
"url": {
|
||||||
|
"raw": "{{URL}}/contracts",
|
||||||
|
"host": [
|
||||||
|
"{{URL}}"
|
||||||
|
],
|
||||||
|
"path": [
|
||||||
|
"contracts"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"status": "OK",
|
"status": "OK",
|
||||||
"code": 200,
|
"code": 200,
|
||||||
@@ -210,7 +260,16 @@
|
|||||||
"request": {
|
"request": {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"header": [],
|
"header": [],
|
||||||
"url": "{{URL}}/contracts/1"
|
"url": {
|
||||||
|
"raw": "{{URL}}/contracts/1",
|
||||||
|
"host": [
|
||||||
|
"{{URL}}"
|
||||||
|
],
|
||||||
|
"path": [
|
||||||
|
"contracts",
|
||||||
|
"1"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"response": [
|
"response": [
|
||||||
{
|
{
|
||||||
@@ -218,7 +277,15 @@
|
|||||||
"originalRequest": {
|
"originalRequest": {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"header": [],
|
"header": [],
|
||||||
"url": "{{URL}}/contracts"
|
"url": {
|
||||||
|
"raw": "{{URL}}/contracts",
|
||||||
|
"host": [
|
||||||
|
"{{URL}}"
|
||||||
|
],
|
||||||
|
"path": [
|
||||||
|
"contracts"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"status": "OK",
|
"status": "OK",
|
||||||
"code": 200,
|
"code": 200,
|
||||||
@@ -263,7 +330,16 @@
|
|||||||
"request": {
|
"request": {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"header": [],
|
"header": [],
|
||||||
"url": "{{URL}}/contracts/statuses"
|
"url": {
|
||||||
|
"raw": "{{URL}}/contracts/statuses",
|
||||||
|
"host": [
|
||||||
|
"{{URL}}"
|
||||||
|
],
|
||||||
|
"path": [
|
||||||
|
"contracts",
|
||||||
|
"statuses"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"response": [
|
"response": [
|
||||||
{
|
{
|
||||||
@@ -271,7 +347,16 @@
|
|||||||
"originalRequest": {
|
"originalRequest": {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"header": [],
|
"header": [],
|
||||||
"url": "{{URL}}/contracts/statuses"
|
"url": {
|
||||||
|
"raw": "{{URL}}/contracts/statuses",
|
||||||
|
"host": [
|
||||||
|
"{{URL}}"
|
||||||
|
],
|
||||||
|
"path": [
|
||||||
|
"contracts",
|
||||||
|
"statuses"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"status": "OK",
|
"status": "OK",
|
||||||
"code": 200,
|
"code": 200,
|
||||||
@@ -508,7 +593,16 @@
|
|||||||
"request": {
|
"request": {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"header": [],
|
"header": [],
|
||||||
"url": "{{URL}}/product_templates/1"
|
"url": {
|
||||||
|
"raw": "{{URL}}/product_templates/1",
|
||||||
|
"host": [
|
||||||
|
"{{URL}}"
|
||||||
|
],
|
||||||
|
"path": [
|
||||||
|
"product_templates",
|
||||||
|
"1"
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"response": [
|
"response": [
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user