Skip to content

Commit

Permalink
Refactor order
Browse files Browse the repository at this point in the history
  • Loading branch information
jakys committed Aug 29, 2023
1 parent 6a93115 commit 50d0d1b
Show file tree
Hide file tree
Showing 11 changed files with 852 additions and 923 deletions.
97 changes: 10 additions & 87 deletions api/order/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,29 @@ package order
import (
"context"

appgoodmwcli "github.com/NpoolPlatform/good-middleware/pkg/client/app/good"
appgoodpb "github.com/NpoolPlatform/message/npool/good/mw/v1/app/good"

"github.com/NpoolPlatform/libent-cruder/pkg/cruder"

ordertypes "github.com/NpoolPlatform/message/npool/basetypes/order/v1"
basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"

npool "github.com/NpoolPlatform/message/npool/order/gw/v1/order"
ordermwpb "github.com/NpoolPlatform/message/npool/order/mw/v1/order"
order1 "github.com/NpoolPlatform/order-gateway/pkg/order"
ordermwcli "github.com/NpoolPlatform/order-middleware/pkg/client/order"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/NpoolPlatform/go-service-framework/pkg/logger"

"github.com/shopspring/decimal"
)

func createOrder(ctx context.Context, in *npool.CreateOrderRequest) (*npool.Order, error) { //nolint
handler, err := order1.NewHandler(
ctx,
order1.WithAppID(&in.AppID),
order1.WithUserID(&in.AppID, &in.UserID),
order1.WithGoodID(&in.GoodID),
order1.WithUnits(in.Units),
order1.WithPaymentCoinID(&in.PaymentCoinID),
order1.WithParentOrderID(in.ParentOrderID),
order1.WithOrderType(&in.OrderType),
order1.WithPayWithBalanceAmount(in.GetPayWithBalanceAmount()),
order1.WithFixAmountID(in.FixAmountID),
order1.WithDiscountID(in.DiscountID),
order1.WithSpecialOfferID(in.SpecialOfferID),
order1.WithCouponIDs(in.CouponIDs),
order1.WithAppID(&in.AppID, true),
order1.WithUserID(&in.AppID, &in.UserID, true),
order1.WithGoodID(&in.GoodID, true),
order1.WithUnits(in.Units, true),
order1.WithPaymentCoinID(&in.PaymentCoinID, true),
order1.WithParentOrderID(in.ParentOrderID, false),
order1.WithOrderType(&in.OrderType, true),
order1.WithBalanceAmount(in.GetPayWithBalanceAmount(), false),
order1.WithCouponIDs(in.CouponIDs, false),
)
if err != nil {
logger.Sugar().Errorw(
Expand All @@ -64,70 +51,6 @@ func createOrder(ctx context.Context, in *npool.CreateOrderRequest) (*npool.Orde
}

func (s *Server) CreateOrder(ctx context.Context, in *npool.CreateOrderRequest) (*npool.CreateOrderResponse, error) {
ag, err := appgoodmwcli.GetGoodOnly(ctx, &appgoodpb.Conds{
AppID: &basetypes.StringVal{
Op: cruder.EQ,
Value: in.AppID,
},
GoodID: &basetypes.StringVal{
Op: cruder.EQ,
Value: in.GoodID,
},
})
if err != nil {
return &npool.CreateOrderResponse{}, status.Error(codes.Internal, err.Error())
}
if ag == nil {
return &npool.CreateOrderResponse{}, status.Error(codes.Internal, "invalid app good")
}
units, err := decimal.NewFromString(in.Units)
if err != nil {
return &npool.CreateOrderResponse{}, status.Error(codes.Internal, err.Error())
}
if ag.PurchaseLimit > 0 && units.Cmp(decimal.NewFromInt32(ag.PurchaseLimit)) > 0 {
return &npool.CreateOrderResponse{}, status.Error(codes.Internal, "too many units")
}

if !ag.EnablePurchase {
return &npool.CreateOrderResponse{}, status.Error(codes.Internal, "app good is not enabled purchase")
}

purchaseCountStr, err := ordermwcli.SumOrderUnits(
ctx,
&ordermwpb.Conds{
AppID: &basetypes.StringVal{Op: cruder.EQ, Value: in.AppID},
UserID: &basetypes.StringVal{Op: cruder.EQ, Value: in.UserID},
GoodID: &basetypes.StringVal{Op: cruder.EQ, Value: in.GoodID},
States: &basetypes.Uint32SliceVal{
Op: cruder.IN,
Value: []uint32{
uint32(ordertypes.OrderState_OrderStatePaid),
uint32(ordertypes.OrderState_OrderStateInService),
uint32(ordertypes.OrderState_OrderStateExpired),
uint32(ordertypes.OrderState_OrderStateWaitPayment),
},
},
},
)
if err != nil {
return nil, err
}

purchaseCount, err := decimal.NewFromString(purchaseCountStr)
if err != nil {
return nil, err
}

userPurchaseLimit, err := decimal.NewFromString(ag.UserPurchaseLimit)
if err != nil {
logger.Sugar().Errorw("ValidateInit", "error", err)
return &npool.CreateOrderResponse{}, status.Error(codes.Internal, err.Error())
}

if userPurchaseLimit.Cmp(decimal.NewFromInt(0)) > 0 && purchaseCount.Add(units).Cmp(userPurchaseLimit) > 0 {
return &npool.CreateOrderResponse{}, status.Error(codes.Internal, "too many units")
}

in.OrderType = ordertypes.OrderType_Normal
ord, err := createOrder(ctx, in)
if err != nil {
Expand All @@ -150,8 +73,8 @@ func (s *Server) CreateUserOrder(ctx context.Context, in *npool.CreateUserOrderR
AppID: in.AppID,
UserID: in.TargetUserID,
GoodID: in.GoodID,
PaymentCoinID: in.PaymentCoinID,
Units: in.Units,
PaymentCoinID: in.PaymentCoinID,
ParentOrderID: in.ParentOrderID,
OrderType: in.OrderType,
})
Expand Down
6 changes: 3 additions & 3 deletions api/order/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
func (s *Server) GetOrders(ctx context.Context, in *npool.GetOrdersRequest) (*npool.GetOrdersResponse, error) {
handler, err := order1.NewHandler(
ctx,
order1.WithAppID(&in.AppID),
order1.WithUserID(&in.AppID, &in.UserID),
order1.WithAppID(&in.AppID, true),
order1.WithUserID(&in.AppID, &in.UserID, false),
order1.WithOffset(in.GetOffset()),
order1.WithLimit(in.GetLimit()),
)
Expand Down Expand Up @@ -82,7 +82,7 @@ func (s *Server) GetAppUserOrders(ctx context.Context, in *npool.GetAppUserOrder
func (s *Server) GetOrder(ctx context.Context, in *npool.GetOrderRequest) (*npool.GetOrderResponse, error) {
handler, err := order1.NewHandler(
ctx,
order1.WithID(&in.ID),
order1.WithID(&in.ID, true),
)
if err != nil {
logger.Sugar().Errorw(
Expand Down
30 changes: 15 additions & 15 deletions api/order/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
func (s *Server) UpdateOrder(ctx context.Context, in *npool.UpdateOrderRequest) (*npool.UpdateOrderResponse, error) {
handler, err := order1.NewHandler(
ctx,
order1.WithID(&in.ID),
order1.WithAppID(&in.AppID),
order1.WithUserID(&in.AppID, &in.UserID),
order1.WithPaymentID(&in.PaymentID),
order1.WithCanceled(in.Canceled),
order1.WithID(&in.ID, true),
order1.WithAppID(&in.AppID, true),
order1.WithUserID(&in.AppID, &in.UserID, true),
order1.WithPaymentID(&in.PaymentID, true),
order1.WithCanceled(in.Canceled, true),
order1.WithFromAdmin(false),
)
if err != nil {
Expand Down Expand Up @@ -50,11 +50,11 @@ func (s *Server) UpdateOrder(ctx context.Context, in *npool.UpdateOrderRequest)
func (s *Server) UpdateUserOrder(ctx context.Context, in *npool.UpdateUserOrderRequest) (*npool.UpdateUserOrderResponse, error) {
handler, err := order1.NewHandler(
ctx,
order1.WithID(&in.ID),
order1.WithAppID(&in.AppID),
order1.WithUserID(&in.AppID, &in.TargetUserID),
order1.WithPaymentID(&in.PaymentID),
order1.WithCanceled(in.Canceled),
order1.WithID(&in.ID, true),
order1.WithAppID(&in.AppID, true),
order1.WithUserID(&in.AppID, &in.TargetUserID, true),
order1.WithPaymentID(&in.PaymentID, true),
order1.WithCanceled(in.Canceled, true),
order1.WithFromAdmin(false),
)
if err != nil {
Expand Down Expand Up @@ -84,11 +84,11 @@ func (s *Server) UpdateUserOrder(ctx context.Context, in *npool.UpdateUserOrderR
func (s *Server) UpdateAppUserOrder(ctx context.Context, in *npool.UpdateAppUserOrderRequest) (*npool.UpdateAppUserOrderResponse, error) {
handler, err := order1.NewHandler(
ctx,
order1.WithID(&in.ID),
order1.WithAppID(&in.TargetAppID),
order1.WithUserID(&in.TargetAppID, &in.TargetUserID),
order1.WithPaymentID(&in.PaymentID),
order1.WithCanceled(in.Canceled),
order1.WithID(&in.ID, true),
order1.WithAppID(&in.TargetAppID, true),
order1.WithUserID(&in.TargetAppID, &in.TargetUserID, true),
order1.WithPaymentID(&in.PaymentID, true),
order1.WithCanceled(in.Canceled, true),
order1.WithFromAdmin(false),
)
if err != nil {
Expand Down
29 changes: 22 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ require (
github.com/NpoolPlatform/appuser-middleware v0.0.0-20230513100509-b19f356ebd4b
github.com/NpoolPlatform/basal-middleware v0.0.0-20230518061816-62bd589c0f63
github.com/NpoolPlatform/chain-middleware v0.0.0-20230712041523-2e1d215244e0
github.com/NpoolPlatform/dtm-cluster v0.0.0-20230518062456-3e53bebe90da
github.com/NpoolPlatform/go-service-framework v0.0.0-20230814035104-e1466b5c157e
github.com/NpoolPlatform/good-middleware v0.0.0-20230823034851-d6449a27f3e1
github.com/NpoolPlatform/inspire-middleware v0.0.0-20230811101104-636b885293d2
github.com/NpoolPlatform/ledger-middleware v0.0.0-20230823035222-525fd923c4df
github.com/NpoolPlatform/libent-cruder v0.0.0-20230822060122-512da6e5c8bf
github.com/NpoolPlatform/message v0.0.0-20230823033748-9a43fb49ad63
github.com/NpoolPlatform/order-middleware v0.0.0-20230823034327-d4735e33c3c3
github.com/NpoolPlatform/ledger-middleware v0.0.0-20230827161127-ec28b0f14ec0
github.com/NpoolPlatform/libent-cruder v0.0.0-20230825073905-d23e4d838f99
github.com/NpoolPlatform/message v0.0.0-20230828122703-3aa63ff5da47
github.com/NpoolPlatform/order-middleware v0.0.0-20230828144010-05748d247a78
github.com/NpoolPlatform/sphinx-proxy v0.0.0-20230216075025-a90a86bfd19b
github.com/dtm-labs/dtm v1.17.1
github.com/go-resty/resty/v2 v2.7.0
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3
Expand Down Expand Up @@ -46,6 +48,8 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dtm-labs/dtmdriver v0.0.6 // indirect
github.com/dtm-labs/logger v0.0.2 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-chassis/go-archaius v1.5.3 // indirect
Expand All @@ -55,7 +59,9 @@ require (
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
Expand All @@ -70,17 +76,19 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/hashicorp/serf v0.9.7 // indirect
github.com/klauspost/compress v1.15.1 // indirect
github.com/lithammer/shortuuid/v3 v3.0.7 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
Expand All @@ -95,22 +103,29 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.12.0 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.0.2 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
go.mongodb.org/mongo-driver v1.9.1 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.31.0 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.6.3 // indirect
go.opentelemetry.io/otel/sdk v1.6.3 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/ratelimit v0.2.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 50d0d1b

Please sign in to comment.