app-cloudep-member-server/pkg/usecase/binding.go

146 lines
3.7 KiB
Go
Raw Normal View History

2024-12-30 03:58:14 +00:00
package usecase
import (
"app-cloudep-member-server/pkg/domain"
"app-cloudep-member-server/pkg/domain/entity"
"app-cloudep-member-server/pkg/domain/usecase"
"context"
"errors"
"fmt"
"code.30cm.net/digimon/library-go/errs"
"code.30cm.net/digimon/library-go/errs/code"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/mon"
)
func (use *MemberUseCase) BindUserInfo(ctx context.Context, req usecase.CreateUserInfoRequest) error {
// prepare 準備資料
// 準備資料
insert := &entity.User{
UID: req.UID,
AvatarURL: req.AvatarURL,
FullName: req.FullName,
Nickname: req.Nickname,
GenderCode: req.GenderCode,
Birthdate: req.Birthdate,
Address: req.Address,
AlarmCategory: req.AlarmCategory,
UserStatus: req.UserStatus,
PreferredLanguage: req.PreferredLanguage,
Currency: req.Currency,
}
// Insert 新增
if err := use.User.Insert(ctx, insert); err != nil {
e := errs.DatabaseErrorWithScopeL(
code.CloudEPMember,
domain.BindingUserTabletErrorCode,
logx.WithContext(ctx),
[]logx.LogField{
{Key: "req", Value: req},
{Key: "func", Value: "User.Insert"},
{Key: "err", Value: err.Error()},
},
"failed to binding user info").Wrap(err)
return e
}
return nil
}
func (use *MemberUseCase) BindAccount(ctx context.Context, req usecase.BindingUser) (usecase.BindingUser, error) {
// 先確定有這個Account
_, err := use.Account.FindOneByAccount(ctx, req.LoginID)
if err != nil {
var e *errs.LibError
switch {
case errors.Is(err, mon.ErrNotFound):
e = errs.ResourceNotFoundWithScope(
code.CloudEPMember,
domain.FailedToFindAccountErrorCode,
fmt.Sprintf("failed to insert account: %s", req.UID),
)
default:
e = errs.DatabaseErrorWithScopeL(
code.CloudEPMember,
domain.FailedToFindAccountErrorCode,
logx.WithContext(ctx),
[]logx.LogField{
{Key: "req", Value: req},
{Key: "func", Value: "User.FindOneByAccount"},
{Key: "err", Value: err.Error()},
},
"failed to find account").Wrap(err)
}
return usecase.BindingUser{}, e
}
uid := req.UID
// 有帳號沒UID 表示他是要產生一個新的UID產一個給他
if req.UID == "" {
uid, err = use.Generate(ctx)
if err != nil {
// generate 裡面會產生 error
// usecase 印出錯誤其中一個準則,在最基底的 uc 裡面印錯誤
return usecase.BindingUser{}, err
}
}
if err := use.AccountUID.Insert(ctx, &entity.AccountUID{
LoginID: req.LoginID,
UID: uid,
Type: req.Type,
}); err != nil {
e := errs.DatabaseErrorWithScopeL(
code.CloudEPMember,
domain.FailedToBindAccountErrorCode,
logx.WithContext(ctx),
[]logx.LogField{
{Key: "req", Value: req},
{Key: "func", Value: "User.Insert"},
{Key: "err", Value: err.Error()},
},
"failed to bind account").Wrap(err)
return usecase.BindingUser{}, e
}
return usecase.BindingUser{
LoginID: req.LoginID,
UID: uid,
Type: req.Type,
}, nil
}
func (use *MemberUseCase) BindVerifyEmail(ctx context.Context, uid, email string) error {
err := use.User.UpdateEmailVerifyStatus(ctx, uid, email)
if err != nil {
e := errs.DatabaseErrorWithScope(
code.CloudEPMember,
domain.FailedToFindAccountErrorCode,
fmt.Sprintf("failed to Binding uid: %s, email: %s", uid, email),
)
return e
}
return nil
}
func (use *MemberUseCase) BindVerifyPhone(ctx context.Context, uid, phone string) error {
err := use.User.UpdatePhoneVerifyStatus(ctx, uid, phone)
if err != nil {
e := errs.DatabaseErrorWithScope(
code.CloudEPMember,
domain.FailedToFindAccountErrorCode,
fmt.Sprintf("failed to Binding uid: %s, phone: %s", uid, phone),
)
return e
}
return nil
}