81 lines
2.4 KiB
Go
81 lines
2.4 KiB
Go
package application
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"git.coopgo.io/coopgo-platform/solidarity-transport/servers/grpc/proto/gen"
|
|
"git.coopgo.io/coopgo-platform/solidarity-transport/servers/grpc/transformers"
|
|
"github.com/gorilla/mux"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func (h *ApplicationHandler) SolidarityTransportExternalBookingProposal(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
bookingId := vars["bookingid"]
|
|
|
|
resp, err := h.services.GRPC.SolidarityTransport.GetSolidarityTransportBooking(context.Background(), &gen.GetSolidarityTransportBookingRequest{
|
|
Id: bookingId,
|
|
})
|
|
|
|
booking, err := transformers.BookingProtoToType(resp.Booking)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("could not transform booking type")
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
driver, err := h.services.GetAccount(booking.DriverId)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("driver retrieval issue")
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
passenger, err := h.services.GetAccount(booking.PassengerId)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("passenger retrieval issue")
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if r.Method == "POST" {
|
|
if err = r.ParseForm(); err != nil {
|
|
log.Error().Err(err).Msg("error parsing form data")
|
|
}
|
|
message := r.FormValue("message")
|
|
action := r.FormValue("action")
|
|
var status string
|
|
if action == "confirm" {
|
|
status = "VALIDATED"
|
|
} else if action == "cancel" {
|
|
status = "CANCELLED"
|
|
} else if action == "waitconfirmation" {
|
|
status = "WAITING_CONFIRMATION"
|
|
}
|
|
if status != "" {
|
|
if _, err := h.services.GRPC.SolidarityTransport.UpdateSolidarityTransportBookingStatus(context.Background(), &gen.UpdateSolidarityTransportBookingStatusRequest{
|
|
BookingId: bookingId,
|
|
NewStatus: status,
|
|
Reason: "Refusé par le bénévole",
|
|
}); err != nil {
|
|
log.Error().Err(err).Msg("update booking status issue")
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
booking.Status = status
|
|
if status == "VALIDATED" {
|
|
h.GenerateSMS(passenger.ID, message)
|
|
} else if status == "CANCELLED" {
|
|
if err := h.creditWallet(passenger.ID, booking.Journey.Price.Amount); err != nil {
|
|
log.Error().Err(err).Msg("could not credit wallet")
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
h.Renderer.SolidarityTransportExternalBookingDisplay(w, r, booking, driver, passenger)
|
|
}
|