This commit is contained in:
Salim Amine Bou Aram
2024-10-31 20:32:54 +01:00
parent 5628125ad3
commit ef2768c431
26 changed files with 706 additions and 379 deletions

View File

@@ -2,15 +2,19 @@ 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 {
func (h *SilverMobiHandler) GetAccountInfos(ctx context.Context, id string) (account *models.Account, err error) {
var resp *grpcapi.GetAccountResponse
if resp, err = h.Services.MobilityAccounts.Client.GetAccount(ctx, &grpcapi.GetAccountRequest{Id: id}); err != nil {
return nil, err
}
account = h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
return account, nil
}

View File

@@ -3,24 +3,39 @@ package handler
import (
"context"
"time"
"git.coopgo.io/coopgo-apps/silvermobi/models"
)
func (h *SilvermobiHandler) 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 {
func (h *SilverMobiHandler) Login(ctx context.Context, username string, password string) (jwt string, err error) {
var (
account *models.Account
pTTL time.Duration
)
if account, err = h.Services.MobilityAccounts.Login(ctx, username, password, "silvermobi"); 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 {
if pTTL, err = time.ParseDuration(ttl); err != nil {
return "", err
}
return account.CreateToken(parsedttl, key)
return account.CreateToken(pTTL, key)
}
func (h *SilvermobiHandler) 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(
func (h *SilverMobiHandler) Register(ctx context.Context, username string, password string, email string,
phone_number string, first_name string, last_name string) (jwt string, err error) {
var (
account *models.Account
pTTL time.Duration
)
if account, err = h.Services.MobilityAccounts.Register(
ctx,
username,
password,
@@ -33,19 +48,16 @@ func (h *SilvermobiHandler) Register(ctx context.Context, username string, passw
"phone_number": phone_number,
},
"silvermobi",
)
if err != nil {
); 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 {
if pTTL, err = time.ParseDuration(ttl); err != nil {
return "", err
}
return account.CreateToken(parsedttl, key)
return account.CreateToken(pTTL, key)
}

View File

@@ -3,8 +3,9 @@ package handler
import (
"context"
"crypto/rand"
"crypto/tls"
"encoding/base64"
"git.coopgo.io/coopgo-apps/silvermobi/models"
gomail "gopkg.in/mail.v2"
)
@@ -18,29 +19,50 @@ func generateRandomPassword(length int) (string, error) {
return password, nil
}
func (h *SilvermobiHandler) 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 {
func (h *SilverMobiHandler) ForgetAccount(ctx context.Context, username string, namespace string) (response bool,
access_Code string) {
var (
err error
account *models.Account
)
account, err = h.Services.MobilityAccounts.GetAccountUsername(ctx, username, namespace)
if err != nil {
return false, ""
}
message := []byte("Hello dear,\nYour new Silvermobi password is: ")
password, _ := generateRandomPassword(10)
if update := h.Services.MobilityAccounts.UpdatePassword(ctx, account.ID, password); !update {
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.StartTLSPolicy = gomail.MandatoryStartTLS
if err = d.DialAndSend(m); err != nil {
return false, ""
}
return true, password
}

View File

@@ -2,12 +2,13 @@ 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) {
func (h *SilverMobiHandler) GeoAutocomplete(text string) (*geojson.FeatureCollection, error) {
result, err := h.Services.Geocoder.Autocomplete(text)
if err != nil {
return nil, err
@@ -15,25 +16,27 @@ func (h *SilvermobiHandler) GeoAutocomplete(text string, lat, lon float64) (*geo
return result, nil
}
func (h *SilvermobiHandler) GeoRoute(locations geojson.FeatureCollection) (route *routing.Route, err error) {
route_locations := []orb.Point{}
features_type := ""
func (h *SilverMobiHandler) GeoRoute(locations geojson.FeatureCollection) (route *routing.Route, err error) {
var (
routeLocations []orb.Point
featuresType string
)
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)
if ft != featuresType {
return nil, fmt.Errorf("mixing different types of geometries in"+
" the feature collection is not allowed : %s and %s found", featuresType, ft)
}
features_type = ft
featuresType = ft
if features_type == "Point" {
if featuresType == "Point" {
if point, ok := f.Geometry.(orb.Point); ok {
route_locations = append(route_locations, point)
routeLocations = append(routeLocations, point)
}
} else {
return nil, fmt.Errorf("feature type %s not supported", features_type)
return nil, fmt.Errorf("feature type %s not supported", featuresType)
}
return nil, err
@@ -42,9 +45,10 @@ func (h *SilvermobiHandler) GeoRoute(locations geojson.FeatureCollection) (route
return route, nil
}
func (h *SilvermobiHandler) GeoReturnRoute(locations geojson.FeatureCollection) (route *routing.Route, err error) {
func (h *SilverMobiHandler) GeoReturnRoute(locations geojson.FeatureCollection) (route *routing.Route, err error) {
loc := locations
route.Polyline.String()
reverse(loc.Features)
return h.GeoRoute(loc)

View File

@@ -6,14 +6,15 @@ import (
"github.com/spf13/viper"
)
type SilvermobiHandler struct {
type SilverMobiHandler struct {
Config *viper.Viper
Services *services.ServicesHandler
Services *services.ServiceHandler
Storage storage.Storage
}
func NewSilvermobiHandler(cfg *viper.Viper, services *services.ServicesHandler, storage storage.Storage) (*SilvermobiHandler, error) {
return &SilvermobiHandler{
func NewSilverMobiHandler(cfg *viper.Viper, services *services.ServiceHandler,
storage storage.Storage) (*SilverMobiHandler, error) {
return &SilverMobiHandler{
Config: cfg,
Services: services,
Storage: storage,

View File

@@ -2,63 +2,69 @@ 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) {
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)
func (h *SilverMobiHandler) SendNotification(id, title, message string) error {
firebaseToken, platform, err := 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,
},
)
notify := services.Notification{
Recipients: []string{firebaseToken},
Title: title,
Message: message,
}
if platform == "android" {
notify.Platform = services.PushToAndroid
err = h.Services.Push.Send(notify)
} else {
_ = h.Services.Push.Send(
services.Notification{
Platform: services.PushToIos,
Recipients: []string{firebase_token},
Title: title,
Message: message,
},
)
notify.Platform = services.PushToIos
err = h.Services.Push.Send(notify)
}
return nil
return err
}
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 {
func (h *SilverMobiHandler) SendEmail(id, title, message string) (err error) {
var resp *grpcapi.GetAccountResponse
if resp, err = h.Services.MobilityAccounts.Client.GetAccount(context.Background(),
&grpcapi.GetAccountRequest{Id: id}); 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"),
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 {
d.StartTLSPolicy = gomail.MandatoryStartTLS
if err = d.DialAndSend(m); err != nil {
return err
}
return nil
}

View File

@@ -3,48 +3,53 @@ package handler
import (
"context"
"fmt"
"math/rand"
"git.coopgo.io/coopgo-apps/silvermobi/services"
"git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"math/rand"
)
func (h *SilvermobiHandler) UpdatePhoneNumber(ctx context.Context, id string, phone_number string) error {
func (h *SilverMobiHandler) UpdatePhoneNumber(ctx context.Context, id string, phoneNumber string) (err error) {
code := rand.Intn(9999)
err := h.Services.MobilityAccounts.UpdatePhoneNumber(
if err = h.Services.MobilityAccounts.UpdatePhoneNumber(
ctx,
id,
phone_number,
phoneNumber,
false,
fmt.Sprintf("%04d", code),
)
if err != nil {
); err != nil {
log.Error().Err(err).Msg("updating phone number failed")
return err
}
err = h.Services.Push.Send(
if err = h.Services.Push.Send(
services.Notification{
Platform: services.PushToSMSFactor,
Recipients: []string{phone_number[1:]},
Recipients: []string{phoneNumber[1:]},
Title: "SILVERMOBI",
Message: fmt.Sprintf("SILVERMOBI - Votre code de validation : %04d", code),
},
)
if err != nil {
); err != nil {
log.Error().Err(err).Msg("issue sending verification code by sms")
return err
}
return nil
}
func (h *SilvermobiHandler) VerifyPhoneNumber(ctx context.Context, id string, phone_number string, verification_code string) error {
func (h *SilverMobiHandler) VerifyPhoneNumber(ctx context.Context, id string, phoneNumber string,
verificationCode string) error {
request := &grpcapi.GetAccountRequest{
Id: id,
}
resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, request)
if err != nil {
return err
}
@@ -52,20 +57,21 @@ func (h *SilvermobiHandler) VerifyPhoneNumber(ctx context.Context, id string, ph
account_ := resp.Account
log.Debug().
Str("phone_number", phone_number).
Str("phone_number", phoneNumber).
Str("account.phone_number", account_.Authentication.Local.PhoneNumber).
Str("verification_code", verification_code).
Str("verification_code", verificationCode).
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")
if account_.Authentication.Local.PhoneNumber != phoneNumber ||
account_.Authentication.Local.PhoneNumberValidation.ValidationCode != verificationCode {
return errors.New("could not validate phone number : verification code mismatch")
}
err = h.Services.MobilityAccounts.UpdatePhoneNumber(
ctx,
id,
phone_number,
phoneNumber,
true,
"",
)
@@ -76,7 +82,7 @@ func (h *SilvermobiHandler) VerifyPhoneNumber(ctx context.Context, id string, ph
return nil
}
func (h *SilvermobiHandler) UpdateBirthDate(ctx context.Context, id string, birthdate string) error {
func (h *SilverMobiHandler) UpdateBirthDate(ctx context.Context, id string, birthdate string) error {
err := h.Services.MobilityAccounts.UpdateAccountBirthDate(ctx, id, "silvermobi", birthdate)
if err != nil {
return err
@@ -84,65 +90,81 @@ func (h *SilvermobiHandler) UpdateBirthDate(ctx context.Context, id string, birt
return nil
}
func (h *SilvermobiHandler) SetAccountType(ctx context.Context, id string, accountType string) error {
func (h *SilverMobiHandler) 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 *SilvermobiHandler) GetAccountType(ctx context.Context, id string) (account_type string, err error) {
func (h *SilverMobiHandler) GetAccountType(ctx context.Context, id string) (account_type string, err error) {
var resp *grpcapi.GetAccountResponse
request := &grpcapi.GetAccountRequest{
Id: id,
}
resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, request)
if err != nil {
if resp, err = h.Services.MobilityAccounts.Client.GetAccount(ctx, request); 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 *SilvermobiHandler) GetAccountPhone(ctx context.Context, id string) (phone_number string, err error) {
func (h *SilverMobiHandler) GetAccountPhone(ctx context.Context, id string) (phoneNumber string, err error) {
var resp *grpcapi.GetAccountResponse
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 != "" {
if resp, err = h.Services.MobilityAccounts.Client.GetAccount(ctx, request); err != nil {
return "", err
}
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) {
func (h *SilverMobiHandler) CheckValidation(ctx context.Context, id string) (phone_validation, birth_validation, type_validation bool, err error) {
var resp *grpcapi.GetAccountResponse
request := &grpcapi.GetAccountRequest{
Id: id,
}
resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, request)
if err != nil {
if resp, err = h.Services.MobilityAccounts.Client.GetAccount(ctx, request); 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
}

View File

@@ -2,15 +2,17 @@ package handler
import "context"
func (h *SilvermobiHandler) UpdatePassword(ctx context.Context, username string, password string) (response bool) {
func (h *SilverMobiHandler) 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
}
return false
}