2023-08-08 10:28:43 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2023-09-19 09:06:20 +00:00
|
|
|
func (h *SilvermobiHandler) UpdatePhoneNumber(ctx context.Context, id string, phone_number string) error {
|
2023-08-08 10:28:43 +00:00
|
|
|
code := rand.Intn(9999)
|
2023-09-19 09:06:20 +00:00
|
|
|
err := h.Services.MobilityAccounts.UpdatePhoneNumber(
|
2023-08-08 10:28:43 +00:00
|
|
|
ctx,
|
2023-09-19 09:06:20 +00:00
|
|
|
id,
|
2023-08-08 10:28:43 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-19 09:06:20 +00:00
|
|
|
func (h *SilvermobiHandler) VerifyPhoneNumber(ctx context.Context, id string, phone_number string, verification_code 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
|
|
|
}
|
|
|
|
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,
|
2023-09-19 09:06:20 +00:00
|
|
|
id,
|
2023-08-08 10:28:43 +00:00
|
|
|
phone_number,
|
|
|
|
true,
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-19 09:06:20 +00:00
|
|
|
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)
|
2023-08-08 10:28:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-19 09:06:20 +00:00
|
|
|
func (h *SilvermobiHandler) 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")
|
|
|
|
}
|
2023-12-08 06:35:04 +00:00
|
|
|
func (h *SilvermobiHandler) 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 ")
|
|
|
|
}
|
2023-08-08 10:28:43 +00:00
|
|
|
|
2023-09-19 09:06:20 +00:00
|
|
|
func (h *SilvermobiHandler) 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)
|
2023-08-08 10:28:43 +00:00
|
|
|
if err != nil {
|
2023-09-19 09:06:20 +00:00
|
|
|
return false, false, false, err
|
2023-08-08 10:28:43 +00:00
|
|
|
log.Error().Err(err).Msg("Failed get validation response")
|
|
|
|
}
|
2023-09-19 09:06:20 +00:00
|
|
|
account := h.Services.MobilityAccounts.ToAccountModel(resp.Account.ToStorageType())
|
2023-08-08 10:28:43 +00:00
|
|
|
phone_validation = false
|
|
|
|
birth_validation = false
|
2023-09-19 09:06:20 +00:00
|
|
|
type_validation = false
|
2023-08-08 10:28:43 +00:00
|
|
|
if account.LocalCredentials.PhoneNumberVerified {
|
|
|
|
phone_validation = true
|
|
|
|
}
|
|
|
|
if account.BirthDate != "" {
|
|
|
|
birth_validation = true
|
|
|
|
}
|
2023-09-19 09:06:20 +00:00
|
|
|
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
|
2023-08-08 10:28:43 +00:00
|
|
|
}
|