Compare commits
10 Commits
Author | SHA1 | Date |
---|---|---|
sbouaram | 0e00a839a3 | |
sbouaram | c354bece5f | |
sbouaram | 6299a6c7be | |
sbouaram | 718c39de00 | |
sbouaram | 9f66851529 | |
sbouaram | aa13d60ca6 | |
sbouaram | d169566cd1 | |
root | b54961b619 | |
root | c34d08ff70 | |
root | 46a72e1aef |
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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{}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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,0 +1 @@
|
|||
Subproject commit d6de19d8e238fec2f03957abdfa7b49a463d4a3c
|
|
@ -0,0 +1 @@
|
|||
package storage
|
|
@ -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,0 +1 @@
|
|||
package storage
|
|
@ -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
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue