Add option for not importing location

This commit is contained in:
Alf Sebastian Houge
2022-04-06 12:06:45 +02:00
parent 15cf09f326
commit dc33aaad49
3 changed files with 24 additions and 6 deletions

View File

@@ -2,6 +2,7 @@ package controllers
import ( import (
"net/http" "net/http"
"strconv"
"github.com/akhilrex/hammond/service" "github.com/akhilrex/hammond/service"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -37,7 +38,13 @@ func drivvoImport(c *gin.Context) {
c.JSON(http.StatusUnprocessableEntity, "Missing Vehicle ID") c.JSON(http.StatusUnprocessableEntity, "Missing Vehicle ID")
return return
} }
errors := service.DrivvoImport(bytes, c.MustGet("userId").(string), vehicleId) importLocation, err := strconv.ParseBool(c.PostForm("importLocation"))
if err != nil {
c.JSON(http.StatusUnprocessableEntity, "Please include importLocation option.")
return
}
errors := service.DrivvoImport(bytes, c.MustGet("userId").(string), vehicleId, importLocation)
if len(errors) > 0 { if len(errors) > 0 {
c.JSON(http.StatusUnprocessableEntity, gin.H{"errors": errors}) c.JSON(http.StatusUnprocessableEntity, gin.H{"errors": errors})
return return

View File

@@ -65,7 +65,7 @@ func DrivvoParseExpenses(content []byte, user *db.User, vehicle *db.Vehicle) ([]
return expenses, errors return expenses, errors
} }
func DrivvoParseRefuelings(content []byte, user *db.User, vehicle *db.Vehicle) ([]db.Fillup, []string) { func DrivvoParseRefuelings(content []byte, user *db.User, vehicle *db.Vehicle, importLocation bool) ([]db.Fillup, []string) {
refuelingReader := csv.NewReader(bytes.NewReader(content)) refuelingReader := csv.NewReader(bytes.NewReader(content))
refuelingReader.Comment = '#' refuelingReader.Comment = '#'
refuelingRecords, err := refuelingReader.ReadAll() refuelingRecords, err := refuelingReader.ReadAll()
@@ -99,8 +99,10 @@ func DrivvoParseRefuelings(content []byte, user *db.User, vehicle *db.Vehicle) (
errors = append(errors, "Found an invalid odometer reading at refuel row "+strconv.Itoa(index+1)) errors = append(errors, "Found an invalid odometer reading at refuel row "+strconv.Itoa(index+1))
} }
// TODO: Make optional location := ""
location := record[17] if importLocation {
location = record[17]
}
pricePerUnit, err := strconv.ParseFloat(record[3], 32) pricePerUnit, err := strconv.ParseFloat(record[3], 32)
if err != nil { if err != nil {
@@ -142,7 +144,7 @@ func DrivvoParseRefuelings(content []byte, user *db.User, vehicle *db.Vehicle) (
return fillups, errors return fillups, errors
} }
func DrivvoImport(content []byte, userId string, vehicleId string) []string { func DrivvoImport(content []byte, userId string, vehicleId string, importLocation bool) []string {
var errors []string var errors []string
user, err := GetUserById(userId) user, err := GetUserById(userId)
if err != nil { if err != nil {
@@ -172,7 +174,7 @@ func DrivvoImport(content []byte, userId string, vehicleId string) []string {
expenseSectionIndex = endParseIndex expenseSectionIndex = endParseIndex
} }
fillups, errors := DrivvoParseRefuelings(content[:serviceSectionIndex], user, vehicle) fillups, errors := DrivvoParseRefuelings(content[:serviceSectionIndex], user, vehicle, importLocation)
_ = fillups _ = fillups
var allExpenses []db.Expense var allExpenses []db.Expense

View File

@@ -22,6 +22,7 @@ export default {
selectedVehicle: null, selectedVehicle: null,
tryingToCreate: false, tryingToCreate: false,
errors: [], errors: [],
importLocation: true,
} }
}, },
computed: { computed: {
@@ -56,6 +57,7 @@ export default {
this.errorMessage = '' this.errorMessage = ''
const formData = new FormData() const formData = new FormData()
formData.append('vehicleID', this.selectedVehicle) formData.append('vehicleID', this.selectedVehicle)
formData.append('importLocation', this.importLocation)
formData.append('file', this.file, this.file.name) formData.append('file', this.file, this.file.name)
axios axios
.post(`/api/import/drivvo`, formData) .post(`/api/import/drivvo`, formData)
@@ -129,6 +131,13 @@ export default {
</b-select> </b-select>
</b-field> </b-field>
</div> </div>
<div class="column">
<b-field>
<b-tooltip label="Whether to import the location for fillups and services or not." multilined>
<b-checkbox v-model="importLocation">Import Location?</b-checkbox>
</b-tooltip>
</b-field>
</div>
<div class="column"> <div class="column">
<b-field class="file is-primary" :class="{ 'has-name': !!file }"> <b-field class="file is-primary" :class="{ 'has-name': !!file }">