Files
gochat/internal/service/channel_email_service.go
T
2026-06-04 15:44:48 +08:00

74 lines
2.6 KiB
Go

package service
import (
"context"
channelmodel "github.com/gochat/gochat/internal/model/channel"
"github.com/gochat/gochat/internal/repository"
applogger "github.com/gochat/gochat/pkg/logger"
)
// ChannelEmailService implements business logic for Email channel operations.
// Reference: Chatwoot app/controllers/api/v1/accounts/channels/email_controller.rb
//
// Email channels use IMAP for inbound message polling and SMTP for outbound
// message delivery. Channel configuration includes IMAP/SMTP server settings,
// credentials, and SSL mode preferences.
type ChannelEmailService struct {
repo *repository.ChannelEmailRepo
}
// NewChannelEmailService creates a new ChannelEmail service.
func NewChannelEmailService(repo *repository.ChannelEmailRepo) *ChannelEmailService {
return &ChannelEmailService{repo: repo}
}
// GetByID retrieves an Email channel by ID.
func (s *ChannelEmailService) GetByID(ctx context.Context, id uint) (*channelmodel.ChannelEmail, error) {
return s.repo.FindByID(ctx, id)
}
// GetByInboxID retrieves an Email channel by inbox ID.
func (s *ChannelEmailService) GetByInboxID(ctx context.Context, inboxID uint) (*channelmodel.ChannelEmail, error) {
return s.repo.FindByInboxID(ctx, inboxID)
}
// GetByEmail retrieves an Email channel by email address.
func (s *ChannelEmailService) GetByEmail(ctx context.Context, email string) (*channelmodel.ChannelEmail, error) {
return s.repo.FindByEmail(ctx, email)
}
// Create inserts a new Email channel record.
func (s *ChannelEmailService) Create(ctx context.Context, ch *channelmodel.ChannelEmail) error {
if err := s.repo.Create(ctx, ch); err != nil {
applogger.L().Errorf("Failed to create Email channel: %v", err)
return err
}
applogger.L().Infof("Email channel created: id=%d email=%s", ch.ID, ch.Email)
return nil
}
// Update modifies an existing Email channel.
func (s *ChannelEmailService) Update(ctx context.Context, ch *channelmodel.ChannelEmail) error {
if err := s.repo.Update(ctx, ch); err != nil {
applogger.L().Errorf("Failed to update Email channel: %v", err)
return err
}
return nil
}
// Delete soft-deletes an Email channel.
func (s *ChannelEmailService) Delete(ctx context.Context, id uint) error {
if err := s.repo.Delete(ctx, id); err != nil {
applogger.L().Errorf("Failed to delete Email channel: %v", err)
return err
}
applogger.L().Infof("Email channel deleted: id=%d", id)
return nil
}
// ListByAccount retrieves all Email channels for an account.
func (s *ChannelEmailService) ListByAccount(ctx context.Context, accountID uint) ([]channelmodel.ChannelEmail, error) {
return s.repo.ListByAccount(ctx, accountID)
}