fix: align business V2 fields and page interactions
This commit is contained in:
@@ -98,13 +98,10 @@ func validateAccount(req *dto.BusinessAccountRequest, create bool) (*int, error)
|
||||
if req == nil {
|
||||
return nil, business.ErrAccountInput
|
||||
}
|
||||
if create && req.HireDate == nil {
|
||||
return nil, business.ErrMemberInput
|
||||
}
|
||||
if req.Phone != nil && !validMemberPhone(*req.Phone) {
|
||||
return nil, business.ErrMemberInput
|
||||
}
|
||||
if req.HireDate != nil {
|
||||
if req.HireDate != nil && (strings.TrimSpace(*req.HireDate) != "" || !create) {
|
||||
if _, err := time.Parse("2006-01-02", *req.HireDate); err != nil {
|
||||
return nil, business.ErrMemberInput
|
||||
}
|
||||
@@ -158,7 +155,10 @@ func (s *AccountService) Create(ctx context.Context, req *dto.BusinessAccountReq
|
||||
}
|
||||
a := &business.Account{Username: stringValue(req.Username), NickName: stringValue(req.NickName), RealName: stringValue(req.RealName), Phone: stringValue(req.Phone),
|
||||
Email: stringValue(req.Email), QQNumber: stringValue(req.QQNumber), Password: req.Password, RejectReason: stringValue(req.RejectReason)}
|
||||
a.HireDate = stringValue(req.HireDate)
|
||||
a.HireDate = strings.TrimSpace(stringValue(req.HireDate))
|
||||
if a.HireDate == "" {
|
||||
a.HireDate = time.Now().In(time.FixedZone("Asia/Shanghai", 8*3600)).Format("2006-01-02")
|
||||
}
|
||||
if status != nil {
|
||||
a.Status = *status
|
||||
}
|
||||
|
||||
@@ -6,8 +6,28 @@ import (
|
||||
"kra/internal/biz/business"
|
||||
"kra/internal/service/dto"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCreateMemberDefaultsOptionalHireDate(t *testing.T) {
|
||||
f := &accountUsecaseFake{}
|
||||
s := &AccountService{uc: f}
|
||||
username, name := "newmember", "负责人"
|
||||
for _, date := range []*string{nil, new(string)} {
|
||||
_, err := s.Create(context.Background(), &dto.BusinessAccountRequest{Username: &username, RealName: &name, Password: "abc123", HireDate: date})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f.created.HireDate != time.Now().In(time.FixedZone("Asia/Shanghai", 8*3600)).Format("2006-01-02") {
|
||||
t.Fatalf("default date: %s", f.created.HireDate)
|
||||
}
|
||||
}
|
||||
invalid := "2026-02-30"
|
||||
if _, err := s.Create(context.Background(), &dto.BusinessAccountRequest{Username: &username, RealName: &name, Password: "abc123", HireDate: &invalid}); err == nil {
|
||||
t.Fatal("invalid explicit hire date accepted")
|
||||
}
|
||||
}
|
||||
|
||||
type accountUsecaseFake struct {
|
||||
AccountUsecase
|
||||
created *business.Account
|
||||
@@ -68,14 +88,15 @@ func TestAccountQueryEmptyFiltersAndPageBounds(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateBusinessRequiresHireDateAndValidPhone(t *testing.T) {
|
||||
func TestCreateBusinessDefaultsHireDateAndValidatesPhone(t *testing.T) {
|
||||
f := &accountUsecaseFake{}
|
||||
s := &AccountService{uc: f}
|
||||
name, owner, phone, hire := "member", "负责人", "abc", "2026-11-01"
|
||||
req := &dto.BusinessAccountRequest{Username: &name, RealName: &owner, Password: "test-password"}
|
||||
if _, err := s.Create(context.Background(), req); err != business.ErrMemberInput || f.created != nil {
|
||||
t.Fatal("missing hire date must not reach usecase")
|
||||
if _, err := s.Create(context.Background(), req); err != nil || f.created.HireDate == "" {
|
||||
t.Fatal("missing hire date should default to today")
|
||||
}
|
||||
f.created = nil
|
||||
req.HireDate, req.Phone = &hire, &phone
|
||||
if _, err := s.Create(context.Background(), req); err != business.ErrMemberInput || f.created != nil {
|
||||
t.Fatal("invalid phone must not reach usecase")
|
||||
|
||||
Reference in New Issue
Block a user