initial generic import backend code
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"hammond/models"
|
||||||
"hammond/service"
|
"hammond/service"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -12,6 +13,7 @@ import (
|
|||||||
func RegisteImportController(router *gin.RouterGroup) {
|
func RegisteImportController(router *gin.RouterGroup) {
|
||||||
router.POST("/import/fuelly", fuellyImport)
|
router.POST("/import/fuelly", fuellyImport)
|
||||||
router.POST("/import/drivvo", drivvoImport)
|
router.POST("/import/drivvo", drivvoImport)
|
||||||
|
router.POST("/import/generic", genericImport)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fuellyImport(c *gin.Context) {
|
func fuellyImport(c *gin.Context) {
|
||||||
@@ -52,3 +54,21 @@ func drivvoImport(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
c.JSON(http.StatusOK, gin.H{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func genericImport(c *gin.Context) {
|
||||||
|
var json models.ImportData
|
||||||
|
if err := c.ShouldBindJSON(&json); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if json.VehicleId == "" {
|
||||||
|
c.JSON(http.StatusUnprocessableEntity, "Missing Vehicle ID")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
errors := service.GenericImport(json, c.MustGet("userId").(string))
|
||||||
|
if len(errors) > 0 {
|
||||||
|
c.JSON(http.StatusUnprocessableEntity, gin.H{"errors": errors})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{})
|
||||||
|
}
|
||||||
|
|||||||
22
server/models/import.go
Normal file
22
server/models/import.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type ImportData struct {
|
||||||
|
Data []ImportFillup `json:"data" binding:"required"`
|
||||||
|
VehicleId string `json:"vehicleId" binding:"required"`
|
||||||
|
TimeZone string `json:"timezone" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ImportFillup struct {
|
||||||
|
VehicleID string `json:"vehicleId"`
|
||||||
|
FuelQuantity float32 `json:"fuelQuantity"`
|
||||||
|
PerUnitPrice float32 `json:"perUnitPrice"`
|
||||||
|
TotalAmount float32 `json:"totalAmount"`
|
||||||
|
OdoReading int `json:"odoReading"`
|
||||||
|
IsTankFull *bool `json:"isTankFull"`
|
||||||
|
HasMissedFillup *bool `json:"hasMissedFillup"`
|
||||||
|
Comments string `json:"comments"`
|
||||||
|
FillingStation string `json:"fillingStation"`
|
||||||
|
UserID string `json:"userId"`
|
||||||
|
Date string `json:"date"`
|
||||||
|
FuelSubType string `json:"fuelSubType"`
|
||||||
|
}
|
||||||
47
server/service/genericImportService.go
Normal file
47
server/service/genericImportService.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"hammond/db"
|
||||||
|
"hammond/models"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenericParseRefuelings(content []models.ImportFillup, user *db.User, vehicle *db.Vehicle, timezone string) ([]db.Fillup, []string) {
|
||||||
|
var errors []string
|
||||||
|
var fillups []db.Fillup
|
||||||
|
dateLayout := "2023-04-16T04:41:25.682Z"
|
||||||
|
loc, _ := time.LoadLocation(timezone)
|
||||||
|
for _, record := range content {
|
||||||
|
date, err := time.ParseInLocation(record.Date, dateLayout, loc)
|
||||||
|
if err != nil {
|
||||||
|
date = time.Date(2000, time.December, 0, 0, 0, 0, 0, loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
var missedFillup bool
|
||||||
|
if record.HasMissedFillup == nil {
|
||||||
|
missedFillup = false
|
||||||
|
} else {
|
||||||
|
missedFillup = *record.HasMissedFillup
|
||||||
|
}
|
||||||
|
|
||||||
|
fillups = append(fillups, db.Fillup{
|
||||||
|
VehicleID: vehicle.ID,
|
||||||
|
UserID: user.ID,
|
||||||
|
Date: date,
|
||||||
|
IsTankFull: record.IsTankFull,
|
||||||
|
HasMissedFillup: &missedFillup,
|
||||||
|
FuelQuantity: float32(record.FuelQuantity),
|
||||||
|
PerUnitPrice: float32(record.PerUnitPrice),
|
||||||
|
FillingStation: record.FillingStation,
|
||||||
|
OdoReading: record.OdoReading,
|
||||||
|
TotalAmount: float32(record.TotalAmount),
|
||||||
|
FuelUnit: vehicle.FuelUnit,
|
||||||
|
Currency: user.Currency,
|
||||||
|
DistanceUnit: user.DistanceUnit,
|
||||||
|
Comments: record.Comments,
|
||||||
|
Source: "Generic Import",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return fillups, errors
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
"hammond/db"
|
"hammond/db"
|
||||||
|
"hammond/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WriteToDB(fillups []db.Fillup, expenses []db.Expense) []string {
|
func WriteToDB(fillups []db.Fillup, expenses []db.Expense) []string {
|
||||||
@@ -105,3 +106,27 @@ func FuellyImport(content []byte, userId string) []string {
|
|||||||
|
|
||||||
return WriteToDB(fillups, expenses)
|
return WriteToDB(fillups, expenses)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GenericImport(content models.ImportData, userId string) []string {
|
||||||
|
var errors []string
|
||||||
|
user, err := GetUserById(userId)
|
||||||
|
if err != nil {
|
||||||
|
errors = append(errors, err.Error())
|
||||||
|
return errors
|
||||||
|
}
|
||||||
|
|
||||||
|
vehicle, err := GetVehicleById(content.VehicleId)
|
||||||
|
if err != nil {
|
||||||
|
errors = append(errors, err.Error())
|
||||||
|
return errors
|
||||||
|
}
|
||||||
|
|
||||||
|
var fillups []db.Fillup
|
||||||
|
fillups, errors = GenericParseRefuelings(content.Data, user, vehicle, content.TimeZone)
|
||||||
|
|
||||||
|
if len(errors) != 0 {
|
||||||
|
return errors
|
||||||
|
}
|
||||||
|
|
||||||
|
return WriteToDB(fillups, nil)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user