66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
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)
|
|
}
|