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
+7 -4
View File
@@ -27,8 +27,11 @@ func NewIngredientProcessor(ingredientRepo IngredientRepository, unitRepo UnitRe
}
func (p *IngredientProcessorImpl) CreateIngredient(ctx context.Context, req *models.CreateIngredientRequest) (*models.IngredientResponse, error) {
if _, err := p.unitRepo.GetByID(ctx, req.UnitID, req.OrganizationID); err != nil {
return nil, err
// The unit is optional, so it is only validated when one is supplied.
if req.UnitID != nil {
if _, err := p.unitRepo.GetByID(ctx, *req.UnitID, req.OrganizationID); err != nil {
return nil, err
}
}
ingredient := &entities.Ingredient{
@@ -107,8 +110,8 @@ func (p *IngredientProcessorImpl) UpdateIngredient(ctx context.Context, id uuid.
return nil, err
}
if req.UnitID != existing.UnitID {
if _, err := p.unitRepo.GetByID(ctx, req.UnitID, organizationID); err != nil {
if req.UnitID != nil && (existing.UnitID == nil || *req.UnitID != *existing.UnitID) {
if _, err := p.unitRepo.GetByID(ctx, *req.UnitID, organizationID); err != nil {
return nil, err
}
}