fix(listing-review): 按商品来源站点隔离审核
This commit is contained in:
@@ -75,7 +75,7 @@ func (r *Repository) AdjustExternalListing(
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
listing, account, err := r.findForReviewUpdate(tx, listingID)
|
||||
listing, account, err := r.findForReviewUpdate(ctx, tx, listingID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -176,7 +177,13 @@ func yuanToAdminCent(value float64) int64 {
|
||||
}
|
||||
|
||||
func (r *Repository) FindAdmin(ctx context.Context, listingID uint64) (*ListingDTO, error) {
|
||||
return r.findDTO(ctx, "l.id = ?", listingID)
|
||||
query := applyListingSourceTenantScope(ctx, r.baseQuery(ctx), "l")
|
||||
var row listingRow
|
||||
if err := query.Where("l.id = ?", listingID).First(&row).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto := row.toDTO()
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
func (r *Repository) ListMine(ctx context.Context, ownerID uint64, siteCode ...string) ([]ListingDTO, error) {
|
||||
@@ -253,9 +260,22 @@ func (r *Repository) baseQuery(ctx context.Context) *gorm.DB {
|
||||
)`)
|
||||
}
|
||||
|
||||
func (r *Repository) findForReviewUpdate(tx *gorm.DB, listingID uint64) (*model.RentalListing, *model.GameAccount, error) {
|
||||
func applyListingSourceTenantScope(ctx context.Context, query *gorm.DB, alias string) *gorm.DB {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID == 0 {
|
||||
return query
|
||||
}
|
||||
column := "source_tenant_id"
|
||||
if alias != "" {
|
||||
column = alias + "." + column
|
||||
}
|
||||
return query.Where(column+" = ?", tenantID)
|
||||
}
|
||||
|
||||
func (r *Repository) findForReviewUpdate(ctx context.Context, tx *gorm.DB, listingID uint64) (*model.RentalListing, *model.GameAccount, error) {
|
||||
var listing model.RentalListing
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&listing, listingID).Error; err != nil {
|
||||
query := applyListingSourceTenantScope(ctx, tx.Where("id = ?", listingID), "")
|
||||
if err := query.Clauses(clause.Locking{Strength: "UPDATE"}).First(&listing).Error; err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var account model.GameAccount
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/notification"
|
||||
smsnode "hfb_sys/backend/internal/modules/sms"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
@@ -18,7 +19,8 @@ import (
|
||||
|
||||
func (r *Repository) ListPendingReview(ctx context.Context) ([]ListingDTO, error) {
|
||||
var rows []listingRow
|
||||
err := r.baseQuery(ctx).
|
||||
query := applyListingSourceTenantScope(ctx, r.baseQuery(ctx), "l")
|
||||
err := query.
|
||||
Where("l.review_status = ? AND l.status <> ?", "pending", "offline").
|
||||
Order("l.updated_at ASC, l.id ASC").
|
||||
Limit(200).
|
||||
@@ -40,7 +42,7 @@ func (r *Repository) AdminMarkAbnormal(ctx context.Context, adminID uint64, list
|
||||
func (r *Repository) TransferOwner(ctx context.Context, adminID uint64, listingID uint64, req TransferOwnerRequest, meta AuditMeta) (*ListingDTO, error) {
|
||||
var dto *ListingDTO
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
listing, account, err := r.findForReviewUpdate(tx, listingID)
|
||||
listing, account, err := r.findForReviewUpdate(ctx, tx, listingID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -53,8 +55,12 @@ func (r *Repository) TransferOwner(ctx context.Context, adminID uint64, listingI
|
||||
}
|
||||
|
||||
var target model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("id = ? AND status = ? AND realname_status = ?", req.TargetUserID, "active", "verified").
|
||||
targetQuery := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("id = ? AND status = ? AND realname_status = ?", req.TargetUserID, "active", "verified")
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
targetQuery = targetQuery.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := targetQuery.
|
||||
First(&target).Error; err != nil {
|
||||
if IsNotFound(err) {
|
||||
return ErrTargetOwnerInvalid
|
||||
@@ -162,7 +168,7 @@ func emptyJSONListForListing() datatypes.JSON {
|
||||
func (r *Repository) adminUpdateStatus(ctx context.Context, adminID uint64, listingID uint64, req AdminActionRequest, meta AuditMeta, listingStatus string, accountStatus string, action string, title string, content string) (*ListingDTO, error) {
|
||||
var dto *ListingDTO
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
listing, account, err := r.findForReviewUpdate(tx, listingID)
|
||||
listing, account, err := r.findForReviewUpdate(ctx, tx, listingID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -228,7 +234,7 @@ func (r *Repository) adminUpdateStatus(ctx context.Context, adminID uint64, list
|
||||
func (r *Repository) Approve(ctx context.Context, listingID uint64) (*ListingDTO, error) {
|
||||
var dto *ListingDTO
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
listing, account, err := r.findForReviewUpdate(tx, listingID)
|
||||
listing, account, err := r.findForReviewUpdate(ctx, tx, listingID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -275,7 +281,7 @@ func (r *Repository) Approve(ctx context.Context, listingID uint64) (*ListingDTO
|
||||
func (r *Repository) AdjustReviewPrice(ctx context.Context, adminID uint64, listingID uint64, req AdminPriceAdjustRequest, meta AuditMeta) (*ListingDTO, error) {
|
||||
var dto *ListingDTO
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
listing, account, err := r.findForReviewUpdate(tx, listingID)
|
||||
listing, account, err := r.findForReviewUpdate(ctx, tx, listingID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -366,7 +372,7 @@ func (r *Repository) AdjustReviewPrice(ctx context.Context, adminID uint64, list
|
||||
func (r *Repository) Reject(ctx context.Context, listingID uint64, req ReviewRequest) (*ListingDTO, error) {
|
||||
var dto *ListingDTO
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
listing, account, err := r.findForReviewUpdate(tx, listingID)
|
||||
listing, account, err := r.findForReviewUpdate(ctx, tx, listingID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -414,7 +420,7 @@ func (r *Repository) Reject(ctx context.Context, listingID uint64, req ReviewReq
|
||||
func (r *Repository) UpdateScreenshots(ctx context.Context, adminID uint64, listingID uint64, req AdminUpdateScreenshotsRequest, meta AuditMeta) (*ListingDTO, error) {
|
||||
var dto *ListingDTO
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
listing, account, err := r.findForReviewUpdate(tx, listingID)
|
||||
listing, account, err := r.findForReviewUpdate(ctx, tx, listingID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/systemconfig"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
)
|
||||
|
||||
type stubConfigReader map[string]string
|
||||
@@ -479,6 +480,74 @@ func TestListPendingReviewIncludesSourceChannel(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPendingReviewAndApprovalAreScopedToSourceTenant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
if err := database.MigrateListingLifecycleTestSchema(db); err != nil {
|
||||
t.Fatalf("failed to migrate test db: %v", err)
|
||||
}
|
||||
tenants := []model.Tenant{
|
||||
{Code: "tenant_a", Name: "A站", Status: "active"},
|
||||
{Code: "tenant_b", Name: "B站", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&tenants).Error; err != nil {
|
||||
t.Fatalf("failed to create tenants: %v", err)
|
||||
}
|
||||
users := []model.User{
|
||||
{TenantID: tenants[0].ID, Phone: "18800000101", RealnameStatus: "verified", Status: "active"},
|
||||
{TenantID: tenants[1].ID, Phone: "18800000102", RealnameStatus: "verified", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&users).Error; err != nil {
|
||||
t.Fatalf("failed to create users: %v", err)
|
||||
}
|
||||
listings := make([]model.RentalListing, 0, 2)
|
||||
for index, tenant := range tenants {
|
||||
account := model.GameAccount{
|
||||
SourceTenantID: tenant.ID,
|
||||
OwnerID: users[index].ID,
|
||||
GameName: "delta_force",
|
||||
ServerRegion: "QQ",
|
||||
LoginPlatform: "扫码",
|
||||
Title: tenant.Name + "待审核账号",
|
||||
Status: "draft",
|
||||
}
|
||||
if err := db.Create(&account).Error; err != nil {
|
||||
t.Fatalf("failed to create account: %v", err)
|
||||
}
|
||||
listing := model.RentalListing{
|
||||
SourceTenantID: tenant.ID,
|
||||
ListingNo: "PENDING-" + tenant.Code,
|
||||
AccountID: account.ID,
|
||||
OwnerID: users[index].ID,
|
||||
PriceCent: 10000,
|
||||
Status: "draft",
|
||||
ReviewStatus: "pending",
|
||||
}
|
||||
if err := db.Create(&listing).Error; err != nil {
|
||||
t.Fatalf("failed to create listing: %v", err)
|
||||
}
|
||||
listings = append(listings, listing)
|
||||
}
|
||||
|
||||
repo := NewRepository(db, nil)
|
||||
ctxA := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: tenants[0].ID})
|
||||
items, err := repo.ListPendingReview(ctxA)
|
||||
if err != nil {
|
||||
t.Fatalf("ListPendingReview() error = %v", err)
|
||||
}
|
||||
if len(items) != 1 || items[0].ID != listings[0].ID {
|
||||
t.Fatalf("pending items = %+v, want only listing %d", items, listings[0].ID)
|
||||
}
|
||||
if _, err := repo.FindAdmin(ctxA, listings[1].ID); !IsNotFound(err) {
|
||||
t.Fatalf("cross-tenant FindAdmin error = %v, want not found", err)
|
||||
}
|
||||
if _, err := repo.Approve(ctxA, listings[1].ID); !IsNotFound(err) {
|
||||
t.Fatalf("cross-tenant Approve error = %v, want not found", err)
|
||||
}
|
||||
if _, err := repo.Approve(ctxA, listings[0].ID); err != nil {
|
||||
t.Fatalf("Approve current tenant listing: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalAccountToCreateRequestMapsUploadFields(t *testing.T) {
|
||||
req := externalAccountToCreateRequest("客服1", 1772526103000, ExternalAccountData{
|
||||
LoginMethod: "QQ账号密码",
|
||||
|
||||
Reference in New Issue
Block a user