From 7c3407c86dbc468279dcbdfae6763f633166cbb6 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Tue, 17 Oct 2023 10:32:52 +0200 Subject: [PATCH] upstream sync --- controllers/contracts_controller.go | 28 +++++++++++++++------------- controllers/invoices_controller.go | 5 +++-- database/contract/contract.go | 25 ++++++++++++++++++++++++- database/invoice/invoice.go | 8 +++++++- models/contract.go | 18 +++++------------- shared/database.go | 5 ++--- 6 files changed, 56 insertions(+), 33 deletions(-) diff --git a/controllers/contracts_controller.go b/controllers/contracts_controller.go index 7ccd7e9..37cc646 100644 --- a/controllers/contracts_controller.go +++ b/controllers/contracts_controller.go @@ -200,19 +200,21 @@ func CreateContract(c *gin.Context) { db := shared.GetDb() newContract := models.Contract{ - BuyerID: payload.BuyerID, - SellerID: payload.SellerID, - Description: payload.Description, - ProductID: payload.ProductID, - MinTemp: payload.MinTemp, - MaxTemp: payload.MaxTemp, - ArrivalDate: time.Unix(payload.ArrivalDate, 0), - PenaltyType: payload.PenaltyRec, - PenaltyValue: payload.PenaltyValue, - PenaltyRec: payload.PenaltyRec, - StartLat: payload.StartLat, - StartLon: payload.StartLon, - Status: models.ContractStatusDraft, + BuyerID: payload.BuyerID, + SellerID: payload.SellerID, + Description: payload.Description, + ProductID: payload.ProductID, + MinTemp: payload.MinTemp, + MaxTemp: payload.MaxTemp, + ArrivalDate: time.Unix(payload.ArrivalDate, 0), + PenaltyType: payload.PenaltyType, + PenaltyValue: payload.PenaltyValue, + PenaltyRec: payload.PenaltyRec, + StartLat: payload.StartLat, + StartLon: payload.StartLon, + Status: models.ContractStatusDraft, + StartPlaceName: payload.StartPlaceName, + EndPlaceName: payload.EndPlaceName, } if err := db.Create(&newContract).Error; err != nil { diff --git a/controllers/invoices_controller.go b/controllers/invoices_controller.go index 7269905..4fc8c33 100644 --- a/controllers/invoices_controller.go +++ b/controllers/invoices_controller.go @@ -15,6 +15,7 @@ func GetInvoices(c *gin.Context) { buyerName := c.Query("buyer_name") sortBy := c.Query("sort_by") iDsStr := c.QueryArray("ids[]") + status := c.QueryArray("status") limit, err := strconv.Atoi(limitStr) if err != nil { @@ -39,7 +40,7 @@ func GetInvoices(c *gin.Context) { invoiceIDs = append(invoiceIDs, id) } - invoices, total, err := invoice.GetInvoices(buyerName, sortBy, limit, offset, invoiceIDs) + invoices, total, err := invoice.GetInvoices(buyerName, sortBy, limit, offset, invoiceIDs, status) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return @@ -59,7 +60,7 @@ func GetInvoiceByID(c *gin.Context) { return } - invoices, _, err := invoice.GetInvoices("", "", 1, 0, []int64{int64(id)}) + invoices, _, err := invoice.GetInvoices("", "", 1, 0, []int64{int64(id)}, nil) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return diff --git a/database/contract/contract.go b/database/contract/contract.go index 368fb2c..e84f8f6 100644 --- a/database/contract/contract.go +++ b/database/contract/contract.go @@ -182,6 +182,29 @@ func GetContractByID(contractID uint64) (models.Contract, int, error) { return contract, http.StatusInternalServerError, err } - return contract, http.StatusOK, nil + // Fetch the product + var product models.ProductTemplate + if err := shared.GetDb().Unscoped().Where("id = ?", contract.ProductID).First(&product).Error; err != nil { + log.Printf("GetContractByID Error: Could not fetch product: %v", err) + return contract, http.StatusInternalServerError, err + } + contract.ProductName = product.Name + // Fetch the seller + var seller models.Company + if err := shared.GetDb().Unscoped().Where("id = ?", contract.SellerID).First(&seller).Error; err != nil { + log.Printf("GetContractByID Error: Could not fetch seller: %v", err) + return contract, http.StatusInternalServerError, err + } + contract.SellerName = seller.Name + + // Fetch the buyer + var buyer models.Company + if err := shared.GetDb().Unscoped().Where("id = ?", contract.BuyerID).First(&buyer).Error; err != nil { + log.Printf("GetContractByID Error: Could not fetch buyer: %v", err) + return contract, http.StatusInternalServerError, err + } + contract.BuyerName = buyer.Name + + return contract, http.StatusOK, nil } diff --git a/database/invoice/invoice.go b/database/invoice/invoice.go index dc17672..f9066fb 100644 --- a/database/invoice/invoice.go +++ b/database/invoice/invoice.go @@ -8,7 +8,7 @@ import ( "gitlab.com/pactual1/backend/shared" ) -func GetInvoices(buyerName string, sortBy string, limit int, offset int, ids []int64) ([]models.Invoice, int64, error) { +func GetInvoices(buyerName string, sortBy string, limit int, offset int, ids []int64, status []string) ([]models.Invoice, int64, error) { var invoices []models.Invoice // Default sort by InvoiceDate DESC @@ -35,6 +35,12 @@ func GetInvoices(buyerName string, sortBy string, limit int, offset int, ids []i countDb = countDb.Where("id in (?)", ids) } + // Added conditional for status search + if len(status) > 0 { + db = db.Where("status in (?)", status) + countDb = countDb.Where("status in (?)", status) + } + var total int64 if err := countDb.Model(&models.Invoice{}).Count(&total).Error; err != nil { return nil, 0, err diff --git a/models/contract.go b/models/contract.go index cbc5cac..e1a607a 100644 --- a/models/contract.go +++ b/models/contract.go @@ -33,6 +33,8 @@ type Contract struct { PenaltyValue int `json:"penaltyValue"` PenaltyRec string `json:"penaltyRec"` BuyerName string `json:"buyerName" gorm:"-"` + SellerName string `json:"sellerName" gorm:"-"` + ProductName string `json:"productName" gorm:"-"` NumberOfDevices int `json:"numberOfDevices" gorm:"-"` } @@ -91,11 +93,6 @@ type ContractResponse struct { ID uint `json:"id"` Name string `json:"name"` } `json:"product"` - Template struct { - ID uint `json:"id"` - Name string `json:"name"` - Value string `json:"value"` - } `json:"template"` Description string `json:"description"` Status string `json:"status"` BlockchainID string `json:"blockchainId"` @@ -118,7 +115,6 @@ func ConvertContractsToContractResponse(contracts []Contract) []ContractResponse } func ConvertContractToContractResponse(contract Contract) ContractResponse { - contractResponse := ContractResponse{ BaseModel: BaseModel{ID: contract.ID, CreatedAt: contract.CreatedAt, UpdatedAt: contract.UpdatedAt}, Name: contract.Name, @@ -128,12 +124,14 @@ func ConvertContractToContractResponse(contract Contract) ContractResponse { Name string "json:\"name\"" }{ ID: contract.SellerID, + Name: contract.SellerName, }, Buyer: struct { ID uint "json:\"id\"" Name string "json:\"name\"" }{ ID: contract.BuyerID, + Name: contract.BuyerName, }, Start: struct { Name string "json:\"name\"" @@ -162,13 +160,7 @@ func ConvertContractToContractResponse(contract Contract) ContractResponse { Name string "json:\"name\"" }{ ID: contract.ProductID, - }, - Template: struct { - ID uint "json:\"id\"" - Name string "json:\"name\"" - Value string "json:\"value\"" - }{ - ID: contract.TemplateID, + Name: contract.ProductName, }, Description: contract.Description, Status: contract.Status, diff --git a/shared/database.go b/shared/database.go index 9533bfd..06c4318 100644 --- a/shared/database.go +++ b/shared/database.go @@ -4,11 +4,10 @@ import ( "fmt" "log" - "gitlab.com/pactual1/backend/config" - "gitlab.com/pactual1/backend/models" - "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" + "gitlab.com/pactual1/backend/config" + "gitlab.com/pactual1/backend/models" ) var db *gorm.DB