完善配送端列表头像展示
This commit is contained in:
@@ -14,21 +14,26 @@ import (
|
||||
|
||||
func scopedStaff(ctx *gin.Context, identity string, point models.DeliveryBasic) (models.StaffAccount, bool) {
|
||||
var staff models.StaffAccount
|
||||
if err := common.ActiveRecords(db()).Where("identity = ? AND gas_basic_id = ? AND delivery_basic_id = ?",
|
||||
identity, point.GasBasicID, point.ID).First(&staff).Error; err != nil {
|
||||
if err := deliveryStaffQuery(db(), point).Where("identity = ?", identity).First(&staff).Error; err != nil {
|
||||
common.RespondRecordError(ctx, err)
|
||||
return staff, false
|
||||
}
|
||||
return staff, true
|
||||
}
|
||||
|
||||
// deliveryStaffQuery 固定配送人员的气站、配送点及角色范围。
|
||||
func deliveryStaffQuery(databaseService *gorm.DB, point models.DeliveryBasic) *gorm.DB {
|
||||
return common.ActiveRecords(databaseService.Model(&models.StaffAccount{})).
|
||||
Where("gas_basic_id = ? AND delivery_basic_id = ? AND role_code = ?",
|
||||
point.GasBasicID, point.ID, "delivery")
|
||||
}
|
||||
|
||||
func ListStaff(ctx *gin.Context) {
|
||||
point, _, ok := currentScope(ctx)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
query := common.ActiveRecords(db().Model(&models.StaffAccount{})).
|
||||
Where("gas_basic_id = ? AND delivery_basic_id = ? AND role_code = ?", point.GasBasicID, point.ID, "delivery")
|
||||
query := deliveryStaffQuery(db(), point)
|
||||
listScoped(ctx, &models.StaffAccount{}, query, "staff_account.created_at desc")
|
||||
}
|
||||
|
||||
@@ -38,8 +43,7 @@ func GetStaff(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
var staff models.StaffAccount
|
||||
respondRecord(ctx, common.ActiveRecords(db()).Where("identity = ? AND gas_basic_id = ? AND delivery_basic_id = ? AND role_code = ?",
|
||||
ctx.Param("identity"), point.GasBasicID, point.ID, "delivery"), &staff)
|
||||
respondRecord(ctx, deliveryStaffQuery(db(), point).Where("identity = ?", ctx.Param("identity")), &staff)
|
||||
}
|
||||
|
||||
// GetStaffAvatar 返回当前配送点范围内配送人员的受保护头像。
|
||||
|
||||
33
backend/api/internal/logic/delivery/staff_test.go
Normal file
33
backend/api/internal/logic/delivery/staff_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// 功能描述:验证配送点人员查询始终限制为当前配送点的配送角色。版本:v1.0.0。
|
||||
package delivery
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TestDeliveryStaffQueryKeepsRoleScope 验证列表、详情和头像共用的范围包含 delivery 角色。
|
||||
func TestDeliveryStaffQueryKeepsRoleScope(t *testing.T) {
|
||||
connection, _, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("创建 SQL mock 失败:%v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = connection.Close() })
|
||||
databaseService, err := gorm.Open(postgres.New(postgres.Config{Conn: connection}), &gorm.Config{DryRun: true})
|
||||
if err != nil {
|
||||
t.Fatalf("打开 GORM 失败:%v", err)
|
||||
}
|
||||
point := models.DeliveryBasic{Entity: models.Entity{ID: 22}, GasBasicID: 11}
|
||||
statement := deliveryStaffQuery(databaseService, point).
|
||||
Where("identity = ?", "staff-identity").Find(&models.StaffAccount{}).Statement.SQL.String()
|
||||
for _, required := range []string{"gas_basic_id", "delivery_basic_id", "role_code"} {
|
||||
if !strings.Contains(statement, required) {
|
||||
t.Fatalf("配送人员范围缺少 %s:%s", required, statement)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user