fix commit

This commit is contained in:
2024-10-31 16:01:16 +01:00
parent f82f771bfa
commit 0f5da4b5d9
16 changed files with 981 additions and 28 deletions

16
handler/account.go Normal file
View File

@@ -0,0 +1,16 @@
package handler
import (
"context"
"solidarity-service/models"
"git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
)
func (h *SolidarityServiceHandler) 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
}

51
handler/authentication.go Normal file
View File

@@ -0,0 +1,51 @@
package handler
import (
"context"
"time"
)
func (h *SolidarityServiceHandler) Login(ctx context.Context, username string, password string) (jwt string, err error) {
account, err := h.Services.MobilityAccounts.Login(ctx, username, password, "silvermobi")
if err != nil {
return
}
key := h.Config.GetString("identification.local.jwt_secret")
ttl := h.Config.GetString("identification.local.ttl")
parsedttl, err := time.ParseDuration(ttl)
if err != nil {
return "", err
}
return account.CreateToken(parsedttl, key)
}
func (h *SolidarityServiceHandler) Register(ctx context.Context, username string, password string, email string, phone_number string, first_name string, last_name string) (jwt string, err error) {
account, err := h.Services.MobilityAccounts.Register(
ctx,
username,
password,
email,
phone_number,
map[string]any{
"first_name": first_name,
"last_name": last_name,
"email": email,
"phone_number": phone_number,
},
"silvermobi",
)
if err != nil {
return "", err
}
key := h.Config.GetString("identification.local.jwt_secret")
ttl := h.Config.GetString("identification.local.ttl")
parsedttl, err := time.ParseDuration(ttl)
if err != nil {
return "", err
}
return account.CreateToken(parsedttl, key)
}

View File

@@ -0,0 +1,46 @@
package handler
import (
"context"
"crypto/rand"
"crypto/tls"
"encoding/base64"
gomail "gopkg.in/mail.v2"
)
func generateRandomPassword(length int) (string, error) {
buffer := make([]byte, length)
_, err := rand.Read(buffer)
if err != nil {
return "", err
}
password := base64.StdEncoding.EncodeToString(buffer)[:length]
return password, nil
}
func (h *SolidarityServiceHandler) ForgetAccount(ctx context.Context, username string, namespace string) (response bool, access_Code string) {
account, err := h.Services.MobilityAccounts.GetAccountUsername(ctx, username, namespace)
if err == nil {
message := []byte("Hello dear,\nYour new Silvermobi password is: ")
password, _ := generateRandomPassword(10)
update := h.Services.MobilityAccounts.UpdatePassword(ctx, account.ID, password)
if update == false {
return false, ""
}
m := gomail.NewMessage()
m.SetHeader("From", h.Config.GetString("emailing.smtp.username"))
m.SetHeader("To", username)
m.SetHeader("Subject", "Silvermobi Password Recovery")
message = append(message, []byte(password)...)
m.SetBody("text/plain", string(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 false, ""
}
return true, password
} else {
return false, ""
}
}

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 *SolidarityServiceHandler) 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 *SolidarityServiceHandler) 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 *SolidarityServiceHandler) 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

@@ -4,12 +4,16 @@ import (
"git.coopgo.io/coopgo-platform/routing-service"
"github.com/spf13/viper"
"solidarity-service/storage"
"solidarity-service/services"
)
type SolidarityServiceHandler struct {
Config *viper.Viper
Routing routing.RoutingService
Storage storage.Storage
Services *services.ServicesHandler
}
func NewSolidarityServiceHandler(cfg *viper.Viper, routing routing.RoutingService, storage storage.Storage) (*SolidarityServiceHandler, error) {

64
handler/notifications.go Normal file
View File

@@ -0,0 +1,64 @@
package handler
import (
"context"
"crypto/tls"
"solidarity-service/services"
"git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
gomail "gopkg.in/mail.v2"
)
func (h *SolidarityServiceHandler) 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 *SolidarityServiceHandler) 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 *SolidarityServiceHandler) 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

@@ -0,0 +1,152 @@
package handler
import (
"context"
"fmt"
"solidarity-service/services"
"git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"math/rand"
)
func (h *SolidarityServiceHandler) UpdatePhoneNumber(ctx context.Context, id string, phone_number string) error {
code := rand.Intn(9999)
err := h.Services.MobilityAccounts.UpdatePhoneNumber(
ctx,
id,
phone_number,
false,
fmt.Sprintf("%04d", code),
)
if err != nil {
log.Error().Err(err).Msg("updating phone number failed")
return err
}
err = h.Services.Push.Send(
services.Notification{
Platform: services.PushToSMSFactor,
Recipients: []string{phone_number[1:]},
Title: "SILVERMOBI",
Message: fmt.Sprintf("SILVERMOBI - Votre code de validation : %04d", code),
},
)
if err != nil {
log.Error().Err(err).Msg("issue sending verification code by sms")
}
return nil
}
func (h *SolidarityServiceHandler) VerifyPhoneNumber(ctx context.Context, id string, phone_number string, verification_code string) error {
request := &grpcapi.GetAccountRequest{
Id: id,
}
resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, request)
if err != nil {
return err
}
account_ := resp.Account
log.Debug().
Str("phone_number", phone_number).
Str("account.phone_number", account_.Authentication.Local.PhoneNumber).
Str("verification_code", verification_code).
Str("account.verification_code", account_.Authentication.Local.PhoneNumberValidation.ValidationCode).
Msg("Verify phone number")
if account_.Authentication.Local.PhoneNumber != phone_number || account_.Authentication.Local.PhoneNumberValidation.ValidationCode != verification_code {
return errors.New("cound not validate phone number : verification code mismatch")
}
err = h.Services.MobilityAccounts.UpdatePhoneNumber(
ctx,
id,
phone_number,
true,
"",
)
if err != nil {
return err
}
return nil
}
func (h *SolidarityServiceHandler) UpdateBirthDate(ctx context.Context, id string, birthdate string) error {
err := h.Services.MobilityAccounts.UpdateAccountBirthDate(ctx, id, "silvermobi", birthdate)
if err != nil {
return err
}
return nil
}
func (h *SolidarityServiceHandler) SetAccountType(ctx context.Context, id string, accountType string) error {
err := h.Services.MobilityAccounts.SetAccountType(ctx, id, accountType)
if err != nil {
return err
}
return nil
}
func (h *SolidarityServiceHandler) GetAccountType(ctx context.Context, id string) (account_type 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.Type != "" {
return account.Type, nil
}
return "", errors.New("account type not set")
}
func (h *SolidarityServiceHandler) 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 *SolidarityServiceHandler) CheckValidation(ctx context.Context, id string) (phone_validation, birth_validation, type_validation bool, err error) {
request := &grpcapi.GetAccountRequest{
Id: id,
}
resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, request)
if err != nil {
return false, false, false, err
log.Error().Err(err).Msg("Failed get validation response")
}
account := h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
phone_validation = false
birth_validation = false
type_validation = false
if account.LocalCredentials.PhoneNumberVerified {
phone_validation = true
}
if account.BirthDate != "" {
birth_validation = true
}
if account.Type != "" {
type_validation = true
}
log.Info().Msg("Getting phone and birth validation for " + account.Email)
return phone_validation, birth_validation, type_validation, nil
}

View File

@@ -0,0 +1,16 @@
package handler
import "context"
func (h *SolidarityServiceHandler) UpdatePassword(ctx context.Context, username string, password string) (response bool) {
account, err := h.Services.MobilityAccounts.GetAccountUsername(ctx, username, "silvermobi")
if err != nil {
return false
}
result := h.Services.MobilityAccounts.UpdatePassword(ctx, account.ID, password)
if result == true {
return true
} else {
return false
}
}