Upstream sync

This commit is contained in:
Senad Uka
2023-10-13 11:48:14 +02:00
parent 7369739bdd
commit d01c1a0232
29 changed files with 563 additions and 444 deletions

18
Dockerfile Normal file
View File

@@ -0,0 +1,18 @@
# Builder image
FROM --platform=linux/amd64 golang:1.21 as builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -mod=readonly -v -o pactual-backend main.go
# Runtime image
FROM --platform=linux/amd64 golang:1.21
WORKDIR /app
COPY --from=builder /src/pactual-backend .
CMD ["/app/pactual-backend"]

View File

@@ -1,60 +0,0 @@
package controllers
import (
"errors"
"log"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"gitlab.com/pactual1/backend/models"
"gitlab.com/pactual1/backend/shared"
)
func ListBuyers(c *gin.Context) {
// Get limit and offset from query parameter with defaults
limitStr := c.DefaultQuery("limit", "99999999999999999999999999999999")
offsetStr := c.DefaultQuery("offset", "0")
// Convert limit and offset to int
limit, err := strconv.Atoi(limitStr)
if err != nil {
log.Printf("ListBuyers Error: Invalid limit value: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit value"})
return
}
offset, err := strconv.Atoi(offsetStr)
if err != nil {
log.Printf("ListBuyers Error: Invalid offset value: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
return
}
// Create a slice to hold the buyers
var buyers []models.Buyer
// Count the total number of buyers
var total int64
if err := shared.GetDb().Model(&models.Buyer{}).Count(&total).Error; err != nil {
log.Printf("ListBuyers Error: Database error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
return
}
// Fetch the buyers from the database with LIMIT and OFFSET
if err := shared.GetDb().Order("created_at desc").Limit(limit).Offset(offset).Find(&buyers).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("ListBuyers Error: No buyers found: %v", err)
c.JSON(http.StatusNotFound, gin.H{"error": "No buyers found"})
} else {
log.Printf("ListBuyers Error: Database error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
}
return
}
// Respond with the buyers and the total count
c.JSON(http.StatusOK, gin.H{"total": total, "data": buyers})
}

View File

@@ -0,0 +1,81 @@
package controllers
import (
"log"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gitlab.com/pactual1/backend/database/company"
)
func ListCompanies(c *gin.Context) {
// Get limit and offset from query parameter with defaults
limitStr := c.DefaultQuery("limit", "50")
offsetStr := c.DefaultQuery("offset", "0")
iDsStr := c.QueryArray("ids[]")
// Convert limit and offset to int
limit, err := strconv.Atoi(limitStr)
if err != nil {
log.Printf("ListCompanies Error: Invalid limit value: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit value"})
return
}
offset, err := strconv.Atoi(offsetStr)
if err != nil {
log.Printf("ListCompanies Error: Invalid offset value: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
return
}
// Convert ids to []int64
var companyIDs []int64
for _, idStr := range iDsStr {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id value"})
return
}
companyIDs = append(companyIDs, id)
}
// Fetch companies
companies, total, st, err := company.GetCompanies(companyIDs, limit, offset)
if err != nil {
c.JSON(st, gin.H{"error": err.Error()})
return
}
// Respond with the companies and the total count
c.JSON(http.StatusOK, gin.H{"total": total, "data": companies})
}
func GetCompanyByID(c *gin.Context) {
// Get the company ID from url parameters
companyIDStr := c.Param("company_id")
if companyIDStr == "" {
log.Printf("GetCompanyByID Error: ID is required")
c.JSON(http.StatusBadRequest, gin.H{"error": "ID is required"})
return
}
companyID, err := strconv.ParseUint(companyIDStr, 10, 32)
if err != nil {
log.Printf("GetCompanyByID Error: Invalid ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
return
}
// Fetch company
companies, _, st, err := company.GetCompanies([]int64{int64(companyID)}, 1, 0)
if err != nil {
c.JSON(st, gin.H{"error": err.Error()})
return
}
// Respond with the companies and the total count
c.JSON(http.StatusOK, gin.H{"data": companies[0]})
}

View File

@@ -9,7 +9,7 @@ import (
"gitlab.com/pactual1/backend/shared" "gitlab.com/pactual1/backend/shared"
) )
func TestListBuyers(t *testing.T) { func TestListCompanies(t *testing.T) {
ts := runTestServer() ts := runTestServer()
defer ts.Close() defer ts.Close()
defer shared.CloseDb() defer shared.CloseDb()

View File

@@ -15,10 +15,10 @@ import (
) )
func GetLatestContracts(c *gin.Context) { func GetLatestContracts(c *gin.Context) {
// Existing parameters // Existing parameters
limitStr := c.DefaultQuery("limit", "50") limitStr := c.DefaultQuery("limit", "50")
offsetStr := c.DefaultQuery("offset", "0") offsetStr := c.DefaultQuery("offset", "0")
status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",") status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",")
// New/Updated optional parameters // New/Updated optional parameters
companyName := c.Query("company_name") companyName := c.Query("company_name")
@@ -29,6 +29,7 @@ func GetLatestContracts(c *gin.Context) {
startTimeStr := c.Query("start_time") startTimeStr := c.Query("start_time")
endTimeStr := c.Query("end_time") endTimeStr := c.Query("end_time")
deviceIDsStr := c.QueryArray("deviceIDs[]") deviceIDsStr := c.QueryArray("deviceIDs[]")
iDsStr := c.QueryArray("ids[]")
// Convert limit and offset to int // Convert limit and offset to int
limit, err := strconv.Atoi(limitStr) limit, err := strconv.Atoi(limitStr)
@@ -76,83 +77,118 @@ func GetLatestContracts(c *gin.Context) {
deviceIDs = append(deviceIDs, id) deviceIDs = append(deviceIDs, id)
} }
// Fetch contracts // Convert ids to []int64
contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, &startTime, &endTime, contractName, deviceIDs, 0, nil, limit, offset) var contractIDs []int64
for _, idStr := range iDsStr {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id value"})
return
}
contractIDs = append(contractIDs, id)
}
if err != nil { // Fetch contracts
c.JSON(st, gin.H{"error": err.Error()}) contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, &startTime, &endTime, contractName, deviceIDs, contractIDs, nil, limit, offset)
return
}
// Respond with the contracts and the total count if err != nil {
c.JSON(http.StatusOK, gin.H{"total": total, "data": models.ConvertContractToResponse(contracts)}) c.JSON(st, gin.H{"error": err.Error()})
} return
}
func GetBuyerContracts(c *gin.Context) {
// Existing parameters
limitStr := c.DefaultQuery("limit", "50")
offsetStr := c.DefaultQuery("offset", "0")
status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",")
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 // Respond with the contracts and the total count
c.JSON(http.StatusOK, gin.H{"total": total, "data": models.ConvertContractToResponseModel(contracts)}) c.JSON(http.StatusOK, gin.H{"total": total, "data": models.ConvertContractToResponse(contracts)})
}
func GetBuyerContracts(c *gin.Context) {
// Existing parameters
limitStr := c.DefaultQuery("limit", "50")
offsetStr := c.DefaultQuery("offset", "0")
status := c.QueryArray("status")
iDsStr := c.QueryArray("ids[]")
dateCreatedStr := c.Query("date_created")
startTimeStr := c.Query("start_time")
endTimeStr := c.Query("end_time")
// 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
}
startTimeValue := time.Unix(startTimeUnix, 0)
startTime = &startTimeValue
}
// 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
}
endTimeValue := time.Unix(endTimeUnix, 0)
endTime = &endTimeValue
}
// 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)
}
// Convert ids to []int64
var contractIDs []int64
for _, idStr := range iDsStr {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id value"})
return
}
contractIDs = append(contractIDs, id)
}
// Fetch contracts
contracts, total, st, err := contract.GetContracts(status, "", "", "", "", startTime, endTime, "", nil, contractIDs, &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": models.ConvertContractToResponseModel(contracts)})
} }
func GetContractStatuses(c *gin.Context) { func GetContractStatuses(c *gin.Context) {
// Respond with the contract statuses // Respond with the contract statuses
c.JSON(http.StatusOK, gin.H{"data": models.GetContractStatuses()}) c.JSON(http.StatusOK, gin.H{"data": models.GetContractStatuses()})
} }
func CreateContract(c *gin.Context) { func CreateContract(c *gin.Context) {
var payload models.CreateContractRequestPayload var payload models.CreateContractRequestPayload
if err := c.ShouldBindJSON(&payload); err != nil { if err := c.ShouldBindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"})
@@ -163,16 +199,19 @@ 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.PenaltyRec, PenaltyType: payload.PenaltyRec,
PenaltyValue : payload.PenaltyValue, PenaltyValue: payload.PenaltyValue,
PenaltyRec : payload.PenaltyRec, PenaltyRec: payload.PenaltyRec,
StartLat: payload.StartLat,
StartLon: payload.StartLon,
Status: models.ContractStatusDraft,
} }
if err := db.Create(&newContract).Error; err != nil { if err := db.Create(&newContract).Error; err != nil {
@@ -228,7 +267,6 @@ func UpdateContract(c *gin.Context) {
// Get the contract ID from url parameters // Get the contract ID from url parameters
contractIDStr := c.Param("contract_id") contractIDStr := c.Param("contract_id")
if contractIDStr == "" { if contractIDStr == "" {
log.Printf("UpdateContract Error: ID is required") log.Printf("UpdateContract Error: ID is required")
c.JSON(http.StatusBadRequest, gin.H{"error": "ID is required"}) c.JSON(http.StatusBadRequest, gin.H{"error": "ID is required"})
@@ -254,4 +292,3 @@ func UpdateContract(c *gin.Context) {
// Respond with the contracts and the total count // Respond with the contracts and the total count
c.JSON(http.StatusOK, gin.H{"data": contractModel}) c.JSON(http.StatusOK, gin.H{"data": contractModel})
} }

View File

@@ -57,7 +57,7 @@ func SaveDeviceInfo(c *gin.Context) {
} else { } else {
log.Printf("Current device deleted at: %v", device.DeletedAt) log.Printf("Current device deleted at: %v", device.DeletedAt)
if device.DeletedAt != nil { if device.DeletedAt.Valid {
// Use raw SQL to update the record // Use raw SQL to update the record
if err := shared.GetDb().Exec("UPDATE devices SET deleted_at = NULL, company_id = NULL WHERE id = ?", device.ID).Error; err != nil { if err := shared.GetDb().Exec("UPDATE devices SET deleted_at = NULL, company_id = NULL WHERE id = ?", device.ID).Error; err != nil {
log.Printf("UNDELETE -Device DB Error: %v", err) log.Printf("UNDELETE -Device DB Error: %v", err)
@@ -161,5 +161,5 @@ func GetDevicesByContract(c *gin.Context) {
return return
} }
// Respond with the devices // Respond with the devices
c.JSON(http.StatusOK,gin.H{"data" : models.ConvertDeviceToResponse(devices)}) c.JSON(http.StatusOK, gin.H{"data": models.ConvertDeviceToResponse(devices)})
} }

View File

@@ -0,0 +1,11 @@
package controllers
import (
"net/http"
"github.com/gin-gonic/gin"
)
func HealthCheck(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"health": "ready"})
}

View File

@@ -14,6 +14,7 @@ func GetInvoices(c *gin.Context) {
offsetStr := c.DefaultQuery("offset", "0") offsetStr := c.DefaultQuery("offset", "0")
buyerName := c.Query("buyer_name") buyerName := c.Query("buyer_name")
sortBy := c.Query("sort_by") sortBy := c.Query("sort_by")
iDsStr := c.QueryArray("ids[]")
limit, err := strconv.Atoi(limitStr) limit, err := strconv.Atoi(limitStr)
if err != nil { if err != nil {
@@ -27,7 +28,18 @@ func GetInvoices(c *gin.Context) {
return return
} }
invoices, total, err := invoice.GetInvoices(buyerName, sortBy, limit, offset, 0) // Convert ids to []int64
var invoiceIDs []int64
for _, idStr := range iDsStr {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id value"})
return
}
invoiceIDs = append(invoiceIDs, id)
}
invoices, total, err := invoice.GetInvoices(buyerName, sortBy, limit, offset, invoiceIDs)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return return
@@ -40,32 +52,30 @@ func GetInvoices(c *gin.Context) {
} }
func GetInvoiceByID(c *gin.Context) { func GetInvoiceByID(c *gin.Context) {
idStr := c.Param("id") idStr := c.Param("id")
id, err := strconv.Atoi(idStr) id, err := strconv.Atoi(idStr)
if err != nil { if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
return return
} }
invoices, _, err := invoice.GetInvoices("", "", 1, 0, uint(id)) invoices, _, err := invoice.GetInvoices("", "", 1, 0, []int64{int64(id)})
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return return
} }
invoiceResponses := models.ConvertInvoiceToResponse(invoices) invoiceResponses := models.ConvertInvoiceToResponse(invoices)
if len(invoices) > 0 { if len(invoices) > 0 {
c.JSON(http.StatusOK, gin.H{"data": invoiceResponses[0]}) c.JSON(http.StatusOK, gin.H{"data": invoiceResponses[0]})
} else { } else {
c.JSON(http.StatusNotFound, gin.H{"error": "Invoice not found"}) c.JSON(http.StatusNotFound, gin.H{"error": "Invoice not found"})
} }
} }
func convertToResponseModel(invoices []models.Invoice) []models.ListInvoiceResponse { func convertToResponseModel(invoices []models.Invoice) []models.ListInvoiceResponse {
var listInvoiceResponses []models.ListInvoiceResponse listInvoiceResponses := []models.ListInvoiceResponse{}
// Get all statuses // Get all statuses
statuses := models.GetInvoiceStatuses() statuses := models.GetInvoiceStatuses()
@@ -82,16 +92,14 @@ func convertToResponseModel(invoices []models.Invoice) []models.ListInvoiceRespo
} }
listInvoiceResponse := models.ListInvoiceResponse{ listInvoiceResponse := models.ListInvoiceResponse{
Status: models.KeyValue{Key: status.Key, Value: status.Value}, Status: models.KeyValue{Key: status.Key, Value: status.Value},
Buyer: models.CompanyShortResponse{ID: int(invoice.BuyerID), Name: invoice.BuyerName}, Buyer: models.CompanyShortResponse{ID: int(invoice.BuyerID), Name: invoice.BuyerName},
ContractID: int(invoice.ContractID), ContractID: int(invoice.ContractID),
DateCreated: invoice.InvoiceDate, DateCreated: invoice.InvoiceDate,
DueDate: invoice.InvoiceDueDate, DueDate: invoice.InvoiceDueDate,
Amount: strconv.FormatInt(invoice.PriceCents, 10), Amount: strconv.FormatInt(invoice.PriceCents, 10),
} }
listInvoiceResponses = append(listInvoiceResponses, listInvoiceResponse) listInvoiceResponses = append(listInvoiceResponses, listInvoiceResponse)
} }
return listInvoiceResponses return listInvoiceResponses
} }

View File

@@ -15,8 +15,9 @@ import (
func ListProductTemplates(c *gin.Context) { func ListProductTemplates(c *gin.Context) {
// Get limit and offset from query parameter with defaults // Get limit and offset from query parameter with defaults
limitStr := c.DefaultQuery("limit", "99999999999999999999999999999999") limitStr := c.DefaultQuery("limit", "50")
offsetStr := c.DefaultQuery("offset", "0") offsetStr := c.DefaultQuery("offset", "0")
iDsStr := c.QueryArray("ids[]")
// Convert limit and offset to int // Convert limit and offset to int
limit, err := strconv.Atoi(limitStr) limit, err := strconv.Atoi(limitStr)
@@ -34,18 +35,36 @@ func ListProductTemplates(c *gin.Context) {
} }
// Create a slice to hold the product templates // Create a slice to hold the product templates
var productTemplates []models.ProductTemplate productTemplates := []models.ProductTemplate{}
// Convert ids to []int64
var productIDs []int64
for _, idStr := range iDsStr {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id value"})
return
}
productIDs = append(productIDs, id)
}
countDb := shared.GetDb()
db := shared.GetDb()
if len(productIDs) > 0 {
countDb = countDb.Where("id IN (?)", productIDs)
db = db.Where("id IN (?)", productIDs)
}
// Count the total number of product templates // Count the total number of product templates
var total int64 var total int64
if err := shared.GetDb().Model(&models.ProductTemplate{}).Count(&total).Error; err != nil { if err := countDb.Model(&models.ProductTemplate{}).Count(&total).Error; err != nil {
log.Printf("ListProductTemplates Error: Database error: %v", err) log.Printf("ListProductTemplates Error: Database error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
return return
} }
// Fetch the product templates from the database with LIMIT and OFFSET // Fetch the product templates from the database with LIMIT and OFFSET
if err := shared.GetDb().Order("created_at desc").Limit(limit).Offset(offset).Find(&productTemplates).Error; err != nil { if err := db.Order("created_at desc").Limit(limit).Offset(offset).Find(&productTemplates).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("ListProductTemplates Error: No product templates found: %v", err) log.Printf("ListProductTemplates Error: No product templates found: %v", err)
c.JSON(http.StatusNotFound, gin.H{"error": "No product templates found"}) c.JSON(http.StatusNotFound, gin.H{"error": "No product templates found"})

View File

@@ -15,7 +15,7 @@ func TestListProductTemplates(t *testing.T) {
defer shared.CloseDb() defer shared.CloseDb()
t.Run("it should return 200", func(t *testing.T) { t.Run("it should return 200", func(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("%s/product_templates?limit=20", ts.URL)) resp, err := http.Get(fmt.Sprintf("%s/products?limit=20", ts.URL))
if err != nil { if err != nil {
t.Fatalf("Expected no error, got %v", err) t.Fatalf("Expected no error, got %v", err)

View File

@@ -15,8 +15,9 @@ import (
func ListTextTemplates(c *gin.Context) { func ListTextTemplates(c *gin.Context) {
// Get limit and offset from query parameter with defaults // Get limit and offset from query parameter with defaults
limitStr := c.DefaultQuery("limit", "99999999999999999999999999999999") limitStr := c.DefaultQuery("limit", "50")
offsetStr := c.DefaultQuery("offset", "0") offsetStr := c.DefaultQuery("offset", "0")
iDsStr := c.QueryArray("ids[]")
// Convert limit and offset to int // Convert limit and offset to int
limit, err := strconv.Atoi(limitStr) limit, err := strconv.Atoi(limitStr)
@@ -33,19 +34,36 @@ func ListTextTemplates(c *gin.Context) {
return return
} }
// Convert ids to []int64
var templateIDs []int64
for _, idStr := range iDsStr {
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id value"})
return
}
templateIDs = append(templateIDs, id)
}
// Create a slice to hold the text templates // Create a slice to hold the text templates
var textTemplates []models.TextTemplate textTemplates := []models.TextTemplate{}
// Count the total number of text templates // Count the total number of text templates
var total int64 var total int64
if err := shared.GetDb().Model(&models.TextTemplate{}).Count(&total).Error; err != nil { countDb := shared.GetDb()
db := shared.GetDb()
if len(templateIDs) > 0 {
countDb = countDb.Where("id IN (?)", templateIDs)
db = db.Where("id IN (?)", templateIDs)
}
if err := countDb.Model(&models.TextTemplate{}).Count(&total).Error; err != nil {
log.Printf("ListTextTemplates Error: Database error: %v", err) log.Printf("ListTextTemplates Error: Database error: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
return return
} }
// Fetch the text templates from the database with LIMIT and OFFSET // Fetch the text templates from the database with LIMIT and OFFSET
if err := shared.GetDb().Order("created_at desc").Limit(limit).Offset(offset).Find(&textTemplates).Error; err != nil { if err := db.Order("created_at desc").Limit(limit).Offset(offset).Find(&textTemplates).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("ListTextTemplates Error: No text templates found: %v", err) log.Printf("ListTextTemplates Error: No text templates found: %v", err)
c.JSON(http.StatusNotFound, gin.H{"error": "No text templates found"}) c.JSON(http.StatusNotFound, gin.H{"error": "No text templates found"})
@@ -82,4 +100,3 @@ func CreateTextTemplate(c *gin.Context) {
log.Printf("Successfully received and saved text template: %v", textTemplate) log.Printf("Successfully received and saved text template: %v", textTemplate)
c.JSON(http.StatusOK, gin.H{"data": textTemplate}) c.JSON(http.StatusOK, gin.H{"data": textTemplate})
} }

View File

@@ -17,7 +17,7 @@ func TestListTextTemplates(t *testing.T) {
defer shared.CloseDb() defer shared.CloseDb()
t.Run("it should return 200", func(t *testing.T) { t.Run("it should return 200", func(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("%s/text_templates?limit=20", ts.URL)) resp, err := http.Get(fmt.Sprintf("%s/templates?limit=20", ts.URL))
if err != nil { if err != nil {
t.Fatalf("Expected no error, got %v", err) t.Fatalf("Expected no error, got %v", err)
@@ -39,7 +39,7 @@ func CreateTextTemplate(t *testing.T) {
} }
payload, _ := json.Marshal(data) payload, _ := json.Marshal(data)
resp, err := http.Post(fmt.Sprintf("%s/text_templates/save", ts.URL), "application/json", bytes.NewBuffer(payload)) resp, err := http.Post(fmt.Sprintf("%s/templates/save", ts.URL), "application/json", bytes.NewBuffer(payload))
if err != nil { if err != nil {
t.Fatalf("Expected no error, got %v", err) t.Fatalf("Expected no error, got %v", err)
} }

View File

@@ -0,0 +1,48 @@
package company
import (
"errors"
"log"
"net/http"
"github.com/jinzhu/gorm"
"gitlab.com/pactual1/backend/models"
"gitlab.com/pactual1/backend/shared"
)
func GetCompanies(companyIDs []int64, limit, offset int) ([]models.Company, int64, int, error) {
companies := []models.Company{}
db := shared.GetDb()
countDb := db
// Search by IDs
if len(companyIDs) > 0 {
db = db.Where("companies.id IN (?)", companyIDs)
countDb = countDb.Where("companies.id IN (?)", companyIDs)
}
// Fetch total count of filtered records
var total int64
if err := countDb.Model(&models.Company{}).Count(&total).Error; err != nil {
log.Printf("GetCompanies Error: Database error: %v", err)
return companies, total, http.StatusInternalServerError, err
}
// Fetch companies with custom fields
if err := db.Select("*").
Order("created_at desc").
Limit(limit).
Offset(offset).
Find(&companies).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("GetCompanies Error: No companies found: %v", err)
return companies, total, http.StatusNotFound, err
} else {
log.Printf("GetCompanies Error: Database error: %v", err)
return companies, total, http.StatusInternalServerError, err
}
}
return companies, total, http.StatusOK, nil
}

View File

@@ -12,17 +12,16 @@ import (
"gitlab.com/pactual1/backend/shared" "gitlab.com/pactual1/backend/shared"
) )
func GetContracts(status []string, companyName string, companyAddress string, func GetContracts(status []string, companyName string, companyAddress string,
companyEmail string, companyPhone string, startTime *time.Time, endTime *time.Time, 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 var contracts []models.Contract
db := shared.GetDb() db := shared.GetDb()
countDb := db countDb := db
// Define custom fields to be selected, varies based on joined tables // 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 // Search by Statuses
if len(status) > 0 { if len(status) > 0 {
@@ -30,6 +29,12 @@ func GetContracts(status []string, companyName string, companyAddress string,
countDb = countDb.Where("contracts.status IN (?)", status) 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 // Search by Company Fields
db = db.Joins("left join companies on companies.id = contracts.buyer_id") db = db.Joins("left join companies on companies.id = contracts.buyer_id")
countDb = countDb.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+"%") 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 // Search by Start Time and End Time
if startTime != nil &&!startTime.IsZero() { if startTime != nil && !startTime.IsZero() {
db = db.Where("start_time >= ?", startTime) db = db.Where("start_time >= ?", startTime)
countDb = countDb.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() { if dateCreated != nil && !dateCreated.IsZero() {
db = db.Where("contracts.created_at = ?", dateCreated) db = db.Where("contracts.created_at = ?", dateCreated)
countDb = countDb.Where("created_at = ?", dateCreated) countDb = countDb.Where("contracts.created_at = ?", dateCreated)
} }
// Search by Device IDs // Search by Device IDs
if len(deviceIDs) > 0 { if len(deviceIDs) > 0 {
db = db.Where("device_ids && ?", pq.Array(deviceIDs)) 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) { func UpdateContract(contract models.Contract) (models.Contract, int, error) {
// Update contract // 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) log.Printf("UpdateContractByID Error: Could not update contract: %v", err)
return contract, http.StatusInternalServerError, err return contract, http.StatusInternalServerError, err
} }

View File

@@ -8,55 +8,55 @@ import (
"gitlab.com/pactual1/backend/shared" "gitlab.com/pactual1/backend/shared"
) )
func GetInvoices(buyerName string, sortBy string, limit int, offset int, id uint) ([]models.Invoice, int64, error) { func GetInvoices(buyerName string, sortBy string, limit int, offset int, ids []int64) ([]models.Invoice, int64, error) {
var invoices []models.Invoice var invoices []models.Invoice
// Default sort by InvoiceDate DESC // Default sort by InvoiceDate DESC
sortField := "invoice_date" sortField := "invoice_date"
sortOrder := "desc" sortOrder := "desc"
if sortBy == "InvoiceDate" || sortBy == "InvoiceDueDate" { if sortBy == "InvoiceDate" || sortBy == "InvoiceDueDate" {
sortField = strings.ToLower(sortBy) sortField = strings.ToLower(sortBy)
} }
dbOrder := fmt.Sprintf("%s %s", sortField, sortOrder) dbOrder := fmt.Sprintf("%s %s", sortField, sortOrder)
db := shared.GetDb() db := shared.GetDb()
countDb := db countDb := db
if buyerName != "" { if buyerName != "" {
db = db.Where("buyer_name LIKE ?", "%"+buyerName+"%") db = db.Where("buyer_name LIKE ?", "%"+buyerName+"%")
countDb = countDb.Where("buyer_name LIKE ?", "%"+buyerName+"%") countDb = countDb.Where("buyer_name LIKE ?", "%"+buyerName+"%")
} }
// Added conditional for ID search // Added conditional for ID search
if id != 0 { if len(ids) > 0 {
db = db.Where("id = ?", id) db = db.Where("id in (?)", ids)
countDb = countDb.Where("id = ?", id) countDb = countDb.Where("id in (?)", ids)
} }
var total int64 var total int64
if err := countDb.Model(&models.Invoice{}).Count(&total).Error; err != nil { if err := countDb.Model(&models.Invoice{}).Count(&total).Error; err != nil {
return nil, 0, err return nil, 0, err
} }
if err := db.Preload("InvoiceItem"). if err := db.Preload("InvoiceItem").
Order(dbOrder). Order(dbOrder).
Limit(limit). Limit(limit).
Offset(offset). Offset(offset).
Find(&invoices).Error; err != nil { Find(&invoices).Error; err != nil {
return nil, 0, err return nil, 0, err
} }
for i := range invoices { for i := range invoices {
var sum int64 var sum int64
if invoices[i].InvoiceItem != nil { if invoices[i].InvoiceItem != nil {
for _, item := range *invoices[i].InvoiceItem { for _, item := range *invoices[i].InvoiceItem {
sum += item.PriceCents * item.Quantity sum += item.PriceCents * item.Quantity
} }
invoices[i].PriceCents = sum invoices[i].PriceCents = sum
} }
} }
return invoices, total, nil return invoices, total, nil
} }

View File

@@ -48,7 +48,7 @@ func main() {
// Add User and Device resources // Add User and Device resources
Admin.AddResource(&models.User{}) Admin.AddResource(&models.User{})
Admin.AddResource(&models.Device{}) Admin.AddResource(&models.Device{})
Admin.AddResource(&models.Buyer{}) Admin.AddResource(&models.Company{})
Admin.AddResource(&models.ProductTemplate{}) Admin.AddResource(&models.ProductTemplate{})
// Initialize HTTP request multiplexer // Initialize HTTP request multiplexer
mux := http.NewServeMux() mux := http.NewServeMux()

View File

@@ -1,18 +0,0 @@
package models
import "github.com/jinzhu/gorm"
type Buyer struct {
gorm.Model
Name string
}
func (Buyer) Update() (bool, error) {
return false, nil
}
func (Buyer) Create() (bool, error) {
return false, nil
}
func (Buyer) Delete() (bool, error) {
return false, nil
}

View File

@@ -1,23 +1,24 @@
package models package models
import ( import (
"database/sql"
"time" "time"
"github.com/jinzhu/gorm"
) )
type BaseModel struct { type BaseModel struct {
ID uint `json:"id" gorm:"primaryKey"` ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"createdAt" gorm:"autoCreateTime"` CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" gorm:"autoUpdateTime"` UpdatedAt time.Time `json:"updatedAt"`
DeletedAt sql.NullTime `json:"deletedAt,omitempty" gorm:"index"`
} }
type Company struct { type Company struct {
gorm.Model BaseModel
Name string `json:"name"` Name string `json:"name"`
Address string `json:"address"` Address string `json:"address"`
Email string `json:"email"` Email string `json:"email"`
Phone string `json:"phone"` Phone string `json:"phone"`
Users []User `json:"users"` Users []User `json:"users"`
Devices []Device `json:"devices"` Devices []Device `json:"devices"`
} }
@@ -26,7 +27,6 @@ type CompanyShortResponse struct {
Name string `json:"name"` Name string `json:"name"`
} }
func (Company) Update() (bool, error) { func (Company) Update() (bool, error) {
return false, nil return false, nil
} }

View File

@@ -3,12 +3,11 @@ package models
import ( import (
"time" "time"
"github.com/jinzhu/gorm"
"github.com/lib/pq" "github.com/lib/pq"
) )
type Contract struct { type Contract struct {
gorm.Model 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"`
@@ -64,7 +63,7 @@ type ContractResponse struct {
} }
func ConvertContractToResponse(contracts []Contract) []ContractResponse { func ConvertContractToResponse(contracts []Contract) []ContractResponse {
var contractResponses []ContractResponse contractResponses := []ContractResponse{}
for _, contract := range contracts { for _, contract := range contracts {
contractResponse := ContractResponse{ contractResponse := ContractResponse{
BaseModel: BaseModel{ BaseModel: BaseModel{
@@ -102,13 +101,13 @@ func ConvertContractToResponse(contracts []Contract) []ContractResponse {
} }
func ConvertContractToResponseModel(contracts []Contract) []ListContractResponse { func ConvertContractToResponseModel(contracts []Contract) []ListContractResponse {
var listInvoiceResponses []ListContractResponse listInvoiceResponses := []ListContractResponse{}
// Get all statuses // Get all statuses
statuses := GetContractStatuses() statuses := GetContractStatuses()
statusMap := make(map[string]Status) statusMap := make(map[string]Status)
for _, s := range statuses { for _, s := range statuses {
statusMap[s.Value] = s statusMap[s.Key] = s
} }
for _, contract := range contracts { for _, contract := range contracts {
@@ -171,16 +170,22 @@ type ListContractResponse struct {
} }
type CreateContractRequestPayload struct { type CreateContractRequestPayload struct {
SellerID uint `json:"sellerId" binding:"required"` SellerID uint `json:"sellerId" binding:"required"`
BuyerID uint `json:"buyerId" binding:"required"` BuyerID uint `json:"buyerId" binding:"required"`
Description string `json:"description"` Description string `json:"description"`
ProductID uint `json:"productId"` ProductID uint `json:"productId"`
MinTemp float64 `json:"minTemp" binding:"required"` MinTemp float64 `json:"minTemp" binding:"required"`
MaxTemp float64 `json:"maxTemp" binding:"required"` MaxTemp float64 `json:"maxTemp" binding:"required"`
ArrivalDate int64 `json:"arrivalDate"` ArrivalDate int64 `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"`
StartPlaceName string `json:"startPlaceName"`
StartLat float64 `json:"startLat"`
StartLon float64 `json:"startLon"`
EndPlaceName string `json:"endPlaceName"`
EndLat float64 `json:"endLat"`
EndLon float64 `json:"endLon"`
} }
func (Contract) Update() (bool, error) { func (Contract) Update() (bool, error) {

View File

@@ -1,9 +1,7 @@
package models package models
import "github.com/jinzhu/gorm"
type ContractInfo struct { type ContractInfo struct {
gorm.Model BaseModel
RawJSON string `json:"raw_json" gorm:"type:json"` RawJSON string `json:"raw_json" gorm:"type:json"`
} }

View File

@@ -4,26 +4,25 @@ import (
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
) )
type Device struct { type Device struct {
gorm.Model BaseModel
DeviceID string `json:"deviceId"` DeviceID string `json:"deviceId"`
DeviceName string `json:"deviceName"` DeviceName string `json:"deviceName"`
IMEI string `json:"imei"` IMEI string `json:"imei"`
IMSI string `json:"imsi"` IMSI string `json:"imsi"`
DeviceConfiguration string `json:"deviceConfiguration" gorm:"type:json"` DeviceConfiguration string `json:"deviceConfiguration" gorm:"type:json"`
CompanyID uint `json:"companyId"` CompanyID uint `json:"companyId"`
DeviceInfos *[]DeviceInfo `json:"deviceInfos"` DeviceInfos *[]DeviceInfo `json:"deviceInfos"`
} }
type DeviceResponse struct { type DeviceResponse struct {
BaseModel BaseModel
DeviceID string `json:"deviceId"` DeviceID string `json:"deviceId"`
DeviceName string `json:"deviceName"` DeviceName string `json:"deviceName"`
IMEI string `json:"imei"` IMEI string `json:"imei"`
IMSI string `json:"imsi"` IMSI string `json:"imsi"`
DeviceConfiguration string `json:"deviceConfiguration" gorm:"type:json"` DeviceConfiguration string `json:"deviceConfiguration" gorm:"type:json"`
CompanyID uint `json:"companyId"` CompanyID uint `json:"companyId"`
DeviceInfos *[]DeviceInfo `json:"deviceInfos"` DeviceInfos *[]DeviceInfo `json:"deviceInfos"`
} }
@@ -69,4 +68,3 @@ func (d *Device) Delete(db *gorm.DB) (bool, error) {
return true, nil return true, nil
} }

View File

@@ -1,9 +1,5 @@
package models package models
import (
"github.com/jinzhu/gorm"
)
// Location holds latitude and longitude. // Location holds latitude and longitude.
type Location struct { type Location struct {
Lat float64 `json:"lat"` Lat float64 `json:"lat"`
@@ -12,32 +8,31 @@ type Location struct {
// ImportantInfo holds fields that are important for quick access. // ImportantInfo holds fields that are important for quick access.
type SensorData struct { type SensorData struct {
IMEI string `json:"imei"` IMEI string `json:"imei"`
IMSI string `json:"imsi"` IMSI string `json:"imsi"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
Lat float64 `json:"lat"` Lat float64 `json:"lat"`
Lon float64 `json:"lon"` Lon float64 `json:"lon"`
WifiLoc Location `json:"wifiLocation"` WifiLoc Location `json:"wifiLocation"`
CellLoc Location `json:"cellLocation"` CellLoc Location `json:"cellLocation"`
Temperature float64 `json:"temperature"` Temperature float64 `json:"temperature"`
AccInfo AccelerometerInfo `json:"accelerometerInfo"` AccInfo AccelerometerInfo `json:"accelerometerInfo"`
AccelerometerInfo AccelerometerInfo
} }
type DeviceInfo struct { type DeviceInfo struct {
gorm.Model BaseModel
RawJSON string `json:"raw_json" gorm:"type:json"` RawJSON string `json:"raw_json" gorm:"type:json"`
SensorData SensorData
DeviceID uint DeviceID uint
ExternalDeviceID string `json:"deviceId"` ExternalDeviceID string `json:"deviceId"`
} }
type DeviceInfoResponse struct { type DeviceInfoResponse struct {
BaseModel BaseModel
RawJSON string `json:"rawJson" gorm:"type:json"` RawJSON string `json:"rawJson" gorm:"type:json"`
SensorData SensorData
DeviceID uint `json:"deviceId"` DeviceID uint `json:"deviceId"`
ExternalDeviceID string `json:"externalDeviceId"` ExternalDeviceID string `json:"externalDeviceId"`
} }
@@ -71,10 +66,10 @@ type GeoJSONFeatureCollection struct {
} }
type GeoJSONFeature struct { type GeoJSONFeature struct {
Type string `json:"type"` Type string `json:"type"`
Geometry GeoJSONGeometry `json:"geometry"` Geometry GeoJSONGeometry `json:"geometry"`
DeviceInfoResponse *DeviceInfoResponse `json:"deviceInfo,omitempty"` DeviceInfoResponse *DeviceInfoResponse `json:"deviceInfo,omitempty"`
Properties map[string]interface{} `json:"properties"` Properties map[string]interface{} `json:"properties"`
} }
type GeoJSONGeometry struct { type GeoJSONGeometry struct {
@@ -91,6 +86,3 @@ func (DeviceInfo) Create() (bool, error) {
func (DeviceInfo) Delete() (bool, error) { func (DeviceInfo) Delete() (bool, error) {
return false, nil return false, nil
} }

View File

@@ -3,68 +3,62 @@ package models
import ( import (
"database/sql" "database/sql"
"time" "time"
"github.com/jinzhu/gorm"
) )
type Invoice struct { type Invoice struct {
gorm.Model BaseModel
ID uint `json:"id" gorm:"primaryKey"` BuyerID uint `json:"buyerId"`
CreatedAt time.Time `json:"createdAt"` BuyerName string `json:"buyerName"`
UpdatedAt time.Time `json:"updatedAt"` BuyerAddress string `json:"buyerAddress"`
DeletedAt sql.NullTime `json:"deletedAt" gorm:"index"` BuyerEmail string `json:"buyerEmail"`
BuyerID uint `json:"buyerId"` BuyerPhone string `json:"buyerPhone"`
BuyerName string `json:"buyerName"` SellerID uint `json:"sellerId"`
BuyerAddress string `json:"buyerAddress"` SellerName string `json:"sellerName"`
BuyerEmail string `json:"buyerEmail"` SellerAddress string `json:"sellerAddress"`
BuyerPhone string `json:"buyerPhone"` SellerEmail string `json:"sellerEmail"`
SellerID uint `json:"sellerId"` SellerPhone string `json:"sellerPhone"`
SellerName string `json:"sellerName"` PriceCents int64 `json:"priceCents" gorm:"column:price_cents"`
SellerAddress string `json:"sellerAddress"` Discount int64 `json:"discount"`
SellerEmail string `json:"sellerEmail"` Tax int64 `json:"tax"`
SellerPhone string `json:"sellerPhone"` TermsAndConditions string `json:"termsAndConditions"`
PriceCents int64 `json:"priceCents" gorm:"column:price_cents"` InvoiceName string `json:"invoiceName"`
Discount int64 `json:"discount"` InvoiceDate time.Time `json:"invoiceDate"`
Tax int64 `json:"tax"` InvoiceDueDate time.Time `json:"invoiceDueDate"`
TermsAndConditions string `json:"termsAndConditions"` ContractID uint `json:"contractId"`
InvoiceName string `json:"invoiceName"`
InvoiceDate time.Time `json:"invoiceDate"`
InvoiceDueDate time.Time `json:"invoiceDueDate"`
ContractID uint `json:"contractId"`
InvoiceItem *[]InvoiceItem `json:"invoiceItem"` InvoiceItem *[]InvoiceItem `json:"invoiceItem"`
Status string `json:"status"` Status string `json:"status"`
} }
type InvoiceResponse struct { type InvoiceResponse struct {
BaseModel BaseModel
ID uint `json:"id" gorm:"primaryKey"` ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"` UpdatedAt time.Time `json:"updatedAt"`
DeletedAt sql.NullTime `json:"deletedAt" gorm:"index"` DeletedAt sql.NullTime `json:"deletedAt" gorm:"index"`
BuyerID uint `json:"buyerId"` BuyerID uint `json:"buyerId"`
BuyerName string `json:"buyerName"` BuyerName string `json:"buyerName"`
BuyerAddress string `json:"buyerAddress"` BuyerAddress string `json:"buyerAddress"`
BuyerEmail string `json:"buyerEmail"` BuyerEmail string `json:"buyerEmail"`
BuyerPhone string `json:"buyerPhone"` BuyerPhone string `json:"buyerPhone"`
SellerID uint `json:"sellerId"` SellerID uint `json:"sellerId"`
SellerName string `json:"sellerName"` SellerName string `json:"sellerName"`
SellerAddress string `json:"sellerAddress"` SellerAddress string `json:"sellerAddress"`
SellerEmail string `json:"sellerEmail"` SellerEmail string `json:"sellerEmail"`
SellerPhone string `json:"sellerPhone"` SellerPhone string `json:"sellerPhone"`
PriceCents int64 `json:"priceCents" gorm:"column:price_cents"` PriceCents int64 `json:"priceCents" gorm:"column:price_cents"`
Discount int64 `json:"discount"` Discount int64 `json:"discount"`
Tax int64 `json:"tax"` Tax int64 `json:"tax"`
TermsAndConditions string `json:"termsAndConditions"` TermsAndConditions string `json:"termsAndConditions"`
InvoiceName string `json:"invoiceName"` InvoiceName string `json:"invoiceName"`
InvoiceDate time.Time `json:"invoiceDate"` InvoiceDate time.Time `json:"invoiceDate"`
InvoiceDueDate time.Time `json:"invoiceDueDate"` InvoiceDueDate time.Time `json:"invoiceDueDate"`
ContractID uint `json:"contractId"` ContractID uint `json:"contractId"`
InvoiceItem *[]InvoiceItem `json:"invoiceItem"` InvoiceItem *[]InvoiceItem `json:"invoiceItem"`
Status string `json:"status"` Status string `json:"status"`
} }
func ConvertInvoiceToResponse(invoices []Invoice) []InvoiceResponse { func ConvertInvoiceToResponse(invoices []Invoice) []InvoiceResponse {
var invoiceResponses []InvoiceResponse invoiceResponses := []InvoiceResponse{}
for _, invoice := range invoices { for _, invoice := range invoices {
if invoice.InvoiceItem == nil { if invoice.InvoiceItem == nil {
emptySlice := make([]InvoiceItem, 0) emptySlice := make([]InvoiceItem, 0)
@@ -103,23 +97,19 @@ func ConvertInvoiceToResponse(invoices []Invoice) []InvoiceResponse {
return invoiceResponses return invoiceResponses
} }
type ListInvoiceResponse struct { type ListInvoiceResponse 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"`
DateCreated time.Time `json:"dateCreated"` DateCreated time.Time `json:"dateCreated"`
DueDate time.Time `json:"dueDate"` DueDate time.Time `json:"dueDate"`
Amount string `json:"amount"` Amount string `json:"amount"`
} }
type KeyValue struct { type KeyValue struct {
Key string `json:"key"` Key string `json:"key"`
Value string `json:"value"` Value string `json:"value"`
} }
func GetInvoiceStatuses() []Status { func GetInvoiceStatuses() []Status {
return []Status{ return []Status{
{Key: "Insurance claimed", Value: "insurance_claimed"}, {Key: "Insurance claimed", Value: "insurance_claimed"},

View File

@@ -3,34 +3,28 @@ package models
import ( import (
"database/sql" "database/sql"
"time" "time"
"github.com/jinzhu/gorm"
) )
type InvoiceItem struct { type InvoiceItem struct {
gorm.Model BaseModel
ID uint `json:"id" gorm:"primaryKey"` Description string `json:"description"`
CreatedAt time.Time `json:"createdAt"` Quantity int64 `json:"quantity"`
UpdatedAt time.Time `json:"updatedAt"` Unit string `json:"unit"`
DeletedAt sql.NullTime `json:"deletedAt" gorm:"index"` PriceCents int64 `json:"priceCents" gorm:"column:price_cents"`
Description string `json:"description"` InvoiceID uint `json:"invoiceId"`
Quantity int64 `json:"quantity"`
Unit string `json:"unit"`
PriceCents int64 `json:"priceCents" gorm:"column:price_cents"`
InvoiceID uint `json:"invoiceId"`
} }
type InvoiceItemResponse struct { type InvoiceItemResponse struct {
BaseModel BaseModel
ID uint `json:"id" gorm:"primaryKey"` ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"` UpdatedAt time.Time `json:"updatedAt"`
DeletedAt sql.NullTime `json:"deletedAt" gorm:"index"` DeletedAt sql.NullTime `json:"deletedAt" gorm:"index"`
Description string `json:"description"` Description string `json:"description"`
Quantity int64 `json:"quantity"` Quantity int64 `json:"quantity"`
Unit string `json:"unit"` Unit string `json:"unit"`
PriceCents int64 `json:"priceCents" gorm:"column:price_cents"` PriceCents int64 `json:"priceCents" gorm:"column:price_cents"`
InvoiceID uint `json:"invoiceId"` InvoiceID uint `json:"invoiceId"`
} }
// ConvertSliceOfInvoiceItemToResponse converts a slice of InvoiceItem models to a slice of InvoiceItemResponse models // ConvertSliceOfInvoiceItemToResponse converts a slice of InvoiceItem models to a slice of InvoiceItemResponse models
@@ -54,9 +48,6 @@ func ConvertInvoiceItemToResponse(items []InvoiceItem) []InvoiceItemResponse {
return itemResponses return itemResponses
} }
func (InvoiceItem) Update() (bool, error) { func (InvoiceItem) Update() (bool, error) {
return false, nil return false, nil
} }
@@ -66,4 +57,3 @@ func (InvoiceItem) Create() (bool, error) {
func (InvoiceItem) Delete() (bool, error) { func (InvoiceItem) Delete() (bool, error) {
return false, nil return false, nil
} }

View File

@@ -1,12 +1,8 @@
package models package models
import (
"github.com/jinzhu/gorm"
)
type ProductTemplate struct { type ProductTemplate struct {
gorm.Model BaseModel
Name string Name string `json:"name"`
ProductTemplateConfig map[string]interface{} `json:",omitempty" sql:"-"` ProductTemplateConfig map[string]interface{} `json:",omitempty" sql:"-"`
Config string `json:"raw_config" gorm:"type:json"` Config string `json:"raw_config" gorm:"type:json"`
} }

View File

@@ -1,10 +1,9 @@
package models package models
import "github.com/jinzhu/gorm"
type TextTemplate struct { type TextTemplate struct {
gorm.Model BaseModel
Value string Name string `json:"name"`
Value string `json:"value"`
} }
func (TextTemplate) Update() (bool, error) { func (TextTemplate) Update() (bool, error) {

View File

@@ -1,18 +1,7 @@
package models package models
import (
"database/sql"
"time"
"github.com/jinzhu/gorm"
)
type User struct { type User struct {
gorm.Model BaseModel
ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt sql.NullTime `json:"deletedAt" gorm:"index"`
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` Password string `json:"password"`
Email string `json:"email"` Email string `json:"email"`

View File

@@ -8,6 +8,9 @@ import (
func RegisterPublicRoutes(r *gin.Engine) { func RegisterPublicRoutes(r *gin.Engine) {
// Health checks
r.GET("/health", controllers.HealthCheck)
// Map dashboard // Map dashboard
r.GET("/dashboard/map/contract/devices", controllers.GetDevicesByContract) r.GET("/dashboard/map/contract/devices", controllers.GetDevicesByContract)
r.GET("/dashboard/map/contracts", controllers.GetLatestContracts) r.GET("/dashboard/map/contracts", controllers.GetLatestContracts)
@@ -15,14 +18,14 @@ func RegisterPublicRoutes(r *gin.Engine) {
// Invoices // Invoices
r.GET("/invoices", controllers.GetInvoices) r.GET("/invoices", controllers.GetInvoices)
r.GET("/invoice/:id", controllers.GetInvoiceByID) r.GET("/invoices/:id", controllers.GetInvoiceByID)
r.POST("/device_data/save", controllers.SaveDeviceInfo) r.POST("/device_data/save", controllers.SaveDeviceInfo)
r.GET("/buyers/", controllers.ListBuyers) r.GET("/buyers", controllers.ListCompanies)
r.GET("/product_templates/", controllers.ListProductTemplates) r.GET("/products", controllers.ListProductTemplates)
r.GET("/text_templates/", controllers.ListTextTemplates) r.GET("/templates", controllers.ListTextTemplates)
r.POST("/text_templates/save", controllers.CreateTextTemplate) r.POST("/templates/save", controllers.CreateTextTemplate)
r.GET("/product_templates/:template_id", controllers.GetProductTemplate) r.GET("/products/:template_id", controllers.GetProductTemplate)
// Contracts // Contracts
r.GET("/contracts/statuses", controllers.GetContractStatuses) r.GET("/contracts/statuses", controllers.GetContractStatuses)

View File

@@ -32,7 +32,7 @@ func Init() error {
} }
//TODO AUTOMIGRATE models once we have them //TODO AUTOMIGRATE models once we have them
db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{}, &models.DeviceInfo{}, db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{}, &models.DeviceInfo{},
&models.Contract{}, &models.ContractInfo{}, &models.Buyer{}, &models.Contract{}, &models.ContractInfo{},
&models.ProductTemplate{}, &models.TextTemplate{}, &models.Invoice{}, &models.InvoiceItem{} ) &models.ProductTemplate{}, &models.TextTemplate{}, &models.Invoice{}, &models.InvoiceItem{} )
return nil return nil