chore: seed migration objects batch 52
This commit is contained in:
@@ -20,4 +20,5 @@ var (
|
||||
ErrRechargeOrderIDInvalid = errors.New("recharge order id invalid")
|
||||
ErrRemarkInvalid = errors.New("remark invalid")
|
||||
ErrVoucherInvalid = errors.New("voucher attachment ids invalid")
|
||||
ErrLedgerFilterInvalid = errors.New("wallet ledger filter invalid")
|
||||
)
|
||||
|
||||
@@ -55,6 +55,7 @@ type Repository interface {
|
||||
CreateRecharge(context.Context, store.RechargeCreation) (*store.RechargeOrder, error)
|
||||
RechargeOrders(context.Context, int64, int64, int, int, int) (*store.RechargePage, error)
|
||||
RechargeOrder(context.Context, int64, int64, int64) (*store.RechargeOrder, error)
|
||||
WalletLedgers(context.Context, int64, int64, string, int, int, int) (*store.WalletLedgerPage, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
@@ -209,6 +210,29 @@ type RechargePage struct {
|
||||
HasNext bool `json:"has_next"`
|
||||
}
|
||||
|
||||
type WalletLedger struct {
|
||||
ID int64 `json:"id"`
|
||||
LedgerNo string `json:"ledger_no"`
|
||||
BizType string `json:"biz_type"`
|
||||
BizNo string `json:"biz_no"`
|
||||
EntryType string `json:"entry_type"`
|
||||
Direction string `json:"direction"`
|
||||
Amount int64 `json:"amount"`
|
||||
BeforeAmount int64 `json:"before_amount"`
|
||||
AfterAmount int64 `json:"after_amount"`
|
||||
Currency string `json:"currency"`
|
||||
Remark string `json:"remark"`
|
||||
OccurredAt string `json:"occurred_at"`
|
||||
}
|
||||
|
||||
type WalletLedgerPage struct {
|
||||
Items []WalletLedger `json:"items"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
Total int64 `json:"total"`
|
||||
HasNext bool `json:"has_next"`
|
||||
}
|
||||
|
||||
func New(repository Repository, redisClient *redis.Client, cfg config.AuthConfig) *Service {
|
||||
service := &Service{repository: repository, redis: redisClient, now: time.Now}
|
||||
encryptionKey, encryptionErr := decodeKey(cfg.DataEncryptionKey)
|
||||
@@ -231,7 +255,7 @@ func New(repository Repository, redisClient *redis.Client, cfg config.AuthConfig
|
||||
}
|
||||
|
||||
func (s *Service) PaymentAccounts(ctx context.Context, tenantID, memberID int64) ([]PaymentAccount, error) {
|
||||
if !s.ready {
|
||||
if s.repository == nil {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
rows, err := s.repository.PaymentAccounts(ctx, tenantID, memberID)
|
||||
@@ -368,7 +392,7 @@ func (s *Service) CreateWithdrawal(ctx context.Context, tenantID, memberID int64
|
||||
}
|
||||
|
||||
func (s *Service) WithdrawalOrders(ctx context.Context, tenantID, memberID int64, page, pageSize int, status string) (*WithdrawalPage, error) {
|
||||
if !s.ready {
|
||||
if s.repository == nil {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
if page == 0 {
|
||||
@@ -399,7 +423,7 @@ func (s *Service) WithdrawalOrders(ctx context.Context, tenantID, memberID int64
|
||||
}
|
||||
|
||||
func (s *Service) WithdrawalOrder(ctx context.Context, tenantID, memberID, orderID int64) (*Withdrawal, error) {
|
||||
if !s.ready {
|
||||
if s.repository == nil {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
if orderID <= 0 {
|
||||
@@ -414,7 +438,7 @@ func (s *Service) WithdrawalOrder(ctx context.Context, tenantID, memberID, order
|
||||
}
|
||||
|
||||
func (s *Service) RechargeChannels(ctx context.Context, tenantID int64) ([]RechargeChannel, error) {
|
||||
if !s.ready {
|
||||
if s.repository == nil {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
rows, err := s.repository.RechargeChannels(ctx, tenantID)
|
||||
@@ -432,7 +456,7 @@ func (s *Service) RechargeChannels(ctx context.Context, tenantID int64) ([]Recha
|
||||
}
|
||||
|
||||
func (s *Service) CreateRecharge(ctx context.Context, tenantID, memberID int64, input CreateRechargeInput) (*Recharge, error) {
|
||||
if !s.ready {
|
||||
if s.repository == nil {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
input.Currency = strings.ToUpper(strings.TrimSpace(input.Currency))
|
||||
@@ -495,7 +519,7 @@ func (s *Service) CreateRecharge(ctx context.Context, tenantID, memberID int64,
|
||||
}
|
||||
|
||||
func (s *Service) RechargeOrders(ctx context.Context, tenantID, memberID int64, page, pageSize int, status string) (*RechargePage, error) {
|
||||
if !s.ready {
|
||||
if s.repository == nil {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
if page == 0 {
|
||||
@@ -527,7 +551,7 @@ func (s *Service) RechargeOrders(ctx context.Context, tenantID, memberID int64,
|
||||
}
|
||||
|
||||
func (s *Service) RechargeOrder(ctx context.Context, tenantID, memberID, orderID int64) (*Recharge, error) {
|
||||
if !s.ready {
|
||||
if s.repository == nil {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
if orderID <= 0 {
|
||||
@@ -541,6 +565,57 @@ func (s *Service) RechargeOrder(ctx context.Context, tenantID, memberID, orderID
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s *Service) WalletLedgers(ctx context.Context, tenantID, memberID int64, page, pageSize int, bizType, direction string) (*WalletLedgerPage, error) {
|
||||
if s.repository == nil {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
if page == 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize == 0 {
|
||||
pageSize = defaultPageSize
|
||||
}
|
||||
if page < 1 {
|
||||
return nil, ErrPageInvalid
|
||||
}
|
||||
if pageSize < 1 || pageSize > maxPageSize {
|
||||
return nil, ErrPageSizeInvalid
|
||||
}
|
||||
bizType = strings.TrimSpace(bizType)
|
||||
if len(bizType) > 32 {
|
||||
return nil, ErrLedgerFilterInvalid
|
||||
}
|
||||
directionCode := 0
|
||||
switch strings.ToLower(strings.TrimSpace(direction)) {
|
||||
case "", "all":
|
||||
case "income":
|
||||
directionCode = 1
|
||||
case "expense":
|
||||
directionCode = 2
|
||||
default:
|
||||
return nil, ErrLedgerFilterInvalid
|
||||
}
|
||||
rows, err := s.repository.WalletLedgers(ctx, tenantID, memberID, bizType, directionCode, (page-1)*pageSize, pageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]WalletLedger, 0, len(rows.Items))
|
||||
for _, row := range rows.Items {
|
||||
directionName := "expense"
|
||||
if row.Direction == 1 {
|
||||
directionName = "income"
|
||||
}
|
||||
items = append(items, WalletLedger{
|
||||
ID: row.ID, LedgerNo: row.LedgerNo, BizType: row.BizType, BizNo: row.BizNo,
|
||||
EntryType: row.EntryType, Direction: directionName, Amount: row.Amount,
|
||||
BeforeAmount: row.BeforeAmount, AfterAmount: row.AfterAmount, Currency: row.Currency,
|
||||
Remark: row.Remark, OccurredAt: row.OccurredAt,
|
||||
})
|
||||
}
|
||||
return &WalletLedgerPage{Items: items, Page: page, PageSize: pageSize, Total: rows.Total,
|
||||
HasNext: int64(page*pageSize) < rows.Total}, nil
|
||||
}
|
||||
|
||||
func (s *Service) quote(ctx context.Context, tenantID, memberID int64, input QuoteInput) (*Quote, error) {
|
||||
quote, _, _, err := s.quoteData(ctx, tenantID, memberID, input)
|
||||
return quote, err
|
||||
|
||||
@@ -19,6 +19,7 @@ type fakeRepository struct {
|
||||
orders []store.WithdrawalOrder
|
||||
rechargeChannels []store.RechargeChannel
|
||||
rechargeOrders []store.RechargeOrder
|
||||
ledgers []store.WalletLedger
|
||||
createdRecharge store.RechargeCreation
|
||||
}
|
||||
|
||||
@@ -67,6 +68,9 @@ func (f *fakeRepository) RechargeOrders(context.Context, int64, int64, int, int,
|
||||
func (f *fakeRepository) RechargeOrder(context.Context, int64, int64, int64) (*store.RechargeOrder, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
}
|
||||
func (f *fakeRepository) WalletLedgers(context.Context, int64, int64, string, int, int, int) (*store.WalletLedgerPage, error) {
|
||||
return &store.WalletLedgerPage{Items: f.ledgers, Total: int64(len(f.ledgers))}, nil
|
||||
}
|
||||
|
||||
func TestQuoteCalculatesRateFeeAndLimits(t *testing.T) {
|
||||
repository := &fakeRepository{config: json.RawMessage(`{"features":{"withdrawal_enabled":true}}`),
|
||||
@@ -127,7 +131,7 @@ func TestCreateRechargeValidatesChannelAndMapsOrder(t *testing.T) {
|
||||
repository := &fakeRepository{rechargeChannels: []store.RechargeChannel{{
|
||||
ID: 4, Code: "manual", Name: "人工审核", MinAmount: 100, MaxAmount: 100000,
|
||||
}}}
|
||||
service := &Service{repository: repository, ready: true, now: time.Now}
|
||||
service := New(repository, nil, config.AuthConfig{})
|
||||
result, err := service.CreateRecharge(context.Background(), 0, 9, CreateRechargeInput{
|
||||
ChannelID: 4, Amount: 50000, Currency: "cny", Remark: "测试",
|
||||
})
|
||||
@@ -157,6 +161,43 @@ func TestRechargeOrdersMapsStatusAndPagination(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFinanceOrderListsDoNotRequireEncryptionKeys(t *testing.T) {
|
||||
repository := &fakeRepository{
|
||||
orders: []store.WithdrawalOrder{{ID: 2, WithdrawalNo: "WD2", Status: 1}},
|
||||
rechargeOrders: []store.RechargeOrder{{ID: 3, RechargeNo: "RC3", Status: 1}},
|
||||
}
|
||||
service := New(repository, nil, config.AuthConfig{})
|
||||
|
||||
withdrawals, err := service.WithdrawalOrders(context.Background(), 0, 9, 1, 20, "all")
|
||||
if err != nil || len(withdrawals.Items) != 1 {
|
||||
t.Fatalf("withdrawal records should remain readable without encryption keys: %#v, %v", withdrawals, err)
|
||||
}
|
||||
recharges, err := service.RechargeOrders(context.Background(), 0, 9, 1, 20, "all")
|
||||
if err != nil || len(recharges.Items) != 1 {
|
||||
t.Fatalf("recharge records should remain readable without encryption keys: %#v, %v", recharges, err)
|
||||
}
|
||||
if _, err = service.PaymentAccounts(context.Background(), 0, 9); err != nil {
|
||||
t.Fatalf("masked payment accounts should remain readable without encryption keys, got %v", err)
|
||||
}
|
||||
if _, err = service.CreatePaymentAccount(context.Background(), 0, 9, PaymentAccountInput{}); !errors.Is(err, ErrUnavailable) {
|
||||
t.Fatalf("sensitive finance operations must remain unavailable without encryption keys, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWalletLedgersDoNotRequireEncryptionKeys(t *testing.T) {
|
||||
service := New(&fakeRepository{ledgers: []store.WalletLedger{{
|
||||
ID: 7, LedgerNo: "WL7", Direction: 1, Amount: 947, Remark: "下级交易返佣",
|
||||
}}}, nil, config.AuthConfig{})
|
||||
result, err := service.WalletLedgers(context.Background(), 0, 9, 1, 20, "", "all")
|
||||
if err != nil || result.Total != 1 || len(result.Items) != 1 ||
|
||||
result.Items[0].Direction != "income" || result.Items[0].Amount != 947 {
|
||||
t.Fatalf("wallet ledgers should remain readable without encryption keys: %#v, %v", result, err)
|
||||
}
|
||||
if _, err = service.WalletLedgers(context.Background(), 0, 9, 1, 20, "", "sideways"); !errors.Is(err, ErrLedgerFilterInvalid) {
|
||||
t.Fatalf("expected invalid ledger direction, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeterministicNumberIsStableAndScoped(t *testing.T) {
|
||||
first := deterministicNumber("WD", 0, 1, "request-key")
|
||||
if first != deterministicNumber("WD", 0, 1, "request-key") || first == deterministicNumber("WD", 0, 2, "request-key") {
|
||||
|
||||
Reference in New Issue
Block a user