Upstream sync
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"gitlab.com/pactual1/backend/database/device"
|
||||
"gitlab.com/pactual1/backend/models"
|
||||
"gitlab.com/pactual1/backend/services/blockchain"
|
||||
"gitlab.com/pactual1/backend/services/messaging"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
@@ -167,21 +168,42 @@ func UpdateContract(contract models.Contract) (models.Contract, int, error) {
|
||||
}
|
||||
|
||||
// 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 oldContract.Status != contract.Status {
|
||||
|
||||
// Register devices in blockchain when contract is signed
|
||||
for _, device := range devices {
|
||||
err = blockchain.NewService(config.AppConfig.Blockchain).RegisterNewDeviceID(context.Background(), shared.CovertUintToByte32(contract.ID), shared.CovertUintToByte32(device.ID))
|
||||
if 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 register contract device in blockchain: %v", err)
|
||||
log.Printf("UpdateContract Error: Could not create contract in blockchain: %v", err)
|
||||
return contract, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
// Register devices in blockchain when contract is signed
|
||||
for _, device := range devices {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
messagingChannel := messaging.GetMessagingChannel()
|
||||
|
||||
notification := models.Notification{
|
||||
Title: "Contract Alert",
|
||||
NotificationType: "Contract",
|
||||
Text: fmt.Sprintf("Contract %s status updated to %s", contract.Name, contract.Status),
|
||||
CompanyID: int(contract.BuyerID),
|
||||
}
|
||||
messagingChannel <- notification
|
||||
|
||||
sellerNotification := models.Notification{
|
||||
Title: "Contract Alert",
|
||||
NotificationType: "Contract",
|
||||
Text: fmt.Sprintf("Contract %s status updated to %s", contract.Name, contract.Status),
|
||||
CompanyID: int(contract.SellerID),
|
||||
}
|
||||
messagingChannel <- sellerNotification
|
||||
}
|
||||
|
||||
return contract, status, err
|
||||
@@ -243,59 +265,59 @@ func GetContractByID(contractID uint) (models.Contract, int, error) {
|
||||
}
|
||||
|
||||
func CountContractsByMultipleStatusesAndTotal(companyID uint, startTime, endTime time.Time) (int64, int64, int64, map[string]map[string]int64, error) {
|
||||
var activeCount, executedCount, totalCount int64
|
||||
monthlyCounts := make(map[string]map[string]int64)
|
||||
var activeCount, executedCount, totalCount int64
|
||||
monthlyCounts := make(map[string]map[string]int64)
|
||||
|
||||
var contract models.Contract
|
||||
var contract models.Contract
|
||||
|
||||
// Iterate through each month within the specified date range
|
||||
for dt := startTime; dt.Before(endTime); dt = dt.AddDate(0, 1, 0) {
|
||||
monthEnd := dt.AddDate(0, 1, 0)
|
||||
if monthEnd.After(endTime) {
|
||||
monthEnd = endTime
|
||||
}
|
||||
// Iterate through each month within the specified date range
|
||||
for dt := startTime; dt.Before(endTime); dt = dt.AddDate(0, 1, 0) {
|
||||
monthEnd := dt.AddDate(0, 1, 0)
|
||||
if monthEnd.After(endTime) {
|
||||
monthEnd = endTime
|
||||
}
|
||||
|
||||
// Initialize monthlyCounts for the current month
|
||||
monthKey := dt.Format("2006-01")
|
||||
monthlyCounts[monthKey] = map[string]int64{"active": 0, "executed": 0, "total": 0}
|
||||
// Initialize monthlyCounts for the current month
|
||||
monthKey := dt.Format("2006-01")
|
||||
monthlyCounts[monthKey] = map[string]int64{"active": 0, "executed": 0, "total": 0}
|
||||
|
||||
// Query for 'active' contracts for the current month
|
||||
var active int64
|
||||
err := shared.GetDb().Model(&contract).
|
||||
Where("status = ? AND (buyer_id = ? OR seller_id = ?) AND start_time >= ? AND end_time <= ?", "active", companyID, companyID, dt, monthEnd).
|
||||
Count(&active).Error
|
||||
if err != nil {
|
||||
return 0, 0, 0, nil, err
|
||||
}
|
||||
// Query for 'active' contracts for the current month
|
||||
var active int64
|
||||
err := shared.GetDb().Model(&contract).
|
||||
Where("status = ? AND (buyer_id = ? OR seller_id = ?) AND start_time >= ? AND end_time <= ?", "active", companyID, companyID, dt, monthEnd).
|
||||
Count(&active).Error
|
||||
if err != nil {
|
||||
return 0, 0, 0, nil, err
|
||||
}
|
||||
|
||||
// Query for 'executed' contracts for the current month
|
||||
var executed int64
|
||||
err = shared.GetDb().Model(&contract).
|
||||
Where("status = ? AND (buyer_id = ? OR seller_id = ?) AND start_time >= ? AND end_time <= ?", "executed", companyID, companyID, dt, monthEnd).
|
||||
Count(&executed).Error
|
||||
if err != nil {
|
||||
return 0, 0, 0, nil, err
|
||||
}
|
||||
// Query for 'executed' contracts for the current month
|
||||
var executed int64
|
||||
err = shared.GetDb().Model(&contract).
|
||||
Where("status = ? AND (buyer_id = ? OR seller_id = ?) AND start_time >= ? AND end_time <= ?", "executed", companyID, companyID, dt, monthEnd).
|
||||
Count(&executed).Error
|
||||
if err != nil {
|
||||
return 0, 0, 0, nil, err
|
||||
}
|
||||
|
||||
// Query for total contracts for the current month
|
||||
var total int64
|
||||
err = shared.GetDb().Model(&contract).
|
||||
Where("(buyer_id = ? OR seller_id = ?) AND start_time >= ? AND end_time <= ?", companyID, companyID, dt, monthEnd).
|
||||
Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, 0, 0, nil, err
|
||||
}
|
||||
// Query for total contracts for the current month
|
||||
var total int64
|
||||
err = shared.GetDb().Model(&contract).
|
||||
Where("(buyer_id = ? OR seller_id = ?) AND start_time >= ? AND end_time <= ?", companyID, companyID, dt, monthEnd).
|
||||
Count(&total).Error
|
||||
if err != nil {
|
||||
return 0, 0, 0, nil, err
|
||||
}
|
||||
|
||||
// Update the counts for the current month
|
||||
monthlyCounts[monthKey]["active"] = active
|
||||
monthlyCounts[monthKey]["executed"] = executed
|
||||
monthlyCounts[monthKey]["total"] = total
|
||||
// Update the counts for the current month
|
||||
monthlyCounts[monthKey]["active"] = active
|
||||
monthlyCounts[monthKey]["executed"] = executed
|
||||
monthlyCounts[monthKey]["total"] = total
|
||||
|
||||
// Update the total counts
|
||||
activeCount += active
|
||||
executedCount += executed
|
||||
totalCount += total
|
||||
}
|
||||
// Update the total counts
|
||||
activeCount += active
|
||||
executedCount += executed
|
||||
totalCount += total
|
||||
}
|
||||
|
||||
return activeCount, executedCount, totalCount, monthlyCounts, nil
|
||||
return activeCount, executedCount, totalCount, monthlyCounts, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user