Files
gochat/backend/internal/service/email_channel_migration_service.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

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
}