Compare commits

...

10 Commits
main ... salim

Author SHA1 Message Date
sbouaram 0e00a839a3 agenda dispositifs exports 2023-05-30 09:06:48 +02:00
sbouaram c354bece5f refactoring groupcache 2023-05-19 10:58:37 +02:00
sbouaram 6299a6c7be cache refactoring 2023-05-19 10:06:30 +02:00
sbouaram 718c39de00 refactoring 2023-05-16 08:40:05 +02:00
sbouaram 9f66851529 adding groupcache 2023-05-15 14:49:00 +02:00
sbouaram aa13d60ca6 AdministrationGroupInviteMember 2023-04-25 04:37:20 -04:00
sbouaram d169566cd1 administration create group 2023-04-24 05:36:57 -04:00
root b54961b619 handlers/administration goroutines 2023-04-20 19:14:00 +03:00
root c34d08ff70 go routines /handlers/application/journeys 2023-04-20 15:28:27 +03:00
root 46a72e1aef api/cache 2023-04-20 13:14:25 +03:00
74 changed files with 501 additions and 383 deletions

0
Dockerfile Normal file → Executable file
View File

0
README.md Normal file → Executable file
View File

0
config.go Normal file → Executable file
View File

0
go.mod Normal file → Executable file
View File

0
go.sum Normal file → Executable file
View File

0
handlers/api/api.go Normal file → Executable file
View File

78
handlers/api/cache.go Normal file → Executable file
View File

@ -12,40 +12,58 @@ import (
func (h APIHandler) GetCache(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
cacheid := vars["cacheid"]
d, err := h.cache.Get(cacheid)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusNotFound)
return
}
result := d
if data, ok := d.([]any); ok {
if limitsmin, ok := r.URL.Query()["limits.min"]; ok {
min, _ := strconv.Atoi(limitsmin[0])
if limitsmax, ok := r.URL.Query()["limits.max"]; ok {
max, _ := strconv.Atoi(limitsmax[0])
if max > len(data) {
result = data[min:]
} else {
result = data[min:max]
}
} else {
result = data[min:]
}
// Use a channel to synchronize the goroutines
ch := make(chan []byte)
// Fetch data from cache asynchronously
go func() {
d, err := h.cache.Get(cacheid)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusNotFound)
ch <- nil
return
}
var data []any
if val, ok := d.([]any); ok {
data = val
} else {
data = []any{d}
}
j := toJSON(data, w, r)
ch <- j // Signal that the data has been fetched successfully
close(ch)
}()
// wait for the JSON marshaling goroutine to finish
j := <-ch
if j == nil {
return // Stop processing if an error occurred
}
j, err := json.Marshal(result)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
// Send the JSON response to the client
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(j)
<-ch
}
func toJSON(data []any, w http.ResponseWriter, r *http.Request) []byte {
result := data
if limitsmin, ok := r.URL.Query()["limits.min"]; ok {
min, _ := strconv.Atoi(limitsmin[0])
if limitsmax, ok := r.URL.Query()["limits.max"]; ok {
max, _ := strconv.Atoi(limitsmax[0])
if max > len(data) {
result = data[min:]
} else {
result = data[min:max]
}
} else {
result = data[min:]
}
}
j, err := json.Marshal(result)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return nil
}
return j
}

0
handlers/api/export.go Normal file → Executable file
View File

0
handlers/api/geo.go Normal file → Executable file
View File

0
handlers/api/oidc.go Normal file → Executable file
View File

452
handlers/application/administration.go Normal file → Executable file
View File

@ -8,6 +8,7 @@ import (
"io"
"net/http"
"sort"
"sync"
"time"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
@ -27,79 +28,83 @@ import (
)
func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Request) {
accounts, err := h.services.GetAccounts()
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
beneficiaries, err := h.services.GetBeneficiaries()
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
bookings, err := h.services.GetBookings()
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
request := &groupsmanagement.GetGroupsRequest{
Namespaces: []string{"parcoursmob_organizations"},
}
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)
}
sort.Sort(sorting.GroupsByName(groups))
////////////////////////////////////add event////////////////////////////////////////////
rresp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
Namespaces: []string{"parcoursmob_dispositifs"},
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
responses := []agendastorage.Event{}
groupids := []string{}
for _, e := range rresp.Events {
groupids = append(groupids, e.Owners...)
responses = append(responses, e.ToStorageType())
}
sort.Sort(sorting.EventsByStartdate(responses))
groupsresp, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
Groupids: groupids,
})
groupps := map[string]any{}
if err == nil {
for _, g := range groupsresp.Groups {
groupps[g.Id] = g.ToStorageType()
var (
wg sync.WaitGroup
accounts, beneficiaries []mobilityaccountsstorage.Account
bookings []fleetsstorage.Booking
accountsErr, beneficiariesErr, bookingsErr, groupsResponseErr, eventsResponseErr, groupsBatchErr error
groups = []groupstorage.Group{}
responses = []agendastorage.Event{}
groupsResponse *groupsmanagement.GetGroupsResponse
eventsResponse *agenda.GetEventsResponse
groupids = []string{}
groupsBatchResponse *groupsmanagement.GetGroupsBatchResponse
)
// Retrieve accounts in a goroutine
wg.Add(1)
go func() {
defer wg.Done()
accounts, accountsErr = h.services.GetAccounts()
}()
// Retrieve beneficiaries in a goroutine
wg.Add(1)
go func() {
defer wg.Done()
beneficiaries, beneficiariesErr = h.services.GetBeneficiaries()
}()
// Retrieve bookings in a goroutine
wg.Add(1)
go func() {
defer wg.Done()
bookings, bookingsErr = h.services.GetBookings()
}()
// Retrieve groupsRequest in a goroutine
wg.Add(1)
go func() {
defer wg.Done()
request := &groupsmanagement.GetGroupsRequest{
Namespaces: []string{"parcoursmob_organizations"},
}
groupsResponse, groupsResponseErr = h.services.GRPC.GroupsManagement.GetGroups(context.TODO(), request)
for _, group := range groupsResponse.Groups {
g := group.ToStorageType()
groups = append(groups, g)
}
sort.Sort(sorting.GroupsByName(groups))
}()
// Retrieve Events in a goroutine
wg.Add(1)
go func() {
defer wg.Done()
eventsResponse, eventsResponseErr = h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
Namespaces: []string{"parcoursmob_dispositifs"},
})
for _, e := range eventsResponse.Events {
groupids = append(groupids, e.Owners...)
responses = append(responses, e.ToStorageType())
}
sort.Sort(sorting.EventsByStartdate(responses))
}()
wg.Add(1)
// Retrieve groupsBatch in a goroutine
go func() {
defer wg.Done()
groupsBatchResponse, groupsBatchErr = h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
Groupids: groupids,
})
groupps := map[string]any{}
if groupsBatchErr == nil {
for _, g := range groupsBatchResponse.Groups {
groupps[g.Id] = g.ToStorageType()
}
}
}()
wg.Wait()
if accountsErr != nil || beneficiariesErr != nil || bookingsErr != nil || groupsResponseErr != nil || eventsResponseErr != nil {
fmt.Println(accountsErr, beneficiariesErr, bookingsErr, groupsResponseErr, eventsResponseErr, groupsBatchErr)
w.WriteHeader(http.StatusInternalServerError)
return
}
h.Renderer.Administration(w, r, accounts, beneficiaries, groups, bookings, responses)
}
@ -155,22 +160,23 @@ func (h *ApplicationHandler) AdministrationCreateGroup(w http.ResponseWriter, r
Namespace: "parcoursmob_roles",
},
}
_, err = h.services.GRPC.GroupsManagement.AddGroup(context.TODO(), request_organization)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
go func() {
_, err = h.services.GRPC.GroupsManagement.AddGroup(context.TODO(), request_organization)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}()
// Create the admin role for the organization
_, err = h.services.GRPC.GroupsManagement.AddGroup(context.TODO(), request_role)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
go func() {
_, err = h.services.GRPC.GroupsManagement.AddGroup(context.TODO(), request_role)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}()
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
return
}
@ -205,165 +211,167 @@ func (h *ApplicationHandler) AdministrationGroupDisplay(w http.ResponseWriter, r
func (h *ApplicationHandler) AdministrationGroupInviteAdmin(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
groupid := vars["groupid"]
var (
groupresp *groupsmanagement.GetGroupResponse
accountresp *accounts.GetAccountUsernameResponse
err error
)
go func() {
groupresp, err = h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{
Id: groupid,
Namespace: "parcoursmob_organizations",
})
groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{
Id: groupid,
Namespace: "parcoursmob_organizations",
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}()
r.ParseForm()
accountresp, err := h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{
Username: r.FormValue("username"),
Namespace: "parcoursmob",
})
go func() {
accountresp, err = h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{
Username: r.FormValue("username"),
Namespace: "parcoursmob",
})
if err == nil {
// Account already exists : adding the existing account to admin list
account := accountresp.Account.ToStorageType()
account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid)
account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid+":admin")
if err == nil {
// Account already exists : adding the existing account to admin list
account := accountresp.Account.ToStorageType()
account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid)
account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid+":admin")
as, _ := accounts.AccountFromStorageType(&account)
as, _ := accounts.AccountFromStorageType(&account)
_, err = h.services.GRPC.MobilityAccounts.UpdateData(
context.TODO(),
&accounts.UpdateDataRequest{
Account: as,
},
)
_, err = h.services.GRPC.MobilityAccounts.UpdateData(
context.TODO(),
&accounts.UpdateDataRequest{
Account: as,
},
)
fmt.Println(err)
data := map[string]any{
"group": groupresp.Group.ToStorageType().Data["name"],
}
if err := h.emailing.Send("onboarding.existing_administrator", r.FormValue("username"), data); err != nil {
fmt.Println(err)
data := map[string]any{
"group": groupresp.Group.ToStorageType().Data["name"],
}
if err := h.emailing.Send("onboarding.existing_administrator", r.FormValue("username"), data); err != nil {
fmt.Println(err)
}
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
return
} else {
// Onboard now administrator
onboarding := map[string]any{
"username": r.FormValue("username"),
"group": groupid,
"admin": true,
}
b := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
key := base64.RawURLEncoding.EncodeToString(b)
h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL
data := map[string]any{
"group": groupresp.Group.ToStorageType().Data["name"],
"key": key,
}
if err := h.emailing.Send("onboarding.new_administrator", r.FormValue("username"), data); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
return
} else {
// Onboard now administrator
onboarding := map[string]any{
"username": r.FormValue("username"),
"group": groupid,
"admin": true,
}
b := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
key := base64.RawURLEncoding.EncodeToString(b)
h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL
data := map[string]any{
"group": groupresp.Group.ToStorageType().Data["name"],
"key": key,
}
if err := h.emailing.Send("onboarding.new_administrator", r.FormValue("username"), data); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
return
}()
}
func (h *ApplicationHandler) AdministrationGroupInviteMember(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
groupid := vars["groupid"]
var (
group storage.Group
)
groupCh := make(chan storage.Group)
go func() {
groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{
Id: groupid,
Namespace: "parcoursmob_organizations",
})
groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{
Id: groupid,
Namespace: "parcoursmob_organizations",
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
group := groupresp.Group.ToStorageType()
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
group := groupresp.Group.ToStorageType()
groupCh <- group
}()
r.ParseForm()
go func() {
group = <-groupCh
accountresp, err := h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{
Username: r.FormValue("username"),
Namespace: "parcoursmob",
})
if err == nil {
account := accountresp.Account.ToStorageType()
account.Data["groups"] = append(account.Data["groups"].([]any), group.ID)
accountresp, err := h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{
Username: r.FormValue("username"),
Namespace: "parcoursmob",
})
as, _ := accounts.AccountFromStorageType(&account)
if err == nil {
account := accountresp.Account.ToStorageType()
account.Data["groups"] = append(account.Data["groups"].([]any), group.ID)
_, err = h.services.GRPC.MobilityAccounts.UpdateData(
context.TODO(),
&accounts.UpdateDataRequest{
Account: as,
},
)
data := map[string]any{
"group": group.Data["name"],
}
as, _ := accounts.AccountFromStorageType(&account)
if err := h.emailing.Send("onboarding.existing_member", r.FormValue("username"), data); err != nil {
}
_, err = h.services.GRPC.MobilityAccounts.UpdateData(
context.TODO(),
&accounts.UpdateDataRequest{
Account: as,
},
)
fmt.Println(err)
data := map[string]any{
"group": group.Data["name"],
}
if err := h.emailing.Send("onboarding.existing_member", r.FormValue("username"), data); err != nil {
fmt.Println(err)
}
http.Redirect(w, r, "/app/group/settings", http.StatusFound)
return
} else {
// Onboard now administrator
onboarding := map[string]any{
"username": r.FormValue("username"),
"group": group.ID,
"admin": false,
}
b := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
http.Redirect(w, r, "/app/group/settings", http.StatusFound)
return
} else {
// Onboard now administrator
onboarding := map[string]any{
"username": r.FormValue("username"),
"group": group.ID,
"admin": false,
}
b := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
key := base64.RawURLEncoding.EncodeToString(b)
h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL
data := map[string]any{
"group": group.Data["name"],
"key": key,
}
if err := h.emailing.Send("onboarding.new_member", r.FormValue("username"), data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
key := base64.RawURLEncoding.EncodeToString(b)
h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL
data := map[string]any{
"group": group.Data["name"],
"key": key,
}
if err := h.emailing.Send("onboarding.new_member", r.FormValue("username"), data); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}()
http.Redirect(w, r, "/app/administration/groups/"+group.ID, http.StatusFound)
return
}

0
handlers/application/agenda.go Normal file → Executable file
View File

0
handlers/application/application.go Normal file → Executable file
View File

0
handlers/application/beneficiaries.go Normal file → Executable file
View File

0
handlers/application/dashboard.go Normal file → Executable file
View File

0
handlers/application/directory.go Normal file → Executable file
View File

0
handlers/application/group.go Normal file → Executable file
View File

0
handlers/application/group_module.go Normal file → Executable file
View File

157
handlers/application/journeys.go Normal file → Executable file
View File

@ -27,7 +27,14 @@ var Arrive any
func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
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)
locTime, errTime := time.LoadLocation("Europe/Paris")
if errTime != nil {
fmt.Println("Loading timezone location Europe/Paris error : ")
@ -70,91 +77,101 @@ func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Reque
w.WriteHeader(http.StatusBadRequest)
return
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
navitiaCh <- nil
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
}
navitiaCh <- journeys
//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"
carpoolRequest := func() {
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)
client := &http.Client{}
req, err := http.NewRequest("GET", carpoolrequest, nil)
if err != nil {
fmt.Println(err)
}
if carpoolresults == nil {
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{}
}
} else {
carpoolresults = []any{}
carpoolCh <- carpoolresults
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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)
vehicleRequest := func() {
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)
}
}
vehiclech <- vehicles
}
go journeysRequest()
go carpoolRequest()
go vehicleRequest()
carpool_results = <-carpoolCh
journeys_results = <-navitiaCh
vehicle_results = <-vehiclech
}
h.Renderer.JourneysSearch(w, r, carpoolresults, journeys, vehicles, searched, departuregeo, destinationgeo, departuredate, departuretime)
h.Renderer.JourneysSearch(w, r, carpool_results, journeys_results, vehicle_results, searched, departuregeo, destinationgeo, departuredate, departuretime)
}
type GroupsModule []groupstorage.Group

0
handlers/application/members.go Normal file → Executable file
View File

0
handlers/application/support.go Normal file → Executable file
View File

0
handlers/application/vehicles-management.go Normal file → Executable file
View File

2
handlers/application/vehicles.go Normal file → Executable file
View File

@ -26,7 +26,7 @@ import (
func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Println("invoked")
var beneficiary mobilityaccountsstorage.Account
beneficiarydocuments := []filestorage.FileInfo{}

0
handlers/auth/auth.go Normal file → Executable file
View File

0
handlers/auth/disconnect.go Normal file → Executable file
View File

0
handlers/auth/groups.go Normal file → Executable file
View File

0
handlers/auth/lost_password.go Normal file → Executable file
View File

0
handlers/auth/onboarding.go Normal file → Executable file
View File

165
handlers/exports/agenda.go Normal file → Executable file
View File

@ -3,9 +3,6 @@ package exports
import (
"context"
"fmt"
"net/http"
"sort"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting"
agenda "git.coopgo.io/coopgo-platform/agenda/grpcapi"
agendastorage "git.coopgo.io/coopgo-platform/agenda/storage"
@ -13,69 +10,129 @@ import (
groupsstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
accounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
accountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
"github.com/gorilla/mux"
"github.com/xuri/excelize/v2"
"net/http"
"sort"
)
func (h *ExportsHandler) Agenda(w http.ResponseWriter, r *http.Request) {
resp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
Namespaces: []string{"parcoursmob_dispositifs"},
})
func (h *ExportsHandler) Agenda(filter string) func(w http.ResponseWriter, r *http.Request) {
switch filter {
case "allEvents":
return func(w http.ResponseWriter, r *http.Request) {
resp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
Namespaces: []string{"parcoursmob_dispositifs"},
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
events := []agendastorage.Event{}
events := []agendastorage.Event{}
groupids := []string{}
beneficiaries_ids := []string{}
for _, e := range resp.Events {
groupids = append(groupids, e.Owners...)
events = append(events, e.ToStorageType())
groupids := []string{}
beneficiaries_ids := []string{}
for _, e := range resp.Events {
groupids = append(groupids, e.Owners...)
events = append(events, e.ToStorageType())
for _, subscriptions := range e.Subscriptions {
beneficiaries_ids = append(beneficiaries_ids, subscriptions.Subscriber)
}
}
sort.Sort(sorting.EventsByStartdate(events))
groupsresp, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
Groupids: groupids,
})
groups := map[string]groupsstorage.Group{}
if err == nil {
for _, g := range groupsresp.Groups {
groups[g.Id] = g.ToStorageType()
}
}
beneficiaries, err := h.services.GRPC.MobilityAccounts.GetAccountsBatch(context.TODO(), &accounts.GetAccountsBatchRequest{
Accountids: beneficiaries_ids,
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
beneficiaries_map := map[string]accountsstorage.Account{}
for _, ben := range beneficiaries.Accounts {
beneficiaries_map[ben.Id] = ben.ToStorageType()
}
f := h.generateExcel(events, groups, beneficiaries_map)
h.writeFileResponse(f, w)
}
case "oneEvent":
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
eventId := vars["eventid"]
resp, err := h.services.GRPC.Agenda.GetEvent(context.TODO(), &agenda.GetEventRequest{
Id: eventId,
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
groupids := []string{}
beneficiaries_ids := []string{}
groupids = append(groupids, resp.Event.Owners...)
for _, subscriptions := range resp.Event.Subscriptions {
beneficiaries_ids = append(beneficiaries_ids, subscriptions.Subscriber)
}
groupsresp, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
Groupids: groupids,
})
groups := map[string]groupsstorage.Group{}
if err == nil {
for _, g := range groupsresp.Groups {
groups[g.Id] = g.ToStorageType()
}
}
beneficiaries, err := h.services.GRPC.MobilityAccounts.GetAccountsBatch(context.TODO(), &accounts.GetAccountsBatchRequest{
Accountids: beneficiaries_ids,
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
beneficiaries_map := map[string]accountsstorage.Account{}
for _, ben := range beneficiaries.Accounts {
beneficiaries_map[ben.Id] = ben.ToStorageType()
}
f := h.generateExcel([]agendastorage.Event{resp.Event.ToStorageType()}, groups, beneficiaries_map)
h.writeFileResponse(f, w)
for _, subscriptions := range e.Subscriptions {
beneficiaries_ids = append(beneficiaries_ids, subscriptions.Subscriber)
}
}
return nil
}
sort.Sort(sorting.EventsByStartdate(events))
groupsresp, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
Groupids: groupids,
})
groups := map[string]groupsstorage.Group{}
if err == nil {
for _, g := range groupsresp.Groups {
groups[g.Id] = g.ToStorageType()
}
}
beneficiaries, err := h.services.GRPC.MobilityAccounts.GetAccountsBatch(context.TODO(), &accounts.GetAccountsBatchRequest{
Accountids: beneficiaries_ids,
})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
beneficiaries_map := map[string]accountsstorage.Account{}
for _, ben := range beneficiaries.Accounts {
beneficiaries_map[ben.Id] = ben.ToStorageType()
}
/////////////// Generate file
func (h *ExportsHandler) generateExcel(events []agendastorage.Event, groups map[string]groupsstorage.Group,
beneficiaries_map map[string]accountsstorage.Account) *excelize.File {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
f.SetCellValue("Sheet1", "A1", "Evénement")
f.SetCellValue("Sheet1", "B1", "Date de début")
f.SetCellValue("Sheet1", "C1", "Date de fin")
@ -85,8 +142,6 @@ func (h *ExportsHandler) Agenda(w http.ResponseWriter, r *http.Request) {
f.SetCellValue("Sheet1", "G1", "Prescipteur")
f.SetCellValue("Sheet1", "H1", "Prescipteur Nom")
f.SetCellValue("Sheet1", "I1", "Gestionnaire événement")
// f.SetCellValue("Sheet1", "I1", "Prescripteur téléphone")
i := 2
for _, e := range events {
if len(e.Owners) == 0 {
@ -128,10 +183,14 @@ func (h *ExportsHandler) Agenda(w http.ResponseWriter, r *http.Request) {
}
}
return f
}
func (h *ExportsHandler) writeFileResponse(file *excelize.File, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
w.Header().Set("Content-Transfer-Encoding", "binary")
w.Header().Set("Expires", "0")
f.Write(w)
file.Write(w)
}

0
handlers/exports/exports.go Normal file → Executable file
View File

0
handlers/exports/fleets.go Normal file → Executable file
View File

10
main.go Normal file → Executable file
View File

@ -19,6 +19,7 @@ import (
func main() {
cfg, err := ReadConfig()
if err != nil {
panic(err)
}
@ -29,17 +30,15 @@ func main() {
templates_public_dir = cfg.GetString("templates.public_dir")
dev_env = cfg.GetBool("dev_env")
)
svc, err := services.NewServicesHandler(cfg)
if err != nil {
panic(err)
}
fmt.Println(cfg)
kv, err := cache.NewKVHandler(cfg)
if err != nil {
panic(err)
}
filestorage, err := cache.NewFileStorage(cfg)
idp, err := identification.NewIdentificationProvider(cfg, svc, kv)
@ -163,12 +162,11 @@ func main() {
export := r.PathPrefix("/exports").Subrouter()
export.HandleFunc("/fleets/bookings", exportsHandler.Bookings)
export.HandleFunc("/agenda/subscriptions", exportsHandler.Agenda)
export.HandleFunc("/agenda/subscriptions", exportsHandler.Agenda("allEvents"))
export.HandleFunc("/agenda/{eventid}", exportsHandler.Agenda("oneEvent"))
export.Use(idp.Middleware)
export.Use(idp.GroupsMiddleware)
fmt.Println("-> HTTP server listening on", address)
srv := &http.Server{
Handler: r,
Addr: address,

0
renderer/administration.go Normal file → Executable file
View File

0
renderer/agenda.go Normal file → Executable file
View File

0
renderer/auth.go Normal file → Executable file
View File

0
renderer/beneficiaries.go Normal file → Executable file
View File

0
renderer/dashboard.go Normal file → Executable file
View File

0
renderer/directory.go Normal file → Executable file
View File

0
renderer/func-maps.go Normal file → Executable file
View File

0
renderer/group.go Normal file → Executable file
View File

0
renderer/group_module.go Normal file → Executable file
View File

0
renderer/journeys.go Normal file → Executable file
View File

0
renderer/layout.go Normal file → Executable file
View File

0
renderer/mailer.go Normal file → Executable file
View File

0
renderer/members.go Normal file → Executable file
View File

0
renderer/renderer.go Normal file → Executable file
View File

0
renderer/support.go Normal file → Executable file
View File

0
renderer/vehicle-management.go Normal file → Executable file
View File

0
renderer/vehicles.go Normal file → Executable file
View File

0
services/agenda.go Normal file → Executable file
View File

0
services/fleets.go Normal file → Executable file
View File

0
services/groupsmanagement.go Normal file → Executable file
View File

0
services/mobilityaccounts.go Normal file → Executable file
View File

0
services/services.go Normal file → Executable file
View File

1
themes Submodule

@ -0,0 +1 @@
Subproject commit d6de19d8e238fec2f03957abdfa7b49a463d4a3c

0
utils/form-validators/form-validators.go Normal file → Executable file
View File

0
utils/form-validators/phone-numbers.go Normal file → Executable file
View File

0
utils/icons/svg-icons.go Normal file → Executable file
View File

0
utils/identification/groups.go Normal file → Executable file
View File

0
utils/identification/oidc.go Normal file → Executable file
View File

0
utils/profile-pictures/profile-pictures.go Normal file → Executable file
View File

0
utils/sorting/beneficiaries.go Normal file → Executable file
View File

0
utils/sorting/events.go Normal file → Executable file
View File

0
utils/sorting/fleets.go Normal file → Executable file
View File

0
utils/sorting/groups.go Normal file → Executable file
View File

0
utils/sorting/sorting.go Normal file → Executable file
View File

1
utils/storage/badger.go Normal file
View File

@ -0,0 +1 @@
package storage

0
utils/storage/cache.go Normal file → Executable file
View File

1
utils/storage/etcd.go Normal file → Executable file
View File

@ -74,6 +74,7 @@ func NewEtcdHandler(cfg *viper.Viper) (*EtcdHandler, error) {
Password: password,
DialTimeout: 5 * time.Second,
})
fmt.Println(endpoints,prefix,username,password)
if err != nil {
fmt.Println(err)
return nil, err

0
utils/storage/files.go Normal file → Executable file
View File

View File

@ -0,0 +1 @@
package storage

16
utils/storage/kv.go Normal file → Executable file
View File

@ -1,6 +1,7 @@
package storage
import (
"fmt"
"time"
"github.com/spf13/viper"
@ -14,5 +15,18 @@ type KVHandler interface {
}
func NewKVHandler(cfg *viper.Viper) (KVHandler, error) {
return NewEtcdHandler(cfg)
cacheType := cfg.GetString("storage.kv.dbType")
switch cacheType {
case "etcd":
return NewEtcdHandler(cfg)
case "badger":
return NewBadgerHandler(cfg)
case "etcdGroupcache":
return NewGroupCacheHandler(cfg)
fmt.Println("here")
case "badgerGroupcache":
return NewGroupCacheHandler(cfg)
}
return nil, nil
}

0
utils/storage/minio.go Normal file → Executable file
View File

0
utils/storage/sessions.go Normal file → Executable file
View File