update customer controller

This commit is contained in:
Pavel 2024-05-26 16:05:49 +03:00
parent 4d6e4e9efe
commit 318a1697a2
4 changed files with 67 additions and 13 deletions

@ -56,7 +56,7 @@ message Name {
}
enum AccountStatus {
ACCOUNT_STATUS_NKO = 0;
ACCOUNT_STATUS_NO = 1;
ACCOUNT_STATUS_ORG = 2;
nko = 0;
no = 1;
org = 2;
}

@ -98,8 +98,9 @@ func Run(config *models.Config, logger *zap.Logger) (appErr error) {
})
rpcControllers := initialize.NewRpcControllers(initialize.RpcControllersDeps{
Logger: logger,
Services: services,
Logger: logger,
Services: services,
Repositories: repositories,
})
encrypt := qutils.NewEncrypt(config.Service.PubKey, config.Service.PrivKey)

@ -16,8 +16,9 @@ import (
)
type RpcControllersDeps struct {
Logger *zap.Logger
Services *Services
Logger *zap.Logger
Services *Services
Repositories *Repositories
}
type RpcControllers struct {
@ -34,6 +35,7 @@ func NewRpcControllers(deps RpcControllersDeps) *RpcControllers {
CustomerController: customer.New(customer.Deps{
Logger: deps.Logger,
HistoryService: deps.Services.HistoryService,
AccountRepo: deps.Repositories.AccountRepository,
}),
}
}

@ -2,7 +2,9 @@ package customer
import (
"context"
"google.golang.org/protobuf/types/known/timestamppb"
"log"
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/interface/repository"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/emptypb"
@ -15,34 +17,42 @@ import (
type Deps struct {
Logger *zap.Logger
HistoryService *history.Service
AccountRepo *repository.AccountRepository
}
type Controller struct {
logger *zap.Logger
historyService *history.Service
accountRepo *repository.AccountRepository
customer.UnimplementedCustomerServiceServer
}
func New(deps Deps) *Controller {
if deps.Logger == nil {
log.Panicln("logger is nil on <NewPaymentController>")
log.Panicln("logger is nil on <NewCustomerController>")
}
if deps.HistoryService == nil {
log.Panicln("HistoryService is nil on <NewPaymentController>")
log.Panicln("HistoryService is nil on <NewCustomerController>")
}
if deps.AccountRepo == nil {
log.Panicln("AccountRepo is nil on <NewCustomerController>")
}
return &Controller{
logger: deps.Logger,
historyService: deps.HistoryService,
accountRepo: deps.AccountRepo,
}
}
func (receiver *Controller) InsertHistory(ctx context.Context, in *customer.History) (*emptypb.Empty, error) {
if _, err := receiver.historyService.CreateHistory(ctx, &models.History{
UserID: in.UserID,
Comment: in.Comment,
Key: in.Key,
//RawDetails: in.RawDetails,
UserID: in.UserID,
Comment: in.Comment,
Key: in.Key,
//RawDetails: in.RawDetails,
}); err != nil {
receiver.logger.Error("failed to insert history on <InsertHistory> of <HistoryController>", zap.Error(err))
return nil, errors.GRPC("failed to insert history", err)
@ -50,3 +60,44 @@ func (receiver *Controller) InsertHistory(ctx context.Context, in *customer.Hist
return &emptypb.Empty{}, nil
}
func (receiver *Controller) SetAccountVerificationStatus(ctx context.Context, req *customer.SetVerificationReq) (*customer.Account, error) {
status := models.AccountStatus(req.Status.String())
account, err := receiver.accountRepo.SetStatus(ctx, req.UserID, status)
if err != nil {
receiver.logger.Error("failed to set status on <SetAccountVerificationStatus> of <RPC_CustomerController>", zap.Error(err))
return nil, err
}
var deletedAt *timestamppb.Timestamp
if account.DeletedAt != nil {
deletedAt = timestamppb.New(*account.DeletedAt)
}
return &customer.Account{
Id: account.ID,
UserID: account.UserID,
Cart: account.Cart,
Wallet: &customer.Wallet{
Cash: account.Wallet.Cash,
Currency: account.Wallet.Currency,
Spent: account.Wallet.Spent,
PurchasesAmount: account.Wallet.PurchasesAmount,
Money: account.Wallet.Money,
LastPaymentId: account.Wallet.LastPaymentID,
},
Name: &customer.Name{
Middlename: account.Name.Middlename,
Firstname: account.Name.FirstName,
Orgname: account.Name.Orgname,
Secondname: account.Name.Secondname,
},
Status: customer.AccountStatus(customer.AccountStatus_value[string(account.Status)]),
IsDeleted: account.Deleted,
CreatedAt: timestamppb.New(account.CreatedAt),
UpdatedAt: timestamppb.New(account.UpdatedAt),
DeletedAt: deletedAt,
From: account.From,
Partner: account.Partner,
}, nil
}