upstream sync
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -9,8 +10,10 @@ import (
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/lib/pq"
|
||||
"gitlab.com/pactual1/backend/config"
|
||||
"gitlab.com/pactual1/backend/database/device"
|
||||
"gitlab.com/pactual1/backend/models"
|
||||
"gitlab.com/pactual1/backend/services/blockchain"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
@@ -128,6 +131,12 @@ func UpdateContract(contract models.Contract) (models.Contract, int, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// get old contract to compare updates
|
||||
oldContract, status, err := GetContractByID(contract.ID)
|
||||
if err != nil {
|
||||
return contract, status, err
|
||||
}
|
||||
|
||||
err = shared.GetDb().Transaction(func(tx *gorm.DB) error {
|
||||
// Update contract
|
||||
if err := tx.Model(contract).Updates(contract).Error; err != nil {
|
||||
@@ -151,14 +160,48 @@ func UpdateContract(contract models.Contract) (models.Contract, int, error) {
|
||||
return contract, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return GetContractByID(uint64(contract.ID))
|
||||
contract, status, err = GetContractByID(contract.ID)
|
||||
if err != nil {
|
||||
return contract, status, err
|
||||
}
|
||||
|
||||
// Create contract in blockchain only when it is signed
|
||||
if oldContract.Status != contract.Status && contract.Status == models.ContractStatusSigned {
|
||||
err = blockchain.NewService(config.AppConfig.Blockchain).CreateContract(context.Background(), shared.CovertUintToByte32(contract.ID))
|
||||
if err != nil {
|
||||
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
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return contract, status, err
|
||||
}
|
||||
|
||||
func validateContractDevices(contractID uint, devices []models.Device) (int, error) {
|
||||
|
||||
for _, device := range devices {
|
||||
if device.CurrentContractID != nil && *device.CurrentContractID != contractID {
|
||||
currentDeviceContract, status, err := GetContractByID(uint64(*device.CurrentContractID))
|
||||
currentDeviceContract, status, err := GetContractByID(*device.CurrentContractID)
|
||||
if err != nil {
|
||||
return status, err
|
||||
}
|
||||
@@ -173,7 +216,7 @@ func validateContractDevices(contractID uint, devices []models.Device) (int, err
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
func GetContractByID(contractID uint64) (models.Contract, int, error) {
|
||||
func GetContractByID(contractID uint) (models.Contract, int, error) {
|
||||
|
||||
// Fetch the contract creation date based on contractID
|
||||
var contract models.Contract
|
||||
|
||||
Reference in New Issue
Block a user