solidarity transport updates
This commit is contained in:
19
utils/protectapi/api_key.go
Normal file
19
utils/protectapi/api_key.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package protectapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func ApiKey(apiKey string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if apiKey == "" || r.Header.Get("X_API_KEY") != apiKey {
|
||||
log.Info().Msg("call to api not allowed")
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,15 @@
|
||||
package sorting
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/fleets/storage"
|
||||
fleetsstorage "git.coopgo.io/coopgo-platform/fleets/storage"
|
||||
"github.com/paulmach/orb/geo"
|
||||
"github.com/paulmach/orb/geojson"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type VehiclesByLicencePlate []fleetsstorage.Vehicle
|
||||
@@ -21,3 +27,47 @@ func (a BookingsByStartdate) Less(i, j int) bool {
|
||||
return a[i].Startdate.Before(a[j].Startdate)
|
||||
}
|
||||
func (a BookingsByStartdate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
|
||||
// Functions
|
||||
|
||||
func VehiclesByDistanceFrom(from geojson.Feature) func(vehicle1, vehicle2 storage.Vehicle) int {
|
||||
return func(vehicle1, vehicle2 storage.Vehicle) int {
|
||||
vehicle1Address, ok := vehicle1.Data["address"]
|
||||
if !ok {
|
||||
return 1
|
||||
}
|
||||
vehicle1Json, err := json.Marshal(vehicle1Address)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed marshalling vehicle 1 json")
|
||||
return 1
|
||||
}
|
||||
|
||||
vehicle1Geojson, err := geojson.UnmarshalFeature(vehicle1Json)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed unmarshalling vehicle 1 geojson")
|
||||
return 1
|
||||
}
|
||||
|
||||
vehicle2Address, ok := vehicle2.Data["address"]
|
||||
if !ok {
|
||||
log.Debug().Msg("Vehicle 2 does not have an address")
|
||||
return -1
|
||||
}
|
||||
vehicle2Json, err := json.Marshal(vehicle2Address)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed marshalling vehicle 2 json")
|
||||
return -1
|
||||
}
|
||||
|
||||
vehicle2Geojson, err := geojson.UnmarshalFeature(vehicle2Json)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed unmarshalling vehicle 2 geojson")
|
||||
return -1
|
||||
}
|
||||
|
||||
distance1 := geo.Distance(from.Point(), vehicle1Geojson.Point())
|
||||
distance2 := geo.Distance(from.Point(), vehicle2Geojson.Point())
|
||||
|
||||
return cmp.Compare(distance1, distance2)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package sorting
|
||||
|
||||
import (
|
||||
mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
||||
)
|
||||
|
||||
type SolidarityDriversByName []mobilityaccountsstorage.Account
|
||||
|
||||
func (e SolidarityDriversByName) Len() int { return len(e) }
|
||||
func (e SolidarityDriversByName) Less(i, j int) bool {
|
||||
return e[i].Data["first_name"].(string) < e[j].Data["first_name"].(string)
|
||||
}
|
||||
func (e SolidarityDriversByName) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
|
||||
32
utils/sorting/solidarity-transport.go
Normal file
32
utils/sorting/solidarity-transport.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package sorting
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
||||
"git.coopgo.io/coopgo-platform/solidarity-transport/servers/grpc/proto/gen"
|
||||
)
|
||||
|
||||
type SolidarityDriversByName []mobilityaccountsstorage.Account
|
||||
|
||||
func (e SolidarityDriversByName) Len() int { return len(e) }
|
||||
func (e SolidarityDriversByName) Less(i, j int) bool {
|
||||
return e[i].Data["first_name"].(string) < e[j].Data["first_name"].(string)
|
||||
}
|
||||
func (e SolidarityDriversByName) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
|
||||
|
||||
type SolidarityAvailabilitiesByDay []*gen.DriverRegularAvailability
|
||||
|
||||
func (e SolidarityAvailabilitiesByDay) Len() int { return len(e) }
|
||||
func (e SolidarityAvailabilitiesByDay) Less(i, j int) bool {
|
||||
if e[i].Day == e[j].Day {
|
||||
return strings.Compare(e[i].StartTime, e[j].StartTime) < 0
|
||||
}
|
||||
|
||||
if e[i].Day == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return e[i].Day < e[j].Day
|
||||
}
|
||||
func (e SolidarityAvailabilitiesByDay) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
|
||||
@@ -8,10 +8,12 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
PREFIX_BENEFICIARIES = "beneficiaries"
|
||||
PREFIX_BOOKINGS = "fleets_bookings"
|
||||
PREFIX_AGENDA = "event_files"
|
||||
PREFIX_DIAGS = "diags"
|
||||
PREFIX_BENEFICIARIES = "beneficiaries"
|
||||
PREFIX_SOLIDARITY_TRANSPORT_DRIVERS = "solidarity_transport/drivers"
|
||||
PREFIX_ORGANIZED_CARPOOL_DRIVERS = "organized_carpool/drivers"
|
||||
PREFIX_BOOKINGS = "fleets_bookings"
|
||||
PREFIX_AGENDA = "event_files"
|
||||
PREFIX_DIAGS = "diags"
|
||||
)
|
||||
|
||||
type FileInfo struct {
|
||||
|
||||
102
utils/validated-profile/validated-profile.go
Normal file
102
utils/validated-profile/validated-profile.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package validatedprofile
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
|
||||
mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/objx"
|
||||
)
|
||||
|
||||
type Comparison struct {
|
||||
Field string
|
||||
Type string
|
||||
Value any
|
||||
}
|
||||
|
||||
func ValidateProfile(cfg *viper.Viper) func(mobilityaccountsstorage.Account, []storage.FileInfo) bool {
|
||||
enabled := cfg.GetBool("enabled")
|
||||
requiredDocuments := cfg.GetStringSlice("required.documents")
|
||||
requiredFields := cfg.GetStringSlice("required.fields")
|
||||
comp := cfg.Get("assert.compare")
|
||||
var comparisons []Comparison
|
||||
err := mapstructure.Decode(comp, &comparisons)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("reading comparisons issue")
|
||||
}
|
||||
return func(account mobilityaccountsstorage.Account, docs []storage.FileInfo) bool {
|
||||
if !enabled {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, d := range requiredDocuments {
|
||||
if !slices.ContainsFunc(docs, func(f storage.FileInfo) bool {
|
||||
log.Debug().Str("required", d).Str("checked", f.Metadata["Type"]).Msg("file check")
|
||||
return f.Metadata["Type"] == d
|
||||
}) {
|
||||
log.Debug().Msg("file missing")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
obj := objx.Map(account.Data)
|
||||
|
||||
for _, f := range requiredFields {
|
||||
if obj.Get(f) == nil {
|
||||
log.Debug().Msg("field missing")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
for _, c := range comparisons {
|
||||
val := obj.Get(c.Field)
|
||||
if val == nil {
|
||||
return false
|
||||
}
|
||||
value := ""
|
||||
if v, ok := c.Value.(string); ok {
|
||||
value = v
|
||||
} else if v, ok := c.Value.(time.Time); ok {
|
||||
value = v.Format("2006-01-02")
|
||||
} else {
|
||||
log.Error().Msg("could not get type")
|
||||
return false
|
||||
}
|
||||
result := cmp.Compare(val.String(), value)
|
||||
|
||||
if c.Type == "gte" {
|
||||
if result < 0 {
|
||||
log.Debug().Int("comparison result", result).Str("operand", c.Type).Msg("comparison issue")
|
||||
return false
|
||||
}
|
||||
} else if c.Type == "gt" {
|
||||
if result <= 0 {
|
||||
log.Debug().Int("comparison result", result).Str("operand", c.Type).Msg("comparison issue")
|
||||
return false
|
||||
}
|
||||
} else if c.Type == "lt" {
|
||||
if result >= 0 {
|
||||
log.Debug().Int("comparison result", result).Str("operand", c.Type).Msg("comparison issue")
|
||||
return false
|
||||
}
|
||||
} else if c.Type == "lte" {
|
||||
if result < 0 {
|
||||
log.Debug().Int("comparison result", result).Str("operand", c.Type).Msg("comparison issue")
|
||||
return false
|
||||
}
|
||||
} else if c.Type == "eq" {
|
||||
if result != 0 {
|
||||
log.Debug().Int("comparison result", result).Str("operand", c.Type).Msg("comparison issue")
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user