ContactAddress.java

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

  2. import lombok.AllArgsConstructor;
  3. import lombok.Builder;
  4. import lombok.Data;
  5. import lombok.NoArgsConstructor;
  6. import lombok.experimental.FieldNameConstants;
  7. import no.nav.data.common.validator.Validated;
  8. import no.nav.data.common.validator.Validator;
  9. import no.nav.data.team.contact.dto.ContactAddressResponse;
  10. import no.nav.data.team.integration.slack.SlackClient;

  11. import static org.apache.commons.lang3.StringUtils.trimToNull;

  12. @Data
  13. @Builder
  14. @NoArgsConstructor
  15. @AllArgsConstructor
  16. @FieldNameConstants
  17. public class ContactAddress implements Validated {

  18.     private String address;
  19.     private Channel type;

  20.     @Override
  21.     public void format() {
  22.         setAddress(trimToNull(address));
  23.     }

  24.     @Override
  25.     public void validateFieldValues(Validator<?> validator) {
  26.         validator.checkNull(Fields.type, type);
  27.         validator.checkNull(Fields.address, address);
  28.         if (type == Channel.EPOST) {
  29.             validator.checkEmail(Fields.address, address);
  30.         }
  31.     }

  32.     public ContactAddressResponse toResponse(SlackClient slackClient) {
  33.         return ContactAddressResponse.builder()
  34.                 .address(address)
  35.                 .type(type)
  36.                 .slackChannel(type == Channel.SLACK ? slackClient.getChannel(address) : null)
  37.                 .slackUser(type == Channel.SLACK_USER ? slackClient.getUserBySlackId(address) : null)
  38.                 .build();
  39.     }
  40. }