2022-10-17 03:02:19 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-12-07 10:12:43 +00:00
|
|
|
"sort"
|
2023-01-17 13:45:09 +00:00
|
|
|
"strconv"
|
2022-12-07 10:12:43 +00:00
|
|
|
"strings"
|
2022-10-17 03:02:19 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
|
2022-12-07 10:12:43 +00:00
|
|
|
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"
|
2022-10-17 03:02:19 +00:00
|
|
|
geojson "github.com/paulmach/go.geojson"
|
2024-11-11 18:50:17 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-10-17 03:02:19 +00:00
|
|
|
"gitlab.scity.coop/maas/navitia-golang"
|
|
|
|
"gitlab.scity.coop/maas/navitia-golang/types"
|
2022-12-07 10:12:43 +00:00
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
2022-10-17 03:02:19 +00:00
|
|
|
)
|
|
|
|
|
2022-12-07 10:12:43 +00:00
|
|
|
var Depart any
|
|
|
|
var Arrive any
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.ParseForm()
|
2023-04-20 12:28:27 +00:00
|
|
|
var (
|
|
|
|
journeys_results *navitia.JourneyResults
|
|
|
|
carpool_results any
|
|
|
|
vehicle_results []any
|
|
|
|
)
|
|
|
|
vehiclech := make(chan []any, 1)
|
|
|
|
navitiaCh := make(chan *navitia.JourneyResults, 1)
|
|
|
|
carpoolCh := make(chan any, 1)
|
2022-10-17 03:02:19 +00:00
|
|
|
locTime, errTime := time.LoadLocation("Europe/Paris")
|
|
|
|
if errTime != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Panic().Err(errTime).Msg("Tried to load timezone location Europe/Paris. Error. Missing zones in container ?")
|
2022-10-17 03:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-10-17 03:02:19 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
destinationgeo, err = geojson.UnmarshalFeature([]byte(destination))
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-10-17 03:02:19 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2023-04-20 12:28:27 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
journeysRequest := func() {
|
|
|
|
//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 {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2023-04-20 12:28:27 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
navitiaCh <- nil
|
|
|
|
return
|
|
|
|
}
|
2022-10-17 03:02:19 +00:00
|
|
|
|
2023-04-20 12:28:27 +00:00
|
|
|
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
|
|
|
|
}
|
2022-11-07 00:24:16 +00:00
|
|
|
|
2023-04-20 12:28:27 +00:00
|
|
|
journeys, err = session.Journeys(context.Background(), request)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2023-04-20 12:28:27 +00:00
|
|
|
// w.WriteHeader(http.StatusBadRequest)
|
|
|
|
// return
|
|
|
|
}
|
|
|
|
navitiaCh <- journeys
|
2022-10-17 03:02:19 +00:00
|
|
|
|
|
|
|
}
|
2023-04-20 12:28:27 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2022-10-17 03:02:19 +00:00
|
|
|
//CARPOOL
|
2023-04-20 12:28:27 +00:00
|
|
|
carpoolRequest := func() {
|
|
|
|
carpoolrequest := "https://api.rdex.ridygo.fr/journeys.json"
|
2022-10-17 03:02:19 +00:00
|
|
|
|
2023-04-20 12:28:27 +00:00
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", carpoolrequest, nil)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2023-04-20 12:28:27 +00:00
|
|
|
}
|
2022-10-17 03:02:19 +00:00
|
|
|
|
2023-04-20 12:28:27 +00:00
|
|
|
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"))
|
2022-10-17 03:02:19 +00:00
|
|
|
|
2023-04-20 12:28:27 +00:00
|
|
|
req.Header.Set("X-API-KEY", "123456")
|
|
|
|
resp, err := client.Do(req)
|
2022-10-30 19:11:36 +00:00
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-10-30 19:11:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 12:28:27 +00:00
|
|
|
if err == nil && resp.StatusCode == http.StatusOK {
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&carpoolresults)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2023-04-20 12:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if carpoolresults == nil {
|
|
|
|
carpoolresults = []any{}
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-30 19:11:36 +00:00
|
|
|
carpoolresults = []any{}
|
|
|
|
}
|
2023-04-20 12:28:27 +00:00
|
|
|
carpoolCh <- carpoolresults
|
2022-10-17 03:02:19 +00:00
|
|
|
}
|
2023-04-20 12:28:27 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2022-10-17 03:02:19 +00:00
|
|
|
// Vehicles
|
2023-04-20 12:28:27 +00:00
|
|
|
vehicleRequest := func() {
|
|
|
|
vehiclerequest := &fleets.GetVehiclesRequest{
|
|
|
|
Namespaces: []string{"parcoursmob"},
|
|
|
|
}
|
|
|
|
vehicleresp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), vehiclerequest)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2023-04-20 12:28:27 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-10-17 03:02:19 +00:00
|
|
|
}
|
2023-04-20 12:28:27 +00:00
|
|
|
vehiclech <- vehicles
|
2022-10-17 03:02:19 +00:00
|
|
|
}
|
2023-04-20 12:28:27 +00:00
|
|
|
go journeysRequest()
|
|
|
|
go carpoolRequest()
|
|
|
|
go vehicleRequest()
|
|
|
|
carpool_results = <-carpoolCh
|
|
|
|
journeys_results = <-navitiaCh
|
|
|
|
vehicle_results = <-vehiclech
|
2022-10-17 03:02:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 12:28:27 +00:00
|
|
|
h.Renderer.JourneysSearch(w, r, carpool_results, journeys_results, vehicle_results, searched, departuregeo, destinationgeo, departuredate, departuretime)
|
2022-10-17 03:02:19 +00:00
|
|
|
}
|
2022-12-07 10:12:43 +00:00
|
|
|
|
|
|
|
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 {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-12-07 10:12:43 +00:00
|
|
|
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
|
2023-01-04 10:55:18 +00:00
|
|
|
|
2022-12-07 10:12:43 +00:00
|
|
|
if r.FormValue("beneficiaryid") != "" {
|
2023-01-04 10:55:18 +00:00
|
|
|
|
2022-12-07 10:12:43 +00:00
|
|
|
searched = true
|
|
|
|
|
|
|
|
requestbeneficiary := &mobilityaccounts.GetAccountRequest{
|
|
|
|
Id: r.FormValue("beneficiaryid"),
|
|
|
|
}
|
|
|
|
|
|
|
|
respbeneficiary, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), requestbeneficiary)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-12-07 10:12:43 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
beneficiary = respbeneficiary.Account.ToStorageType()
|
|
|
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
departure := r.FormValue("departure")
|
|
|
|
destination := r.FormValue("destination")
|
|
|
|
|
|
|
|
if departure != "" && destination != "" {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
departurgeo, err = geojson.UnmarshalFeature([]byte(departure))
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-12-07 10:12:43 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dstinationgeo, err = geojson.UnmarshalFeature([]byte(destination))
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-12-07 10:12:43 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
if r.FormValue("name") == "" {
|
|
|
|
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Msg("invalid name")
|
2022-12-07 10:12:43 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.FormValue("number") == "" {
|
|
|
|
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Msg("invalid number of personne")
|
2022-12-07 10:12:43 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2023-01-04 10:55:18 +00:00
|
|
|
|
2022-12-07 10:12:43 +00:00
|
|
|
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",
|
|
|
|
}
|
2023-01-04 10:55:18 +00:00
|
|
|
|
2022-12-07 10:12:43 +00:00
|
|
|
groupidd := uuid.NewString()
|
2023-01-04 10:55:18 +00:00
|
|
|
|
2022-12-07 10:12:43 +00:00
|
|
|
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"),
|
2023-01-04 10:55:18 +00:00
|
|
|
|
2022-12-07 10:12:43 +00:00
|
|
|
"planDays": planDays,
|
|
|
|
"recurrent": r.FormValue("recurrent"),
|
|
|
|
"pontuelle": r.FormValue("ponctuelle"),
|
|
|
|
}
|
2023-01-04 10:55:18 +00:00
|
|
|
|
2022-12-07 10:12:43 +00:00
|
|
|
data, err := structpb.NewValue(dataMap)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-12-07 10:12:43 +00:00
|
|
|
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 {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-12-07 10:12:43 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-01-04 10:55:18 +00:00
|
|
|
|
2022-12-07 10:12:43 +00:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("/app/journeys/groups_covoiturage/create/%s", request_organization.Group.ToStorageType().ID), http.StatusFound)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
accountsBeneficaire, err := h.beneficiaries(r)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-12-07 10:12:43 +00:00
|
|
|
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 {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-12-07 10:12:43 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var accounts = []any{}
|
|
|
|
|
|
|
|
requesst := &mobilityaccounts.GetAccountsBatchRequest{
|
|
|
|
Accountids: resp.Group.Members,
|
|
|
|
}
|
|
|
|
|
|
|
|
ressp, _ := h.services.GRPC.MobilityAccounts.GetAccountsBatch(context.TODO(), requesst)
|
2023-01-04 10:55:18 +00:00
|
|
|
|
2022-12-07 10:12:43 +00:00
|
|
|
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.FormValue("beneficiaryid") != "" {
|
2023-01-04 10:55:18 +00:00
|
|
|
|
2022-12-07 10:12:43 +00:00
|
|
|
searched = true
|
|
|
|
|
|
|
|
requestbeneficiary := &mobilityaccounts.GetAccountRequest{
|
|
|
|
Id: r.FormValue("beneficiaryid"),
|
|
|
|
}
|
|
|
|
|
|
|
|
respbeneficiary, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), requestbeneficiary)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-12-07 10:12:43 +00:00
|
|
|
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 {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-12-07 10:12:43 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-01-04 15:12:49 +00:00
|
|
|
/*******************Code to store more information about mermbers groupscovoiturage**************/
|
2023-01-04 10:55:18 +00:00
|
|
|
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()
|
|
|
|
dataMap := map[string]any{
|
|
|
|
|
|
|
|
"depart": Depart,
|
|
|
|
"arrive": Arrive,
|
|
|
|
}
|
2023-01-17 13:45:09 +00:00
|
|
|
id := uuid.NewString()
|
2023-01-04 10:55:18 +00:00
|
|
|
data, err := structpb.NewValue(dataMap)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2023-01-04 10:55:18 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-17 13:45:09 +00:00
|
|
|
request_organizatio := &groupsmanagement.AddGroupMemberRequest{
|
2023-01-04 10:55:18 +00:00
|
|
|
Group: &groupsmanagement.GroupMember{
|
2023-01-17 13:45:09 +00:00
|
|
|
Id: id,
|
2023-01-04 10:55:18 +00:00
|
|
|
Memberid: respbeneficiary.Account.Id,
|
|
|
|
Groupid: resp.Group.ToStorageType().ID,
|
|
|
|
Data: data.GetStructValue(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-17 13:45:09 +00:00
|
|
|
_, err = h.services.GRPC.GroupsManagement.AddGroupMember(context.TODO(), request_organizatio)
|
2023-01-04 10:55:18 +00:00
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2023-01-04 10:55:18 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-07 10:12:43 +00:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("/app/journeys/groups_covoiturage/create/%s", resp.Group.ToStorageType().ID), http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
2023-01-17 13:45:09 +00:00
|
|
|
//////////find all groups to store the adresse passenger///////
|
2023-01-23 09:15:23 +00:00
|
|
|
// grp := &groupsmanagement.GetGroupsBatchMemberRequest{
|
|
|
|
|
|
|
|
// Groupids: []string{resp.Group.ToStorageType().ID},
|
|
|
|
// }
|
|
|
|
// s, err := h.services.GRPC.GroupsManagement.GetGroupsBatchMember(context.TODO(), grp)
|
|
|
|
// if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
// log.Error().Err(err).Msg("")
|
2023-01-23 09:15:23 +00:00
|
|
|
// w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// groups := map[string]any{}
|
|
|
|
|
|
|
|
// if err == nil {
|
|
|
|
// for _, g := range s.Groups {
|
|
|
|
// groups[g.Memberid] = g.ToStorageType()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//////////find all groups to store the adresse passenger///////
|
|
|
|
///////////try to optimise the code ////////////////////////////
|
|
|
|
groups, _ := h.services.GetGroupsMemberMap(resp.Group.ToStorageType().ID)
|
2023-01-17 13:45:09 +00:00
|
|
|
var number string = strconv.Itoa(len(resp.Group.Members))
|
2022-12-07 10:12:43 +00:00
|
|
|
accountsBeneficaire, err := h.beneficiaries(r)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2022-12-07 10:12:43 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-17 13:45:09 +00:00
|
|
|
h.Renderer.DisplayGroupCovoiturage(w, r, number, resp.Group.ToStorageType().ID, Depart, Arrive, accounts, cacheid, searched, beneficiary, resp.Group.ToStorageType(), accountsBeneficaire, groups)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ApplicationHandler) UpdateGroupCovoiturage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
|
|
|
groupid := vars["groupid"]
|
|
|
|
memberid := vars["memberid"]
|
|
|
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
|
|
|
|
//////////get groupid covoiturage//////////
|
|
|
|
request := &groupsmanagement.GetGroupRequest{
|
|
|
|
Id: groupid,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), request)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2023-01-17 13:45:09 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////get group member////////////////////////////////
|
|
|
|
|
|
|
|
reequest := &groupsmanagement.GetGroupMemberRequest{
|
|
|
|
Id: id,
|
|
|
|
}
|
|
|
|
|
|
|
|
ressp, err := h.services.GRPC.GroupsManagement.GetGroupMember(context.TODO(), reequest)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2023-01-17 13:45:09 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req := &groupsmanagement.UnsubscribeMemberRequest{
|
|
|
|
Id: ressp.Group.Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, errr := h.services.GRPC.GroupsManagement.UnsubscribeMember(context.TODO(), req)
|
|
|
|
if errr != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(errr).Msg("Issue in groups management service - InsubscribeMember")
|
2023-01-17 13:45:09 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
members := resp.Group.Members
|
|
|
|
for i := 0; i < len(members); i++ {
|
|
|
|
if members[i] == memberid {
|
|
|
|
members = append(members[:i], members[(i+1):]...)
|
|
|
|
resp.Group.Members = members
|
|
|
|
reequest := &groupsmanagement.UnsubscribeRequest{
|
|
|
|
Groupid: resp.Group.ToStorageType().ID,
|
|
|
|
Memberid: memberid,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := h.services.GRPC.GroupsManagement.Unsubscribe(context.TODO(), reequest)
|
|
|
|
if err != nil {
|
2024-11-11 18:50:17 +00:00
|
|
|
log.Error().Err(err).Msg("")
|
2023-01-17 13:45:09 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/app/journeys/groups_covoiturage/create/%s", groupid), http.StatusFound)
|
|
|
|
/*
|
|
|
|
I must add "return" to resolve the err
|
|
|
|
http: superfluous response.WriteHeader call from git.coopgo.io/coopgo-apps/parcoursmob/renderer.(*Renderer).Render (renderer.go:50)
|
|
|
|
*/
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.Renderer.UpdateGroupCovoiturage(w, r, groupid, memberid)
|
2022-12-07 10:12:43 +00:00
|
|
|
}
|