Return imeis for contract

This commit is contained in:
Nedim
2023-11-08 22:49:52 +01:00
parent 5296edc4a7
commit 3248359726
4 changed files with 168 additions and 197 deletions

View File

@@ -1,6 +1,8 @@
package models
import (
"errors"
"strings"
"time"
"github.com/lib/pq"
@@ -8,35 +10,66 @@ import (
type Contract struct {
BaseModel
Name string `json:"name"`
DeviceIDs pq.Int64Array `json:"deviceIds" gorm:"type:integer[]"`
BuyerID uint `json:"buyerId"`
SellerID uint `json:"sellerId"`
Description string `json:"description"`
StartPlaceName string `json:"startPlaceName"`
StartLat float64 `json:"startLat"`
StartLon float64 `json:"startLon"`
EndPlaceName string `json:"endPlaceName"`
EndLat float64 `json:"endLat"`
EndLon float64 `json:"endLon"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
Status string `json:"status"`
BlockchainSecret string `json:"blockchainSecret"`
ContractInfos []ContractInfo `json:"contractInfos"`
ProductID uint `json:"productId"`
TemplateID uint `json:"templateId"`
MaxTemp float64 `json:"maxTemp"`
MinTemp float64 `json:"minTemp"`
ArrivalDate time.Time `json:"arrivalDate"`
PenaltyType string `json:"penaltyType"`
PenaltyValue int `json:"penaltyValue"`
PenaltyRec string `json:"penaltyRec"`
BuyerName string `json:"buyerName" gorm:"-"`
SellerName string `json:"sellerName" gorm:"-"`
ProductName string `json:"productName" gorm:"-"`
NumberOfDevices int `json:"numberOfDevices" gorm:"-"`
DevicesImeis []string `json:"devicesImeis" gorm:"-"`
Name string `json:"name"`
DeviceIDs pq.Int64Array `json:"deviceIds" gorm:"type:integer[]"`
BuyerID uint `json:"buyerId"`
SellerID uint `json:"sellerId"`
Description string `json:"description"`
StartPlaceName string `json:"startPlaceName"`
StartLat float64 `json:"startLat"`
StartLon float64 `json:"startLon"`
EndPlaceName string `json:"endPlaceName"`
EndLat float64 `json:"endLat"`
EndLon float64 `json:"endLon"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
Status string `json:"status"`
BlockchainSecret string `json:"blockchainSecret"`
ContractInfos []ContractInfo `json:"contractInfos"`
ProductID uint `json:"productId"`
TemplateID uint `json:"templateId"`
MaxTemp float64 `json:"maxTemp"`
MinTemp float64 `json:"minTemp"`
ArrivalDate time.Time `json:"arrivalDate"`
PenaltyType string `json:"penaltyType"`
PenaltyValue int `json:"penaltyValue"`
PenaltyRec string `json:"penaltyRec"`
BuyerName string `json:"buyerName" gorm:"-"`
SellerName string `json:"sellerName" gorm:"-"`
ProductName string `json:"productName" gorm:"-"`
NumberOfDevices int `json:"numberOfDevices" gorm:"-"`
DevicesImeis []string `json:"devicesImeis" gorm:"-"`
DevicesImeisScanner PQStringArray `json:"contractDevicesImeis" gorm:"-"`
}
type PQStringArray []string
func (a *PQStringArray) Scan(src interface{}) error {
if src == nil {
*a = nil
return nil
}
// The database driver will give us a string in the format "{item1,item2,item3}"
// We need to convert this to a slice
var str string
switch src := src.(type) {
case []byte:
str = string(src)
case string:
str = src
default:
return errors.New("incompatible type for PQStringArray")
}
// Remove the curly braces and split the string
str = strings.Trim(str, "{}")
if str != "" {
*a = strings.Split(str, ",")
} else {
*a = []string{}
}
return nil
}
type DashboardContractResponse struct {
@@ -104,6 +137,7 @@ type ContractResponse struct {
PenaltyType string `json:"penaltyType"`
PenaltyValue int `json:"penaltyValue"`
PenaltyRec string `json:"penaltyRec"`
DeviceImeis []string `json:"devicesImeis"`
}
func ConvertContractsToContractResponse(contracts []Contract) []ContractResponse {
@@ -173,6 +207,7 @@ func ConvertContractToContractResponse(contract Contract) ContractResponse {
PenaltyType: contract.PenaltyType,
PenaltyValue: contract.PenaltyValue,
PenaltyRec: contract.PenaltyRec,
DeviceImeis: contract.DevicesImeis,
}
return contractResponse
@@ -240,6 +275,7 @@ func ConvertContractToListResponse(contracts []Contract) []ListContractResponse
ContractName: contract.Name,
DateCreated: contract.CreatedAt,
NumberOfDevices: contract.NumberOfDevices,
DeviceImeis: contract.DevicesImeis,
}
listInvoiceResponses = append(listInvoiceResponses, listInvoiceResponse)
}
@@ -291,6 +327,7 @@ type ListContractResponse struct {
ContractName string `json:"contractName"`
NumberOfDevices int `json:"numberOfDevices"`
DateCreated time.Time `json:"dateCreated"`
DeviceImeis []string `json:"deviceImeis"`
}
type CreateContractRequestPayload struct {

View File

@@ -27,6 +27,11 @@ type DeviceResponse struct {
DeviceInfos *[]DeviceInfo `json:"deviceInfos"`
}
type ContractDeviceInfo struct {
ID uint `json:"id"`
IMEI string `json:"imei"`
}
func ConvertDeviceToResponse(devices []Device) []DeviceResponse {
var deviceResponses []DeviceResponse
for _, device := range devices {