Files
gochat/internal/service/channel_tiktok_service.go
T
2026-06-04 15:44:48 +08:00

78 lines
3.0 KiB
Go

package service
import (
"context"
channelmodel "github.com/gochat/gochat/internal/model/channel"
"github.com/gochat/gochat/internal/repository"
applogger "github.com/gochat/gochat/pkg/logger"
)
// ChannelTikTokService implements business logic for TikTok channel operations.
// Reference: TikTok Business API — https://business-api.tiktok.com/portal/docs
//
// TikTok Business channels use the TikTok Business Messaging API to send/receive
// messages via a connected TikTok Business account. The access token grants
// messaging permissions for TikTok Direct Message interactions.
type ChannelTikTokService struct {
repo *repository.ChannelTikTokRepo
}
// NewChannelTikTokService creates a new ChannelTikTok service.
func NewChannelTikTokService(repo *repository.ChannelTikTokRepo) *ChannelTikTokService {
return &ChannelTikTokService{repo: repo}
}
// GetByID retrieves a TikTok channel by ID.
func (s *ChannelTikTokService) GetByID(ctx context.Context, id uint) (*channelmodel.ChannelTikTok, error) {
return s.repo.FindByID(ctx, id)
}
// GetByInboxID retrieves a TikTok channel by inbox ID.
func (s *ChannelTikTokService) GetByInboxID(ctx context.Context, inboxID uint) (*channelmodel.ChannelTikTok, error) {
return s.repo.FindByInboxID(ctx, inboxID)
}
// GetByAccountAndInboxID retrieves a TikTok channel scoped to an account and inbox.
func (s *ChannelTikTokService) GetByAccountAndInboxID(ctx context.Context, accountID, inboxID uint) (*channelmodel.ChannelTikTok, error) {
return s.repo.FindByAccountAndInboxID(ctx, accountID, inboxID)
}
// FindByTikTokBusinessID retrieves a TikTok channel by TikTok Business ID.
func (s *ChannelTikTokService) FindByTikTokBusinessID(ctx context.Context, businessID string) (*channelmodel.ChannelTikTok, error) {
return s.repo.FindByTikTokBusinessID(ctx, businessID)
}
// Create inserts a new TikTok channel record.
func (s *ChannelTikTokService) Create(ctx context.Context, ch *channelmodel.ChannelTikTok) error {
if err := s.repo.Create(ctx, ch); err != nil {
applogger.L().Errorf("Failed to create TikTok channel: %v", err)
return err
}
applogger.L().Infof("TikTok channel created: id=%d business_id=%s", ch.ID, ch.TikTokBusinessID)
return nil
}
// Update modifies an existing TikTok channel.
func (s *ChannelTikTokService) Update(ctx context.Context, ch *channelmodel.ChannelTikTok) error {
if err := s.repo.Update(ctx, ch); err != nil {
applogger.L().Errorf("Failed to update TikTok channel: %v", err)
return err
}
return nil
}
// Delete soft-deletes a TikTok channel.
func (s *ChannelTikTokService) Delete(ctx context.Context, id uint) error {
if err := s.repo.Delete(ctx, id); err != nil {
applogger.L().Errorf("Failed to delete TikTok channel: %v", err)
return err
}
applogger.L().Infof("TikTok channel deleted: id=%d", id)
return nil
}
// ListByAccount retrieves all TikTok channels for an account.
func (s *ChannelTikTokService) ListByAccount(ctx context.Context, accountID uint) ([]channelmodel.ChannelTikTok, error) {
return s.repo.ListByAccount(ctx, accountID)
}