86 lines
2.7 KiB
Go
86 lines
2.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// CustomRoleRepo implements GORM repository for CustomRole.
|
|
// Reference: Chatwoot enterprise/app/models/custom_role.rb
|
|
type CustomRoleRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewCustomRoleRepo creates a new CustomRole repository.
|
|
func NewCustomRoleRepo(db *gorm.DB) *CustomRoleRepo {
|
|
return &CustomRoleRepo{db: db}
|
|
}
|
|
|
|
// FindByID retrieves a custom role by primary key, including soft-deleted records.
|
|
func (r *CustomRoleRepo) FindByID(ctx context.Context, id uint) (*model.CustomRole, error) {
|
|
var role model.CustomRole
|
|
if err := r.db.WithContext(ctx).Unscoped().First(&role, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &role, nil
|
|
}
|
|
|
|
// FindByIDAndAccount retrieves a custom role scoped to an account, excluding soft-deleted.
|
|
func (r *CustomRoleRepo) FindByIDAndAccount(ctx context.Context, id, accountID uint) (*model.CustomRole, error) {
|
|
var role model.CustomRole
|
|
if err := r.db.WithContext(ctx).
|
|
Where("id = ? AND account_id = ? AND deleted_at IS NULL", id, accountID).
|
|
First(&role).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &role, nil
|
|
}
|
|
|
|
// FindByAccount retrieves all custom roles for an account with pagination.
|
|
// Filters by account_id AND deleted_at IS NULL to exclude soft-deleted records.
|
|
func (r *CustomRoleRepo) FindByAccount(ctx context.Context, accountID uint, offset, limit int) ([]model.CustomRole, int64, error) {
|
|
var roles []model.CustomRole
|
|
var total int64
|
|
|
|
if err := r.db.WithContext(ctx).Model(&model.CustomRole{}).
|
|
Where("account_id = ? AND deleted_at IS NULL", accountID).
|
|
Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if err := r.db.WithContext(ctx).
|
|
Where("account_id = ? AND deleted_at IS NULL", accountID).
|
|
Order("id ASC").Offset(offset).Limit(limit).
|
|
Find(&roles).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return roles, total, nil
|
|
}
|
|
|
|
// Create creates a new custom role.
|
|
func (r *CustomRoleRepo) Create(ctx context.Context, role *model.CustomRole) error {
|
|
return r.db.WithContext(ctx).Create(role).Error
|
|
}
|
|
|
|
// Update updates an existing custom role.
|
|
func (r *CustomRoleRepo) Update(ctx context.Context, role *model.CustomRole) error {
|
|
return r.db.WithContext(ctx).Save(role).Error
|
|
}
|
|
|
|
// Delete soft-deletes a custom role (GORM DeletedAt field).
|
|
func (r *CustomRoleRepo) Delete(ctx context.Context, id, accountID uint) error {
|
|
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Model(&model.AccountUser{}).
|
|
Where("account_id = ? AND custom_role_id = ?", accountID, id).
|
|
Updates(map[string]interface{}{"role": "agent", "custom_role_id": 0}).Error; err != nil {
|
|
return err
|
|
}
|
|
return tx.Where("id = ? AND account_id = ?", id, accountID).
|
|
Delete(&model.CustomRole{}).Error
|
|
})
|
|
}
|