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:
@@ -279,7 +279,7 @@ type CreateTaskRequest struct {
|
||||
|
||||
// CreateContractorRequest for creating a new contractor
|
||||
type CreateContractorRequest struct {
|
||||
ResidenceID uint `json:"residence_id" binding:"required"`
|
||||
ResidenceID *uint `json:"residence_id"`
|
||||
CreatedByID uint `json:"created_by_id" binding:"required"`
|
||||
Name string `json:"name" binding:"required,max=200"`
|
||||
Company string `json:"company" binding:"max=200"`
|
||||
|
||||
@@ -151,8 +151,8 @@ type TaskDetailResponse struct {
|
||||
// ContractorResponse represents a contractor in admin responses
|
||||
type ContractorResponse struct {
|
||||
ID uint `json:"id"`
|
||||
ResidenceID uint `json:"residence_id"`
|
||||
ResidenceName string `json:"residence_name"`
|
||||
ResidenceID *uint `json:"residence_id,omitempty"`
|
||||
ResidenceName string `json:"residence_name,omitempty"`
|
||||
CreatedByID uint `json:"created_by_id"`
|
||||
CreatedByName string `json:"created_by_name"`
|
||||
Name string `json:"name"`
|
||||
|
||||
@@ -149,7 +149,7 @@ func (h *AdminContractorHandler) Update(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Residence not found"})
|
||||
return
|
||||
}
|
||||
contractor.ResidenceID = *req.ResidenceID
|
||||
contractor.ResidenceID = req.ResidenceID
|
||||
}
|
||||
// Verify created_by if changing
|
||||
if req.CreatedByID != nil {
|
||||
@@ -229,11 +229,13 @@ func (h *AdminContractorHandler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Verify residence exists
|
||||
var residence models.Residence
|
||||
if err := h.db.First(&residence, req.ResidenceID).Error; err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Residence not found"})
|
||||
return
|
||||
// Verify residence exists if provided
|
||||
if req.ResidenceID != nil {
|
||||
var residence models.Residence
|
||||
if err := h.db.First(&residence, *req.ResidenceID).Error; err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Residence not found"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Verify created_by user exists
|
||||
@@ -342,7 +344,7 @@ func (h *AdminContractorHandler) toContractorResponse(contractor *models.Contrac
|
||||
UpdatedAt: contractor.UpdatedAt.Format("2006-01-02T15:04:05Z"),
|
||||
}
|
||||
|
||||
if contractor.Residence.ID != 0 {
|
||||
if contractor.Residence != nil && contractor.Residence.ID != 0 {
|
||||
response.ResidenceName = contractor.Residence.Name
|
||||
}
|
||||
if contractor.CreatedBy.ID != 0 {
|
||||
|
||||
Reference in New Issue
Block a user