110 lines
3.0 KiB
Go
110 lines
3.0 KiB
Go
|
package handler
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"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, email string, phone_number string) error {
|
||
|
code := rand.Intn(9999)
|
||
|
account, err := h.Services.MobilityAccounts.GetAccountUsername(ctx, email, "silvermobi")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
err = h.Services.MobilityAccounts.UpdatePhoneNumber(
|
||
|
ctx,
|
||
|
account.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 *SilvermobiHandler) VerifyPhoneNumber(ctx context.Context, email string, phone_number string, verification_code string) error {
|
||
|
account, err := h.Services.MobilityAccounts.GetAccountUsername(ctx, email, "silvermobi")
|
||
|
request := &grpcapi.GetAccountRequest{
|
||
|
Id: account.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,
|
||
|
account.ID,
|
||
|
phone_number,
|
||
|
true,
|
||
|
"",
|
||
|
)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (h *SilvermobiHandler) UpdateBirthDate(ctx context.Context, username string, birthdate string) error {
|
||
|
err := h.Services.MobilityAccounts.UpdateAccountBirthDate(ctx, username, "silvermobi", birthdate)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (h *SilvermobiHandler) CheckValidation(ctx context.Context, email string) (phone_validation, birth_validation bool, err error) {
|
||
|
account, err := h.Services.MobilityAccounts.GetAccountUsername(ctx, email, "silvermobi")
|
||
|
if err != nil {
|
||
|
return false, false, err
|
||
|
log.Error().Err(err).Msg("Failed get validation response")
|
||
|
}
|
||
|
phone_validation = false
|
||
|
birth_validation = false
|
||
|
if account.LocalCredentials.PhoneNumberVerified {
|
||
|
phone_validation = true
|
||
|
|
||
|
}
|
||
|
if account.BirthDate != "" {
|
||
|
birth_validation = true
|
||
|
}
|
||
|
log.Info().Msg("Getting phone and birth validation for " + email)
|
||
|
return phone_validation, birth_validation, nil
|
||
|
}
|