feat: profit sharing

This commit is contained in:
Efril
2026-08-05 19:28:38 +07:00
parent 2b80c92caa
commit b9ac97178f
26 changed files with 1378 additions and 4 deletions
+30
View File
@@ -53,6 +53,18 @@ func (p *CategoryProcessorImpl) CreateCategory(ctx context.Context, req *models.
return nil, fmt.Errorf("category with name '%s' already exists for this organization", req.Name)
}
var parentName *string
if req.ParentID != nil {
parentCategory, err := p.categoryRepo.GetByID(ctx, *req.ParentID)
if err != nil {
return nil, fmt.Errorf("parent category not found: %w", err)
}
if parentCategory.OrganizationID != req.OrganizationID {
return nil, fmt.Errorf("parent category must belong to the same organization")
}
parentName = &parentCategory.Name
}
// Map request to entity
categoryEntity := mappers.CreateCategoryRequestToEntity(req)
@@ -63,6 +75,7 @@ func (p *CategoryProcessorImpl) CreateCategory(ctx context.Context, req *models.
// Map entity to response model
response := mappers.CategoryEntityToResponse(categoryEntity)
response.ParentName = parentName
return response, nil
}
@@ -84,6 +97,23 @@ func (p *CategoryProcessorImpl) UpdateCategory(ctx context.Context, id uuid.UUID
}
}
if req.ParentID != nil {
if *req.ParentID == id {
return nil, fmt.Errorf("category cannot be its own parent")
}
parentCategory, err := p.categoryRepo.GetByID(ctx, *req.ParentID)
if err != nil {
return nil, fmt.Errorf("parent category not found: %w", err)
}
if parentCategory.OrganizationID != existingCategory.OrganizationID {
return nil, fmt.Errorf("parent category must belong to the same organization")
}
// Refresh the preloaded association so the response carries the new parent
existingCategory.Parent = parentCategory
}
// Apply updates to entity
mappers.UpdateCategoryEntityFromRequest(existingCategory, req)