499 lines
14 KiB
Go
499 lines
14 KiB
Go
package application
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
|
|
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
|
groupstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
|
|
mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
|
"github.com/google/uuid"
|
|
"github.com/gorilla/mux"
|
|
geojson "github.com/paulmach/go.geojson"
|
|
"gitlab.scity.coop/maas/navitia-golang"
|
|
"gitlab.scity.coop/maas/navitia-golang/types"
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
)
|
|
|
|
var Depart any
|
|
var Arrive any
|
|
|
|
// type Typep string
|
|
|
|
// const (
|
|
// rec Typep = "rec"
|
|
// pon Typep = "pon"
|
|
// )
|
|
|
|
// var departdate string
|
|
// var departTime string
|
|
|
|
func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Request) {
|
|
r.ParseForm()
|
|
|
|
locTime, errTime := time.LoadLocation("Europe/Paris")
|
|
if errTime != nil {
|
|
fmt.Println("Loading timezone location Europe/Paris error : ")
|
|
fmt.Println("Missing zones in container ? ")
|
|
panic(errTime)
|
|
}
|
|
|
|
departuredate := r.FormValue("departuredate")
|
|
departuretime := r.FormValue("departuretime")
|
|
departuredatetime, _ := time.ParseInLocation("2006-01-02 15:04", fmt.Sprintf("%s %s", departuredate, departuretime), locTime)
|
|
|
|
departure := r.FormValue("departure")
|
|
destination := r.FormValue("destination")
|
|
|
|
searched := false
|
|
|
|
var (
|
|
departuregeo *geojson.Feature
|
|
destinationgeo *geojson.Feature
|
|
journeys *navitia.JourneyResults
|
|
carpoolresults any
|
|
vehicles = []any{}
|
|
)
|
|
|
|
if departuredate != "" && departuretime != "" && departure != "" && destination != "" {
|
|
searched = true
|
|
|
|
var err error
|
|
|
|
departuregeo, err = geojson.UnmarshalFeature([]byte(departure))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
destinationgeo, err = geojson.UnmarshalFeature([]byte(destination))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
//TODO make it a library
|
|
session, _ := navitia.NewCustom(
|
|
h.config.GetString("services.navitia.api_key"),
|
|
"https://api.navitia.io/v1",
|
|
&http.Client{})
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
request := navitia.JourneyRequest{
|
|
From: types.ID(fmt.Sprintf("%f", departuregeo.Geometry.Point[0]) + ";" + fmt.Sprintf("%f", departuregeo.Geometry.Point[1])),
|
|
To: types.ID(fmt.Sprintf("%f", destinationgeo.Geometry.Point[0]) + ";" + fmt.Sprintf("%f", destinationgeo.Geometry.Point[1])),
|
|
Date: departuredatetime.Add(-2 * time.Hour),
|
|
DateIsArrival: false, //TODO
|
|
}
|
|
|
|
journeys, err = session.Journeys(context.Background(), request)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
// w.WriteHeader(http.StatusBadRequest)
|
|
// return
|
|
}
|
|
|
|
//CARPOOL
|
|
// carpoolrequest := fmt.Sprintf(
|
|
// "https://api.rdex.ridygo.fr/journeys.json?p[driver][state]=1&frequency=punctual&p[passenger][state]=0&p[from][latitude]=%f&p[from][longitude]=%f&p[to][latitude]=%f&p[to][longitude]=%f&p[outward][mindate]=%s&p[outward][maxdate]=%s",
|
|
// departuregeo.Geometry.Point[1], departuregeo.Geometry.Point[0],
|
|
// destinationgeo.Geometry.Point[1], destinationgeo.Geometry.Point[0],
|
|
// departuredatetime.Format("2006-01-02"), departuredatetime.Add(24*time.Hour).Format("2006-01-02"))
|
|
carpoolrequest := "https://api.rdex.ridygo.fr/journeys.json"
|
|
|
|
client := &http.Client{}
|
|
req, err := http.NewRequest("GET", carpoolrequest, nil)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
req.URL.RawQuery = fmt.Sprintf(
|
|
"p[driver][state]=1&frequency=punctual&p[passenger][state]=0&p[from][latitude]=%f&p[from][longitude]=%f&p[to][latitude]=%f&p[to][longitude]=%f&p[outward][mindate]=%s&p[outward][maxdate]=%s",
|
|
departuregeo.Geometry.Point[1], departuregeo.Geometry.Point[0],
|
|
destinationgeo.Geometry.Point[1], destinationgeo.Geometry.Point[0],
|
|
departuredatetime.Format("2006-01-02"), departuredatetime.Format("2006-01-02"))
|
|
|
|
req.Header.Set("X-API-KEY", "123456")
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
if err == nil && resp.StatusCode == http.StatusOK {
|
|
err = json.NewDecoder(resp.Body).Decode(&carpoolresults)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
if carpoolresults == nil {
|
|
carpoolresults = []any{}
|
|
}
|
|
} else {
|
|
carpoolresults = []any{}
|
|
}
|
|
|
|
// Vehicles
|
|
|
|
vehiclerequest := &fleets.GetVehiclesRequest{
|
|
Namespaces: []string{"parcoursmob"},
|
|
}
|
|
vehicleresp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), vehiclerequest)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
|
|
for _, vehicle := range vehicleresp.Vehicles {
|
|
v := vehicle.ToStorageType()
|
|
if v.Free(departuredatetime.Add(-24*time.Hour), departuredatetime.Add(168*time.Hour)) {
|
|
vehicles = append(vehicles, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
h.Renderer.JourneysSearch(w, r, carpoolresults, journeys, vehicles, searched, departuregeo, destinationgeo, departuredate, departuretime)
|
|
}
|
|
|
|
type GroupsModule []groupstorage.Group
|
|
|
|
func (a GroupsModule) Len() int { return len(a) }
|
|
func (a GroupsModule) Less(i, j int) bool {
|
|
return strings.Compare(a[i].Data["name"].(string), a[j].Data["name"].(string)) < 0
|
|
}
|
|
func (a GroupsModule) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (h *ApplicationHandler) GroupsGestion(w http.ResponseWriter, r *http.Request) {
|
|
|
|
request := &groupsmanagement.GetGroupsRequest{
|
|
Namespaces: []string{"parcoursmob_groups_covoiturage"},
|
|
}
|
|
|
|
resp, err := h.services.GRPC.GroupsManagement.GetGroups(context.TODO(), request)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
var groups = []groupstorage.Group{}
|
|
|
|
for _, group := range resp.Groups {
|
|
g := group.ToStorageType()
|
|
groups = append(groups, g)
|
|
}
|
|
//////////////////
|
|
|
|
cacheid := uuid.NewString()
|
|
|
|
/////////////////
|
|
sort.Sort(GroupsModule(groups))
|
|
h.cache.PutWithTTL(cacheid, groups, 1*time.Hour)
|
|
|
|
h.Renderer.GroupsGestion(w, r, groups, cacheid)
|
|
}
|
|
func filterAcc(r *http.Request, a *mobilityaccounts.Account) bool {
|
|
searchFilter, ok := r.URL.Query()["search"]
|
|
|
|
if ok && len(searchFilter[0]) > 0 {
|
|
name := a.Data.AsMap()["first_name"].(string) + " " + a.Data.AsMap()["last_name"].(string)
|
|
if !strings.Contains(strings.ToLower(name), strings.ToLower(searchFilter[0])) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (h *ApplicationHandler) CreateGroup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var beneficiary any
|
|
var (
|
|
departurgeo *geojson.Feature
|
|
dstinationgeo *geojson.Feature
|
|
)
|
|
searched := false
|
|
// var accounts = []any{}
|
|
// cacheid := uuid.NewString()
|
|
// accountsBeneficaire, err := h.beneficiaries(r)
|
|
// if err != nil {
|
|
// fmt.Println(err)
|
|
// w.WriteHeader(http.StatusBadRequest)
|
|
// return
|
|
// }
|
|
if r.FormValue("beneficiaryid") != "" {
|
|
// Handler form
|
|
searched = true
|
|
|
|
requestbeneficiary := &mobilityaccounts.GetAccountRequest{
|
|
Id: r.FormValue("beneficiaryid"),
|
|
}
|
|
|
|
respbeneficiary, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), requestbeneficiary)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
beneficiary = respbeneficiary.Account.ToStorageType()
|
|
|
|
if r.Method == "POST" {
|
|
fmt.Println("herrre")
|
|
///////////////////////////////////////////
|
|
departure := r.FormValue("departure")
|
|
destination := r.FormValue("destination")
|
|
|
|
if departure != "" && destination != "" {
|
|
|
|
var err error
|
|
|
|
departurgeo, err = geojson.UnmarshalFeature([]byte(departure))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
dstinationgeo, err = geojson.UnmarshalFeature([]byte(destination))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
}
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
if r.FormValue("departure") != "" {
|
|
var a any
|
|
json.Unmarshal([]byte(r.FormValue("departure")), &a)
|
|
|
|
Depart = a
|
|
}
|
|
if r.FormValue("destination") != "" {
|
|
var a any
|
|
json.Unmarshal([]byte(r.FormValue("destination")), &a)
|
|
|
|
Arrive = a
|
|
}
|
|
r.ParseForm()
|
|
|
|
/////////////////
|
|
// locTime, errTime := time.LoadLocation("Europe/Paris")
|
|
// if errTime != nil {
|
|
// fmt.Println("Loading timezone location Europe/Paris error : ")
|
|
// fmt.Println("Missing zones in container ? ")
|
|
// panic(errTime)
|
|
// }
|
|
|
|
// departdate := r.FormValue("departuredate")
|
|
// departTime := r.FormValue("departuretime")
|
|
//departuredatetime, _ := time.ParseInLocation("2006-01-02 15:04", fmt.Sprintf("%s %s", departuredate, departuretime), locTime)
|
|
|
|
////////////////
|
|
if r.FormValue("name") == "" {
|
|
|
|
fmt.Println("invalid name")
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
// if r.FormValue("driver") == "" {
|
|
|
|
// fmt.Println("invalid name")
|
|
// w.WriteHeader(http.StatusBadRequest)
|
|
// return
|
|
// }
|
|
if r.FormValue("number") == "" {
|
|
|
|
fmt.Println("invalid number of personne")
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
// planType := map[string]any{
|
|
// "rrecurrent": r.FormValue("recurrent") == "on",
|
|
// "pponctuelle": r.FormValue("ponctuelle") == "on",
|
|
// }
|
|
planDays := map[string]any{
|
|
"lundi": r.FormValue("lundi") == "on",
|
|
"mardi": r.FormValue("mardi") == "on",
|
|
"mercredi": r.FormValue("mercredi") == "on",
|
|
"jeudi": r.FormValue("jeudi") == "on",
|
|
"vendredi": r.FormValue("vendredi") == "on",
|
|
"samedi": r.FormValue("samedi") == "on",
|
|
"dimanche": r.FormValue("dimanche") == "on",
|
|
}
|
|
// var rec bool = r.FormValue("recurrent")
|
|
// var ponc bool
|
|
groupidd := uuid.NewString()
|
|
fmt.Println("herrr&e")
|
|
dataMap := map[string]any{
|
|
"name": r.FormValue("name"),
|
|
"number": r.FormValue("number"),
|
|
"driver_first_name": respbeneficiary.Account.ToStorageType().Data["first_name"],
|
|
"driver_last_name": respbeneficiary.Account.ToStorageType().Data["last_name"],
|
|
"depart": Depart,
|
|
"arrive": Arrive,
|
|
"departdate": r.FormValue("departdate"),
|
|
"date": r.FormValue("date"),
|
|
"enddate": r.FormValue("enddate"),
|
|
"departtime": r.FormValue("departtime"),
|
|
"time": r.FormValue("time"),
|
|
// "ponctdate": r.FormValue("ponctdate"),
|
|
//"planType": planType,
|
|
"planDays": planDays,
|
|
"recurrent": r.FormValue("recurrent"),
|
|
"pontuelle": r.FormValue("ponctuelle"),
|
|
// "r": rec,
|
|
// "p": pon,
|
|
}
|
|
fmt.Println(dataMap["ponctuelle"])
|
|
data, err := structpb.NewValue(dataMap)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
request_organization := &groupsmanagement.AddGroupRequest{
|
|
Group: &groupsmanagement.Group{
|
|
Id: groupidd,
|
|
Namespace: "parcoursmob_groups_covoiturage",
|
|
Data: data.GetStructValue(),
|
|
},
|
|
}
|
|
|
|
_, err = h.services.GRPC.GroupsManagement.AddGroup(context.TODO(), request_organization)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
//////////////////////////////////////////////////
|
|
// request_organizatio := &groupsmanagement.AddGroupMemberRequest{
|
|
// Group: &groupsmanagement.GroupMember{
|
|
// Id: groupidd,
|
|
// //Namespace: "parcoursmob_groups_covoiturage",
|
|
// Data: data.GetStructValue(),
|
|
// },
|
|
// }
|
|
|
|
// _, err = h.services.GRPC.GroupsManagement.AddGroupMember(context.TODO(), request_organizatio)
|
|
// if err != nil {
|
|
// fmt.Println(err)
|
|
// w.WriteHeader(http.StatusInternalServerError)
|
|
// return
|
|
// }
|
|
////////////////////////////////////////////////
|
|
http.Redirect(w, r, fmt.Sprintf("/app/journeys/groups_covoiturage/create/%s", request_organization.Group.ToStorageType().ID), http.StatusFound)
|
|
|
|
//http.Redirect(w, r, fmt.Print("/app/journeys"), http.StatusFound)
|
|
return
|
|
}
|
|
}
|
|
accountsBeneficaire, err := h.beneficiaries(r)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
h.Renderer.CreateGroup(w, r, Depart, Arrive, searched, beneficiary, accountsBeneficaire, departurgeo, dstinationgeo)
|
|
|
|
}
|
|
|
|
func (h *ApplicationHandler) DisplayGroupCovoiturage(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
groupid := vars["groupid"]
|
|
|
|
request := &groupsmanagement.GetGroupRequest{
|
|
Id: groupid,
|
|
}
|
|
|
|
resp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), request)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
var accounts = []any{}
|
|
|
|
requesst := &mobilityaccounts.GetAccountsBatchRequest{
|
|
Accountids: resp.Group.Members,
|
|
}
|
|
|
|
ressp, _ := h.services.GRPC.MobilityAccounts.GetAccountsBatch(context.TODO(), requesst)
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
for _, account := range ressp.Accounts {
|
|
if filterAcc(r, account) {
|
|
a := account.ToStorageType()
|
|
accounts = append(accounts, a)
|
|
}
|
|
}
|
|
|
|
cacheid := uuid.NewString()
|
|
h.cache.PutWithTTL(cacheid, accounts, 1*time.Hour)
|
|
r.ParseForm()
|
|
|
|
var beneficiary any
|
|
|
|
searched := false
|
|
|
|
// if r.Method == "POST" {
|
|
if r.FormValue("beneficiaryid") != "" {
|
|
// Handler form
|
|
searched = true
|
|
|
|
requestbeneficiary := &mobilityaccounts.GetAccountRequest{
|
|
Id: r.FormValue("beneficiaryid"),
|
|
}
|
|
|
|
respbeneficiary, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), requestbeneficiary)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
beneficiary = respbeneficiary.Account.ToStorageType()
|
|
|
|
subscribe := &groupsmanagement.SubscribeRequest{
|
|
Groupid: resp.Group.ToStorageType().ID,
|
|
Memberid: respbeneficiary.Account.Id,
|
|
}
|
|
|
|
_, err = h.services.GRPC.GroupsManagement.Subscribe(context.TODO(), subscribe)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/app/journeys/groups_covoiturage/create/%s", resp.Group.ToStorageType().ID), http.StatusFound)
|
|
return
|
|
}
|
|
|
|
accountsBeneficaire, err := h.beneficiaries(r)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
//h.Renderer.BeneficaireSearch(w, r, accounts, searched, beneficiary, resp.Group.ToStorageType())
|
|
h.Renderer.DisplayGroupCovoiturage(w, r, resp.Group.ToStorageType().ID, accounts, cacheid, searched, beneficiary, resp.Group.ToStorageType(), accountsBeneficaire)
|
|
}
|