parcoursmob/handlers/application/diags.go

495 lines
13 KiB
Go
Raw Normal View History

2024-11-27 14:54:33 +00:00
package application
import (
"context"
2024-12-04 08:23:45 +00:00
"encoding/json"
"errors"
2024-11-27 14:54:33 +00:00
"fmt"
2024-12-04 08:23:45 +00:00
2024-11-27 14:54:33 +00:00
// "io"
"net/http"
// "strconv"
"strings"
2024-12-04 16:27:38 +00:00
// "time"
2024-11-27 14:54:33 +00:00
2024-12-04 08:23:45 +00:00
"git.coopgo.io/coopgo-apps/parcoursmob/services"
2024-11-27 14:54:33 +00:00
"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"
2024-12-04 08:23:45 +00:00
2024-11-27 14:54:33 +00:00
// 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"
// "github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/types/known/structpb"
2024-12-04 16:27:38 +00:00
// "google.golang.org/protobuf/types/known/timestamppb"
2024-11-27 14:54:33 +00:00
)
type DiagsForm struct {
Name string `json:"name" validate:"required"`
Namespace string `json:"namespace" validate:"required"`
JsonSchema string `json:"json_schema"`
UiSchema string `json:"ui_schema"`
Data map[string]any `json:"data"`
Deleted bool `json:"deleted"`
}
func (h *ApplicationHandler) DiagsHome(w http.ResponseWriter, r *http.Request) {
resp, err := h.services.GRPC.Diags.GetDiags(context.TODO(), &diags.GetDiagsRequest{
Namespaces: []string{"parcoursmob_beneficiaries", "parcoursmob_diagnostiques", "parcoursmob_vehicles", "parcoursmob_bookings"},
2024-12-04 16:27:38 +00:00
// Mindate: timestamppb.New(time.Now().Add(-24 * time.Hour)),
2024-11-27 14:54:33 +00:00
})
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
responses := []diagsstorage.Diag{}
for _, e := range resp.Diags {
responses = append(responses, e.ToStorageType())
}
h.Renderer.DiagsHome(w, r, responses)
}
func (h *ApplicationHandler) DiagsHistory(w http.ResponseWriter, r *http.Request) {
resp, err := h.services.GRPC.Diags.GetDiags(context.TODO(), &diags.GetDiagsRequest{
Namespaces: []string{"parcoursmob_beneficiaries", "parcoursmob_diagnostiques", "parcoursmob_vehicles", "parcoursmob_bookings"},
2024-11-27 14:54:33 +00:00
//Maxdate: timestamppb.New(time.Now().Add(24 * time.Hour)),
})
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
responses := []diagsstorage.Diag{}
for _, e := range resp.Diags {
responses = append(responses, e.ToStorageType())
}
h.Renderer.DiagsHistory(w, r, responses)
}
2024-12-04 08:23:45 +00:00
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 {
2024-11-27 14:54:33 +00:00
g := r.Context().Value(identification.GroupKey)
if g == nil {
2024-12-04 08:23:45 +00:00
http.Error(w, "Missing group information", http.StatusBadRequest)
2024-11-27 14:54:33 +00:00
return
}
diagForm, err := parseDiagsForm(r)
if err != nil {
2024-12-04 08:23:45 +00:00
log.Error().Err(err).Msg("Invalid form data")
http.Error(w, err.Error(), http.StatusBadRequest)
2024-11-27 14:54:33 +00:00
return
}
2024-12-04 08:23:45 +00:00
log.Debug().Interface("diagForm", diagForm).Msg("Form data parsed")
2024-11-27 14:54:33 +00:00
2024-12-04 08:23:45 +00:00
data, err := structpb.NewStruct(map[string]any{
"beneficiary": beneficiaryID,
2024-11-27 14:54:33 +00:00
})
2024-12-04 08:23:45 +00:00
if err != nil {
log.Error().Err(err).Msg("Failed to create protobuf struct")
w.WriteHeader(http.StatusInternalServerError)
return
}
2024-11-27 14:54:33 +00:00
request := &diags.CreateDiagRequest{
Diag: &diags.Diag{
2024-12-04 08:23:45 +00:00
Name: diagForm.Name,
Namespace: diagForm.Namespace,
JsonSchema: diagForm.JsonSchema,
UiSchema: diagForm.UiSchema,
Data: data,
Deleted: diagForm.Deleted,
2024-11-27 14:54:33 +00:00
},
}
resp, err := h.services.GRPC.Diags.CreateDiag(context.TODO(), request)
if err != nil {
2024-12-04 08:23:45 +00:00
log.Error().Err(err).Msg("Failed to create diagnostic")
2024-11-27 14:54:33 +00:00
w.WriteHeader(http.StatusInternalServerError)
return
}
2024-12-04 17:21:29 +00:00
http.Redirect(w, r, fmt.Sprintf("/app/diags/%s", resp.Diag.Id), http.StatusFound)
2024-11-27 14:54:33 +00:00
return
}
2024-12-04 08:23:45 +00:00
h.Renderer.BeneficiariesCreateDiag(w, r, beneficiaryID)
2024-11-27 14:54:33 +00:00
}
2024-12-10 15:14:43 +00:00
func (h *ApplicationHandler) VehiclesCreateDiag(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
vehicleID := vars["vehicleid"]
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 {
g := r.Context().Value(identification.GroupKey)
if g == nil {
http.Error(w, "Missing group information", http.StatusBadRequest)
return
}
diagForm, err := parseDiagsForm(r)
if err != nil {
log.Error().Err(err).Msg("Invalid form data")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Debug().Interface("diagForm", diagForm).Msg("Form data parsed")
data, err := structpb.NewStruct(map[string]any{
"vehicle": vehicleID,
})
if err != nil {
log.Error().Err(err).Msg("Failed to create protobuf struct")
w.WriteHeader(http.StatusInternalServerError)
return
}
request := &diags.CreateDiagRequest{
Diag: &diags.Diag{
Name: diagForm.Name,
Namespace: diagForm.Namespace,
JsonSchema: diagForm.JsonSchema,
UiSchema: diagForm.UiSchema,
Data: data,
Deleted: diagForm.Deleted,
},
}
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
}
http.Redirect(w, r, fmt.Sprintf("/app/diags/%s", resp.Diag.Id), http.StatusFound)
return
}
h.Renderer.VehiclesCreateDiag(w, r, vehicleID)
}
2024-12-16 11:37:40 +00:00
func (h *ApplicationHandler) BookingsCreateDiag(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bookingID := vars["bookingid"]
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 {
g := r.Context().Value(identification.GroupKey)
if g == nil {
http.Error(w, "Missing group information", http.StatusBadRequest)
return
}
diagForm, err := parseDiagsForm(r)
if err != nil {
log.Error().Err(err).Msg("Invalid form data")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Debug().Interface("diagForm", diagForm).Msg("Form data parsed")
data, err := structpb.NewStruct(map[string]any{
"booking": bookingID,
})
if err != nil {
log.Error().Err(err).Msg("Failed to create protobuf struct")
w.WriteHeader(http.StatusInternalServerError)
return
}
request := &diags.CreateDiagRequest{
Diag: &diags.Diag{
Name: diagForm.Name,
Namespace: diagForm.Namespace,
JsonSchema: diagForm.JsonSchema,
UiSchema: diagForm.UiSchema,
Data: data,
Deleted: diagForm.Deleted,
},
}
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
}
http.Redirect(w, r, fmt.Sprintf("/app/diags/%s", resp.Diag.Id), http.StatusFound)
return
}
h.Renderer.BookingsCreateDiag(w, r, bookingID)
}
func (h *ApplicationHandler) VehicleBookingsCreateDiag(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bookingID := vars["bookingid"]
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 {
g := r.Context().Value(identification.GroupKey)
if g == nil {
http.Error(w, "Missing group information", http.StatusBadRequest)
return
}
diagForm, err := parseDiagsForm(r)
if err != nil {
log.Error().Err(err).Msg("Invalid form data")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Debug().Interface("diagForm", diagForm).Msg("Form data parsed")
data, err := structpb.NewStruct(map[string]any{
"booking": bookingID,
})
if err != nil {
log.Error().Err(err).Msg("Failed to create protobuf struct")
w.WriteHeader(http.StatusInternalServerError)
return
}
request := &diags.CreateDiagRequest{
Diag: &diags.Diag{
Name: diagForm.Name,
Namespace: diagForm.Namespace,
JsonSchema: diagForm.JsonSchema,
UiSchema: diagForm.UiSchema,
Data: data,
Deleted: diagForm.Deleted,
},
}
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
}
http.Redirect(w, r, fmt.Sprintf("/app/diags/%s", resp.Diag.Id), http.StatusFound)
return
}
h.Renderer.VehicleBookingsCreateDiag(w, r, bookingID)
}
2024-11-27 14:54:33 +00:00
func (h *ApplicationHandler) DiagsDisplayDiag(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
diagid := vars["diagid"]
request := &diags.GetDiagRequest{
Id: diagid,
}
resp, err := h.services.GRPC.Diags.GetDiag(context.TODO(), request)
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
g := r.Context().Value(identification.GroupKey)
if g == nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
h.Renderer.DiagsDisplayDiag(w, r, resp.Diag.ToStorageType())
}
func parseDiagsForm(r *http.Request) (*DiagsForm, error) {
if err := r.ParseForm(); err != nil {
return nil, err
}
formData := &DiagsForm{
2024-12-04 08:23:45 +00:00
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
2024-11-27 14:54:33 +00:00
}
return formData, nil
}
2024-12-04 16:27:38 +00:00
func (h *ApplicationHandler) DiagUpdate(w http.ResponseWriter, r *http.Request) {
2024-11-27 14:54:33 +00:00
adm := strings.Split(r.URL.Path, "/")
diagID := adm[3]
request := &diags.GetDiagRequest{
Id: diagID,
}
resp, err := h.services.GRPC.Diags.GetDiag(context.TODO(), request)
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
if r.Method == "POST" {
g := r.Context().Value(identification.GroupKey)
if g == nil {
w.WriteHeader(http.StatusBadRequest)
return
}
diagForm, err := parseDiagsForm(r)
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusBadRequest)
return
}
data, _ := structpb.NewStruct(map[string]any{})
request := &diags.UpdateDiagRequest{
Diag: &diags.Diag{
2024-12-10 15:14:43 +00:00
Namespace: diagForm.Namespace,
2024-11-27 14:54:33 +00:00
Id: diagID,
Name: diagForm.Name,
JsonSchema: diagForm.JsonSchema,
UiSchema: diagForm.UiSchema,
Data: data,
},
}
resp, err := h.services.GRPC.Diags.UpdateDiag(context.TODO(), request)
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
http.Redirect(w, r, fmt.Sprintf("/app/diags/%s", resp.Diag.Id), http.StatusFound)
return
}
2024-12-04 16:27:38 +00:00
h.Renderer.DiagUpdate(w, r, resp.Diag.ToStorageType())
2024-11-27 14:54:33 +00:00
}
2024-12-04 16:27:38 +00:00
func (h *ApplicationHandler) DiagDelete(w http.ResponseWriter, r *http.Request) {
2024-11-27 14:54:33 +00:00
vars := mux.Vars(r)
diagID := vars["diagid"]
request := &diags.GetDiagRequest{
Id: diagID,
}
resp, err := h.services.GRPC.Diags.GetDiag(context.TODO(), request)
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
if r.Method == "POST" {
request := &diags.UpdateDiagRequest{
Diag: &diags.Diag{
Namespace: resp.Diag.Namespace,
Id: resp.Diag.Id,
Name: resp.Diag.Name,
JsonSchema: resp.Diag.JsonSchema,
UiSchema: resp.Diag.UiSchema,
Data: resp.Diag.Data,
Deleted: true,
},
}
_, err := h.services.GRPC.Diags.UpdateDiag(context.TODO(), request)
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/app/diags/", http.StatusFound)
return
}
2024-12-04 16:27:38 +00:00
h.Renderer.DiagDelete(w, r, resp.Diag.ToStorageType())
2024-11-27 14:54:33 +00:00
}
func (h *ApplicationHandler) DiagsHistoryDiag(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
diagId := vars["diagid"]
request := &diags.GetDiagRequest{
Id: diagId,
}
resp, err := h.services.GRPC.Diags.GetDiag(context.TODO(), request)
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
h.Renderer.DiagsHistoryDiag(w, r, resp.Diag.ToStorageType())
}