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.
94 lines
3.5 KiB
Go
94 lines
3.5 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"
|
|
)
|
|
|
|
// ChannelGoogleService implements business logic for Google Chat channel operations.
|
|
// This is a gochat addition — Chatwoot does not have a native Google Chat channel.
|
|
type ChannelGoogleService struct {
|
|
repo *repository.ChannelGoogleRepo
|
|
}
|
|
|
|
// NewChannelGoogleService creates a new ChannelGoogle service.
|
|
func NewChannelGoogleService(repo *repository.ChannelGoogleRepo) *ChannelGoogleService {
|
|
return &ChannelGoogleService{repo: repo}
|
|
}
|
|
|
|
// GetByID retrieves a Google channel by ID.
|
|
func (s *ChannelGoogleService) GetByID(ctx context.Context, id uint) (*channelmodel.ChannelGoogle, error) {
|
|
return s.repo.FindByID(ctx, id)
|
|
}
|
|
|
|
// GetByInboxID retrieves a Google channel by inbox ID.
|
|
func (s *ChannelGoogleService) GetByInboxID(ctx context.Context, inboxID uint) (*channelmodel.ChannelGoogle, error) {
|
|
return s.repo.FindByInboxID(ctx, inboxID)
|
|
}
|
|
|
|
// GetByAccountAndInboxID retrieves a Google channel scoped to an account and inbox.
|
|
func (s *ChannelGoogleService) GetByAccountAndInboxID(ctx context.Context, accountID, inboxID uint) (*channelmodel.ChannelGoogle, error) {
|
|
return s.repo.FindByAccountAndInboxID(ctx, accountID, inboxID)
|
|
}
|
|
|
|
// GetByGoogleUserID retrieves a Google channel by Google user ID.
|
|
func (s *ChannelGoogleService) GetByGoogleUserID(ctx context.Context, googleUserID string) (*channelmodel.ChannelGoogle, error) {
|
|
return s.repo.FindByGoogleUserID(ctx, googleUserID)
|
|
}
|
|
|
|
// Create creates a new Google channel record.
|
|
func (s *ChannelGoogleService) Create(ctx context.Context, ch *channelmodel.ChannelGoogle) error {
|
|
if err := s.repo.Create(ctx, ch); err != nil {
|
|
applogger.L().Errorf("Failed to create google channel: %v", err)
|
|
return fmt.Errorf("failed to create google channel: %w", err)
|
|
}
|
|
applogger.L().Infof("Google channel created (id=%d, inbox_id=%d)", ch.ID, ch.InboxID)
|
|
return nil
|
|
}
|
|
|
|
// Update updates a Google channel record.
|
|
func (s *ChannelGoogleService) Update(ctx context.Context, ch *channelmodel.ChannelGoogle) error {
|
|
if err := s.repo.Update(ctx, ch); err != nil {
|
|
applogger.L().Errorf("Failed to update google channel: %v", err)
|
|
return fmt.Errorf("failed to update google channel: %w", err)
|
|
}
|
|
applogger.L().Infof("Google channel updated (id=%d)", ch.ID)
|
|
return nil
|
|
}
|
|
|
|
// Delete soft-deletes a Google channel record.
|
|
func (s *ChannelGoogleService) Delete(ctx context.Context, id uint) error {
|
|
if err := s.repo.Delete(ctx, id); err != nil {
|
|
applogger.L().Errorf("Failed to delete google channel: %v", err)
|
|
return err
|
|
}
|
|
applogger.L().Infof("Google channel deleted (id=%d)", id)
|
|
return nil
|
|
}
|
|
|
|
// ListByAccount retrieves all Google channels for an account.
|
|
func (s *ChannelGoogleService) ListByAccount(ctx context.Context, accountID uint) ([]channelmodel.ChannelGoogle, error) {
|
|
return s.repo.ListByAccount(ctx, accountID)
|
|
}
|
|
|
|
// MarkReauthorizationRequired marks a Google channel as needing reauthorization.
|
|
func (s *ChannelGoogleService) MarkReauthorizationRequired(ctx context.Context, inboxID uint) error {
|
|
ch, err := s.repo.FindByInboxID(ctx, inboxID)
|
|
if err != nil {
|
|
return fmt.Errorf("google channel not found: %w", err)
|
|
}
|
|
|
|
ch.ReauthorizationRequired = true
|
|
if err := s.repo.Update(ctx, ch); err != nil {
|
|
applogger.L().Errorf("Failed to mark google channel for reauthorization: %v", err)
|
|
return err
|
|
}
|
|
|
|
applogger.L().Infof("Google channel marked for reauthorization (inbox_id=%d)", inboxID)
|
|
return nil
|
|
}
|