ContactAddressController.java

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

  2. import io.swagger.v3.oas.annotations.Operation;
  3. import io.swagger.v3.oas.annotations.responses.ApiResponse;
  4. import io.swagger.v3.oas.annotations.tags.Tag;
  5. import lombok.RequiredArgsConstructor;
  6. import lombok.extern.slf4j.Slf4j;
  7. import no.nav.data.common.exceptions.NotFoundException;
  8. import no.nav.data.common.exceptions.ValidationException;
  9. import no.nav.data.common.rest.RestResponsePage;
  10. import no.nav.data.team.contact.domain.SlackChannel;
  11. import no.nav.data.team.contact.domain.SlackUser;
  12. import no.nav.data.team.contact.dto.ContactAddressResponse;
  13. import no.nav.data.team.integration.slack.SlackClient;
  14. import no.nav.data.team.team.TeamService;
  15. import no.nav.data.team.team.domain.Team;
  16. import org.springframework.http.HttpStatus;
  17. import org.springframework.http.ResponseEntity;
  18. import org.springframework.web.bind.annotation.GetMapping;
  19. import org.springframework.web.bind.annotation.PathVariable;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RestController;

  22. import java.util.UUID;
  23. import java.util.stream.Collectors;
  24. import java.util.stream.Stream;

  25. @Slf4j
  26. @RestController
  27. @RequiredArgsConstructor
  28. @RequestMapping("/contactaddress")
  29. @Tag(name = "Contact Address", description = "REST API for Contact Address and slack")
  30. public class ContactAddressController {

  31.     private final SlackClient slackClient;
  32.     private final TeamService service;

  33.     @Operation(summary = "Get ContactAddress for team")
  34.     @ApiResponse(description = "ok")
  35.     @GetMapping("/team/{id}")
  36.     public ResponseEntity<RestResponsePage<ContactAddressResponse>> getContactAddressesByTeamId(@PathVariable UUID id) {
  37.         log.info("Get ContactAddress Team id={}", id);
  38.         Team team = service.get(id);
  39.         return ResponseEntity.ok(new RestResponsePage<>(team.getContactAddresses()).convert(contactAddress -> contactAddress.toResponse(slackClient)));
  40.     }

  41.     // Slack

  42.     @Operation(summary = "Search slack channels")
  43.     @ApiResponse(description = "Channels fetched")
  44.     @GetMapping("/slack/channel/search/{name}")
  45.     public ResponseEntity<RestResponsePage<SlackChannel>> searchSlackChannel(@PathVariable String name) {
  46.         log.info("Slack channel search '{}'", name);
  47.         validateLen(name);
  48.         var channels = slackClient.searchChannel(name);
  49.         log.info("Returned {} channels", channels.size());
  50.         return new ResponseEntity<>(new RestResponsePage<>(channels), HttpStatus.OK);
  51.     }

  52.     @Operation(summary = "Get slack channel")
  53.     @ApiResponse(description = "Channel fetched")
  54.     @GetMapping("/slack/channel/{id}")
  55.     public ResponseEntity<SlackChannel> getSlackChannel(@PathVariable String id) {
  56.         log.info("Slack channel '{}'", id);
  57.         var channel = slackClient.getChannel(id);
  58.         if (channel == null) {
  59.             throw new NotFoundException("no channel for id " + id);
  60.         }
  61.         return new ResponseEntity<>(channel, HttpStatus.OK);
  62.     }

  63.     @Operation(summary = "Get slack user by email")
  64.     @ApiResponse(description = "User fetched")
  65.     @GetMapping("/slack/user/email/{email}")
  66.     public ResponseEntity<SlackUser> getSlackUserByEmail(@PathVariable String email) {
  67.         log.info("Slack user email '{}'", email);
  68.         var user = slackClient.getUserByEmail(email);
  69.         if (user == null) {
  70.             throw new NotFoundException("no user for email " + email);
  71.         }
  72.         return new ResponseEntity<>(user, HttpStatus.OK);
  73.     }

  74.     @Operation(summary = "Get slack user by id")
  75.     @ApiResponse(description = "User fetched")
  76.     @GetMapping("/slack/user/id/{id}")
  77.     public ResponseEntity<SlackUser> getSlackUserById(@PathVariable String id) {
  78.         log.info("Slack user id '{}'", id);
  79.         var user = slackClient.getUserBySlackId(id);
  80.         if (user == null) {
  81.             throw new NotFoundException("no user for id " + id);
  82.         }
  83.         return new ResponseEntity<>(user, HttpStatus.OK);
  84.     }

  85.     private void validateLen(String name) {
  86.         if (Stream.of(name.split(" ")).sorted().distinct().collect(Collectors.joining("")).length() < 3) {
  87.             throw new ValidationException("Search must be at least 3 characters");
  88.         }
  89.     }
  90. }