Upstream sync
This commit is contained in:
@@ -12,17 +12,16 @@ import (
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
|
||||
func GetContracts(status []string, companyName string, companyAddress string,
|
||||
companyEmail string, companyPhone string, startTime *time.Time, endTime *time.Time,
|
||||
contractName string, deviceIDs []int64, contractID int, dateCreated *time.Time, limit, offset int) ([]models.Contract, int64, int, error) {
|
||||
contractName string, deviceIDs []int64, contractIDs []int64, dateCreated *time.Time, limit, offset int) ([]models.Contract, int64, int, error) {
|
||||
|
||||
var contracts []models.Contract
|
||||
db := shared.GetDb()
|
||||
countDb := db
|
||||
|
||||
// Define custom fields to be selected, varies based on joined tables
|
||||
customFields := "contracts.*, array_length(contracts.device_ids, 1) as number_of_devices"
|
||||
customFields := "distinct contracts.*, array_length(contracts.device_ids, 1) as number_of_devices"
|
||||
|
||||
// Search by Statuses
|
||||
if len(status) > 0 {
|
||||
@@ -30,6 +29,12 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
||||
countDb = countDb.Where("contracts.status IN (?)", status)
|
||||
}
|
||||
|
||||
// Search by IDs
|
||||
if len(contractIDs) > 0 {
|
||||
db = db.Where("contracts.id IN (?)", contractIDs)
|
||||
countDb = countDb.Where("contracts.id IN (?)", contractIDs)
|
||||
}
|
||||
|
||||
// Search by Company Fields
|
||||
db = db.Joins("left join companies on companies.id = contracts.buyer_id")
|
||||
countDb = countDb.Joins("left join companies on companies.id = contracts.buyer_id")
|
||||
@@ -57,14 +62,8 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
||||
countDb = countDb.Where("contracts.name LIKE ?", "%"+contractName+"%")
|
||||
}
|
||||
|
||||
// Search by Contract Name
|
||||
if contractID != 0{
|
||||
db = db.Where("contracts.id = ?", contractID)
|
||||
countDb = countDb.Where("contracts.id = ?", contractID)
|
||||
}
|
||||
|
||||
// Search by Start Time and End Time
|
||||
if startTime != nil &&!startTime.IsZero() {
|
||||
if startTime != nil && !startTime.IsZero() {
|
||||
db = db.Where("start_time >= ?", startTime)
|
||||
countDb = countDb.Where("start_time >= ?", startTime)
|
||||
}
|
||||
@@ -76,10 +75,9 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
||||
|
||||
if dateCreated != nil && !dateCreated.IsZero() {
|
||||
db = db.Where("contracts.created_at = ?", dateCreated)
|
||||
countDb = countDb.Where("created_at = ?", dateCreated)
|
||||
countDb = countDb.Where("contracts.created_at = ?", dateCreated)
|
||||
}
|
||||
|
||||
|
||||
// Search by Device IDs
|
||||
if len(deviceIDs) > 0 {
|
||||
db = db.Where("device_ids && ?", pq.Array(deviceIDs))
|
||||
@@ -114,7 +112,7 @@ func GetContracts(status []string, companyName string, companyAddress string,
|
||||
func UpdateContract(contract models.Contract) (models.Contract, int, error) {
|
||||
|
||||
// Update contract
|
||||
if err := shared.GetDb().Update(contract).Error; err != nil {
|
||||
if err := shared.GetDb().Model(contract).Updates(contract).Error; err != nil {
|
||||
log.Printf("UpdateContractByID Error: Could not update contract: %v", err)
|
||||
return contract, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user