Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
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
|
|
})
|
|
}
|