2022-08-11 15:26:55 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-17 03:02:19 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
2022-08-11 15:26:55 +00:00
|
|
|
"fmt"
|
2022-10-17 03:02:19 +00:00
|
|
|
"io"
|
2022-08-11 15:26:55 +00:00
|
|
|
"net/http"
|
2022-10-17 03:02:19 +00:00
|
|
|
"sort"
|
|
|
|
"time"
|
2022-08-11 15:26:55 +00:00
|
|
|
|
2022-11-01 10:32:13 +00:00
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting"
|
2023-02-22 10:01:29 +00:00
|
|
|
agenda "git.coopgo.io/coopgo-platform/agenda/grpcapi"
|
|
|
|
agendastorage "git.coopgo.io/coopgo-platform/agenda/storage"
|
2023-01-17 07:31:07 +00:00
|
|
|
fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
|
|
|
|
"git.coopgo.io/coopgo-platform/fleets/storage"
|
2022-08-11 15:26:55 +00:00
|
|
|
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
2022-10-17 03:02:19 +00:00
|
|
|
groupstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
|
2022-09-06 13:02:59 +00:00
|
|
|
accounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
2022-11-07 00:24:16 +00:00
|
|
|
mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
2022-08-11 15:26:55 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
2023-02-22 10:01:29 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2022-08-11 15:26:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2022-12-05 19:06:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:26:55 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
var groups = []groupstorage.Group{}
|
2022-08-11 15:26:55 +00:00
|
|
|
|
|
|
|
for _, group := range resp.Groups {
|
|
|
|
g := group.ToStorageType()
|
|
|
|
groups = append(groups, g)
|
|
|
|
}
|
|
|
|
|
2022-11-01 10:32:13 +00:00
|
|
|
sort.Sort(sorting.GroupsByName(groups))
|
2023-02-22 10:01:29 +00:00
|
|
|
////////////////////////////////////add event////////////////////////////////////////////
|
|
|
|
rresp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
|
|
|
|
Namespaces: []string{"parcoursmob_dispositifs"},
|
|
|
|
Mindate: timestamppb.New(time.Now().Add(-24 * time.Hour)),
|
|
|
|
})
|
2022-10-17 03:02:19 +00:00
|
|
|
|
2023-02-22 10:01:29 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
h.Renderer.Administration(w, r, accounts, beneficiaries, groups, bookings, responses)
|
2022-08-11 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ApplicationHandler) AdministrationCreateGroup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "POST" {
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
if r.FormValue("name") == "" {
|
|
|
|
|
|
|
|
fmt.Println("invalid name")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
modules := map[string]any{
|
|
|
|
"beneficiaries": r.FormValue("modules.beneficiaries") == "on",
|
|
|
|
"journeys": r.FormValue("modules.journeys") == "on",
|
|
|
|
"vehicles": r.FormValue("modules.vehicles") == "on",
|
|
|
|
"vehicles_management": r.FormValue("modules.vehicles_management") == "on",
|
|
|
|
"events": r.FormValue("modules.events") == "on",
|
2022-12-05 19:06:22 +00:00
|
|
|
"agenda": r.FormValue("modules.agenda") == "on",
|
|
|
|
"groups": r.FormValue("modules.groups") == "on",
|
2022-08-11 15:26:55 +00:00
|
|
|
"administration": r.FormValue("modules.administration") == "on",
|
2022-12-19 14:56:32 +00:00
|
|
|
"support": r.FormValue("modules.support") == "on",
|
|
|
|
"group_module": r.FormValue("modules.group_module") == "on",
|
2022-08-11 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
groupid := uuid.NewString()
|
|
|
|
|
|
|
|
dataMap := map[string]any{
|
|
|
|
"name": r.FormValue("name"),
|
|
|
|
"modules": modules,
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := structpb.NewValue(dataMap)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
request_organization := &groupsmanagement.AddGroupRequest{
|
|
|
|
Group: &groupsmanagement.Group{
|
|
|
|
Id: groupid,
|
|
|
|
Namespace: "parcoursmob_organizations",
|
|
|
|
Data: data.GetStructValue(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
request_role := &groupsmanagement.AddGroupRequest{
|
|
|
|
Group: &groupsmanagement.Group{
|
|
|
|
Id: groupid + ":admin",
|
|
|
|
Namespace: "parcoursmob_roles",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.Renderer.AdministrationCreateGroup(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ApplicationHandler) AdministrationGroupDisplay(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
groupid := vars["groupid"]
|
|
|
|
|
|
|
|
request := &groupsmanagement.GetGroupRequest{
|
|
|
|
Id: groupid,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), request)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-07 00:24:16 +00:00
|
|
|
groupmembers, admins, err := h.groupmembers(groupid)
|
2022-09-06 13:02:59 +00:00
|
|
|
if err != nil {
|
2022-11-07 00:24:16 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
2022-09-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.Renderer.AdministrationGroupDisplay(w, r, resp.Group.ToStorageType(), groupmembers, admins)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ApplicationHandler) AdministrationGroupInviteAdmin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
groupid := vars["groupid"]
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{
|
|
|
|
Id: groupid,
|
|
|
|
Namespace: "parcoursmob_organizations",
|
|
|
|
})
|
2022-09-06 13:02:59 +00:00
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-09-06 13:02:59 +00:00
|
|
|
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
accountresp, err := h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{
|
|
|
|
Username: r.FormValue("username"),
|
|
|
|
Namespace: "parcoursmob",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
2022-10-17 03:02:19 +00:00
|
|
|
// Account already exists : adding the existing account to admin list
|
2022-09-06 13:02:59 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
_, err = h.services.GRPC.MobilityAccounts.UpdateData(
|
|
|
|
context.TODO(),
|
|
|
|
&accounts.UpdateDataRequest{
|
|
|
|
Account: as,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-09-06 13:02:59 +00:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
|
|
|
|
return
|
2022-10-17 03:02:19 +00:00
|
|
|
} 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)
|
|
|
|
|
2022-10-30 19:11:36 +00:00
|
|
|
h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL
|
2022-10-17 03:02:19 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-09-06 13:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-30 19:11:36 +00:00
|
|
|
func (h *ApplicationHandler) AdministrationGroupInviteMember(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
groupid := vars["groupid"]
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
as, _ := accounts.AccountFromStorageType(&account)
|
|
|
|
|
|
|
|
_, 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)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-17 07:31:07 +00:00
|
|
|
func (h ApplicationHandler) AdminStatVehicles(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
request := &fleets.GetBookingsRequest{}
|
|
|
|
resp, err := h.services.GRPC.Fleets.GetBookings(context.TODO(), request)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bookings := []storage.Booking{}
|
|
|
|
|
|
|
|
for _, b := range resp.Bookings {
|
|
|
|
booking := b.ToStorageType()
|
|
|
|
bookings = append(bookings, booking)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sorting.BookingsByStartdate(bookings))
|
|
|
|
|
|
|
|
vehicles, _ := h.services.GetVehiclesMap()
|
|
|
|
groups, _ := h.services.GetGroupsMap()
|
2023-02-22 10:01:29 +00:00
|
|
|
// fmt.Println(vehicles)
|
|
|
|
// fmt.Println("////////////////////////////////////////:")
|
2023-01-17 07:31:07 +00:00
|
|
|
h.Renderer.VehicleBookingsList(w, r, bookings, vehicles, groups)
|
|
|
|
}
|
|
|
|
|
2022-09-06 13:02:59 +00:00
|
|
|
func (h *ApplicationHandler) members() ([]*accounts.Account, error) {
|
|
|
|
resp, err := h.services.GRPC.MobilityAccounts.GetAccounts(context.TODO(), &accounts.GetAccountsRequest{
|
|
|
|
Namespaces: []string{"parcoursmob"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Accounts, nil
|
2022-08-11 15:26:55 +00:00
|
|
|
}
|
2022-11-07 00:24:16 +00:00
|
|
|
|
|
|
|
func (h *ApplicationHandler) groupmembers(groupid string) (groupmembers []mobilityaccountsstorage.Account, admins []mobilityaccountsstorage.Account, err error) {
|
|
|
|
members, err := h.members()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
groupmembers = []mobilityaccountsstorage.Account{}
|
|
|
|
admins = []mobilityaccountsstorage.Account{}
|
|
|
|
|
|
|
|
for _, m := range members {
|
|
|
|
mm := m.ToStorageType()
|
|
|
|
for _, g := range mm.Data["groups"].([]any) {
|
|
|
|
if g.(string) == groupid {
|
|
|
|
groupmembers = append(groupmembers, mm)
|
|
|
|
}
|
|
|
|
if g.(string) == groupid+":admin" {
|
|
|
|
admins = append(admins, mm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return groupmembers, admins, err
|
|
|
|
}
|