feat: improve gas station management workflow
This commit is contained in:
@@ -386,9 +386,8 @@ func ResourceResponse(value any) any {
|
||||
return stripInternalIDs(decoded)
|
||||
}
|
||||
|
||||
// PublicResourceResponse additionally resolves persisted relation keys into
|
||||
// their public identities. It is used by list/detail endpoints so an edit form
|
||||
// can round-trip the relation without ever receiving a surrogate database ID.
|
||||
// PublicResourceResponse preserves a resource's own ID for administrative
|
||||
// display while resolving and removing persistence-only relation IDs.
|
||||
func PublicResourceResponse(value any) (any, error) {
|
||||
encoded, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
@@ -450,7 +449,7 @@ type relationIdentityRecord struct {
|
||||
|
||||
func projectRelationIdentities(value any) (any, error) {
|
||||
groups := map[string]*relationIdentityGroup{}
|
||||
collectRelationIdentityReferences(value, groups)
|
||||
collectRelationIdentityReferences(value, groups, true)
|
||||
groupKeys := make([]string, 0, len(groups))
|
||||
for key := range groups {
|
||||
groupKeys = append(groupKeys, key)
|
||||
@@ -477,12 +476,14 @@ func projectRelationIdentities(value any) (any, error) {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func collectRelationIdentityReferences(value any, groups map[string]*relationIdentityGroup) {
|
||||
func collectRelationIdentityReferences(value any, groups map[string]*relationIdentityGroup, preserveRecordID bool) {
|
||||
switch data := value.(type) {
|
||||
case map[string]any:
|
||||
for key, item := range data {
|
||||
if key == "id" {
|
||||
delete(data, key)
|
||||
if !preserveRecordID {
|
||||
delete(data, key)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if strings.HasSuffix(key, "_id") {
|
||||
@@ -515,11 +516,11 @@ func collectRelationIdentityReferences(value any, groups map[string]*relationIde
|
||||
delete(data, key)
|
||||
continue
|
||||
}
|
||||
collectRelationIdentityReferences(item, groups)
|
||||
collectRelationIdentityReferences(item, groups, false)
|
||||
}
|
||||
case []any:
|
||||
for _, item := range data {
|
||||
collectRelationIdentityReferences(item, groups)
|
||||
collectRelationIdentityReferences(item, groups, preserveRecordID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,38 @@ func TestResourceResponseStripsInternalIDsRecursively(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicResourceResponsePreservesOnlyRecordID(t *testing.T) {
|
||||
got, err := PublicResourceResponse(map[string]any{
|
||||
"id": uint64(7), "identity": "record",
|
||||
"child": map[string]any{"id": uint64(8), "identity": "child"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
record := got.(map[string]any)
|
||||
if record["id"] != float64(7) {
|
||||
t.Fatalf("record ID = %#v, want 7", record["id"])
|
||||
}
|
||||
if _, exists := record["child"].(map[string]any)["id"]; exists {
|
||||
t.Fatal("nested database ID was exposed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicResourceResponsePreservesListRecordIDs(t *testing.T) {
|
||||
got, err := PublicResourceResponse([]map[string]any{
|
||||
{"id": uint64(11), "identity": "first"},
|
||||
{"id": uint64(12), "identity": "second"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
list := got.([]any)
|
||||
if list[0].(map[string]any)["id"] != float64(11) ||
|
||||
list[1].(map[string]any)["id"] != float64(12) {
|
||||
t.Fatalf("list record IDs were not preserved: %#v", list)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicFieldProtectionMasksGasorderContacts(t *testing.T) {
|
||||
value := map[string]any{"contact_name": "张三", "contact_phone": "13800138000"}
|
||||
ProtectPublicFields(value, false, false, false)
|
||||
|
||||
@@ -1,16 +1,38 @@
|
||||
package delivery
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ListDeliveryBasic 查询配送点分页列表。
|
||||
func ListDeliveryBasic(ctx *gin.Context) { common.ListPage[models.DeliveryBasic](ctx) }
|
||||
func ListDeliveryBasic(ctx *gin.Context) {
|
||||
gasIdentities := strings.Split(strings.TrimSpace(ctx.Query("gas_basic_identities")), ",")
|
||||
if len(gasIdentities) == 1 && gasIdentities[0] == "" {
|
||||
gasIdentities = nil
|
||||
}
|
||||
if len(gasIdentities) > 100 {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
common.ListPageFiltered[models.DeliveryBasic](ctx, func(query *gorm.DB) *gorm.DB {
|
||||
if len(gasIdentities) == 0 {
|
||||
return query
|
||||
}
|
||||
return query.Where(
|
||||
"gas_basic_id IN (SELECT id FROM gas_basic WHERE identity IN ? AND status <> ?)",
|
||||
gasIdentities,
|
||||
common.StatusArchived,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// GetDeliveryBasic 查询一个配送点。
|
||||
func GetDeliveryBasic(ctx *gin.Context) { common.GetByIdentity[models.DeliveryBasic](ctx) }
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
package gas
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// ListGasBasic 查询可燃气体站分页列表。
|
||||
@@ -46,3 +52,78 @@ func UpdateGasBasic(ctx *gin.Context) {
|
||||
}
|
||||
common.UpdateAllowedByIdentity(ctx, &models.GasBasic{}, gin.H{"name": request.Name, "credit_code": request.CreditCode, "principal": request.Principal, "address": request.Address, "longitude": request.Longitude, "latitude": request.Latitude}, []string{"name", "credit_code", "principal", "address", "longitude", "latitude"})
|
||||
}
|
||||
|
||||
// UpdateGasBasicStatus 仅允许已审核气站在启用和停用之间切换。
|
||||
func UpdateGasBasicStatus(ctx *gin.Context) {
|
||||
var request struct {
|
||||
Status int `json:"status" binding:"required"`
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&request); err != nil ||
|
||||
(request.Status != common.StatusEnable && request.Status != common.StatusDisable) {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
result := impl.DBService.Model(&models.GasBasic{}).
|
||||
Where("identity = ? AND status IN ?", ctx.Param("identity"), []int{common.StatusEnable, common.StatusDisable}).
|
||||
Update("status", request.Status)
|
||||
if result.Error != nil {
|
||||
infra.Response.Error(ctx, result.Error)
|
||||
return
|
||||
}
|
||||
if result.RowsAffected != 1 {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, gin.H{"updated": true})
|
||||
}
|
||||
|
||||
// ReviewGasBasic 审核待审核气站;不通过时必须填写理由。
|
||||
func ReviewGasBasic(ctx *gin.Context) {
|
||||
var request struct {
|
||||
Approved bool `json:"approved"`
|
||||
Reason string `json:"reason" binding:"max=2000"`
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&request); err != nil ||
|
||||
(!request.Approved && strings.TrimSpace(request.Reason) == "") {
|
||||
infra.Response.Error(ctx, errcode.ErrInvalidArgument)
|
||||
return
|
||||
}
|
||||
|
||||
operatorIdentity, operatorName := common.PlatformOperator(ctx)
|
||||
targetStatus := common.StatusDisable
|
||||
reviewStatus := common.StatusRejected
|
||||
if request.Approved {
|
||||
targetStatus = common.StatusEnable
|
||||
reviewStatus = common.StatusApproved
|
||||
}
|
||||
now := time.Now()
|
||||
err := impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
var station models.GasBasic
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("identity = ? AND status <> ?", ctx.Param("identity"), common.StatusArchived).
|
||||
First(&station).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if station.Status != common.StatusDraft {
|
||||
return errors.New("gas station is not pending review")
|
||||
}
|
||||
if err := tx.Model(&station).Update("status", targetStatus).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
review := models.GasBasicReview{
|
||||
Entity: common.NewEntity(common.StatusEnable),
|
||||
GasBasicID: station.ID,
|
||||
ReviewStatus: reviewStatus,
|
||||
ReviewReason: strings.TrimSpace(request.Reason),
|
||||
ReviewerIdentity: operatorIdentity,
|
||||
ReviewerName: operatorName,
|
||||
ReviewedAt: now,
|
||||
}
|
||||
return tx.Create(&review).Error
|
||||
})
|
||||
if err != nil {
|
||||
common.RespondRecordError(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, gin.H{"updated": true, "status": targetStatus})
|
||||
}
|
||||
|
||||
@@ -46,6 +46,9 @@ func platformMenuAllowsRequest(menus []platformbase.Menu, requestPath, method st
|
||||
(resource == "delivery_basic" || resource == "staff_account") {
|
||||
return true
|
||||
}
|
||||
if method == "GET" && menu.Identity == "gas_basic" && resource == "delivery_basic" {
|
||||
return true
|
||||
}
|
||||
if method == "GET" && relative == "wallet_basic" &&
|
||||
(menu.Identity == "gas_basic" || menu.Identity == "delivery_basic" ||
|
||||
menu.Identity == "staff" || menu.Identity == "user_account" ||
|
||||
|
||||
@@ -58,6 +58,16 @@ func TestOwnerMenusCanReadEmbeddedWallets(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGasManagementCanReadDeliveryPointCounts(t *testing.T) {
|
||||
menus := []platformbase.Menu{{Identity: "gas_basic"}}
|
||||
if !platformMenuAllowsRequest(menus, "/heqi/platform/v1/delivery_basic", "GET") {
|
||||
t.Fatal("gas management could not read its delivery point counts")
|
||||
}
|
||||
if platformMenuAllowsRequest(menus, "/heqi/platform/v1/delivery_basic", "POST") {
|
||||
t.Fatal("gas management unexpectedly received delivery point creation access")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocationScopeValuesAreExplicit(t *testing.T) {
|
||||
if !validLocationScope("standard") || !validLocationScope("precise") {
|
||||
t.Fatal("supported location scopes were rejected")
|
||||
|
||||
Reference in New Issue
Block a user