Contracts for buyer
This commit is contained in:
@@ -73,7 +73,7 @@ func GetLatestContracts(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Fetch contracts
|
||||
contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, startTime, endTime, contractName, deviceIDs, limit, offset)
|
||||
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()})
|
||||
@@ -81,6 +81,100 @@ func GetLatestContracts(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Respond with the contracts and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "contracts": contracts})
|
||||
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{"data": 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
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ func GetDeviceData(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Respond with the GeoJSON feature collection
|
||||
c.JSON(http.StatusOK, featureCollection)
|
||||
c.JSON(http.StatusOK, gin.H{"data": featureCollection})
|
||||
}
|
||||
|
||||
func GetDevicesByContract(c *gin.Context) {
|
||||
@@ -161,5 +161,5 @@ func GetDevicesByContract(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
// Respond with the devices
|
||||
c.JSON(http.StatusOK, devices)
|
||||
c.JSON(http.StatusOK,gin.H{"data" : devices})
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gitlab.com/pactual1/backend/database/invoice"
|
||||
"gitlab.com/pactual1/backend/models"
|
||||
)
|
||||
|
||||
func GetInvoices(c *gin.Context) {
|
||||
@@ -32,7 +33,10 @@ func GetInvoices(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "invoices": invoices})
|
||||
// Convert to ListInvoiceResponse type
|
||||
listInvoiceResponses := convertToResponseModel(invoices)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "data": listInvoiceResponses})
|
||||
}
|
||||
|
||||
func GetInvoiceByID(c *gin.Context) {
|
||||
@@ -50,9 +54,42 @@ func GetInvoiceByID(c *gin.Context) {
|
||||
}
|
||||
|
||||
if len(invoices) > 0 {
|
||||
c.JSON(http.StatusOK, gin.H{"invoice": invoices[0]})
|
||||
c.JSON(http.StatusOK, gin.H{"data": invoices[0]})
|
||||
} else {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Invoice not found"})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func convertToResponseModel(invoices []models.Invoice) []models.ListInvoiceResponse {
|
||||
var listInvoiceResponses []models.ListInvoiceResponse
|
||||
|
||||
// Get all statuses
|
||||
statuses := models.GetInvoiceStatuses()
|
||||
statusMap := make(map[string]models.Status)
|
||||
for _, s := range statuses {
|
||||
statusMap[s.Value] = s
|
||||
}
|
||||
|
||||
for _, invoice := range invoices {
|
||||
// Get the status based on Value in the DB
|
||||
status, ok := statusMap[invoice.Status]
|
||||
if !ok {
|
||||
status = models.Status{Key: "Unknown", Value: "unknown"}
|
||||
}
|
||||
|
||||
listInvoiceResponse := models.ListInvoiceResponse{
|
||||
Status: models.KeyValue{Key: status.Key, Value: status.Value},
|
||||
Buyer: models.CompanyShortResponse{ID: int(invoice.BuyerID), Name: invoice.BuyerName},
|
||||
ContractID: int(invoice.ContractID),
|
||||
DateCreated: invoice.InvoiceDate,
|
||||
DueDate: invoice.InvoiceDueDate,
|
||||
Amount: strconv.FormatInt(invoice.PriceCents, 10),
|
||||
}
|
||||
listInvoiceResponses = append(listInvoiceResponses, listInvoiceResponse)
|
||||
}
|
||||
return listInvoiceResponses
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user