fix:刷新标记失效时追加备注而不是覆盖

This commit is contained in:
2026-11-09 11:24:32 +08:00
parent f38f11ae96
commit b90252c093
2 changed files with 15 additions and 6 deletions
@@ -229,7 +229,8 @@ func isRockTokenNotFoundError(err error) bool {
// markTokenAbnormalByRefresh 把刷新时确认已失效的 Token 标记为「已上报失效」。
//
// 备注策略:仅在原备注为空时写入系统原因,避免覆盖人工备注。
// 备注策略:原备注为空时直接写入;已有备注时用换行追加系统原因,保留人工内容,
// 且相同原因不会重复追加。
// 已交付(status=redeemed 或 redeemed_at 非空)的 Token 不会被降级。
func (s *RockService) markTokenAbnormalByRefresh(ctx context.Context, token *model.RockToken, remark, message string) (*TokenBindingRefreshResult, error) {
if token == nil {
@@ -243,8 +244,12 @@ func (s *RockService) markTokenAbnormalByRefresh(ctx context.Context, token *mod
"status": model.RockStatusAbnormal,
"updated_at": time.Now(),
}
if strings.TrimSpace(token.Remark) == "" {
existingRemark := strings.TrimSpace(token.Remark)
switch {
case existingRemark == "":
updates["remark"] = remark
case !strings.Contains(existingRemark, remark):
updates["remark"] = existingRemark + "\n" + remark
}
if err := s.db.WithContext(ctx).Model(token).Updates(updates).Error; err != nil {
return nil, err
@@ -515,8 +515,8 @@ func TestRefreshTokenBinding_Generic404KeepsState(t *testing.T) {
}
}
// 已有备注时不被系统原因覆盖。
func TestRefreshTokenBinding_AbnormalKeepsManualRemark(t *testing.T) {
// 已有备注时追加系统原因,不覆盖人工内容。
func TestRefreshTokenBinding_AbnormalAppendsManualRemark(t *testing.T) {
db := newRockTestDB(t)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
@@ -547,7 +547,11 @@ func TestRefreshTokenBinding_AbnormalKeepsManualRemark(t *testing.T) {
if err := db.First(&saved, token.ID).Error; err != nil {
t.Fatalf("读取 Token 失败: %v", err)
}
if saved.Status != model.RockStatusAbnormal || saved.Remark != "客服人工备注" {
t.Fatalf("应保留人工备注并标记 abnormal,实际 %+v", saved)
if saved.Status != model.RockStatusAbnormal {
t.Fatalf("应标记 abnormal,实际 %s", saved.Status)
}
wantRemark := "客服人工备注\n上游状态异常(刷新自动同步)"
if saved.Remark != wantRemark {
t.Fatalf("应追加系统原因并保留人工备注,实际 %q", saved.Remark)
}
}