Skip to content

Commit 88a3677

Browse files
committed
feat(clients): enforce unique subId per client like email
Reject creating or editing a client with a subId already owned by a different client, mirroring the email-uniqueness checks against client_records in Create and Update (BulkCreate inherits via Create). The old multi-inbound model duplicated a client across inbounds sharing one subId, so this check was dropped; the first-class multi-client model makes per-client subId uniqueness correct again. Existing duplicates are left untouched; only new/edited duplicates are blocked.
1 parent d2058f0 commit 88a3677

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

web/service/client.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,18 @@ func (s *ClientService) Create(inboundSvc *InboundService, payload *ClientCreate
475475
}
476476
}
477477

478+
if client.SubID != "" {
479+
var subTaken int64
480+
if err := database.GetDB().Model(&model.ClientRecord{}).
481+
Where("sub_id = ? AND email <> ?", client.SubID, client.Email).
482+
Count(&subTaken).Error; err != nil {
483+
return false, err
484+
}
485+
if subTaken > 0 {
486+
return false, common.NewError("subId already in use:", client.SubID)
487+
}
488+
}
489+
478490
needRestart := false
479491
for _, ibId := range payload.InboundIds {
480492
inbound, getErr := inboundSvc.GetInbound(ibId)
@@ -646,6 +658,18 @@ func (s *ClientService) Update(inboundSvc *InboundService, id int, updated model
646658
}
647659
}
648660

661+
if updated.SubID != "" {
662+
var subCollision int64
663+
if err := database.GetDB().Model(&model.ClientRecord{}).
664+
Where("sub_id = ? AND id <> ?", updated.SubID, id).
665+
Count(&subCollision).Error; err != nil {
666+
return false, err
667+
}
668+
if subCollision > 0 {
669+
return false, common.NewError("Duplicate subId:", updated.SubID)
670+
}
671+
}
672+
649673
needRestart := false
650674
for _, ibId := range inboundIds {
651675
inbound, getErr := inboundSvc.GetInbound(ibId)

0 commit comments

Comments
 (0)