232 lines
8.2 KiB
Go
232 lines
8.2 KiB
Go
package whatsapp
|
|
|
|
// Repository provides GORM-based persistence for WhatsApp channel data.
|
|
// Reference: Chatwoot ActiveRecord for ChannelWhatsapp with callbacks:
|
|
// - before_validation :ensure_valid_channel_attributes
|
|
// - after_create_commit :notify_channel_creation
|
|
// - after_update_commit :notify_channel_update
|
|
// - ChannelWhatsapp EDITABLE_ATTRS: phone_number, provider_config, message_templates
|
|
//
|
|
// Design: Repository pattern abstracts GORM operations, matching gochat's
|
|
// existing repository pattern used by Telegram and Facebook channel providers.
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
channelmodel "github.com/gochat/gochat/internal/model/channel"
|
|
"github.com/gochat/gochat/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
applogger "github.com/gochat/gochat/pkg/logger"
|
|
)
|
|
|
|
// Repository handles WhatsApp channel database operations.
|
|
type Repository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewRepository creates a WhatsApp repository with GORM DB.
|
|
func NewRepository(db *gorm.DB) *Repository {
|
|
return &Repository{db: db}
|
|
}
|
|
|
|
// === CRUD Operations ===
|
|
|
|
// Create persists a new ChannelWhatsApp record.
|
|
// Reference: Chatwoot ChannelWhatsapp.create with before_validation callbacks
|
|
func (r *Repository) Create(ctx context.Context, channel *channelmodel.ChannelWhatsApp) error {
|
|
if err := r.db.WithContext(ctx).Create(channel).Error; err != nil {
|
|
return fmt.Errorf("failed to create ChannelWhatsApp: %w", err)
|
|
}
|
|
|
|
applogger.L().Info("ChannelWhatsApp created",
|
|
"id", channel.ID,
|
|
"account_id", channel.AccountID,
|
|
"phone_number", channel.PhoneNumber,
|
|
"phone_number_id", channel.PhoneNumberID,
|
|
"provider", channel.Provider,
|
|
)
|
|
return nil
|
|
}
|
|
|
|
// Update modifies a ChannelWhatsApp record.
|
|
func (r *Repository) Update(ctx context.Context, channel *channelmodel.ChannelWhatsApp) error {
|
|
result := r.db.WithContext(ctx).Save(channel)
|
|
if result.Error != nil {
|
|
return fmt.Errorf("failed to update ChannelWhatsApp: %w", result.Error)
|
|
}
|
|
|
|
if result.RowsAffected == 0 {
|
|
return fmt.Errorf("ChannelWhatsApp not found (id=%d)", channel.ID)
|
|
}
|
|
|
|
applogger.L().Info("ChannelWhatsApp updated",
|
|
"id", channel.ID,
|
|
"phone_number", channel.PhoneNumber,
|
|
)
|
|
return nil
|
|
}
|
|
|
|
// UpdateAccessToken updates only the access_token field.
|
|
// Reference: Chatwoot's reauthorization flow updates page_access_token
|
|
func (r *Repository) UpdateAccessToken(ctx context.Context, channelID uint, newToken string) error {
|
|
result := r.db.WithContext(ctx).
|
|
Model(&channelmodel.ChannelWhatsApp{}).
|
|
Where("id = ?", channelID).
|
|
Update("access_token", newToken)
|
|
|
|
if result.Error != nil {
|
|
return fmt.Errorf("failed to update access_token: %w", result.Error)
|
|
}
|
|
|
|
if result.RowsAffected == 0 {
|
|
return fmt.Errorf("ChannelWhatsApp not found (id=%d)", channelID)
|
|
}
|
|
|
|
applogger.L().Info("ChannelWhatsApp access_token updated", "id", channelID)
|
|
return nil
|
|
}
|
|
|
|
// MarkReauthorizationRequired sets the reauthorization_required flag.
|
|
// Reference: Chatwoot's reauthorization_required flag when token/connection needs refresh
|
|
func (r *Repository) MarkReauthorizationRequired(ctx context.Context, channelID uint) error {
|
|
result := r.db.WithContext(ctx).
|
|
Model(&channelmodel.ChannelWhatsApp{}).
|
|
Where("id = ?", channelID).
|
|
Update("reauthorization_required", true)
|
|
|
|
if result.Error != nil {
|
|
return fmt.Errorf("failed to mark reauthorization_required: %w", result.Error)
|
|
}
|
|
|
|
applogger.L().Info("ChannelWhatsApp reauthorization_required flagged", "id", channelID)
|
|
return nil
|
|
}
|
|
|
|
// ClearReauthorizationRequired clears the reauthorization_required flag.
|
|
func (r *Repository) ClearReauthorizationRequired(ctx context.Context, channelID uint) error {
|
|
result := r.db.WithContext(ctx).
|
|
Model(&channelmodel.ChannelWhatsApp{}).
|
|
Where("id = ?", channelID).
|
|
Update("reauthorization_required", false)
|
|
|
|
if result.Error != nil {
|
|
return fmt.Errorf("failed to clear reauthorization_required: %w", result.Error)
|
|
}
|
|
|
|
applogger.L().Info("ChannelWhatsApp reauthorization_required cleared", "id", channelID)
|
|
return nil
|
|
}
|
|
|
|
// GetByID retrieves a ChannelWhatsApp by its primary key.
|
|
func (r *Repository) GetByID(ctx context.Context, id uint) (*channelmodel.ChannelWhatsApp, error) {
|
|
var channel channelmodel.ChannelWhatsApp
|
|
if err := r.db.WithContext(ctx).First(&channel, id).Error; err != nil {
|
|
return nil, fmt.Errorf("failed to get ChannelWhatsApp by id: %w", err)
|
|
}
|
|
return &channel, nil
|
|
}
|
|
|
|
// GetByPhoneNumberID retrieves a ChannelWhatsApp by phone_number_id.
|
|
// This is the primary lookup for webhook routing — WhatsApp Cloud API
|
|
// uses phone_number_id to identify the receiving business number.
|
|
func (r *Repository) GetByPhoneNumberID(ctx context.Context, phoneNumberID string) (*channelmodel.ChannelWhatsApp, error) {
|
|
var channel channelmodel.ChannelWhatsApp
|
|
if err := r.db.WithContext(ctx).
|
|
Where("phone_number_id = ?", phoneNumberID).
|
|
First(&channel).Error; err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, fmt.Errorf("ChannelWhatsApp not found for phone_number_id")
|
|
}
|
|
return nil, fmt.Errorf("failed to find ChannelWhatsApp by phone_number_id: %w", err)
|
|
}
|
|
return &channel, nil
|
|
}
|
|
|
|
// GetByInboxID retrieves the ChannelWhatsApp associated with an Inbox.
|
|
// Reference: Chatwoot: inbox.channel (polymorphic association)
|
|
func (r *Repository) GetByInboxID(ctx context.Context, inboxID uint) (*channelmodel.ChannelWhatsApp, error) {
|
|
var channel channelmodel.ChannelWhatsApp
|
|
if err := r.db.WithContext(ctx).Where("inbox_id = ?", inboxID).First(&channel).Error; err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, fmt.Errorf("ChannelWhatsApp not found for inbox_id=%d", inboxID)
|
|
}
|
|
return nil, fmt.Errorf("failed to find ChannelWhatsApp by inbox_id: %w", err)
|
|
}
|
|
return &channel, nil
|
|
}
|
|
|
|
// FindByAccountID retrieves all ChannelWhatsApp records for an account.
|
|
// Reference: Chatwoot scopes channels to account (account_id foreign key)
|
|
func (r *Repository) FindByAccountID(ctx context.Context, accountID uint) ([]channelmodel.ChannelWhatsApp, error) {
|
|
var channels []channelmodel.ChannelWhatsApp
|
|
if err := r.db.WithContext(ctx).
|
|
Where("account_id = ?", accountID).
|
|
Find(&channels).Error; err != nil {
|
|
return nil, fmt.Errorf("failed to find ChannelWhatsApp by account_id: %w", err)
|
|
}
|
|
return channels, nil
|
|
}
|
|
|
|
// Delete removes a ChannelWhatsApp record (soft delete via GORM DeletedAt).
|
|
func (r *Repository) Delete(ctx context.Context, id uint) error {
|
|
result := r.db.WithContext(ctx).Delete(&channelmodel.ChannelWhatsApp{}, id)
|
|
if result.Error != nil {
|
|
return fmt.Errorf("failed to delete ChannelWhatsApp: %w", result.Error)
|
|
}
|
|
|
|
if result.RowsAffected == 0 {
|
|
return fmt.Errorf("ChannelWhatsApp not found (id=%d)", id)
|
|
}
|
|
|
|
applogger.L().Info("ChannelWhatsApp deleted", "id", id)
|
|
return nil
|
|
}
|
|
|
|
// === Inbox Repository ===
|
|
|
|
// InboxRepository handles Inbox model operations for WhatsApp channels.
|
|
// Reference: Chatwoot creates Inbox alongside ChannelWhatsapp
|
|
type InboxRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewInboxRepository creates an Inbox repository.
|
|
func NewInboxRepository(db *gorm.DB) *InboxRepository {
|
|
return &InboxRepository{db: db}
|
|
}
|
|
|
|
// FindByPhoneNumberID finds the Inbox associated with a WhatsApp phone_number_id.
|
|
// This is the primary lookup for webhook routing.
|
|
// Reference: Chatwoot: Inbox.joins(:channel_whatsapp).find_by(channel_whatsapps: {phone_number_id: ...})
|
|
func (r *InboxRepository) FindByPhoneNumberID(ctx context.Context, phoneNumberID string) (*model.Inbox, error) {
|
|
var inbox model.Inbox
|
|
var waChannel channelmodel.ChannelWhatsApp
|
|
|
|
// First find the WhatsApp channel by phone_number_id
|
|
if err := r.db.WithContext(ctx).
|
|
Where("phone_number_id = ?", phoneNumberID).
|
|
First(&waChannel).Error; err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, fmt.Errorf("no WhatsApp channel found for phone_number_id")
|
|
}
|
|
return nil, fmt.Errorf("failed to lookup WhatsApp channel: %w", err)
|
|
}
|
|
|
|
// Then find the associated Inbox
|
|
if err := r.db.WithContext(ctx).First(&inbox, waChannel.InboxID).Error; err != nil {
|
|
return nil, fmt.Errorf("failed to find Inbox for WhatsApp channel: %w", err)
|
|
}
|
|
|
|
return &inbox, nil
|
|
}
|
|
|
|
// FindByID retrieves an Inbox by ID.
|
|
func (r *InboxRepository) FindByID(ctx context.Context, id uint) (*model.Inbox, error) {
|
|
var inbox model.Inbox
|
|
if err := r.db.WithContext(ctx).First(&inbox, id).Error; err != nil {
|
|
return nil, fmt.Errorf("failed to find Inbox by id: %w", err)
|
|
}
|
|
return &inbox, nil
|
|
} |