2023-08-08 10:28:43 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-10-31 19:32:54 +00:00
|
|
|
"math/rand"
|
|
|
|
|
2023-08-08 10:28:43 +00:00
|
|
|
"git.coopgo.io/coopgo-apps/silvermobi/services"
|
|
|
|
"git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2024-10-31 19:32:54 +00:00
|
|
|
func (h *SilverMobiHandler) UpdatePhoneNumber(ctx context.Context, id string, phoneNumber string) (err error) {
|
|
|
|
|
2023-08-08 10:28:43 +00:00
|
|
|
code := rand.Intn(9999)
|
2024-10-31 19:32:54 +00:00
|
|
|
|
|
|
|
if err = h.Services.MobilityAccounts.UpdatePhoneNumber(
|
2023-08-08 10:28:43 +00:00
|
|
|
ctx,
|
2023-09-19 09:06:20 +00:00
|
|
|
id,
|
2024-10-31 19:32:54 +00:00
|
|
|
phoneNumber,
|
2023-08-08 10:28:43 +00:00
|
|
|
false,
|
|
|
|
fmt.Sprintf("%04d", code),
|
2024-10-31 19:32:54 +00:00
|
|
|
); err != nil {
|
2023-08-08 10:28:43 +00:00
|
|
|
log.Error().Err(err).Msg("updating phone number failed")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-31 19:32:54 +00:00
|
|
|
if err = h.Services.Push.Send(
|
2023-08-08 10:28:43 +00:00
|
|
|
services.Notification{
|
|
|
|
Platform: services.PushToSMSFactor,
|
2024-10-31 19:32:54 +00:00
|
|
|
Recipients: []string{phoneNumber[1:]},
|
2023-08-08 10:28:43 +00:00
|
|
|
Title: "SILVERMOBI",
|
|
|
|
Message: fmt.Sprintf("SILVERMOBI - Votre code de validation : %04d", code),
|
|
|
|
},
|
2024-10-31 19:32:54 +00:00
|
|
|
); err != nil {
|
2023-08-08 10:28:43 +00:00
|
|
|
log.Error().Err(err).Msg("issue sending verification code by sms")
|
2024-10-31 19:32:54 +00:00
|
|
|
return err
|
2023-08-08 10:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-31 19:32:54 +00:00
|
|
|
func (h *SilverMobiHandler) VerifyPhoneNumber(ctx context.Context, id string, phoneNumber string,
|
|
|
|
verificationCode string) error {
|
|
|
|
|
2023-08-08 10:28:43 +00:00
|
|
|
request := &grpcapi.GetAccountRequest{
|
2023-09-19 09:06:20 +00:00
|
|
|
Id: id,
|
2023-08-08 10:28:43 +00:00
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-08-08 10:28:43 +00:00
|
|
|
resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, request)
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-08-08 10:28:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
account_ := resp.Account
|
|
|
|
|
|
|
|
log.Debug().
|
2024-10-31 19:32:54 +00:00
|
|
|
Str("phone_number", phoneNumber).
|
2023-08-08 10:28:43 +00:00
|
|
|
Str("account.phone_number", account_.Authentication.Local.PhoneNumber).
|
2024-10-31 19:32:54 +00:00
|
|
|
Str("verification_code", verificationCode).
|
2023-08-08 10:28:43 +00:00
|
|
|
Str("account.verification_code", account_.Authentication.Local.PhoneNumberValidation.ValidationCode).
|
|
|
|
Msg("Verify phone number")
|
|
|
|
|
2024-10-31 19:32:54 +00:00
|
|
|
if account_.Authentication.Local.PhoneNumber != phoneNumber ||
|
|
|
|
account_.Authentication.Local.PhoneNumberValidation.ValidationCode != verificationCode {
|
|
|
|
return errors.New("could not validate phone number : verification code mismatch")
|
2023-08-08 10:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = h.Services.MobilityAccounts.UpdatePhoneNumber(
|
|
|
|
ctx,
|
2023-09-19 09:06:20 +00:00
|
|
|
id,
|
2024-10-31 19:32:54 +00:00
|
|
|
phoneNumber,
|
2023-08-08 10:28:43 +00:00
|
|
|
true,
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-31 19:32:54 +00:00
|
|
|
func (h *SilverMobiHandler) UpdateBirthDate(ctx context.Context, id string, birthdate string) error {
|
2023-09-19 09:06:20 +00:00
|
|
|
err := h.Services.MobilityAccounts.UpdateAccountBirthDate(ctx, id, "silvermobi", birthdate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-31 19:32:54 +00:00
|
|
|
func (h *SilverMobiHandler) SetAccountType(ctx context.Context, id string, accountType string) error {
|
2023-09-19 09:06:20 +00:00
|
|
|
err := h.Services.MobilityAccounts.SetAccountType(ctx, id, accountType)
|
2023-08-08 10:28:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-01 00:23:34 +00:00
|
|
|
func (h *SilverMobiHandler) GetAccountType(ctx context.Context, id string) (accountType string, err error) {
|
2024-10-31 19:32:54 +00:00
|
|
|
var resp *grpcapi.GetAccountResponse
|
|
|
|
|
2023-09-19 09:06:20 +00:00
|
|
|
request := &grpcapi.GetAccountRequest{
|
|
|
|
Id: id,
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
|
|
|
if resp, err = h.Services.MobilityAccounts.Client.GetAccount(ctx, request); err != nil {
|
2023-09-19 09:06:20 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-09-19 09:06:20 +00:00
|
|
|
account := h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-09-19 09:06:20 +00:00
|
|
|
if account.Type != "" {
|
|
|
|
|
|
|
|
return account.Type, nil
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-09-19 09:06:20 +00:00
|
|
|
return "", errors.New("account type not set")
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
func (h *SilverMobiHandler) GetAccountPhone(ctx context.Context, id string) (phoneNumber string, err error) {
|
|
|
|
var resp *grpcapi.GetAccountResponse
|
|
|
|
|
2023-12-08 06:35:04 +00:00
|
|
|
request := &grpcapi.GetAccountRequest{
|
|
|
|
Id: id,
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
|
|
|
if resp, err = h.Services.MobilityAccounts.Client.GetAccount(ctx, request); err != nil {
|
2023-12-08 06:35:04 +00:00
|
|
|
return "", err
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-12-08 06:35:04 +00:00
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-12-08 06:35:04 +00:00
|
|
|
account := h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
|
|
|
|
|
2024-10-31 19:32:54 +00:00
|
|
|
if account.PhoneNumber != "" {
|
2023-12-08 06:35:04 +00:00
|
|
|
return account.PhoneNumber, nil
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-12-08 06:35:04 +00:00
|
|
|
return "", errors.New("invalid request ")
|
|
|
|
}
|
2023-08-08 10:28:43 +00:00
|
|
|
|
2024-11-01 00:23:34 +00:00
|
|
|
func (h *SilverMobiHandler) CheckValidation(ctx context.Context, id string) (phoneValidation, birthValidation,
|
|
|
|
typeValidation bool, err error) {
|
2024-10-31 19:32:54 +00:00
|
|
|
|
|
|
|
var resp *grpcapi.GetAccountResponse
|
|
|
|
|
2023-09-19 09:06:20 +00:00
|
|
|
request := &grpcapi.GetAccountRequest{
|
|
|
|
Id: id,
|
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
|
|
|
if resp, err = h.Services.MobilityAccounts.Client.GetAccount(ctx, request); err != nil {
|
2023-09-19 09:06:20 +00:00
|
|
|
return false, false, false, err
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-08-08 10:28:43 +00:00
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-09-19 09:06:20 +00:00
|
|
|
account := h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2024-11-01 00:23:34 +00:00
|
|
|
phoneValidation = false
|
|
|
|
birthValidation = false
|
|
|
|
typeValidation = false
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-08-08 10:28:43 +00:00
|
|
|
if account.LocalCredentials.PhoneNumberVerified {
|
2024-11-01 00:23:34 +00:00
|
|
|
phoneValidation = true
|
2023-08-08 10:28:43 +00:00
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-08-08 10:28:43 +00:00
|
|
|
if account.BirthDate != "" {
|
2024-11-01 00:23:34 +00:00
|
|
|
birthValidation = true
|
2023-08-08 10:28:43 +00:00
|
|
|
}
|
2024-10-31 19:32:54 +00:00
|
|
|
|
2023-09-19 09:06:20 +00:00
|
|
|
if account.Type != "" {
|
2024-11-01 00:23:34 +00:00
|
|
|
typeValidation = true
|
2023-09-19 09:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().Msg("Getting phone and birth validation for " + account.Email)
|
2024-11-01 00:23:34 +00:00
|
|
|
return phoneValidation, birthValidation, typeValidation, nil
|
2023-08-08 10:28:43 +00:00
|
|
|
}
|