69 lines
2.5 KiB
Go
69 lines
2.5 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"
|
|
)
|
|
|
|
// ChannelTwilioService implements business logic for Twilio SMS channel operations.
|
|
// Reference: Twilio SMS API — https://www.twilio.com/docs/sms
|
|
//
|
|
// Twilio SMS channels use the Twilio API to send/receive SMS messages
|
|
// via a purchased Twilio phone number. The Account SID and Auth Token
|
|
// authenticate API calls, and the phone number serves as the sender identity.
|
|
type ChannelTwilioService struct {
|
|
repo *repository.ChannelTwilioRepo
|
|
}
|
|
|
|
// NewChannelTwilioService creates a new ChannelTwilio service.
|
|
func NewChannelTwilioService(repo *repository.ChannelTwilioRepo) *ChannelTwilioService {
|
|
return &ChannelTwilioService{repo: repo}
|
|
}
|
|
|
|
// Create creates a new Twilio SMS channel.
|
|
func (s *ChannelTwilioService) Create(ctx context.Context, channel *channelmodel.ChannelTwilioSMS) error {
|
|
if err := s.repo.Create(ctx, channel); err != nil {
|
|
applogger.L().Errorf("Failed to create Twilio channel: %v", err)
|
|
return err
|
|
}
|
|
applogger.L().Infof("Created Twilio channel with ID %d", channel.ID)
|
|
return nil
|
|
}
|
|
|
|
// GetByID retrieves a Twilio SMS channel by ID.
|
|
func (s *ChannelTwilioService) GetByID(ctx context.Context, id uint) (*channelmodel.ChannelTwilioSMS, error) {
|
|
return s.repo.FindByID(ctx, id)
|
|
}
|
|
|
|
// GetByInboxID retrieves a Twilio SMS channel by inbox ID.
|
|
func (s *ChannelTwilioService) GetByInboxID(ctx context.Context, inboxID uint) (*channelmodel.ChannelTwilioSMS, error) {
|
|
return s.repo.FindByInboxID(ctx, inboxID)
|
|
}
|
|
|
|
// Update updates a Twilio SMS channel.
|
|
func (s *ChannelTwilioService) Update(ctx context.Context, channel *channelmodel.ChannelTwilioSMS) error {
|
|
if err := s.repo.Update(ctx, channel); err != nil {
|
|
applogger.L().Errorf("Failed to update Twilio channel %d: %v", channel.ID, err)
|
|
return err
|
|
}
|
|
applogger.L().Infof("Updated Twilio channel with ID %d", channel.ID)
|
|
return nil
|
|
}
|
|
|
|
// Delete deletes a Twilio SMS channel by ID.
|
|
func (s *ChannelTwilioService) Delete(ctx context.Context, id uint) error {
|
|
if err := s.repo.Delete(ctx, id); err != nil {
|
|
applogger.L().Errorf("Failed to delete Twilio channel %d: %v", id, err)
|
|
return err
|
|
}
|
|
applogger.L().Infof("Deleted Twilio channel with ID %d", id)
|
|
return nil
|
|
}
|
|
|
|
// ListByAccount lists all Twilio SMS channels for an account.
|
|
func (s *ChannelTwilioService) ListByAccount(ctx context.Context, accountID uint) ([]channelmodel.ChannelTwilioSMS, error) {
|
|
return s.repo.ListByAccount(ctx, accountID)
|
|
} |