Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

fix(contacts): pass workspaceID to RecordSent/RecordFailed so contacts are saved with correct workspace scope

Jonas Kaninda7fdfd87a99c1
diff --git a/internal/services/email/service.go b/internal/services/email/service.go
index 3e125f3..a1d7e27 100644
--- a/internal/services/email/service.go
+++ b/internal/services/email/service.go
@@ -686,7 +686,7 @@ func (s *Service) sendSync(em *models.Email, userID uint, workspaceID *uint, req
s.onFailed()
}
if s.contactRepo != nil {
- go s.contactRepo.RecordFailed(userID, req.To)
+ go s.contactRepo.RecordFailed(userID, workspaceID, req.To)
}
return nil, fmt.Errorf("failed to send email: %w", err)
}
@@ -700,7 +700,7 @@ func (s *Service) sendSync(em *models.Email, userID uint, workspaceID *uint, req
s.onSent()
}
if s.contactRepo != nil {
- go s.contactRepo.RecordSent(userID, req.To)
+ go s.contactRepo.RecordSent(userID, workspaceID, req.To)
}
return &SendResponse{ID: em.UUID, Status: em.Status}, nil
diff --git a/internal/storage/repositories/contact_repo.go b/internal/storage/repositories/contact_repo.go
index 1a6ee82..4b21080 100644
--- a/internal/storage/repositories/contact_repo.go
+++ b/internal/storage/repositories/contact_repo.go
@@ -37,16 +37,17 @@ func NewContactRepository(db *gorm.DB) *ContactRepository {
// RecordSent upserts contacts for the given recipients, incrementing sent_count.
// Recipients may use RFC 5322 format ("Display Name <email>"), in which case
// the name and bare email are extracted.
-func (r *ContactRepository) RecordSent(userID uint, recipients []string) {
+func (r *ContactRepository) RecordSent(userID uint, workspaceID *uint, recipients []string) {
now := time.Now()
for _, raw := range recipients {
addr, name := parseRecipient(raw)
contact := models.Contact{
- UserID: userID,
- Email: addr,
- Name: name,
- SentCount: 1,
- LastSentAt: &now,
+ UserID: userID,
+ WorkspaceID: workspaceID,
+ Email: addr,
+ Name: name,
+ SentCount: 1,
+ LastSentAt: &now,
}
updates := map[string]interface{}{
"sent_count": gorm.Expr("contacts.sent_count + 1"),
@@ -56,21 +57,22 @@ func (r *ContactRepository) RecordSent(userID uint, recipients []string) {
updates["name"] = name
}
r.db.Clauses(clause.OnConflict{
- Columns: []clause.Column{{Name: "user_id"}, {Name: "email"}},
- DoUpdates: clause.Assignments(updates),
+ OnConstraint: "idx_user_email",
+ DoUpdates: clause.Assignments(updates),
}).Create(&contact)
}
}
// RecordFailed upserts contacts for the given recipients, incrementing fail_count.
-func (r *ContactRepository) RecordFailed(userID uint, recipients []string) {
+func (r *ContactRepository) RecordFailed(userID uint, workspaceID *uint, recipients []string) {
for _, raw := range recipients {
addr, name := parseRecipient(raw)
contact := models.Contact{
- UserID: userID,
- Email: addr,
- Name: name,
- FailCount: 1,
+ UserID: userID,
+ WorkspaceID: workspaceID,
+ Email: addr,
+ Name: name,
+ FailCount: 1,
}
updates := map[string]interface{}{
"fail_count": gorm.Expr("contacts.fail_count + 1"),
@@ -79,8 +81,8 @@ func (r *ContactRepository) RecordFailed(userID uint, recipients []string) {
updates["name"] = name
}
r.db.Clauses(clause.OnConflict{
- Columns: []clause.Column{{Name: "user_id"}, {Name: "email"}},
- DoUpdates: clause.Assignments(updates),
+ OnConstraint: "idx_user_email",
+ DoUpdates: clause.Assignments(updates),
}).Create(&contact)
}
}
diff --git a/internal/worker/handler.go b/internal/worker/handler.go
index dd3f81b..3ab9bf3 100644
--- a/internal/worker/handler.go
+++ b/internal/worker/handler.go
@@ -222,7 +222,7 @@ func (h *EmailSendHandler) ProcessTask(ctx context.Context, t *asynq.Task) error
h.onSent()
}
if h.contactRepo != nil {
- go h.contactRepo.RecordSent(em.UserID, em.Recipients)
+ go h.contactRepo.RecordSent(em.UserID, em.WorkspaceID, em.Recipients)
}
logger.Info("worker: email sent successfully", "id", em.ID)
@@ -241,7 +241,7 @@ func (h *EmailSendHandler) markFailed(em *models.Email, reason string, sharedSer
h.onFailed()
}
if h.contactRepo != nil {
- go h.contactRepo.RecordFailed(em.UserID, em.Recipients)
+ go h.contactRepo.RecordFailed(em.UserID, em.WorkspaceID, em.Recipients)
}
}