solidarity transport updates
This commit is contained in:
32
handlers/api/protected/protected.go
Normal file
32
handlers/api/protected/protected.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package protected
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/services"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
||||
cache "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type ProtectedAPIHandler struct {
|
||||
ApiKey string
|
||||
idp *identification.IdentificationProvider
|
||||
config *viper.Viper
|
||||
services *services.ServicesHandler
|
||||
cache cache.CacheHandler
|
||||
}
|
||||
|
||||
func NewProtectedAPIHandler(cfg *viper.Viper, idp *identification.IdentificationProvider, svc *services.ServicesHandler, cache cache.CacheHandler) (*ProtectedAPIHandler, error) {
|
||||
return &ProtectedAPIHandler{
|
||||
ApiKey: cfg.GetString("services.api.api_key"),
|
||||
idp: idp,
|
||||
config: cfg,
|
||||
services: svc,
|
||||
cache: cache,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *ProtectedAPIHandler) NotFound(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
65
handlers/api/protected/users.go
Normal file
65
handlers/api/protected/users.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package protected
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
groupsgrpc "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
||||
"git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
||||
"git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (h *ProtectedAPIHandler) Users(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
h.postUsers(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
|
||||
func (h *ProtectedAPIHandler) postUsers(w http.ResponseWriter, r *http.Request) {
|
||||
var user storage.Account
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&user)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("could not read account input")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
account, err := grpcapi.AccountFromStorageType(&user)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("could not generate protobuf for account")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.services.GRPC.MobilityAccounts.Register(context.Background(), &grpcapi.RegisterRequest{
|
||||
Account: account,
|
||||
})
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("grpc request issue")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if g, ok := user.Metadata["import_in_group"]; ok {
|
||||
if group, ok := g.(string); ok {
|
||||
_, err = h.services.GRPC.GroupsManagement.Subscribe(context.Background(), &groupsgrpc.SubscribeRequest{
|
||||
Groupid: group,
|
||||
Memberid: resp.Account.Id,
|
||||
})
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("grpc request issue, groups")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
Reference in New Issue
Block a user