Upstream sync

This commit is contained in:
Senad Uka
2023-10-24 05:48:19 +02:00
parent 2c267b71e4
commit fd880aeaa2
8 changed files with 83 additions and 77 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/jinzhu/gorm"
@@ -63,8 +64,8 @@ func GetContracts(status []string, companyName string, companyAddress string,
// Search by Contract Name
if contractName != "" {
db = db.Where("contracts.name LIKE ?", "%"+contractName+"%")
countDb = countDb.Where("contracts.name LIKE ?", "%"+contractName+"%")
db = db.Where("lower(contracts.name) LIKE ?", "%"+strings.ToLower(contractName)+"%")
countDb = countDb.Where("lower(contracts.name) LIKE ?", "%"+strings.ToLower(contractName)+"%")
}
// Search by Start Time and End Time
@@ -146,7 +147,7 @@ func UpdateContract(contract models.Contract) (models.Contract, int, error) {
if devices != nil {
// Update devices
if err := tx.Model(models.Device{}).Where("id IN (?)", contract.DeviceIDs).Updates(models.Device{CurrentContractID: &contract.ID}).Error; err != nil {
if err := tx.Model(models.Device{}).Where("id IN (?)", []int64(contract.DeviceIDs)).Updates(models.Device{CurrentContractID: &contract.ID}).Error; err != nil {
log.Printf("UpdateContract Error: Could not update devices: %v", err)
return err
}
@@ -172,24 +173,13 @@ func UpdateContract(contract models.Contract) (models.Contract, int, error) {
log.Printf("UpdateContract Error: Could not create contract in blockchain: %v", err)
return contract, http.StatusInternalServerError, err
}
}
if contract.Status == models.ContractStatusSigned {
// Register devices in blockchain if contract is signed
// Register devices in blockchain when contract is signed
for _, device := range devices {
var found bool
for _, deviceID := range oldContract.DeviceIDs {
if device.ID == uint(deviceID) {
found = true
break
}
}
if !found {
err = blockchain.NewService(config.AppConfig.Blockchain).RegisterNewDeviceID(context.Background(), shared.CovertUintToByte32(contract.ID), shared.CovertUintToByte32(device.ID))
if err != nil {
log.Printf("UpdateContract Error: Could not register contract device in blockchain: %v", err)
return contract, http.StatusInternalServerError, err
}
err = blockchain.NewService(config.AppConfig.Blockchain).RegisterNewDeviceID(context.Background(), shared.CovertUintToByte32(contract.ID), shared.CovertUintToByte32(device.ID))
if err != nil {
log.Printf("UpdateContract Error: Could not register contract device in blockchain: %v", err)
return contract, http.StatusInternalServerError, err
}
}
}