140 lines
5.2 KiB
Go
140 lines
5.2 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// AgentCapacityPolicyRepo implements GORM repository for AgentCapacityPolicy.
|
|
// Reference: Chatwoot AgentCapacityPolicy model
|
|
type AgentCapacityPolicyRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewAgentCapacityPolicyRepo creates a new AgentCapacityPolicy repository.
|
|
func NewAgentCapacityPolicyRepo(db *gorm.DB) *AgentCapacityPolicyRepo {
|
|
return &AgentCapacityPolicyRepo{db: db}
|
|
}
|
|
|
|
// FindByID retrieves an agent capacity policy by primary key.
|
|
func (r *AgentCapacityPolicyRepo) FindByID(ctx context.Context, id uint) (*model.AgentCapacityPolicy, error) {
|
|
var policy model.AgentCapacityPolicy
|
|
err := r.db.WithContext(ctx).
|
|
Preload("InboxCapacityLimits").
|
|
Preload("AccountUsers").
|
|
First(&policy, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &policy, nil
|
|
}
|
|
|
|
// FindByAccount retrieves all agent capacity policies for an account with pagination.
|
|
func (r *AgentCapacityPolicyRepo) FindByAccount(ctx context.Context, accountID uint, offset, limit int) ([]model.AgentCapacityPolicy, int64, error) {
|
|
var policies []model.AgentCapacityPolicy
|
|
var total int64
|
|
|
|
q := r.db.WithContext(ctx).Where("account_id = ?", accountID)
|
|
if err := q.Model(&model.AgentCapacityPolicy{}).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err := q.Preload("InboxCapacityLimits").Preload("AccountUsers").
|
|
Offset(offset).Limit(limit).
|
|
Order("id ASC").Find(&policies).Error
|
|
return policies, total, err
|
|
}
|
|
|
|
// FindInboxByAccount retrieves an inbox scoped to an account.
|
|
func (r *AgentCapacityPolicyRepo) FindInboxByAccount(ctx context.Context, accountID, inboxID uint) (*model.Inbox, error) {
|
|
var inbox model.Inbox
|
|
if err := r.db.WithContext(ctx).Where("account_id = ? AND id = ?", accountID, inboxID).First(&inbox).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &inbox, nil
|
|
}
|
|
|
|
// CreateInboxCapacityLimit creates a per-inbox capacity limit under a policy.
|
|
func (r *AgentCapacityPolicyRepo) CreateInboxCapacityLimit(ctx context.Context, limit *model.InboxCapacityLimit) error {
|
|
return r.db.WithContext(ctx).Create(limit).Error
|
|
}
|
|
|
|
// FindInboxCapacityLimitByPolicy retrieves an inbox capacity limit scoped to a policy.
|
|
func (r *AgentCapacityPolicyRepo) FindInboxCapacityLimitByPolicy(ctx context.Context, policyID, limitID uint) (*model.InboxCapacityLimit, error) {
|
|
var limit model.InboxCapacityLimit
|
|
if err := r.db.WithContext(ctx).
|
|
Preload("Inbox").
|
|
Where("agent_capacity_policy_id = ? AND id = ?", policyID, limitID).
|
|
First(&limit).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &limit, nil
|
|
}
|
|
|
|
// FindInboxCapacityLimitByInbox retrieves an inbox capacity limit scoped by policy and inbox.
|
|
func (r *AgentCapacityPolicyRepo) FindInboxCapacityLimitByInbox(ctx context.Context, policyID, inboxID uint) (*model.InboxCapacityLimit, error) {
|
|
var limit model.InboxCapacityLimit
|
|
if err := r.db.WithContext(ctx).
|
|
Where("agent_capacity_policy_id = ? AND inbox_id = ?", policyID, inboxID).
|
|
First(&limit).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &limit, nil
|
|
}
|
|
|
|
// UpdateInboxCapacityLimit updates an inbox capacity limit.
|
|
func (r *AgentCapacityPolicyRepo) UpdateInboxCapacityLimit(ctx context.Context, limit *model.InboxCapacityLimit) error {
|
|
return r.db.WithContext(ctx).Save(limit).Error
|
|
}
|
|
|
|
// DeleteInboxCapacityLimit deletes an inbox capacity limit.
|
|
func (r *AgentCapacityPolicyRepo) DeleteInboxCapacityLimit(ctx context.Context, limit *model.InboxCapacityLimit) error {
|
|
return r.db.WithContext(ctx).Delete(limit).Error
|
|
}
|
|
|
|
// FindPolicyUsers retrieves users assigned to a capacity policy via account_users.
|
|
func (r *AgentCapacityPolicyRepo) FindPolicyUsers(ctx context.Context, accountID, policyID uint) ([]model.User, error) {
|
|
var users []model.User
|
|
err := r.db.WithContext(ctx).
|
|
Joins("JOIN account_users ON account_users.user_id = users.id").
|
|
Where("account_users.account_id = ? AND account_users.agent_capacity_policy_id = ?", accountID, policyID).
|
|
Order("users.id ASC").
|
|
Find(&users).Error
|
|
return users, err
|
|
}
|
|
|
|
// FindAccountUser retrieves a user's membership in an account.
|
|
func (r *AgentCapacityPolicyRepo) FindAccountUser(ctx context.Context, accountID, userID uint) (*model.AccountUser, error) {
|
|
var accountUser model.AccountUser
|
|
if err := r.db.WithContext(ctx).
|
|
Preload("User").
|
|
Where("account_id = ? AND user_id = ?", accountID, userID).
|
|
First(&accountUser).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &accountUser, nil
|
|
}
|
|
|
|
// UpdateAccountUser updates an account membership.
|
|
func (r *AgentCapacityPolicyRepo) UpdateAccountUser(ctx context.Context, accountUser *model.AccountUser) error {
|
|
return r.db.WithContext(ctx).Save(accountUser).Error
|
|
}
|
|
|
|
// Create creates a new agent capacity policy.
|
|
func (r *AgentCapacityPolicyRepo) Create(ctx context.Context, policy *model.AgentCapacityPolicy) error {
|
|
return r.db.WithContext(ctx).Create(policy).Error
|
|
}
|
|
|
|
// Update updates an existing agent capacity policy.
|
|
func (r *AgentCapacityPolicyRepo) Update(ctx context.Context, policy *model.AgentCapacityPolicy) error {
|
|
return r.db.WithContext(ctx).Save(policy).Error
|
|
}
|
|
|
|
// Delete deletes an agent capacity policy by ID.
|
|
func (r *AgentCapacityPolicyRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.AgentCapacityPolicy{}, id).Error
|
|
}
|