package repository import ( "context" "gorm.io/gorm" "github.com/gochat/gochat/internal/autoassignment" ) // AssignmentPolicyRepo implements GORM repository for AssignmentPolicy. // Reference: Chatwoot app/models/assignment_policy.rb type AssignmentPolicyRepo struct { db *gorm.DB } // NewAssignmentPolicyRepo creates a new AssignmentPolicy repository. func NewAssignmentPolicyRepo(db *gorm.DB) *AssignmentPolicyRepo { return &AssignmentPolicyRepo{db: db} } // Create creates a new assignment policy. func (r *AssignmentPolicyRepo) Create(ctx context.Context, policy *autoassignment.AssignmentPolicy) error { return r.db.WithContext(ctx).Create(policy).Error } // FindByID retrieves an assignment policy by primary key. func (r *AssignmentPolicyRepo) FindByID(ctx context.Context, id uint) (*autoassignment.AssignmentPolicy, error) { var policy autoassignment.AssignmentPolicy if err := r.db.WithContext(ctx).First(&policy, id).Error; err != nil { return nil, err } return &policy, nil } // GetByID retrieves an assignment policy by primary key (alias for FindByID matching service expectations). func (r *AssignmentPolicyRepo) GetByID(ctx context.Context, id uint) (*autoassignment.AssignmentPolicy, error) { return r.FindByID(ctx, id) } // FindByAccount retrieves the active assignment policy for an account. func (r *AssignmentPolicyRepo) FindByAccount(ctx context.Context, accountID uint) (*autoassignment.AssignmentPolicy, error) { var policy autoassignment.AssignmentPolicy if err := r.db.WithContext(ctx). Where("account_id = ? AND active = ?", accountID, true). First(&policy).Error; err != nil { return nil, err } return &policy, nil } // Update updates an assignment policy. func (r *AssignmentPolicyRepo) Update(ctx context.Context, policy *autoassignment.AssignmentPolicy) error { return r.db.WithContext(ctx).Save(policy).Error } // Delete soft-deletes an assignment policy. func (r *AssignmentPolicyRepo) Delete(ctx context.Context, id uint) error { return r.db.WithContext(ctx).Delete(&autoassignment.AssignmentPolicy{}, id).Error } // InboxAssignmentPolicyRepo implements GORM repository for InboxAssignmentPolicy. // Reference: Chatwoot app/models/inbox_assignment_policy.rb type InboxAssignmentPolicyRepo struct { db *gorm.DB } // NewInboxAssignmentPolicyRepo creates a new InboxAssignmentPolicy repository. func NewInboxAssignmentPolicyRepo(db *gorm.DB) *InboxAssignmentPolicyRepo { return &InboxAssignmentPolicyRepo{db: db} } // Create creates a new inbox assignment policy. func (r *InboxAssignmentPolicyRepo) Create(ctx context.Context, policy *autoassignment.InboxAssignmentPolicy) error { return r.db.WithContext(ctx).Create(policy).Error } // FindByID retrieves an inbox assignment policy by primary key. func (r *InboxAssignmentPolicyRepo) FindByID(ctx context.Context, id uint) (*autoassignment.InboxAssignmentPolicy, error) { var policy autoassignment.InboxAssignmentPolicy if err := r.db.WithContext(ctx).First(&policy, id).Error; err != nil { return nil, err } return &policy, nil } // FindByInbox retrieves the active assignment policy override for an inbox. func (r *InboxAssignmentPolicyRepo) FindByInbox(ctx context.Context, accountID, inboxID uint) (*autoassignment.InboxAssignmentPolicy, error) { var policy autoassignment.InboxAssignmentPolicy if err := r.db.WithContext(ctx). Where("account_id = ? AND inbox_id = ? AND active = ?", accountID, inboxID, true). First(&policy).Error; err != nil { return nil, err } return &policy, nil } // Update updates an inbox assignment policy. func (r *InboxAssignmentPolicyRepo) Update(ctx context.Context, policy *autoassignment.InboxAssignmentPolicy) error { return r.db.WithContext(ctx).Save(policy).Error } // Delete soft-deletes an inbox assignment policy. func (r *InboxAssignmentPolicyRepo) Delete(ctx context.Context, id uint) error { return r.db.WithContext(ctx).Delete(&autoassignment.InboxAssignmentPolicy{}, id).Error }