94 lines
3.7 KiB
Go
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
|
|
}
|