Upstream sync

This commit is contained in:
Senad Uka
2023-10-16 05:44:09 +02:00
parent d01c1a0232
commit 533451f39d
3 changed files with 99 additions and 47 deletions

View File

@@ -2,12 +2,14 @@ package contract
import (
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/jinzhu/gorm"
"github.com/lib/pq"
"gitlab.com/pactual1/backend/database/device"
"gitlab.com/pactual1/backend/models"
"gitlab.com/pactual1/backend/shared"
)
@@ -110,26 +112,76 @@ func GetContracts(status []string, companyName string, companyAddress string,
}
func UpdateContract(contract models.Contract) (models.Contract, int, error) {
var devices []models.Device
var status int
var err error
if contract.DeviceIDs != nil {
devices, status, err = device.GetDevicesByID(contract.DeviceIDs)
if err != nil {
return contract, status, err
}
// Update contract
if err := shared.GetDb().Model(contract).Updates(contract).Error; err != nil {
log.Printf("UpdateContractByID Error: Could not update contract: %v", err)
status, err = validateContractDevices(contract.ID, devices)
if err != nil {
log.Printf("UpdateContract Error: Invalid Device ID: %v", err)
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 {
log.Printf("UpdateContract Error: Could not update contract: %v", err)
return err
}
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 {
log.Printf("UpdateContract Error: Could not update devices: %v", err)
return err
}
}
// return nil will commit the whole transaction
return nil
})
if err != nil {
log.Printf("UpdateContract Error: Could not update contract: %v", err)
return contract, http.StatusInternalServerError, err
}
return GetContractByID(uint64(contract.ID))
}
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))
if err != nil {
return status, err
}
if currentDeviceContract.Status != models.ContractStatusExecuted &&
currentDeviceContract.Status == models.ContractStatusRevoked {
return http.StatusBadRequest, fmt.Errorf("device id %d is linked to contract id - %d name %s", device.ID, currentDeviceContract.ID, currentDeviceContract.Name)
}
}
}
return http.StatusOK, nil
}
func GetContractByID(contractID uint64) (models.Contract, int, error) {
// Fetch the contract creation date based on contractID
var contract models.Contract
if err := shared.GetDb().Where("id = ?", contractID).First(&contract).Error; err != nil {
if err := shared.GetDb().Unscoped().Where("id = ?", contractID).First(&contract).Error; err != nil {
log.Printf("GetContractByID Error: Could not fetch contract: %v", err)
return contract, http.StatusInternalServerError, err
}
return contract, http.StatusOK, nil
}
}