Make contractor residence optional with visibility rules
- Make residence_id nullable in contractor model - Add created_by_id field to track contractor creator - Update access control: personal contractors visible only to creator, residence contractors visible to all residence users - Add database migration for schema changes - Update admin panel DTOs and handlers for optional residence - Fix test utilities for new model structure 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -42,14 +42,28 @@ func (r *ContractorRepository) FindByResidence(residenceID uint) ([]models.Contr
|
||||
}
|
||||
|
||||
// FindByUser finds all contractors accessible to a user
|
||||
func (r *ContractorRepository) FindByUser(residenceIDs []uint) ([]models.Contractor, error) {
|
||||
// Returns contractors that either:
|
||||
// 1. Have no residence (personal contractors) AND were created by the user
|
||||
// 2. Belong to a residence the user has access to
|
||||
func (r *ContractorRepository) FindByUser(userID uint, residenceIDs []uint) ([]models.Contractor, error) {
|
||||
var contractors []models.Contractor
|
||||
err := r.db.Preload("CreatedBy").
|
||||
query := r.db.Preload("CreatedBy").
|
||||
Preload("Specialties").
|
||||
Preload("Residence").
|
||||
Where("residence_id IN ? AND is_active = ?", residenceIDs, true).
|
||||
Order("is_favorite DESC, name ASC").
|
||||
Find(&contractors).Error
|
||||
Where("is_active = ?", true)
|
||||
|
||||
if len(residenceIDs) > 0 {
|
||||
// Personal contractors (no residence, created by user) OR residence contractors
|
||||
query = query.Where(
|
||||
"(residence_id IS NULL AND created_by_id = ?) OR (residence_id IN ?)",
|
||||
userID, residenceIDs,
|
||||
)
|
||||
} else {
|
||||
// Only personal contractors
|
||||
query = query.Where("residence_id IS NULL AND created_by_id = ?", userID)
|
||||
}
|
||||
|
||||
err := query.Order("is_favorite DESC, name ASC").Find(&contractors).Error
|
||||
return contractors, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user