Untitled

 avatar
unknown
plain_text
17 days ago
1.1 kB
4
Indexable
func (s *sectorService) CreateSector(ctx context.Context, userID uuid.UUID,
    req dto.CreateSectorRequest) (*dto.SectorResponse, error) {

    if len(req.Indicators) > 0 {
        ids := extractIndicatorIDs(req.Indicators)
        indicators, err := s.frameworkRepo.ListIndicatorsByIDs(ctx, ids)
        if err != nil {
            return nil, err
        }
        if len(indicators) != len(req.Indicators) {
            return nil, apperror.NewBadRequest("one or more indicator IDs not found")
        }
    }

    err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
        sector := &models.Sector{
            Name: req.Name, Description: req.Description,
            Category: req.Category, Status: "active", CreatedBy: &userID,
        }
        if err := s.sectorRepo.Create(ctx, sector); err != nil {
            return err
        }
        if len(req.Indicators) > 0 {
            mats := buildMaterialities(sector.ID, &userID, req.Indicators)
            return s.materialityRepo.CreateBulk(ctx, mats)
        }
        return nil
    })
    ...
}
Editor is loading...
Leave a Comment