55 lines
1.9 KiB
Go
55 lines
1.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
applogger "github.com/gochat/gochat/pkg/logger"
|
|
)
|
|
|
|
// EmailChannelMigrationService implements business logic for EmailChannelMigration operations.
|
|
// Reference: Chatwoot api/v1/accounts/:account_id/email_channel_migrations — create-only endpoint
|
|
// for initiating migration of an email channel from one inbox to another.
|
|
type EmailChannelMigrationService struct {
|
|
repo *repository.EmailChannelMigrationRepo
|
|
}
|
|
|
|
// NewEmailChannelMigrationService creates a new EmailChannelMigrationService.
|
|
func NewEmailChannelMigrationService(repo *repository.EmailChannelMigrationRepo) *EmailChannelMigrationService {
|
|
return &EmailChannelMigrationService{repo: repo}
|
|
}
|
|
|
|
// Create creates a new email channel migration record.
|
|
// Validates that account_id, inbox_id (source), and target_inbox_id are provided.
|
|
func (s *EmailChannelMigrationService) Create(ctx context.Context, migration *model.EmailChannelMigration) error {
|
|
if migration.AccountID == 0 {
|
|
return errors.New("account_id is required")
|
|
}
|
|
if migration.InboxID == 0 {
|
|
return errors.New("inbox_id is required")
|
|
}
|
|
if migration.TargetInboxID == 0 {
|
|
return errors.New("target_inbox_id is required")
|
|
}
|
|
// Default status to pending if not set
|
|
if migration.MigrationStatus == "" {
|
|
migration.MigrationStatus = "pending"
|
|
}
|
|
if err := s.repo.Create(ctx, migration); err != nil {
|
|
applogger.L().Errorf("EmailChannelMigrationService.Create error: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListByAccount retrieves all email channel migrations for a given account.
|
|
func (s *EmailChannelMigrationService) ListByAccount(ctx context.Context, accountID uint) ([]model.EmailChannelMigration, error) {
|
|
migrations, err := s.repo.FindByAccountID(ctx, accountID)
|
|
if err != nil {
|
|
applogger.L().Errorf("EmailChannelMigrationService.ListByAccount error: %v", err)
|
|
return nil, err
|
|
}
|
|
return migrations, nil
|
|
} |