Skip to content

Commit 5eb80ec

Browse files
committed
fix(clients): avoid duplicate ClientRecord when email is changed on edit
SyncInbound (invoked from UpdateInboundClient during a client edit) looks up the ClientRecord row by the email present in the inbound's settings. After an email change the lookup misses the original row, hits the gorm.ErrRecordNotFound branch, and inserts a fresh ClientRecord with the new email. The original row stays in place with its inbound link cleared, so the clients list shows both — the original as an orphan and the new one as if it had just been created. Rename the existing ClientRecord row to the new email up front, before the inbound loop runs. SyncInbound then finds and updates the same row instead of creating a duplicate. A pre-check rejects renames that would collide with another client's email so the unique index keeps its meaning.
1 parent 313d041 commit 5eb80ec

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

web/service/client.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,27 @@ func (s *ClientService) Update(inboundSvc *InboundService, id int, updated model
595595
updated.CreatedAt = existing.CreatedAt
596596
}
597597

598+
// Rename the ClientRecord row up front when the email changes. SyncInbound
599+
// (invoked from UpdateInboundClient below) looks up by email — without
600+
// renaming first it would treat the new email as a brand-new client,
601+
// insert a duplicate ClientRecord, and leave the original orphaned.
602+
if updated.Email != existing.Email {
603+
var collisionCount int64
604+
if err := database.GetDB().Model(&model.ClientRecord{}).
605+
Where("email = ? AND id <> ?", updated.Email, id).
606+
Count(&collisionCount).Error; err != nil {
607+
return false, err
608+
}
609+
if collisionCount > 0 {
610+
return false, common.NewError("Duplicate email:", updated.Email)
611+
}
612+
if err := database.GetDB().Model(&model.ClientRecord{}).
613+
Where("id = ?", id).
614+
Update("email", updated.Email).Error; err != nil {
615+
return false, err
616+
}
617+
}
618+
598619
needRestart := false
599620
for _, ibId := range inboundIds {
600621
inbound, getErr := inboundSvc.GetInbound(ibId)

0 commit comments

Comments
 (0)