add diags services
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
// Redirection après succès
|
||||
http.Redirect(w, r, fmt.Sprintf("/app/benefeciaries/%s", resp.Diag), http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
h.Renderer.DiagsCreateDiag(w, r)
|
||||
h.Renderer.BeneficiariesCreateDiag(w, r, beneficiaryID)
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) DiagsDisplayDiag(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -153,12 +178,24 @@ func parseDiagsForm(r *http.Request) (*DiagsForm, error) {
|
||||
}
|
||||
|
||||
formData := &DiagsForm{
|
||||
Name: r.PostFormValue("name"),
|
||||
Namespace: "parcoursmob_diagnostiques",
|
||||
JsonSchema: r.PostFormValue("json_schema"),
|
||||
UiSchema: r.PostFormValue("ui_schema"),
|
||||
Data: map[string]any{},
|
||||
Deleted: false,
|
||||
Name: r.PostFormValue("name"),
|
||||
Namespace: r.PostFormValue("namespace"), // Récupère le namespace
|
||||
JsonSchema: r.PostFormValue("json_schema"),
|
||||
UiSchema: r.PostFormValue("ui_schema"),
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user