MemberController.java

  1. package no.nav.data.team.member;

  2. import io.swagger.v3.oas.annotations.Operation;
  3. import io.swagger.v3.oas.annotations.media.Content;
  4. import io.swagger.v3.oas.annotations.media.Schema;
  5. import io.swagger.v3.oas.annotations.responses.ApiResponse;
  6. import io.swagger.v3.oas.annotations.tags.Tag;
  7. import jakarta.servlet.http.HttpServletResponse;
  8. import lombok.extern.slf4j.Slf4j;
  9. import no.nav.data.common.TeamCatalogProps;
  10. import no.nav.data.common.exceptions.TechnicalException;
  11. import no.nav.data.common.exceptions.ValidationException;
  12. import no.nav.data.team.cluster.domain.Cluster;
  13. import no.nav.data.team.member.MemberExportService.SpreadsheetType;
  14. import no.nav.data.team.member.dto.MembershipResponse;
  15. import no.nav.data.team.po.domain.ProductArea;
  16. import no.nav.data.team.po.dto.ProductAreaResponse;
  17. import no.nav.data.team.resource.domain.ResourceRepository;
  18. import no.nav.data.team.team.domain.Team;
  19. import org.springframework.http.HttpHeaders;
  20. import org.springframework.http.ResponseEntity;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import org.springframework.util.StreamUtils;
  23. import org.springframework.web.bind.annotation.GetMapping;
  24. import org.springframework.web.bind.annotation.PathVariable;
  25. import org.springframework.web.bind.annotation.RequestMapping;
  26. import org.springframework.web.bind.annotation.RequestParam;
  27. import org.springframework.web.bind.annotation.RestController;

  28. import java.util.Optional;

  29. import static no.nav.data.common.export.ExcelBuilder.SPREADSHEETML_SHEET_MIME;
  30. import static no.nav.data.common.utils.StreamUtils.convert;

  31. @Slf4j
  32. @RestController
  33. @RequestMapping("/member")
  34. @Tag(name = "Member", description = "Member endpoint")
  35. public class MemberController {

  36.     private final ResourceRepository resourceRepository;
  37.     private final MemberExportService memberExportService;
  38.     private final TeamCatalogProps teamCatalogProps;

  39.     public MemberController(ResourceRepository resourceRepository, MemberExportService memberExportService, TeamCatalogProps teamCatalogProps) {
  40.         this.resourceRepository = resourceRepository;
  41.         this.memberExportService = memberExportService;
  42.         this.teamCatalogProps = teamCatalogProps;
  43.     }

  44.     @Operation(summary = "Get Memberships")
  45.     @ApiResponse(description = "ok")
  46.     @GetMapping("/membership/{id}")
  47.     public ResponseEntity<MembershipResponse> getAll(@PathVariable String id) {
  48.         log.info("Get memberships for navident {}",id);
  49.         var memberships = resourceRepository.findByMemberIdent(id);
  50.         return ResponseEntity.ok(new MembershipResponse(
  51.                 convert(memberships.teams(), Team::convertToResponse),
  52.                 convert(memberships.productAreas(), this::convertProductAreaToReponse),
  53.                 convert(memberships.clusters(), Cluster::convertToResponse)
  54.         ));
  55.     }

  56.     @Operation(summary = "Get export for members")
  57.     @ApiResponse(description = "Doc fetched", content = @Content(schema = @Schema(implementation = byte[].class)))
  58.     @Transactional(readOnly = true)
  59.     @GetMapping(value = "/export/{type}", produces = SPREADSHEETML_SHEET_MIME)
  60.     public void getExport(
  61.             HttpServletResponse response,
  62.             @PathVariable("type") SpreadsheetType type,
  63.             @RequestParam(name = "id", required = false) String id
  64.     ) {
  65.         log.info("Export spreadsheet {}, {}", type, id);
  66.         if (type != SpreadsheetType.ALL && id == null) {
  67.             throw new ValidationException("missing id for spreadsheet type " + type);
  68.         }
  69.         byte[] doc = memberExportService.generateSpreadsheet(type, id);
  70.         String filename = "resources_" + type + Optional.ofNullable(id).map(s -> "_" + s).orElse("") + ".xlsx";
  71.         response.setContentType(SPREADSHEETML_SHEET_MIME);
  72.         response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename);
  73.         writeDoc(response, doc);
  74.     }

  75.     private void writeDoc(HttpServletResponse response, byte[] doc) {
  76.         try {
  77.             StreamUtils.copy(doc, response.getOutputStream());
  78.             response.flushBuffer();
  79.         } catch (Exception e) {
  80.             throw new TechnicalException("io error", e);
  81.         }
  82.     }

  83.     private ProductAreaResponse convertProductAreaToReponse(ProductArea pa){
  84.         return pa.convertToResponse(teamCatalogProps.getDefaultProductareaUuid());
  85.     }


  86. }