silvermobi/handler/onboarding_validation.go

175 lines
4.2 KiB
Go

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"
)
func (h *SilverMobiHandler) UpdatePhoneNumber(ctx context.Context, id string, phoneNumber string) (err error) {
code := rand.Intn(9999)
if err = h.Services.MobilityAccounts.UpdatePhoneNumber(
ctx,
id,
phoneNumber,
false,
fmt.Sprintf("%04d", code),
); err != nil {
log.Error().Err(err).Msg("updating phone number failed")
return err
}
if err = h.Services.Push.Send(
services.Notification{
Platform: services.PushToSMSFactor,
Recipients: []string{phoneNumber[1:]},
Title: "SILVERMOBI",
Message: fmt.Sprintf("SILVERMOBI - Votre code de validation : %04d", code),
},
); 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, phoneNumber string,
verificationCode 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", phoneNumber).
Str("account.phone_number", account_.Authentication.Local.PhoneNumber).
Str("verification_code", verificationCode).
Str("account.verification_code", account_.Authentication.Local.PhoneNumberValidation.ValidationCode).
Msg("Verify phone number")
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,
phoneNumber,
true,
"",
)
if err != nil {
return err
}
return nil
}
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
}
return nil
}
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) {
var resp *grpcapi.GetAccountResponse
request := &grpcapi.GetAccountRequest{
Id: id,
}
if resp, err = h.Services.MobilityAccounts.Client.GetAccount(ctx, request); err != nil {
return "", err
}
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) (phoneNumber string, err error) {
var resp *grpcapi.GetAccountResponse
request := &grpcapi.GetAccountRequest{
Id: id,
}
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) {
var resp *grpcapi.GetAccountResponse
request := &grpcapi.GetAccountRequest{
Id: id,
}
if resp, err = h.Services.MobilityAccounts.Client.GetAccount(ctx, request); err != nil {
return false, false, false, err
}
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
}