feat(ingredients): make units nullable

This commit is contained in:
efrilm
2026-08-11 21:20:17 +07:00
parent 9ae5be2c33
commit 0726fcecf0
11 changed files with 54 additions and 36 deletions
@@ -266,15 +266,27 @@ func (p *IngredientUnitConverterProcessorImpl) GetUnitsByIngredientID(ctx contex
return nil, fmt.Errorf("failed to get ingredient: %w", err)
}
// Get the base unit details
baseUnit, err := p.unitRepo.GetByID(ctx, ingredient.UnitID, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to get base unit: %w", err)
response := &models.IngredientUnitsResponse{
IngredientID: ingredientID,
IngredientName: ingredient.Name,
}
// Start with the base unit
units := []*models.UnitResponse{
mappers.MapUnitEntityToResponse(baseUnit),
units := make([]*models.UnitResponse, 0)
unitMap := make(map[uuid.UUID]bool)
// An ingredient does not necessarily have a unit assigned yet. When it has
// none there is no base unit to start from, so the only units on offer are
// the ones its converters mention.
if ingredient.UnitID != nil {
baseUnit, err := p.unitRepo.GetByID(ctx, *ingredient.UnitID, organizationID)
if err != nil {
return nil, fmt.Errorf("failed to get base unit: %w", err)
}
units = append(units, mappers.MapUnitEntityToResponse(baseUnit))
unitMap[baseUnit.ID] = true
response.BaseUnitID = &baseUnit.ID
response.BaseUnitName = baseUnit.Name
}
// Get all converters for this ingredient
@@ -283,10 +295,6 @@ func (p *IngredientUnitConverterProcessorImpl) GetUnitsByIngredientID(ctx contex
return nil, fmt.Errorf("failed to get converters: %w", err)
}
// Add unique units from converters
unitMap := make(map[uuid.UUID]bool)
unitMap[baseUnit.ID] = true
for _, converter := range converters {
if converter.IsActive {
// Add FromUnit if not already added
@@ -309,13 +317,7 @@ func (p *IngredientUnitConverterProcessorImpl) GetUnitsByIngredientID(ctx contex
}
}
response := &models.IngredientUnitsResponse{
IngredientID: ingredientID,
IngredientName: ingredient.Name,
BaseUnitID: baseUnit.ID,
BaseUnitName: baseUnit.Name,
Units: units,
}
response.Units = units
return response, nil
}