124 lines
3.1 KiB
Go
124 lines
3.1 KiB
Go
package application
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
|
"github.com/google/uuid"
|
|
"github.com/gorilla/mux"
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
)
|
|
|
|
func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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 = []any{}
|
|
|
|
for _, group := range resp.Groups {
|
|
g := group.ToStorageType()
|
|
groups = append(groups, g)
|
|
}
|
|
|
|
h.Renderer.Administration(w, r, groups)
|
|
}
|
|
|
|
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",
|
|
"administration": r.FormValue("modules.administration") == "on",
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
h.Renderer.AdministrationGroupDisplay(w, r, resp.Group.ToStorageType())
|
|
}
|