fleets/storage/vehicles.go

74 lines
1.7 KiB
Go
Raw Permalink Normal View History

2022-08-12 12:49:16 +00:00
package storage
2022-11-07 00:33:53 +00:00
import (
"fmt"
"time"
)
2022-08-12 12:49:16 +00:00
type Vehicle struct {
ID string `json:"id" bson:"_id"`
Type string `json:"type"`
Namespace string `json:"namespace"`
Administrators []string `json:"administrators"`
Data map[string]any `json:"data"`
Metadata map[string]any `json:"metadata"`
Bookings []Booking
}
2022-11-07 00:33:53 +00:00
type VehicleFilters struct {
2023-03-10 14:44:46 +00:00
Types []string
Administrators []string
AvailableFrom time.Time
AvailableTo time.Time
2022-11-07 00:33:53 +00:00
}
2022-08-12 12:49:16 +00:00
func (v Vehicle) Free(start time.Time, end time.Time) bool {
for _, b := range v.Bookings {
2022-12-05 16:20:30 +00:00
if ((start.Before(b.Unavailablefrom) || start.Equal(b.Unavailablefrom)) && (end.After(b.Unavailablefrom.Add(24*time.Hour)) || end.Equal(b.Unavailablefrom.Add(24*time.Hour)))) ||
((start.Before(b.Unavailableto) || start.Equal(b.Unavailableto)) && (end.After(b.Unavailableto.Add(24*time.Hour)) || end.Equal(b.Unavailableto.Add(24*time.Hour)))) ||
((start.After(b.Unavailablefrom) || start.Equal(b.Unavailablefrom)) && (end.Before(b.Unavailableto.Add(24*time.Hour)) || end.Equal(b.Unavailableto.Add(24*time.Hour)))) {
2022-08-12 12:49:16 +00:00
return false
}
}
return true
}
2022-11-07 00:33:53 +00:00
func (v Vehicle) MatchesFilters(filters VehicleFilters) bool {
2023-03-10 15:10:32 +00:00
fmt.Println("Filters : ", filters)
2022-11-07 00:33:53 +00:00
if len(filters.Types) > 0 {
found := false
for _, t := range filters.Types {
if t == v.Type {
found = true
break
}
}
if !found {
return false
}
}
2023-03-10 14:44:46 +00:00
if len(filters.Administrators) > 0 {
found := false
for _, a := range filters.Administrators {
for _, va := range v.Administrators {
if a == va {
found = true
break
}
}
}
if !found {
return false
}
}
2022-11-07 00:33:53 +00:00
if !v.Free(filters.AvailableFrom, filters.AvailableTo) {
return false
}
return true
}