Add VehiclesCreateDiag

This commit is contained in:
2024-12-10 16:14:43 +01:00
parent 7665fc75a0
commit 1162f2faf2
5 changed files with 104 additions and 8 deletions

View File

@@ -39,7 +39,7 @@ type DiagsForm struct {
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_vehicle"},
Namespaces: []string{"parcoursmob_beneficiaries", "parcoursmob_diagnostiques", "parcoursmob_vehicles"},
// Mindate: timestamppb.New(time.Now().Add(-24 * time.Hour)),
})
@@ -140,6 +140,67 @@ func (h *ApplicationHandler) BeneficiariesCreateDiag(w http.ResponseWriter, r *h
h.Renderer.BeneficiariesCreateDiag(w, r, beneficiaryID)
}
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)
}
func (h *ApplicationHandler) DiagsDisplayDiag(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
diagid := vars["diagid"]
@@ -226,7 +287,7 @@ func (h *ApplicationHandler) DiagUpdate(w http.ResponseWriter, r *http.Request)
request := &diags.UpdateDiagRequest{
Diag: &diags.Diag{
Namespace: "parcoursmob_diagnostiques",
Namespace: diagForm.Namespace,
Id: diagID,
Name: diagForm.Name,
JsonSchema: diagForm.JsonSchema,

View File

@@ -11,6 +11,8 @@ import (
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/sorting"
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"
fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
fleetsstorage "git.coopgo.io/coopgo-platform/fleets/storage"
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
@@ -198,7 +200,33 @@ func (h *ApplicationHandler) VehiclesFleetDisplay(w http.ResponseWriter, r *http
return
}
h.Renderer.VehiclesFleetDisplay(w, r, resp.Vehicle.ToStorageType())
diag := []diagsstorage.Diag{}
diagsrequest := &diags.GetDiagsRequest{
Namespaces: []string{"parcoursmob_vehicles"},
}
diagsresp, err := h.services.GRPC.Diags.GetDiags(context.TODO(), diagsrequest)
if err != nil {
log.Error().Err(err).Msg("")
w.WriteHeader(http.StatusInternalServerError)
return
}
for _, d := range diagsresp.Diags {
diagData := d.Data.AsMap()
if vehicle, ok := diagData["vehicle"].(string); ok && vehicle == vehicleid {
diag = append(diag, d.ToStorageType())
}
}
diagsAny := make([]any, len(diag))
for i, d := range diag {
diagsAny[i] = d
}
h.Renderer.VehiclesFleetDisplay(w, r, resp.Vehicle.ToStorageType(), diagsAny)
}
func filterVehicle(r *http.Request, v *fleets.Vehicle) bool {