refactoring

This commit is contained in:
2023-12-08 07:35:04 +01:00
parent 4a6326a5ab
commit f1d60881e5
32 changed files with 5949 additions and 434 deletions

16
handler/account.go Normal file
View File

@@ -0,0 +1,16 @@
package handler
import (
"context"
"git.coopgo.io/coopgo-apps/silvermobi/models"
"git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
)
func (h *SilvermobiHandler) GetAccountInfos(ctx context.Context, id string) (account *models.Account, err error) {
resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, &grpcapi.GetAccountRequest{Id: id})
if err != nil {
return nil, err
}
account = h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
return account, nil
}

57
handler/geo.go Normal file
View File

@@ -0,0 +1,57 @@
package handler
import (
"fmt"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"google.golang.org/genproto/googleapis/maps/routing/v2"
)
func (h *SilvermobiHandler) GeoAutocomplete(text string, lat, lon float64) (*geojson.FeatureCollection, error) {
result, err := h.Services.Geocoder.Autocomplete(text)
if err != nil {
return nil, err
}
return result, nil
}
func (h *SilvermobiHandler) GeoRoute(locations geojson.FeatureCollection) (route *routing.Route, err error) {
route_locations := []orb.Point{}
features_type := ""
for _, f := range locations.Features {
ft := f.Geometry.GeoJSONType()
if features_type != "" && ft != features_type {
return nil, fmt.Errorf("mixing different types of geometries in the feature collection is not allowed : %s and %s found", features_type, ft)
}
features_type = ft
if features_type == "Point" {
if point, ok := f.Geometry.(orb.Point); ok {
route_locations = append(route_locations, point)
}
} else {
return nil, fmt.Errorf("feature type %s not supported", features_type)
}
return nil, err
}
return route, nil
}
func (h *SilvermobiHandler) GeoReturnRoute(locations geojson.FeatureCollection) (route *routing.Route, err error) {
loc := locations
route.Polyline.String()
reverse(loc.Features)
return h.GeoRoute(loc)
}
func reverse[S ~[]E, E any](s S) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}

View File

@@ -7,15 +7,15 @@ import (
)
type SilvermobiHandler struct {
Config *viper.Viper
Services *services.ServicesHandler
KVHandler storage.KVHandler
Config *viper.Viper
Services *services.ServicesHandler
Storage storage.Storage
}
func NewSilvermobiHandler(cfg *viper.Viper, services *services.ServicesHandler, KVHandler storage.KVHandler) (*SilvermobiHandler, error) {
func NewSilvermobiHandler(cfg *viper.Viper, services *services.ServicesHandler, storage storage.Storage) (*SilvermobiHandler, error) {
return &SilvermobiHandler{
Config: cfg,
Services: services,
KVHandler: KVHandler,
Config: cfg,
Services: services,
Storage: storage,
}, nil
}

64
handler/notifications.go Normal file
View File

@@ -0,0 +1,64 @@
package handler
import (
"context"
"crypto/tls"
"git.coopgo.io/coopgo-apps/silvermobi/services"
"git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
gomail "gopkg.in/mail.v2"
)
func (h *SilvermobiHandler) PutFirebase(ctx context.Context, id string, token string, device_platform string) (err error) {
err = h.Storage.CreateFirebaseToken(id, token, device_platform)
return err
}
func (h *SilvermobiHandler) SendNotification(id, title, message string) (err error) {
firebase_token, platfrom, _ := h.Storage.GetFirebaseToken(id)
if err != nil {
return err
}
if platfrom == "android" {
_ = h.Services.Push.Send(
services.Notification{
Platform: services.PushToAndroid,
Recipients: []string{firebase_token},
Title: title,
Message: message,
},
)
} else {
_ = h.Services.Push.Send(
services.Notification{
Platform: services.PushToIos,
Recipients: []string{firebase_token},
Title: title,
Message: message,
},
)
}
return nil
}
func (h *SilvermobiHandler) SendEmail(id, title, message string) (err error) {
resp, err := h.Services.MobilityAccounts.Client.GetAccount(context.Background(), &grpcapi.GetAccountRequest{Id: id})
if err != nil {
return err
}
account := h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
m := gomail.NewMessage()
m.SetHeader("From", h.Config.GetString("emailing.smtp.username"))
m.SetHeader("To", account.Email)
m.SetHeader("Subject", title)
m.SetBody("text/plain", message)
d := gomail.NewDialer(h.Config.GetString("emailing.smtp.host"), h.Config.GetInt("emailing.smtp.port"), h.Config.GetString("emailing.smtp.username"),
h.Config.GetString("emailing.smtp.password"))
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
if err := d.DialAndSend(m); err != nil {
return err
}
return nil
}

View File

@@ -107,6 +107,22 @@ func (h *SilvermobiHandler) GetAccountType(ctx context.Context, id string) (acco
}
return "", errors.New("account type not set")
}
func (h *SilvermobiHandler) GetAccountPhone(ctx context.Context, id string) (phone_number string, err error) {
request := &grpcapi.GetAccountRequest{
Id: id,
}
resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, request)
if err != nil {
return "", err
log.Error().Err(err).Msg("Failed get account type")
}
account := h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
if account.PhoneNumber != "" {
return account.PhoneNumber, nil
}
return "", errors.New("invalid request ")
}
func (h *SilvermobiHandler) CheckValidation(ctx context.Context, id string) (phone_validation, birth_validation, type_validation bool, err error) {
request := &grpcapi.GetAccountRequest{
@@ -127,7 +143,6 @@ func (h *SilvermobiHandler) CheckValidation(ctx context.Context, id string) (pho
if account.BirthDate != "" {
birth_validation = true
}
fmt.Println(account.Type)
if account.Type != "" {
type_validation = true
}