79 lines
3.0 KiB
Go
79 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"
|
|
)
|
|
|
|
// ChannelTwilioSMSService implements business logic for Twilio SMS channel operations.
|
|
// Reference: Twilio SMS API — https://www.twilio.com/docs/sms
|
|
//
|
|
// Twilio SMS channels use the Twilio Programmable Messaging API to send/receive
|
|
// SMS messages via a Twilio phone number. The Account SID + Auth Token grant
|
|
// messaging permissions for Twilio interactions.
|
|
type ChannelTwilioSMSService struct {
|
|
repo *repository.ChannelTwilioSMSRepo
|
|
}
|
|
|
|
// NewChannelTwilioSMSService creates a new ChannelTwilioSMS service.
|
|
func NewChannelTwilioSMSService(repo *repository.ChannelTwilioSMSRepo) *ChannelTwilioSMSService {
|
|
return &ChannelTwilioSMSService{repo: repo}
|
|
}
|
|
|
|
// GetByID retrieves a Twilio SMS channel by ID.
|
|
func (s *ChannelTwilioSMSService) GetByID(ctx context.Context, id uint) (*channelmodel.ChannelTwilioSMS, error) {
|
|
return s.repo.FindByID(ctx, id)
|
|
}
|
|
|
|
// GetByAccountSID retrieves a Twilio SMS channel by Twilio Account SID.
|
|
func (s *ChannelTwilioSMSService) GetByAccountSID(ctx context.Context, accountSID string) (*channelmodel.ChannelTwilioSMS, error) {
|
|
return s.repo.FindByAccountSID(ctx, accountSID)
|
|
}
|
|
|
|
// GetByPhoneNumber retrieves a Twilio SMS channel by phone number.
|
|
func (s *ChannelTwilioSMSService) GetByPhoneNumber(ctx context.Context, phoneNumber string) (*channelmodel.ChannelTwilioSMS, error) {
|
|
return s.repo.FindByPhoneNumber(ctx, phoneNumber)
|
|
}
|
|
|
|
// Create inserts a new Twilio SMS channel record.
|
|
func (s *ChannelTwilioSMSService) Create(ctx context.Context, ch *channelmodel.ChannelTwilioSMS) error {
|
|
if err := s.repo.Create(ctx, ch); err != nil {
|
|
applogger.L().Errorf("Failed to create Twilio SMS channel: %v", err)
|
|
return err
|
|
}
|
|
applogger.L().Infof("Twilio SMS channel created: id=%d phone=%s", ch.ID, ch.PhoneNumber)
|
|
return nil
|
|
}
|
|
|
|
// Update modifies an existing Twilio SMS channel.
|
|
func (s *ChannelTwilioSMSService) Update(ctx context.Context, ch *channelmodel.ChannelTwilioSMS) error {
|
|
if err := s.repo.Update(ctx, ch); err != nil {
|
|
applogger.L().Errorf("Failed to update Twilio SMS channel: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Delete soft-deletes a Twilio SMS channel.
|
|
func (s *ChannelTwilioSMSService) Delete(ctx context.Context, id uint) error {
|
|
if err := s.repo.Delete(ctx, id); err != nil {
|
|
applogger.L().Errorf("Failed to delete Twilio SMS channel: %v", err)
|
|
return err
|
|
}
|
|
applogger.L().Infof("Twilio SMS channel deleted: id=%d", id)
|
|
return nil
|
|
}
|
|
|
|
// List retrieves all Twilio SMS channels.
|
|
func (s *ChannelTwilioSMSService) List(ctx context.Context) ([]channelmodel.ChannelTwilioSMS, error) {
|
|
return s.repo.List(ctx)
|
|
}
|
|
|
|
// ListByAccount retrieves all Twilio SMS channels for a given account.
|
|
func (s *ChannelTwilioSMSService) ListByAccount(ctx context.Context, accountID uint) ([]channelmodel.ChannelTwilioSMS, error) {
|
|
return s.repo.ListByAccount(ctx, accountID)
|
|
}
|