Files
gochat/backend/internal/service/channel_microsoft_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

94 lines
3.7 KiB
Go

package service
import (
"context"
"fmt"
channelmodel "github.com/gochat/gochat/internal/model/channel"
"github.com/gochat/gochat/internal/repository"
applogger "github.com/gochat/gochat/pkg/logger"
)
// ChannelMicrosoftService implements business logic for Microsoft channel operations.
// This is a gochat addition — Chatwoot does not have a native Microsoft channel.
type ChannelMicrosoftService struct {
repo *repository.ChannelMicrosoftRepo
}
// NewChannelMicrosoftService creates a new ChannelMicrosoft service.
func NewChannelMicrosoftService(repo *repository.ChannelMicrosoftRepo) *ChannelMicrosoftService {
return &ChannelMicrosoftService{repo: repo}
}
// GetByID retrieves a Microsoft channel by ID.
func (s *ChannelMicrosoftService) GetByID(ctx context.Context, id uint) (*channelmodel.ChannelMicrosoft, error) {
return s.repo.FindByID(ctx, id)
}
// GetByInboxID retrieves a Microsoft channel by inbox ID.
func (s *ChannelMicrosoftService) GetByInboxID(ctx context.Context, inboxID uint) (*channelmodel.ChannelMicrosoft, error) {
return s.repo.FindByInboxID(ctx, inboxID)
}
// GetByAccountAndInboxID retrieves a Microsoft channel scoped to an account and inbox.
func (s *ChannelMicrosoftService) GetByAccountAndInboxID(ctx context.Context, accountID, inboxID uint) (*channelmodel.ChannelMicrosoft, error) {
return s.repo.FindByAccountAndInboxID(ctx, accountID, inboxID)
}
// GetByMicrosoftUserID retrieves a Microsoft channel by Microsoft user ID.
func (s *ChannelMicrosoftService) GetByMicrosoftUserID(ctx context.Context, microsoftUserID string) (*channelmodel.ChannelMicrosoft, error) {
return s.repo.FindByMicrosoftUserID(ctx, microsoftUserID)
}
// Create creates a new Microsoft channel record.
func (s *ChannelMicrosoftService) Create(ctx context.Context, ch *channelmodel.ChannelMicrosoft) error {
if err := s.repo.Create(ctx, ch); err != nil {
applogger.L().Errorf("Failed to create microsoft channel: %v", err)
return fmt.Errorf("failed to create microsoft channel: %w", err)
}
applogger.L().Infof("Microsoft channel created (id=%d, inbox_id=%d)", ch.ID, ch.InboxID)
return nil
}
// Update updates a Microsoft channel record.
func (s *ChannelMicrosoftService) Update(ctx context.Context, ch *channelmodel.ChannelMicrosoft) error {
if err := s.repo.Update(ctx, ch); err != nil {
applogger.L().Errorf("Failed to update microsoft channel: %v", err)
return fmt.Errorf("failed to update microsoft channel: %w", err)
}
applogger.L().Infof("Microsoft channel updated (id=%d)", ch.ID)
return nil
}
// Delete soft-deletes a Microsoft channel record.
func (s *ChannelMicrosoftService) Delete(ctx context.Context, id uint) error {
if err := s.repo.Delete(ctx, id); err != nil {
applogger.L().Errorf("Failed to delete microsoft channel: %v", err)
return err
}
applogger.L().Infof("Microsoft channel deleted (id=%d)", id)
return nil
}
// ListByAccount retrieves all Microsoft channels for an account.
func (s *ChannelMicrosoftService) ListByAccount(ctx context.Context, accountID uint) ([]channelmodel.ChannelMicrosoft, error) {
return s.repo.ListByAccount(ctx, accountID)
}
// MarkReauthorizationRequired marks a Microsoft channel as needing reauthorization.
func (s *ChannelMicrosoftService) MarkReauthorizationRequired(ctx context.Context, inboxID uint) error {
ch, err := s.repo.FindByInboxID(ctx, inboxID)
if err != nil {
return fmt.Errorf("microsoft channel not found: %w", err)
}
ch.ReauthorizationRequired = true
if err := s.repo.Update(ctx, ch); err != nil {
applogger.L().Errorf("Failed to mark microsoft channel for reauthorization: %v", err)
return err
}
applogger.L().Infof("Microsoft channel marked for reauthorization (inbox_id=%d)", inboxID)
return nil
}