fix(order): 支持客服跨站处理订单与退款
This commit is contained in:
@@ -93,6 +93,9 @@ func (r *Repository) AdminClose(ctx context.Context, adminID uint64, orderID uin
|
||||
"order_no": order.OrderNo,
|
||||
"listing_id": order.ListingID,
|
||||
"account_id": order.AccountID,
|
||||
"buyer_tenant_id": order.BuyerTenantID,
|
||||
"source_tenant_id": order.SourceTenantID,
|
||||
"payment_tenant_id": order.PaymentTenantID,
|
||||
"reason": req.Reason,
|
||||
"before_order_status": beforeOrderStatus,
|
||||
"after_order_status": order.Status,
|
||||
@@ -607,6 +610,9 @@ func (r *Repository) AdminSeal(ctx context.Context, adminID uint64, orderID uint
|
||||
"order_no": order.OrderNo,
|
||||
"listing_id": order.ListingID,
|
||||
"account_id": order.AccountID,
|
||||
"buyer_tenant_id": order.BuyerTenantID,
|
||||
"source_tenant_id": order.SourceTenantID,
|
||||
"payment_tenant_id": order.PaymentTenantID,
|
||||
"reason": req.Reason,
|
||||
"before_order_status": beforeOrderStatus,
|
||||
"after_order_status": order.Status,
|
||||
|
||||
@@ -370,7 +370,6 @@ func (r *Repository) FindAdmin(ctx context.Context, orderID uint64) (*OrderDTO,
|
||||
}
|
||||
dto := row.toAdminDTO()
|
||||
dto.SiteRole = tenantOrderRole(ctx, row.RentalOrder)
|
||||
filterAdminActionsForSiteRole(&dto)
|
||||
applyPaymentDeadline(&dto, row.RentalOrder, pendingPaymentTimeoutMinutes(db))
|
||||
dto.ActiveDispute = r.activeDisputeDTO(ctx, orderID)
|
||||
dto.Checkout = r.latestCheckoutAdminDTO(ctx, orderID)
|
||||
@@ -388,7 +387,6 @@ func (r *Repository) FindLatestAdminByListing(ctx context.Context, listingID uin
|
||||
}
|
||||
dto := row.toAdminDTO()
|
||||
dto.SiteRole = tenantOrderRole(ctx, row.RentalOrder)
|
||||
filterAdminActionsForSiteRole(&dto)
|
||||
applyPaymentDeadline(&dto, row.RentalOrder, pendingPaymentTimeoutMinutes(db))
|
||||
dto.ActiveDispute = r.activeDisputeDTO(ctx, row.ID)
|
||||
dto.Checkout = r.latestCheckoutAdminDTO(ctx, row.ID)
|
||||
@@ -551,16 +549,6 @@ func applyTenantOrderScope(query *gorm.DB, ctx context.Context) *gorm.DB {
|
||||
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)")
|
||||
}
|
||||
|
||||
func applyBuyerTenantOrderScope(query *gorm.DB, ctx context.Context) *gorm.DB {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return query
|
||||
}
|
||||
return query.
|
||||
Where("o.buyer_tenant_id = ?", tenantID).
|
||||
Where("(o.distribution_site_id IS NULL OR o.distribution_site_id = 0)")
|
||||
}
|
||||
|
||||
func tenantOrderRole(ctx context.Context, order model.RentalOrder) string {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
@@ -580,24 +568,6 @@ func tenantOrderRole(ctx context.Context, order model.RentalOrder) string {
|
||||
}
|
||||
}
|
||||
|
||||
func filterAdminActionsForSiteRole(dto *OrderDTO) {
|
||||
if dto == nil || dto.AdminActions == nil || dto.SiteRole == "" || dto.SiteRole == "both" {
|
||||
return
|
||||
}
|
||||
actions := dto.AdminActions
|
||||
if dto.SiteRole == "buyer" {
|
||||
actions.ResetHandoff = nil
|
||||
actions.PlatformHandoff = nil
|
||||
actions.ForceHandoff = nil
|
||||
actions.PlatformOfflineSettlement = nil
|
||||
}
|
||||
if dto.SiteRole == "source" {
|
||||
actions.PlatformCheckoutConfirm = nil
|
||||
actions.PlatformCheckoutCounter = nil
|
||||
actions.PlatformCheckoutDispute = nil
|
||||
}
|
||||
}
|
||||
|
||||
type orderRow struct {
|
||||
model.RentalOrder
|
||||
Title string
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
// ListPendingRefund 查询所有待客服审核退款的订单。
|
||||
func (r *Repository) ListPendingRefund(ctx context.Context) ([]OrderDTO, error) {
|
||||
var rows []orderRow
|
||||
err := applyBuyerTenantOrderScope(r.adminQuery(ctx), ctx).
|
||||
err := applyTenantOrderScope(r.adminQuery(ctx), ctx).
|
||||
Where("o.refund_status = ?", refundStatusPendingReview).
|
||||
Order("o.updated_at ASC, o.id ASC").
|
||||
Limit(200).
|
||||
@@ -34,7 +34,7 @@ func (r *Repository) PendingRefundCount(ctx context.Context) (int64, error) {
|
||||
query := r.db.WithContext(ctx).
|
||||
Table("rental_orders AS o").
|
||||
Where("refund_status = ?", refundStatusPendingReview)
|
||||
query = applyBuyerTenantOrderScope(query, ctx)
|
||||
query = applyTenantOrderScope(query, ctx)
|
||||
err := query.Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
@@ -229,7 +229,7 @@ func (s *Service) AdminClose(ctx context.Context, adminID uint64, orderID uint64
|
||||
if orderID == 0 || req.Reason == "" {
|
||||
return ErrOrderCannotComplete
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityRefund); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.AdminClose(ctx, adminID, orderID, req, meta)
|
||||
@@ -255,7 +255,7 @@ func (s *Service) AdminSeal(ctx context.Context, adminID uint64, orderID uint64,
|
||||
if orderID == 0 || req.Reason == "" {
|
||||
return ErrOrderCannotComplete
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityRefund); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.AdminSeal(ctx, adminID, orderID, req, meta)
|
||||
@@ -268,7 +268,7 @@ func (s *Service) AdminResetHandoff(ctx context.Context, adminID uint64, orderID
|
||||
if orderID == 0 || req.Reason == "" {
|
||||
return ErrOrderCannotResetHandoff
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityHandoff); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.AdminResetHandoff(ctx, adminID, orderID, req, meta)
|
||||
@@ -281,7 +281,7 @@ func (s *Service) AdminPlatformHandoff(ctx context.Context, adminID uint64, orde
|
||||
if orderID == 0 || req.Content == "" || req.Reason == "" {
|
||||
return nil, ErrOrderCannotHandoff
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityHandoff); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.AdminPlatformHandoff(ctx, adminID, orderID, req, meta)
|
||||
@@ -294,7 +294,7 @@ func (s *Service) AdminForceHandoff(ctx context.Context, adminID uint64, orderID
|
||||
if orderID == 0 || req.Reason == "" {
|
||||
return ErrOrderCannotForceHandoff
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityHandoff); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.AdminForceHandoff(ctx, adminID, orderID, req, meta)
|
||||
@@ -307,7 +307,7 @@ func (s *Service) AdminPlatformCheckoutConfirm(ctx context.Context, adminID uint
|
||||
if orderID == 0 || req.Reason == "" {
|
||||
return ErrCheckoutCannotConfirm
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityCheckout); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.AdminPlatformCheckoutConfirm(ctx, adminID, orderID, req, meta)
|
||||
@@ -320,7 +320,7 @@ func (s *Service) AdminPlatformCheckoutCounter(ctx context.Context, adminID uint
|
||||
if orderID == 0 || req.Reason == "" {
|
||||
return nil, ErrCheckoutCannotCounter
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityCheckout); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.AdminPlatformCheckoutCounter(ctx, adminID, orderID, req, meta)
|
||||
@@ -333,7 +333,7 @@ func (s *Service) AdminPlatformCheckoutDispute(ctx context.Context, adminID uint
|
||||
if orderID == 0 || req.Reason == "" {
|
||||
return ErrCheckoutCannotCounter
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityCheckout); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.AdminPlatformCheckoutDispute(ctx, adminID, orderID, req, meta)
|
||||
@@ -346,7 +346,7 @@ func (s *Service) AdminMarkOfflineSettlement(ctx context.Context, adminID uint64
|
||||
if orderID == 0 {
|
||||
return ErrOfflineSettlementCannotMark
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityHandoff); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.AdminMarkOfflineSettlement(ctx, adminID, orderID, req, meta)
|
||||
@@ -359,7 +359,7 @@ func (s *Service) AdminHoldDeposit(ctx context.Context, adminID uint64, orderID
|
||||
if orderID == 0 || req.Reason == "" {
|
||||
return ErrDepositCannotHold
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityRefund); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.AdminHoldDeposit(ctx, adminID, orderID, req, meta)
|
||||
@@ -372,7 +372,7 @@ func (s *Service) AdminReleaseDeposit(ctx context.Context, adminID uint64, order
|
||||
if orderID == 0 || req.Reason == "" {
|
||||
return ErrDepositCannotHold
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityRefund); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.AdminReleaseDeposit(ctx, adminID, orderID, req, meta)
|
||||
@@ -385,7 +385,7 @@ func (s *Service) AdminRefund(ctx context.Context, orderID uint64) (*RefundStatu
|
||||
if orderID == 0 {
|
||||
return nil, ErrOrderCannotComplete
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityRefund); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.AdminRefund(ctx, orderID)
|
||||
@@ -398,7 +398,7 @@ func (s *Service) AdminRefundStatus(ctx context.Context, orderID uint64) (*Refun
|
||||
if orderID == 0 {
|
||||
return nil, ErrOrderCannotComplete
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityRefund); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.AdminRefundStatus(ctx, orderID)
|
||||
@@ -411,7 +411,7 @@ func (s *Service) AdminApproveRefund(ctx context.Context, orderID uint64) error
|
||||
if orderID == 0 {
|
||||
return ErrOrderCannotComplete
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityRefund); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.AdminApproveRefund(ctx, orderID)
|
||||
@@ -424,7 +424,7 @@ func (s *Service) AdminRejectRefund(ctx context.Context, orderID uint64, action
|
||||
if orderID == 0 {
|
||||
return ErrOrderCannotComplete
|
||||
}
|
||||
if err := s.ensureAdminOrderCapability(ctx, orderID, tenantOrderCapabilityRefund); err != nil {
|
||||
if err := s.ensureAdminOrderAccess(ctx, orderID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.AdminRejectRefund(ctx, orderID, action)
|
||||
@@ -460,13 +460,3 @@ func (s *Service) ensureAdminOrderAccess(ctx context.Context, orderID uint64) er
|
||||
}
|
||||
return s.repo.EnsureAdminOrderAccess(ctx, orderID)
|
||||
}
|
||||
|
||||
func (s *Service) ensureAdminOrderCapability(ctx context.Context, orderID uint64, capability string) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
if orderID == 0 {
|
||||
return ErrOrderCannotComplete
|
||||
}
|
||||
return s.repo.EnsureAdminOrderCapability(ctx, orderID, capability)
|
||||
}
|
||||
|
||||
@@ -270,23 +270,13 @@ func TestAdminOrdersAreScopedByCurrentTenant(t *testing.T) {
|
||||
if _, err := service.AdminRefundStatus(orderTenantContext(202), orders[1].ID); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("cross-tenant admin action error = %v", err)
|
||||
}
|
||||
if err := repo.EnsureAdminOrderCapability(orderTenantContext(101), orders[0].ID, tenantOrderCapabilityRefund); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("source tenant refund capability error = %v", err)
|
||||
for _, tenantID := range []uint64{101, 202} {
|
||||
if err := repo.EnsureAdminOrderAccess(orderTenantContext(tenantID), orders[0].ID); err != nil {
|
||||
t.Fatalf("tenant %d order access: %v", tenantID, err)
|
||||
}
|
||||
}
|
||||
if err := repo.EnsureAdminOrderCapability(orderTenantContext(202), orders[0].ID, tenantOrderCapabilityRefund); err != nil {
|
||||
t.Fatalf("buyer tenant refund capability: %v", err)
|
||||
}
|
||||
if err := repo.EnsureAdminOrderCapability(orderTenantContext(101), orders[0].ID, tenantOrderCapabilityHandoff); err != nil {
|
||||
t.Fatalf("source tenant handoff capability: %v", err)
|
||||
}
|
||||
if err := repo.EnsureAdminOrderCapability(orderTenantContext(202), orders[0].ID, tenantOrderCapabilityHandoff); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("buyer tenant handoff capability error = %v", err)
|
||||
}
|
||||
if err := service.AdminClose(orderTenantContext(101), 9, orders[0].ID, AdminActionRequest{Reason: "source close"}, AuditMeta{}); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("source tenant close capability error = %v", err)
|
||||
}
|
||||
if err := service.AdminSeal(orderTenantContext(101), 9, orders[0].ID, AdminActionRequest{Reason: "source seal"}, AuditMeta{}); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("source tenant seal capability error = %v", err)
|
||||
if err := repo.EnsureAdminOrderAccess(orderTenantContext(303), orders[0].ID); !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
t.Fatalf("unrelated tenant order access error = %v", err)
|
||||
}
|
||||
|
||||
sourceResult, err := repo.ListAdmin(orderTenantContext(101), AdminOrderQuery{Page: 1, PageSize: 20})
|
||||
@@ -303,4 +293,22 @@ func TestAdminOrdersAreScopedByCurrentTenant(t *testing.T) {
|
||||
if roles[orders[0].ID] != "source" || roles[orders[1].ID] != "buyer" {
|
||||
t.Fatalf("A tenant order roles = %+v", roles)
|
||||
}
|
||||
|
||||
if err := db.Model(&model.RentalOrder{}).
|
||||
Where("id = ?", orders[0].ID).
|
||||
Update("refund_status", refundStatusPendingReview).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tenantID := range []uint64{101, 202} {
|
||||
items, err := repo.ListPendingRefund(orderTenantContext(tenantID))
|
||||
if err != nil {
|
||||
t.Fatalf("tenant %d pending refunds: %v", tenantID, err)
|
||||
}
|
||||
if len(items) != 1 || items[0].ID != orders[0].ID {
|
||||
t.Fatalf("tenant %d pending refunds = %+v", tenantID, items)
|
||||
}
|
||||
}
|
||||
if items, err := repo.ListPendingRefund(orderTenantContext(303)); err != nil || len(items) != 0 {
|
||||
t.Fatalf("unrelated tenant pending refunds = %+v, err=%v", items, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
package order
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"hfb_sys/backend/internal/logging"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
tenantOrderCapabilityHandoff = "handoff"
|
||||
tenantOrderCapabilityCheckout = "checkout"
|
||||
tenantOrderCapabilityRefund = "refund"
|
||||
)
|
||||
|
||||
func (r *Repository) EnsureAdminOrderCapability(ctx context.Context, orderID uint64, capability string) error {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return nil
|
||||
}
|
||||
var order struct {
|
||||
BuyerTenantID uint64
|
||||
SourceTenantID uint64
|
||||
DistributionSiteID *uint64
|
||||
}
|
||||
if err := r.db.WithContext(ctx).
|
||||
Table("rental_orders").
|
||||
Select("buyer_tenant_id, source_tenant_id, distribution_site_id").
|
||||
Where("id = ?", orderID).
|
||||
Scan(&order).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if order.BuyerTenantID == 0 && order.SourceTenantID == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
if order.DistributionSiteID != nil && *order.DistributionSiteID > 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
isBuyer := order.BuyerTenantID == tenantID
|
||||
isSource := order.SourceTenantID == tenantID
|
||||
if !isBuyer && !isSource {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
if r.adminIsPlatformSuper(ctx) {
|
||||
return nil
|
||||
}
|
||||
allowed := false
|
||||
switch capability {
|
||||
case tenantOrderCapabilityHandoff:
|
||||
allowed = isSource
|
||||
case tenantOrderCapabilityCheckout, tenantOrderCapabilityRefund:
|
||||
allowed = isBuyer
|
||||
default:
|
||||
allowed = true
|
||||
}
|
||||
if !allowed {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) adminIsPlatformSuper(ctx context.Context) bool {
|
||||
adminID := logging.AdminIDFromContext(ctx)
|
||||
if adminID == 0 {
|
||||
return false
|
||||
}
|
||||
var count int64
|
||||
err := r.db.WithContext(ctx).Table("admin_user_roles AS aur").
|
||||
Joins("JOIN roles AS r ON r.id = aur.role_id").
|
||||
Where("aur.admin_user_id = ? AND aur.tenant_id = 0 AND r.code = ?", adminID, "super_admin").
|
||||
Count(&count).Error
|
||||
return err == nil && count > 0
|
||||
}
|
||||
Reference in New Issue
Block a user