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
|
|
}
|