Upstream sync
This commit is contained in:
@@ -25,7 +25,6 @@ func Load() error {
|
|||||||
// 9000 DEFAULT FOR DEV ENVIRONMENT
|
// 9000 DEFAULT FOR DEV ENVIRONMENT
|
||||||
Port: getEnv("NOVATECH_SERVICE_PORT", "9000"),
|
Port: getEnv("NOVATECH_SERVICE_PORT", "9000"),
|
||||||
Environment: getEnv("NOVATECH_SERVICE_ENVIRONMENT", "DEV"),
|
Environment: getEnv("NOVATECH_SERVICE_ENVIRONMENT", "DEV"),
|
||||||
BlockchainSecret: getEnv("NOVATECH_SERVICE_BLOCKCHAIN_SECRET", "novatech_service_blockchain_secret"),
|
|
||||||
MapboxAccessToken: getEnv("NOVATECH_SERVICE_MAPBOX_ACCESS_TOKEN", ""),
|
MapboxAccessToken: getEnv("NOVATECH_SERVICE_MAPBOX_ACCESS_TOKEN", ""),
|
||||||
},
|
},
|
||||||
AdminService: Service{
|
AdminService: Service{
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ type Service struct {
|
|||||||
Port string
|
Port string
|
||||||
Environment string
|
Environment string
|
||||||
WebPageURL string
|
WebPageURL string
|
||||||
BlockchainSecret string
|
|
||||||
MapboxAccessToken string
|
MapboxAccessToken string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ import (
|
|||||||
"gitlab.com/pactual1/backend/shared"
|
"gitlab.com/pactual1/backend/shared"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
BlockchainSecretLength = 16
|
||||||
|
)
|
||||||
|
|
||||||
func GetLatestContracts(c *gin.Context) {
|
func GetLatestContracts(c *gin.Context) {
|
||||||
// Existing parameters
|
// Existing parameters
|
||||||
limitStr := c.DefaultQuery("limit", "50")
|
limitStr := c.DefaultQuery("limit", "50")
|
||||||
@@ -200,24 +204,25 @@ func CreateContract(c *gin.Context) {
|
|||||||
db := shared.GetDb()
|
db := shared.GetDb()
|
||||||
|
|
||||||
newContract := models.Contract{
|
newContract := models.Contract{
|
||||||
BuyerID: payload.BuyerID,
|
BuyerID: payload.BuyerID,
|
||||||
SellerID: payload.SellerID,
|
SellerID: payload.SellerID,
|
||||||
Description: payload.Description,
|
Description: payload.Description,
|
||||||
ProductID: payload.ProductID,
|
ProductID: payload.ProductID,
|
||||||
MinTemp: payload.MinTemp,
|
MinTemp: payload.MinTemp,
|
||||||
MaxTemp: payload.MaxTemp,
|
MaxTemp: payload.MaxTemp,
|
||||||
ArrivalDate: time.Unix(payload.ArrivalDate, 0),
|
ArrivalDate: time.Unix(payload.ArrivalDate, 0),
|
||||||
PenaltyType: payload.PenaltyType,
|
PenaltyType: payload.PenaltyType,
|
||||||
PenaltyValue: payload.PenaltyValue,
|
PenaltyValue: payload.PenaltyValue,
|
||||||
PenaltyRec: payload.PenaltyRec,
|
PenaltyRec: payload.PenaltyRec,
|
||||||
StartLat: payload.StartLat,
|
StartLat: payload.StartLat,
|
||||||
StartLon: payload.StartLon,
|
StartLon: payload.StartLon,
|
||||||
Status: models.ContractStatusDraft,
|
Status: models.ContractStatusPending,
|
||||||
StartPlaceName: payload.StartPlaceName,
|
StartPlaceName: payload.StartPlaceName,
|
||||||
EndPlaceName: payload.EndPlaceName,
|
EndPlaceName: payload.EndPlaceName,
|
||||||
Name: payload.Name,
|
Name: payload.Name,
|
||||||
EndLat: payload.EndLat,
|
EndLat: payload.EndLat,
|
||||||
EndLon: payload.EndLon,
|
EndLon: payload.EndLon,
|
||||||
|
BlockchainSecret: shared.GenerateRandomString(BlockchainSecretLength),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.Create(&newContract).Error; err != nil {
|
if err := db.Create(&newContract).Error; err != nil {
|
||||||
|
|||||||
@@ -46,7 +46,16 @@ func SaveDeviceInfo(c *gin.Context) {
|
|||||||
|
|
||||||
if deviceContract.Status == models.ContractStatusActive {
|
if deviceContract.Status == models.ContractStatusActive {
|
||||||
deviceInfoBytes, _ := json.Marshal(deviceInfo)
|
deviceInfoBytes, _ := json.Marshal(deviceInfo)
|
||||||
deviceInfoEncryptedStr, err := shared.NewEncryptionClient(config.AppConfig.Service.BlockchainSecret).Encrypt(string(deviceInfoBytes))
|
if deviceContract.BlockchainSecret == "" {
|
||||||
|
deviceContract.BlockchainSecret = shared.GenerateRandomString(BlockchainSecretLength)
|
||||||
|
_, _, err := contract.UpdateContract(deviceContract)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("SaveDeviceInfo Update Contract error: %v", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not update contract secret"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
deviceInfoEncryptedStr, err := shared.NewEncryptionClient(deviceContract.BlockchainSecret).Encrypt(string(deviceInfoBytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("SaveDeviceInfo - Enrypt error : %v", err)
|
log.Printf("SaveDeviceInfo - Enrypt error : %v", err)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not encrypt device info"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not encrypt device info"})
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
@@ -63,8 +64,8 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
|||||||
|
|
||||||
// Search by Contract Name
|
// Search by Contract Name
|
||||||
if contractName != "" {
|
if contractName != "" {
|
||||||
db = db.Where("contracts.name LIKE ?", "%"+contractName+"%")
|
db = db.Where("lower(contracts.name) LIKE ?", "%"+strings.ToLower(contractName)+"%")
|
||||||
countDb = countDb.Where("contracts.name LIKE ?", "%"+contractName+"%")
|
countDb = countDb.Where("lower(contracts.name) LIKE ?", "%"+strings.ToLower(contractName)+"%")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search by Start Time and End Time
|
// Search by Start Time and End Time
|
||||||
@@ -146,7 +147,7 @@ func UpdateContract(contract models.Contract) (models.Contract, int, error) {
|
|||||||
|
|
||||||
if devices != nil {
|
if devices != nil {
|
||||||
// Update devices
|
// 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)
|
log.Printf("UpdateContract Error: Could not update devices: %v", err)
|
||||||
return 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)
|
log.Printf("UpdateContract Error: Could not create contract in blockchain: %v", err)
|
||||||
return contract, http.StatusInternalServerError, err
|
return contract, http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if contract.Status == models.ContractStatusSigned {
|
// Register devices in blockchain when contract is signed
|
||||||
// Register devices in blockchain if contract is signed
|
|
||||||
for _, device := range devices {
|
for _, device := range devices {
|
||||||
var found bool
|
err = blockchain.NewService(config.AppConfig.Blockchain).RegisterNewDeviceID(context.Background(), shared.CovertUintToByte32(contract.ID), shared.CovertUintToByte32(device.ID))
|
||||||
for _, deviceID := range oldContract.DeviceIDs {
|
if err != nil {
|
||||||
if device.ID == uint(deviceID) {
|
log.Printf("UpdateContract Error: Could not register contract device in blockchain: %v", err)
|
||||||
found = true
|
return contract, http.StatusInternalServerError, err
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
main.go
7
main.go
@@ -8,8 +8,6 @@ import (
|
|||||||
"gitlab.com/pactual1/backend/config"
|
"gitlab.com/pactual1/backend/config"
|
||||||
"gitlab.com/pactual1/backend/models"
|
"gitlab.com/pactual1/backend/models"
|
||||||
"gitlab.com/pactual1/backend/routes"
|
"gitlab.com/pactual1/backend/routes"
|
||||||
"gitlab.com/pactual1/backend/services/blockchain"
|
|
||||||
"gitlab.com/pactual1/backend/services/contract"
|
|
||||||
"gitlab.com/pactual1/backend/services/erp"
|
"gitlab.com/pactual1/backend/services/erp"
|
||||||
"gitlab.com/pactual1/backend/services/messaging"
|
"gitlab.com/pactual1/backend/services/messaging"
|
||||||
"gitlab.com/pactual1/backend/shared"
|
"gitlab.com/pactual1/backend/shared"
|
||||||
@@ -75,11 +73,6 @@ func main() {
|
|||||||
go messagingService.MessagingService()
|
go messagingService.MessagingService()
|
||||||
go erp.ERPService(erpChannel)
|
go erp.ERPService(erpChannel)
|
||||||
|
|
||||||
encryptionClient := shared.NewEncryptionClient(config.AppConfig.Service.BlockchainSecret)
|
|
||||||
blockchainClient := blockchain.NewService(config.AppConfig.Blockchain)
|
|
||||||
contractService := contract.NewService(contractChannel, shared.GetDb(), encryptionClient, blockchainClient)
|
|
||||||
go contractService.ContractService()
|
|
||||||
|
|
||||||
// Sending messages via channels
|
// Sending messages via channels
|
||||||
// messagingChannel <- "Send an email"
|
// messagingChannel <- "Send an email"
|
||||||
erpChannel <- "Update ERP record"
|
erpChannel <- "Update ERP record"
|
||||||
|
|||||||
@@ -8,34 +8,34 @@ import (
|
|||||||
|
|
||||||
type Contract struct {
|
type Contract struct {
|
||||||
BaseModel
|
BaseModel
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
DeviceIDs pq.Int64Array `json:"deviceIds" gorm:"type:integer[]"`
|
DeviceIDs pq.Int64Array `json:"deviceIds" gorm:"type:integer[]"`
|
||||||
BuyerID uint `json:"buyerId"`
|
BuyerID uint `json:"buyerId"`
|
||||||
SellerID uint `json:"sellerId"`
|
SellerID uint `json:"sellerId"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
StartPlaceName string `json:"startPlaceName"`
|
StartPlaceName string `json:"startPlaceName"`
|
||||||
StartLat float64 `json:"startLat"`
|
StartLat float64 `json:"startLat"`
|
||||||
StartLon float64 `json:"startLon"`
|
StartLon float64 `json:"startLon"`
|
||||||
EndPlaceName string `json:"endPlaceName"`
|
EndPlaceName string `json:"endPlaceName"`
|
||||||
EndLat float64 `json:"endLat"`
|
EndLat float64 `json:"endLat"`
|
||||||
EndLon float64 `json:"endLon"`
|
EndLon float64 `json:"endLon"`
|
||||||
StartTime time.Time `json:"startTime"`
|
StartTime time.Time `json:"startTime"`
|
||||||
EndTime time.Time `json:"endTime"`
|
EndTime time.Time `json:"endTime"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
BlockchainID string `json:"blockchainId"`
|
BlockchainSecret string `json:"blockchainSecret"`
|
||||||
ContractInfos []ContractInfo `json:"contractInfos"`
|
ContractInfos []ContractInfo `json:"contractInfos"`
|
||||||
ProductID uint `json:"productId"`
|
ProductID uint `json:"productId"`
|
||||||
TemplateID uint `json:"templateId"`
|
TemplateID uint `json:"templateId"`
|
||||||
MaxTemp float64 `json:"maxTemp"`
|
MaxTemp float64 `json:"maxTemp"`
|
||||||
MinTemp float64 `json:"minTemp"`
|
MinTemp float64 `json:"minTemp"`
|
||||||
ArrivalDate time.Time `json:"arrivalDate"`
|
ArrivalDate time.Time `json:"arrivalDate"`
|
||||||
PenaltyType string `json:"penaltyType"`
|
PenaltyType string `json:"penaltyType"`
|
||||||
PenaltyValue int `json:"penaltyValue"`
|
PenaltyValue int `json:"penaltyValue"`
|
||||||
PenaltyRec string `json:"penaltyRec"`
|
PenaltyRec string `json:"penaltyRec"`
|
||||||
BuyerName string `json:"buyerName" gorm:"-"`
|
BuyerName string `json:"buyerName" gorm:"-"`
|
||||||
SellerName string `json:"sellerName" gorm:"-"`
|
SellerName string `json:"sellerName" gorm:"-"`
|
||||||
ProductName string `json:"productName" gorm:"-"`
|
ProductName string `json:"productName" gorm:"-"`
|
||||||
NumberOfDevices int `json:"numberOfDevices" gorm:"-"`
|
NumberOfDevices int `json:"numberOfDevices" gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DashboardContractResponse struct {
|
type DashboardContractResponse struct {
|
||||||
@@ -164,7 +164,7 @@ func ConvertContractToContractResponse(contract Contract) ContractResponse {
|
|||||||
},
|
},
|
||||||
Description: contract.Description,
|
Description: contract.Description,
|
||||||
Status: contract.Status,
|
Status: contract.Status,
|
||||||
BlockchainID: contract.BlockchainID,
|
BlockchainID: contract.BlockchainSecret,
|
||||||
ContractInfos: contract.ContractInfos,
|
ContractInfos: contract.ContractInfos,
|
||||||
MaxTemp: contract.MaxTemp,
|
MaxTemp: contract.MaxTemp,
|
||||||
MinTemp: contract.MinTemp,
|
MinTemp: contract.MinTemp,
|
||||||
@@ -198,7 +198,7 @@ func ConvertContractToDashboardResponse(contracts []Contract) []DashboardContrac
|
|||||||
StartTime: contract.StartTime,
|
StartTime: contract.StartTime,
|
||||||
EndTime: contract.EndTime,
|
EndTime: contract.EndTime,
|
||||||
Status: contract.Status,
|
Status: contract.Status,
|
||||||
BlockchainID: contract.BlockchainID,
|
BlockchainID: contract.BlockchainSecret,
|
||||||
ContractInfos: contract.ContractInfos,
|
ContractInfos: contract.ContractInfos,
|
||||||
ProductID: contract.ProductID,
|
ProductID: contract.ProductID,
|
||||||
MaxTemp: contract.MaxTemp,
|
MaxTemp: contract.MaxTemp,
|
||||||
@@ -236,6 +236,7 @@ func ConvertContractToListResponse(contracts []Contract) []ListContractResponse
|
|||||||
Status: KeyValue{Key: status.Key, Value: status.Value},
|
Status: KeyValue{Key: status.Key, Value: status.Value},
|
||||||
Buyer: CompanyShortResponse{ID: int(contract.BuyerID), Name: contract.BuyerName},
|
Buyer: CompanyShortResponse{ID: int(contract.BuyerID), Name: contract.BuyerName},
|
||||||
ContractID: int(contract.ID),
|
ContractID: int(contract.ID),
|
||||||
|
ContractName: contract.Name,
|
||||||
DateCreated: contract.CreatedAt,
|
DateCreated: contract.CreatedAt,
|
||||||
NumberOfDevices: contract.NumberOfDevices,
|
NumberOfDevices: contract.NumberOfDevices,
|
||||||
}
|
}
|
||||||
@@ -280,6 +281,7 @@ type ListContractResponse struct {
|
|||||||
Status KeyValue `json:"status"`
|
Status KeyValue `json:"status"`
|
||||||
Buyer CompanyShortResponse `json:"buyer"`
|
Buyer CompanyShortResponse `json:"buyer"`
|
||||||
ContractID int `json:"contractID"`
|
ContractID int `json:"contractID"`
|
||||||
|
ContractName string `json:"contractName"`
|
||||||
NumberOfDevices int `json:"numberOfDevices"`
|
NumberOfDevices int `json:"numberOfDevices"`
|
||||||
DateCreated time.Time `json:"dateCreated"`
|
DateCreated time.Time `json:"dateCreated"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,15 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GenerateRandomString(length int) string {
|
||||||
|
b := make([]byte, length)
|
||||||
|
_, err := rand.Read(b)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return base64.StdEncoding.EncodeToString(b)
|
||||||
|
}
|
||||||
|
|
||||||
func CovertUintToByte32(id uint) [32]byte {
|
func CovertUintToByte32(id uint) [32]byte {
|
||||||
b := make([]byte, 32)
|
b := make([]byte, 32)
|
||||||
binary.LittleEndian.PutUint32(b, uint32(id))
|
binary.LittleEndian.PutUint32(b, uint32(id))
|
||||||
|
|||||||
Reference in New Issue
Block a user