修复配送端合同与用户管理问题
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// 功能描述:实现配送点范围内的合同、合同气瓶和配送订单接口。
|
||||
// 版本:v1.4.0。
|
||||
// 版本:v1.4.1。
|
||||
package delivery
|
||||
|
||||
import (
|
||||
@@ -74,7 +74,8 @@ func ListContract(ctx *gin.Context) {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
var list []platformgasorder.ContractPartyDisplay
|
||||
// 空结果也初始化为空切片,确保公开响应保持 JSON 数组而不是 null。
|
||||
list := make([]platformgasorder.ContractPartyDisplay, 0)
|
||||
if err := query.Order("gasorder_contract.created_at desc").Offset((page - 1) * size).Limit(size).Scan(&list).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// 功能描述:验证配送合同附件列表脱敏与公开状态投影。
|
||||
// 版本:v1.0.0。
|
||||
// 版本:v1.1.0。
|
||||
package delivery
|
||||
|
||||
import (
|
||||
@@ -44,3 +44,22 @@ func TestProtectDeliveryContractListResponse(t *testing.T) {
|
||||
t.Fatal("无附件的合同必须返回 has_attachment=false")
|
||||
}
|
||||
}
|
||||
|
||||
// TestProtectDeliveryContractListEmptyResponse 验证没有可用合同时仍返回空数组。
|
||||
func TestProtectDeliveryContractListEmptyResponse(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
ctx.Request = httptest.NewRequest("GET", "/gasorder_contract?candidate=order", nil)
|
||||
contracts := make([]models.GasorderContract, 0)
|
||||
response, err := common.PublicResourceResponse(contracts)
|
||||
if err != nil {
|
||||
t.Fatalf("构造空合同公开响应失败: %v", err)
|
||||
}
|
||||
protected, err := protectDeliveryContractListResponse(ctx, response, contracts)
|
||||
if err != nil {
|
||||
t.Fatalf("空合同列表不应返回结构错误: %v", err)
|
||||
}
|
||||
if protected == nil || len(protected) != 0 {
|
||||
t.Fatalf("空合同列表必须返回非 nil 空数组,实际值: %#v", protected)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// 功能描述:实现配送点范围内的用户、服务关系和收货地址管理。
|
||||
// 版本:v1.1.0。
|
||||
// 版本:v1.2.0。
|
||||
package delivery
|
||||
|
||||
import (
|
||||
@@ -12,9 +12,25 @@ import (
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/upload"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const deliveryUserUsernameConstraint = "idx_user_account_username"
|
||||
|
||||
var errDeliveryUserUsernameExists = errors.New("用户名已存在,请更换用户名")
|
||||
|
||||
// deliveryUserCreateError 仅转换配送点新建用户时的用户名唯一约束冲突。
|
||||
// 参数:err 为用户和服务关系事务返回的错误。
|
||||
// 返回值:用户名冲突返回中文提示,其他错误保持原样。
|
||||
func deliveryUserCreateError(err error) error {
|
||||
var postgresError *pgconn.PgError
|
||||
if errors.As(err, &postgresError) && postgresError.Code == "23505" && postgresError.ConstraintName == deliveryUserUsernameConstraint {
|
||||
return errDeliveryUserUsernameExists
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func userQuery(pointID uint64) *gorm.DB {
|
||||
return common.ActiveRecords(db().Model(&models.UserAccount{})).
|
||||
Joins("JOIN user_service_relation ON user_service_relation.user_account_id = user_account.id AND user_service_relation.status <> ?",
|
||||
@@ -116,7 +132,7 @@ func CreateUser(ctx *gin.Context) {
|
||||
relation.UserAccountID = user.ID
|
||||
return tx.Create(&relation).Error
|
||||
}); err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
infra.Response.Error(ctx, deliveryUserCreateError(err))
|
||||
return
|
||||
}
|
||||
common.RespondCreatedResource(ctx, user)
|
||||
|
||||
33
backend/api/internal/logic/delivery/user_error_test.go
Normal file
33
backend/api/internal/logic/delivery/user_error_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// 功能描述:验证配送点新建用户的用户名冲突提示转换。
|
||||
// 版本:v1.0.0。
|
||||
package delivery
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
// TestDeliveryUserCreateErrorUsernameConflict 验证用户名唯一约束冲突返回指定中文提示。
|
||||
// 参数:t 为 Go 测试上下文。
|
||||
// 返回值:无。
|
||||
func TestDeliveryUserCreateErrorUsernameConflict(t *testing.T) {
|
||||
databaseError := &pgconn.PgError{Code: "23505", ConstraintName: deliveryUserUsernameConstraint}
|
||||
wrapped := fmt.Errorf("create delivery user: %w", databaseError)
|
||||
|
||||
if actual := deliveryUserCreateError(wrapped).Error(); actual != "用户名已存在,请更换用户名" {
|
||||
t.Fatalf("用户名冲突提示不正确,实际为 %q", actual)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeliveryUserCreateErrorKeepsOtherErrors 验证其他错误不被本次局部映射改变。
|
||||
// 参数:t 为 Go 测试上下文。
|
||||
// 返回值:无。
|
||||
func TestDeliveryUserCreateErrorKeepsOtherErrors(t *testing.T) {
|
||||
original := errors.New("other error")
|
||||
if actual := deliveryUserCreateError(original); !errors.Is(actual, original) {
|
||||
t.Fatalf("其他错误应保持原样,实际为 %v", actual)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user