add diags services
This commit is contained in:
parent
e81399eef4
commit
a2a3ac5ffe
|
@ -27,6 +27,8 @@ import (
|
|||
"git.coopgo.io/coopgo-platform/groups-management/storage"
|
||||
mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
||||
mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
||||
diags "git.coopgo.io/coopgo-platform/diags/grpcapi"
|
||||
diagsstorage "git.coopgo.io/coopgo-platform/diags/storage"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
@ -310,6 +312,20 @@ func (h *ApplicationHandler) BeneficiaryDisplay(w http.ResponseWriter, r *http.R
|
|||
}
|
||||
sortByDate(events_list)
|
||||
|
||||
diag := []diagsstorage.Diag{}
|
||||
for _, e := range diag {
|
||||
diagrequest := &diags.GetDiagRequest{
|
||||
Id: e.ID,
|
||||
}
|
||||
diagresp, err := h.services.GRPC.Diags.GetDiag(context.TODO(), diagrequest)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
diag = append(diag, diagresp.Diag.ToStorageType())
|
||||
}
|
||||
|
||||
groupsrequest := &groupsmanagement.GetGroupsRequest{
|
||||
Namespaces: []string{"parcoursmob_organizations"},
|
||||
Member: beneficiaryID,
|
||||
|
@ -328,7 +344,11 @@ func (h *ApplicationHandler) BeneficiaryDisplay(w http.ResponseWriter, r *http.R
|
|||
beneficiaries_file_types := h.config.GetStringSlice("modules.beneficiaries.documents_types")
|
||||
file_types_map := h.config.GetStringMapString("storage.files.file_types")
|
||||
|
||||
h.Renderer.BeneficiaryDisplay(w, r, resp.Account.ToStorageType(), bookings, organizations, beneficiaries_file_types, file_types_map, documents, events_list)
|
||||
diagsAny := make([]any, len(diag))
|
||||
for i, d := range diag {
|
||||
diagsAny[i] = d
|
||||
}
|
||||
h.Renderer.BeneficiaryDisplay(w, r, resp.Account.ToStorageType(), bookings, organizations, beneficiaries_file_types, file_types_map, documents, events_list, diagsAny)
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) BeneficiaryUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
@ -2,18 +2,22 @@ package application
|
|||
|
||||
import (
|
||||
"context"
|
||||
// "encoding/json"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
// "io"
|
||||
"net/http"
|
||||
// "strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/services"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
||||
// filestorage "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
|
||||
diags "git.coopgo.io/coopgo-platform/diags/grpcapi"
|
||||
diagsstorage "git.coopgo.io/coopgo-platform/diags/storage"
|
||||
|
||||
// groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
||||
// "git.coopgo.io/coopgo-platform/groups-management/storage"
|
||||
// mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
||||
|
@ -75,50 +79,71 @@ func (h *ApplicationHandler) DiagsHistory(w http.ResponseWriter, r *http.Request
|
|||
h.Renderer.DiagsHistory(w, r, responses)
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) DiagsCreateDiag(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
// Get current group
|
||||
func (h *ApplicationHandler) BeneficiariesCreateDiag(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
beneficiaryID := vars["beneficiaryid"]
|
||||
|
||||
if h.services == nil || (h.services.GRPC == services.GRPCServices{}) || h.services.GRPC.Diags == nil {
|
||||
log.Error().Msg("Diags service is not initialized")
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
if r.Method == http.MethodPost {
|
||||
// Récupérer le groupe courant
|
||||
g := r.Context().Value(identification.GroupKey)
|
||||
if g == nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
http.Error(w, "Missing group information", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse le formulaire
|
||||
diagForm, err := parseDiagsForm(r)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
log.Error().Err(err).Msg("Invalid form data")
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug().Any("diagFrom", diagForm).Msg("Form data submitted to create diag")
|
||||
log.Debug().Interface("diagForm", diagForm).Msg("Form data parsed")
|
||||
|
||||
data, _ := structpb.NewStruct(map[string]any{
|
||||
// Préparation des données supplémentaires
|
||||
data, err := structpb.NewStruct(map[string]any{
|
||||
"beneficiary": beneficiaryID,
|
||||
})
|
||||
|
||||
request := &diags.CreateDiagRequest{
|
||||
Diag: &diags.Diag{
|
||||
Namespace: "parcoursmob_diagnostiques",
|
||||
Name: diagForm.Name,
|
||||
JsonSchema: diagForm.JsonSchema,
|
||||
Data: data,
|
||||
Deleted: false,
|
||||
},
|
||||
}
|
||||
|
||||
resp, err := h.services.GRPC.Diags.CreateDiag(context.TODO(), request)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("")
|
||||
log.Error().Err(err).Msg("Failed to create protobuf struct")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Crée la requête gRPC pour créer le diagnostic
|
||||
request := &diags.CreateDiagRequest{
|
||||
Diag: &diags.Diag{
|
||||
Name: diagForm.Name,
|
||||
Namespace: diagForm.Namespace,
|
||||
JsonSchema: diagForm.JsonSchema,
|
||||
UiSchema: diagForm.UiSchema,
|
||||
Data: data,
|
||||
Deleted: diagForm.Deleted,
|
||||
},
|
||||
}
|
||||
|
||||
http.Redirect(w, r, fmt.Sprintf("/app/diags/%s", resp.Diag.Id), http.StatusFound)
|
||||
// Appelle le service gRPC
|
||||
resp, err := h.services.GRPC.Diags.CreateDiag(context.TODO(), request)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to create diagnostic")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
h.Renderer.DiagsCreateDiag(w, r)
|
||||
// Redirection après succès
|
||||
http.Redirect(w, r, fmt.Sprintf("/app/benefeciaries/%s", resp.Diag), http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
h.Renderer.BeneficiariesCreateDiag(w, r, beneficiaryID)
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) DiagsDisplayDiag(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -154,13 +179,25 @@ func parseDiagsForm(r *http.Request) (*DiagsForm, error) {
|
|||
|
||||
formData := &DiagsForm{
|
||||
Name: r.PostFormValue("name"),
|
||||
Namespace: "parcoursmob_diagnostiques",
|
||||
Namespace: r.PostFormValue("namespace"), // Récupère le namespace
|
||||
JsonSchema: r.PostFormValue("json_schema"),
|
||||
UiSchema: r.PostFormValue("ui_schema"),
|
||||
Data: map[string]any{},
|
||||
Deleted: false,
|
||||
}
|
||||
|
||||
if formData.Name == "" || formData.Namespace == "" {
|
||||
return nil, errors.New("missing required fields: 'name' or 'namespace'")
|
||||
}
|
||||
|
||||
// Gestion de la valeur JSON dans `data`
|
||||
if rawData := r.PostFormValue("data"); rawData != "" {
|
||||
data := map[string]any{}
|
||||
if err := json.Unmarshal([]byte(rawData), &data); err != nil {
|
||||
return nil, errors.New("invalid 'data' field: must be a valid JSON object")
|
||||
}
|
||||
formData.Data = data
|
||||
}
|
||||
|
||||
return formData, nil
|
||||
}
|
||||
|
||||
|
|
6
main.go
6
main.go
|
@ -179,7 +179,7 @@ func main() {
|
|||
application.HandleFunc("/agenda/{eventid}/documents/{document}", applicationHandler.EventDocumentDownload)
|
||||
////////////////////////////////////////////////////////////////
|
||||
//////////////////Diag in event////////////////////////
|
||||
application.HandleFunc("/agenda/{eventid}/create-diag", applicationHandler.DiagsCreateDiag)
|
||||
// application.HandleFunc("/agenda/{eventid}/create-diag", applicationHandler.DiagsCreateDiag)
|
||||
application.HandleFunc("/agenda/{eventid}/diags", applicationHandler.DiagsHome)
|
||||
application.HandleFunc("/agenda/{eventid}/diags/{diagid}", applicationHandler.DiagsDisplayDiag)
|
||||
application.HandleFunc("/agenda/{eventid}/diags/{diagid}/update", applicationHandler.DiagsUpdateDiag)
|
||||
|
@ -188,7 +188,7 @@ func main() {
|
|||
application.HandleFunc("/agenda/{eventid}/diags/{diagid}/history", applicationHandler.DiagsHistoryDiag)
|
||||
////////////////////////////////////////////////////////////////
|
||||
//////////////////Diag in benefeciaries////////////////////////
|
||||
application.HandleFunc("/beneficiaries/{beneficiaryid}/create-diag", applicationHandler.DiagsCreateDiag)
|
||||
application.HandleFunc("/beneficiaries/{beneficiaryid}/create-diag", applicationHandler.BeneficiariesCreateDiag)
|
||||
application.HandleFunc("/beneficiaries/{beneficiaryid}/diags", applicationHandler.DiagsHome)
|
||||
application.HandleFunc("/beneficiaries/{beneficiaryid}/diags/{diagid}", applicationHandler.DiagsDisplayDiag)
|
||||
application.HandleFunc("/beneficiaries/{beneficiaryid}/diags/{diagid}/update", applicationHandler.DiagsUpdateDiag)
|
||||
|
@ -196,7 +196,7 @@ func main() {
|
|||
application.HandleFunc("/beneficiaries/{beneficiaryid}/diags/history", applicationHandler.DiagsHistory)
|
||||
////////////////////////////////////////////////////////////////
|
||||
//////////////////Diag in vehicules////////////////////////
|
||||
application.HandleFunc("/vehicles-management/fleet/{vehicleid}/create-diag", applicationHandler.DiagsCreateDiag)
|
||||
// application.HandleFunc("/vehicles-management/fleet/{vehicleid}/create-diag", applicationHandler.DiagsCreateDiag)
|
||||
application.HandleFunc("/vehicles-management/fleet/{vehicleid}/diags", applicationHandler.DiagsHome)
|
||||
application.HandleFunc("/vehicles-management/fleet/{vehicleid}/diags/{diagid}", applicationHandler.DiagsDisplayDiag)
|
||||
application.HandleFunc("/vehicles-management/fleet/{vehicleid}/diags/{diagid}/update", applicationHandler.DiagsUpdateDiag)
|
||||
|
|
|
@ -53,7 +53,7 @@ type BeneficiariesDisplayState struct {
|
|||
Beneficiary any
|
||||
}
|
||||
|
||||
func (renderer *Renderer) BeneficiaryDisplay(w http.ResponseWriter, r *http.Request, beneficiary any, bookings []fleetsstorage.Booking, organizations []any, beneficiaries_file_types []string, file_types_map map[string]string, documents any, event interface{}) {
|
||||
func (renderer *Renderer) BeneficiaryDisplay(w http.ResponseWriter, r *http.Request, beneficiary any, bookings []fleetsstorage.Booking, organizations []any, beneficiaries_file_types []string, file_types_map map[string]string, documents any, event interface{}, diags []any) {
|
||||
files := renderer.ThemeConfig.GetStringSlice("views.beneficiaries.display.files")
|
||||
state := NewState(r, renderer.ThemeConfig, beneficiariesMenu)
|
||||
state.ViewState = map[string]any{
|
||||
|
@ -64,6 +64,7 @@ func (renderer *Renderer) BeneficiaryDisplay(w http.ResponseWriter, r *http.Requ
|
|||
"documents": documents,
|
||||
"organizations": organizations,
|
||||
"event": event,
|
||||
"diags": diags,
|
||||
}
|
||||
renderer.Render("beneficiaries_display", w, r, files, state)
|
||||
}
|
||||
|
|
|
@ -28,11 +28,13 @@ func (renderer *Renderer) DiagsHistory(w http.ResponseWriter, r *http.Request, d
|
|||
renderer.Render("diags history", w, r, files, state)
|
||||
}
|
||||
|
||||
func (renderer *Renderer) DiagsCreateDiag(w http.ResponseWriter, r *http.Request) {
|
||||
func (renderer *Renderer) BeneficiariesCreateDiag(w http.ResponseWriter, r *http.Request, beneficiary string) {
|
||||
state := NewState(r, renderer.ThemeConfig, diagsMenu)
|
||||
files := renderer.ThemeConfig.GetStringSlice("views.diags.create_diag.files")
|
||||
|
||||
renderer.Render("diags create diag", w, r, files, state)
|
||||
files := renderer.ThemeConfig.GetStringSlice("views.beneficiaries.create_diag.files")
|
||||
state.ViewState = map[string]any{
|
||||
"beneficiary": beneficiary,
|
||||
}
|
||||
renderer.Render("diag create for beneficiary", w, r, files, state)
|
||||
}
|
||||
|
||||
func (renderer *Renderer) DiagsDisplayDiag(w http.ResponseWriter, r *http.Request, diag any) {
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
// "time"
|
||||
|
||||
diags "git.coopgo.io/coopgo-platform/diags/grpcapi"
|
||||
"git.coopgo.io/coopgo-platform/diags/storage"
|
||||
"google.golang.org/grpc"
|
||||
// "google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
type DiagsService struct {
|
||||
diags.DiagsClient
|
||||
}
|
||||
|
||||
func NewDiagsService(dial string) (*DiagsService, error) {
|
||||
conn, err := grpc.Dial(dial, grpc.WithInsecure())
|
||||
|
||||
client := diags.NewDiagsClient(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &DiagsService{
|
||||
DiagsClient: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *ServicesHandler) GetDiagsDiags() ([]DiagsDiag, error) {
|
||||
resp, err := s.GRPC.Diags.GetDiags(context.TODO(), &diags.GetDiagsRequest{
|
||||
Namespaces: []string{"parcoursmob_beneficiaires", "parcoursmob_diagnostiques"},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
diags := []DiagsDiag{}
|
||||
|
||||
for _, e := range resp.Diags {
|
||||
newDiag := DiagsDiag{
|
||||
Diag: e.ToStorageType(),
|
||||
}
|
||||
diags = append(diags, newDiag)
|
||||
}
|
||||
|
||||
return diags, nil
|
||||
}
|
||||
|
||||
// Enriched types
|
||||
|
||||
type DiagsDiag struct {
|
||||
OwnersGroups []GroupsManagementGroup
|
||||
storage.Diag
|
||||
}
|
|
@ -27,6 +27,7 @@ func NewServicesHandler(cfg *viper.Viper) (*ServicesHandler, error) {
|
|||
groupsManagementDial = cfg.GetString("services.grpc.groupsmanagement.dial")
|
||||
fleetsDial = cfg.GetString("services.grpc.fleets.dial")
|
||||
agendaDial = cfg.GetString("services.grpc.agenda.dial")
|
||||
diagsDial = cfg.GetString("services.grpc.diags.dial")
|
||||
)
|
||||
mobilityAccounts, err := NewMobilityAccountService(mobilityAccountsDial)
|
||||
if err != nil {
|
||||
|
@ -48,12 +49,18 @@ func NewServicesHandler(cfg *viper.Viper) (*ServicesHandler, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
diagsSvc, err := NewDiagsService(diagsDial)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ServicesHandler{
|
||||
GRPC: GRPCServices{
|
||||
MobilityAccounts: mobilityAccounts,
|
||||
GroupsManagement: groupsManagement,
|
||||
Fleets: fleetsSvc,
|
||||
Agenda: agendaSvc,
|
||||
Diags: diagsSvc,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue