Return imeis for contract
This commit is contained in:
@@ -27,8 +27,8 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
||||
db := shared.GetDb()
|
||||
countDb := db
|
||||
|
||||
// Define custom fields to be selected, varies based on joined tables
|
||||
customFields := "distinct contracts.*, array_length(contracts.device_ids, 1) as number_of_devices"
|
||||
customFields := "distinct contracts.*, array_length(contracts.device_ids, 1) as number_of_devices, " +
|
||||
"(SELECT array_agg(devices.imei) FROM devices WHERE devices.id = ANY(contracts.device_ids)) as devices_imeis_scanner"
|
||||
|
||||
// Search by Statuses
|
||||
if len(status) > 0 {
|
||||
@@ -87,8 +87,8 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
||||
|
||||
// Search by Device IDs
|
||||
if len(deviceIDs) > 0 {
|
||||
db = db.Where("device_ids && ?", pq.Array(deviceIDs))
|
||||
countDb = countDb.Where("device_ids && ?", pq.Array(deviceIDs))
|
||||
db = db.Where("contracts.device_ids && ?", pq.Array(deviceIDs))
|
||||
countDb = countDb.Where("contracts.device_ids && ?", pq.Array(deviceIDs))
|
||||
}
|
||||
|
||||
// Fetch total count of filtered records
|
||||
@@ -112,6 +112,10 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
||||
return contracts, total, http.StatusInternalServerError, err
|
||||
}
|
||||
}
|
||||
for i := range contracts {
|
||||
contracts[i].DevicesImeis = []string(contracts[i].DevicesImeisScanner)
|
||||
// Now DevicesImeis contains the IMEIs, and you can use them as needed
|
||||
}
|
||||
|
||||
return contracts, total, http.StatusOK, nil
|
||||
}
|
||||
@@ -262,6 +266,32 @@ func GetContractByID(contractID uint) (models.Contract, int, error) {
|
||||
}
|
||||
contract.BuyerName = buyer.Name
|
||||
|
||||
// Fetch the devices
|
||||
// Convert DeviceIDs from pq.Int64Array to []int64 for the query
|
||||
deviceIDs := make([]int64, 0, len(contract.DeviceIDs))
|
||||
for _, id := range contract.DeviceIDs {
|
||||
deviceIDs = append(deviceIDs, int64(id))
|
||||
}
|
||||
|
||||
// Fetch the devices using the slice of device IDs
|
||||
var devicesInfo []models.ContractDeviceInfo
|
||||
if err := shared.GetDb().
|
||||
Table("devices"). // Specify the actual table name for devices
|
||||
Where("id IN (?)", deviceIDs).
|
||||
Select("id, imei").
|
||||
Find(&devicesInfo).Error; err != nil {
|
||||
log.Printf("GetContractByID Error: Could not fetch devices: %v", err)
|
||||
return contract, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
// Prepare a slice of IMEIs to add to the contract
|
||||
deviceImeis := make([]string, len(devicesInfo))
|
||||
for i, device := range devicesInfo {
|
||||
deviceImeis[i] = device.IMEI
|
||||
}
|
||||
|
||||
contract.DevicesImeis = deviceImeis
|
||||
|
||||
return contract, http.StatusOK, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user