181 lines
5.3 KiB
Go
181 lines
5.3 KiB
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gitlab.com/pactual1/backend/database/contract"
|
|
"gitlab.com/pactual1/backend/models"
|
|
)
|
|
|
|
func GetLatestContracts(c *gin.Context) {
|
|
// Existing parameters
|
|
limitStr := c.DefaultQuery("limit", "50")
|
|
offsetStr := c.DefaultQuery("offset", "0")
|
|
status := c.DefaultQuery("status", models.ContractStatusActive)
|
|
|
|
// New/Updated optional parameters
|
|
companyName := c.Query("company_name")
|
|
companyAddress := c.Query("company_address")
|
|
companyEmail := c.Query("company_email")
|
|
companyPhone := c.Query("company_phone")
|
|
contractName := c.Query("contract_name")
|
|
startTimeStr := c.Query("start_time")
|
|
endTimeStr := c.Query("end_time")
|
|
deviceIDsStr := c.QueryArray("deviceIDs[]")
|
|
|
|
// Convert limit and offset to int
|
|
limit, err := strconv.Atoi(limitStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit value"})
|
|
return
|
|
}
|
|
|
|
offset, err := strconv.Atoi(offsetStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
|
|
return
|
|
}
|
|
|
|
// Convert startTime to time.Time
|
|
var startTime time.Time
|
|
if startTimeStr != "" {
|
|
startTimeUnix, err := strconv.ParseInt(startTimeStr, 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start_time value"})
|
|
return
|
|
}
|
|
startTime = time.Unix(startTimeUnix, 0)
|
|
}
|
|
|
|
// Convert endTime to time.Time
|
|
var endTime time.Time
|
|
if endTimeStr != "" {
|
|
endTimeUnix, err := strconv.ParseInt(endTimeStr, 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid end_time value"})
|
|
return
|
|
}
|
|
endTime = time.Unix(endTimeUnix, 0)
|
|
}
|
|
|
|
// Convert deviceIDs to []int64
|
|
var deviceIDs []int64
|
|
for _, idStr := range deviceIDsStr {
|
|
id, err := strconv.ParseInt(idStr, 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid deviceID value"})
|
|
return
|
|
}
|
|
deviceIDs = append(deviceIDs, id)
|
|
}
|
|
|
|
// Fetch contracts
|
|
contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, &startTime, &endTime, contractName, deviceIDs, 0, nil, limit, offset)
|
|
|
|
if err != nil {
|
|
c.JSON(st, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// Respond with the contracts and the total count
|
|
c.JSON(http.StatusOK, gin.H{"total": total, "data": contracts})
|
|
}
|
|
|
|
|
|
func GetBuyerContracts(c *gin.Context) {
|
|
// Existing parameters
|
|
limitStr := c.DefaultQuery("limit", "50")
|
|
offsetStr := c.DefaultQuery("offset", "0")
|
|
status := c.Query("status")
|
|
|
|
contractIDStr := c.DefaultQuery("contract_id","0")
|
|
dateCreatedStr := c.Query("date_created")
|
|
|
|
|
|
// Convert limit and offset to int
|
|
limit, err := strconv.Atoi(limitStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit value"})
|
|
return
|
|
}
|
|
|
|
offset, err := strconv.Atoi(offsetStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
|
|
return
|
|
}
|
|
|
|
// Convert limit and offset to int
|
|
contractID, err := strconv.Atoi(contractIDStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid contractID value"})
|
|
return
|
|
}
|
|
|
|
// Convert startTime to time.Time
|
|
var dateCreated time.Time
|
|
if dateCreatedStr != "" {
|
|
startTimeUnix, err := strconv.ParseInt(dateCreatedStr, 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start_time value"})
|
|
return
|
|
}
|
|
dateCreated = time.Unix(startTimeUnix, 0)
|
|
}
|
|
|
|
|
|
|
|
// Fetch contracts
|
|
contracts, total, st, err := contract.GetContracts(status, "", "", "", "", nil, nil, "", nil, contractID, &dateCreated, limit, offset)
|
|
|
|
if err != nil {
|
|
c.JSON(st, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// Respond with the contracts and the total count
|
|
c.JSON(http.StatusOK, gin.H{"total": total, "data": convertContractToResponseModel(contracts)})
|
|
}
|
|
|
|
func GetContractStatuses(c *gin.Context) {
|
|
|
|
// Respond with the contract statuses
|
|
c.JSON(http.StatusOK, gin.H{"statuses": models.GetContractStatuses()})
|
|
}
|
|
|
|
|
|
func convertContractToResponseModel(contracts []models.Contract) []models.ListContractResponse {
|
|
var listInvoiceResponses []models.ListContractResponse
|
|
|
|
// Get all statuses
|
|
statuses := models.GetContractStatuses()
|
|
statusMap := make(map[string]models.Status)
|
|
for _, s := range statuses {
|
|
statusMap[s.Value] = s
|
|
}
|
|
|
|
for _, contract := range contracts {
|
|
// Get the status based on Value in the DB
|
|
status, ok := statusMap[contract.Status]
|
|
if !ok {
|
|
status = models.Status{Key: "Unknown", Value: "unknown"}
|
|
}
|
|
|
|
listInvoiceResponse := models.ListContractResponse{
|
|
Status: models.KeyValue{Key: status.Key, Value: status.Value},
|
|
Buyer: models.CompanyShortResponse{ID: int(contract.BuyerID), Name: contract.BuyerName},
|
|
ContractID: int(contract.ID),
|
|
DateCreated: contract.CreatedAt,
|
|
NumberOfDevices: contract.NumberOfDevices,
|
|
}
|
|
listInvoiceResponses = append(listInvoiceResponses, listInvoiceResponse)
|
|
}
|
|
return listInvoiceResponses
|
|
}
|
|
|
|
|
|
|