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"
|
2025-02-10 15:17:20 +00:00
|
|
|
"io"
|
2024-12-04 08:23:45 +00:00
|
|
|
|
2024-11-27 14:54:33 +00:00
|
|
|
// "io"
|
|
|
|
"net/http"
|
|
|
|
// "strconv"
|
|
|
|
"strings"
|
2025-02-10 11:47:41 +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"
|
2025-02-10 15:17:20 +00:00
|
|
|
filestorage "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
|
|
|
|
|
2024-11-27 14:54:33 +00:00
|
|
|
// 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
|
|
|
|
2025-02-12 09:42:26 +00:00
|
|
|
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
|
|
|
"git.coopgo.io/coopgo-platform/groups-management/storage"
|
2024-11-27 14:54:33 +00:00
|
|
|
// mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
|
|
|
// "github.com/google/uuid"
|
2025-02-10 15:17:20 +00:00
|
|
|
"github.com/google/uuid"
|
2024-11-27 14:54:33 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
2025-02-10 11:47:41 +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"`
|
2025-02-10 11:47:41 +00:00
|
|
|
Diagdate *time.Time `json:"diagdate"`
|
2025-02-12 09:42:26 +00:00
|
|
|
Owners []string `json:"owners"`
|
2024-11-27 14:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ApplicationHandler) DiagsHome(w http.ResponseWriter, r *http.Request) {
|
2025-02-12 09:42:26 +00:00
|
|
|
groupID := r.Context().Value(identification.GroupKey).(storage.Group).ID
|
|
|
|
|
2024-11-27 14:54:33 +00:00
|
|
|
resp, err := h.services.GRPC.Diags.GetDiags(context.TODO(), &diags.GetDiagsRequest{
|
2024-12-17 09:32:35 +00:00
|
|
|
Namespaces: []string{"parcoursmob_beneficiaries", "parcoursmob_diagnostiques", "parcoursmob_vehicles", "parcoursmob_bookings"},
|
2024-11-27 14:54:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
responses := []diagsstorage.Diag{}
|
|
|
|
|
2025-02-12 09:42:26 +00:00
|
|
|
groupids := []string{}
|
2024-11-27 14:54:33 +00:00
|
|
|
for _, e := range resp.Diags {
|
2025-02-12 09:42:26 +00:00
|
|
|
groupids = append(groupids, e.Owners...)
|
2024-11-27 14:54:33 +00:00
|
|
|
responses = append(responses, e.ToStorageType())
|
|
|
|
}
|
|
|
|
|
2025-02-12 09:42:26 +00:00
|
|
|
groupsresp, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), &groupsmanagement.GetGroupsBatchRequest{
|
|
|
|
Groupids: groupids,
|
|
|
|
})
|
|
|
|
groups := map[string]any{}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
for _, g := range groupsresp.Groups {
|
|
|
|
groups[g.Id] = g.ToStorageType()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredDiags := []diagsstorage.Diag{}
|
|
|
|
for _, diag := range responses {
|
|
|
|
for _, owner := range diag.Owners {
|
|
|
|
if string(owner) == groupID {
|
|
|
|
filteredDiags = append(filteredDiags, diag)
|
|
|
|
log.Debug().Msgf("Diag %s added to filtered list", diag.ID)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Renderer.DiagsHome(w, r, filteredDiags, groups)
|
2024-11-27 14:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ApplicationHandler) DiagsHistory(w http.ResponseWriter, r *http.Request) {
|
|
|
|
resp, err := h.services.GRPC.Diags.GetDiags(context.TODO(), &diags.GetDiagsRequest{
|
2024-12-17 09:33:17 +00:00
|
|
|
Namespaces: []string{"parcoursmob_beneficiaries", "parcoursmob_diagnostiques", "parcoursmob_vehicles", "parcoursmob_bookings"},
|
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 {
|
2024-12-18 15:00:38 +00:00
|
|
|
if e.Deleted {
|
|
|
|
responses = append(responses, e.ToStorageType())
|
|
|
|
}
|
2024-11-27 14:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2025-02-12 09:42:26 +00:00
|
|
|
group := g.(storage.Group)
|
|
|
|
|
2024-11-27 14:54:33 +00:00
|
|
|
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
|
|
|
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,
|
2025-02-10 11:47:41 +00:00
|
|
|
Diagdate: timestamppb.New(time.Now()),
|
2025-02-12 09:42:26 +00:00
|
|
|
Owners: []string{group.ID},
|
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
|
|
|
|
}
|
|
|
|
|
2025-02-10 15:17:20 +00:00
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
if strings.HasPrefix(contentType, "multipart/form-data") {
|
|
|
|
err = r.ParseMultipartForm(100 * 1024 * 1024) // 100 MB limit
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error parsing multipart form")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, header, err := r.FormFile("file-upload")
|
|
|
|
if err == nil {
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
document_type := r.FormValue("file_type")
|
|
|
|
document_name := r.FormValue("file_name")
|
|
|
|
|
|
|
|
fileid := uuid.NewString()
|
|
|
|
|
|
|
|
metadata := map[string]string{
|
|
|
|
"file_type": document_type,
|
|
|
|
"file_name": document_name,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.filestorage.Put(file, filestorage.PREFIX_DIAGS, fmt.Sprintf("%s/%s_%s", resp.Diag.Id, fileid, header.Filename), header.Size, metadata); err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error uploading file")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if err != http.ErrMissingFile {
|
|
|
|
log.Error().Err(err).Msg("Error retrieving file")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2025-02-12 09:42:26 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2025-02-10 15:17:20 +00:00
|
|
|
h.Renderer.BeneficiariesCreateDiag(w, r, beneficiaryID, h.config.GetStringSlice("modules.diags.documents_types"), h.config.GetStringMapString("storage.files.file_types"), nil)
|
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
|
|
|
|
}
|
|
|
|
|
2025-02-12 09:42:26 +00:00
|
|
|
group := g.(storage.Group)
|
|
|
|
|
2024-12-10 15:14:43 +00:00
|
|
|
request := &diags.CreateDiagRequest{
|
|
|
|
Diag: &diags.Diag{
|
|
|
|
Name: diagForm.Name,
|
|
|
|
Namespace: diagForm.Namespace,
|
|
|
|
JsonSchema: diagForm.JsonSchema,
|
|
|
|
UiSchema: diagForm.UiSchema,
|
|
|
|
Data: data,
|
|
|
|
Deleted: diagForm.Deleted,
|
2025-02-10 11:47:41 +00:00
|
|
|
Diagdate: timestamppb.New(time.Now()),
|
2025-02-12 09:42:26 +00:00
|
|
|
Owners: []string{group.ID},
|
2024-12-10 15:14:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2025-02-12 09:42:26 +00:00
|
|
|
group := g.(storage.Group)
|
|
|
|
|
2024-12-16 11:37:40 +00:00
|
|
|
request := &diags.CreateDiagRequest{
|
|
|
|
Diag: &diags.Diag{
|
|
|
|
Name: diagForm.Name,
|
|
|
|
Namespace: diagForm.Namespace,
|
|
|
|
JsonSchema: diagForm.JsonSchema,
|
|
|
|
UiSchema: diagForm.UiSchema,
|
|
|
|
Data: data,
|
|
|
|
Deleted: diagForm.Deleted,
|
2025-02-10 11:47:41 +00:00
|
|
|
Diagdate: timestamppb.New(time.Now()),
|
2025-02-12 09:42:26 +00:00
|
|
|
Owners: []string{group.ID},
|
2024-12-16 11:37:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-12-16 13:42:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2025-02-12 09:42:26 +00:00
|
|
|
group := g.(storage.Group)
|
|
|
|
|
2024-12-16 13:42:00 +00:00
|
|
|
request := &diags.CreateDiagRequest{
|
|
|
|
Diag: &diags.Diag{
|
|
|
|
Name: diagForm.Name,
|
|
|
|
Namespace: diagForm.Namespace,
|
|
|
|
JsonSchema: diagForm.JsonSchema,
|
|
|
|
UiSchema: diagForm.UiSchema,
|
|
|
|
Data: data,
|
|
|
|
Deleted: diagForm.Deleted,
|
2025-02-10 11:47:41 +00:00
|
|
|
Diagdate: timestamppb.New(time.Now()),
|
2025-02-12 09:42:26 +00:00
|
|
|
Owners: []string{group.ID},
|
2024-12-16 13:42:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-10 15:17:20 +00:00
|
|
|
documents := h.filestorage.List(filestorage.PREFIX_DIAGS + "/" + diagid)
|
|
|
|
|
|
|
|
events_file_types := h.config.GetStringSlice("modules.diags.documents_types")
|
|
|
|
file_types_map := h.config.GetStringMapString("storage.files.file_types")
|
|
|
|
|
|
|
|
h.Renderer.DiagsDisplayDiag(w, r, resp.Diag.ToStorageType(), events_file_types, file_types_map, documents)
|
2024-11-27 14:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2025-02-12 09:42:26 +00:00
|
|
|
group := g.(storage.Group)
|
|
|
|
|
2024-11-27 14:54:33 +00:00
|
|
|
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,
|
2025-02-10 11:47:41 +00:00
|
|
|
Diagdate: timestamppb.New(time.Now()),
|
2025-02-12 09:42:26 +00:00
|
|
|
Owners: []string{group.ID},
|
2024-11-27 14:54:33 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2025-02-10 11:47:41 +00:00
|
|
|
Diagdate: resp.Diag.Diagdate,
|
2024-11-27 14:54:33 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, 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())
|
|
|
|
}
|
2025-02-10 15:17:20 +00:00
|
|
|
|
|
|
|
// //// ADD DOCUMENTS //////
|
|
|
|
func (h *ApplicationHandler) DiagsDocuments(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
diagID := vars["diagid"]
|
|
|
|
|
|
|
|
//r.ParseForm()
|
|
|
|
r.ParseMultipartForm(100 * 1024 * 1024)
|
|
|
|
|
|
|
|
document_type := r.FormValue("type")
|
|
|
|
document_name := r.FormValue("name")
|
|
|
|
|
|
|
|
file, header, err := r.FormFile("file-upload")
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
fileid := uuid.NewString()
|
|
|
|
|
|
|
|
metadata := map[string]string{
|
|
|
|
"type": document_type,
|
|
|
|
"name": document_name,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.filestorage.Put(file, filestorage.PREFIX_DIAGS, fmt.Sprintf("%s/%s_%s", diagID, fileid, header.Filename), header.Size, metadata); err != nil {
|
|
|
|
log.Error().Err(err).Msg("")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/app/diags/%s", diagID), http.StatusFound)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ApplicationHandler) DiagsDocumentDownload(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
diagID := vars["diagid"]
|
|
|
|
document := vars["document"]
|
|
|
|
|
|
|
|
file, info, err := h.filestorage.Get(filestorage.PREFIX_DIAGS, fmt.Sprintf("%s/%s", diagID, document))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", info.ContentType)
|
|
|
|
if _, err = io.Copy(w, file); err != nil {
|
|
|
|
log.Error().Err(err).Msg("")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/app/diags/%s", diagID), http.StatusFound)
|
|
|
|
|
2025-02-12 09:42:26 +00:00
|
|
|
}
|