diff --git a/src/main/java/br/com/acta/common/client/BrevoClient.java b/src/main/java/br/com/acta/common/client/BrevoClient.java new file mode 100644 index 0000000..e4583a6 --- /dev/null +++ b/src/main/java/br/com/acta/common/client/BrevoClient.java @@ -0,0 +1,72 @@ +package br.com.acta.common.client; + +import br.com.acta.common.handler.exception.BusinessRuleException; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ClassPathResource; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestClient; +import org.springframework.web.client.RestClientException; +import org.springframework.web.util.HtmlUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.Map; + +@Component +public class BrevoClient { + private static final RestClient REST_CLIENT = RestClient.create(); + + @Value("${BREVO_API_KEY:}") + private String apiKey; + + @Value("${BREVO_SENDER_EMAIL:}") + private String senderEmail; + + @Value("${EXTERNAL_URL:http://localhost:8080}") + private String url; + + public void enviarConvite(String email, String nome, OffsetDateTime expiraEm, String token) { + String html = carregarTemplate() + .replace("{{nome}}", HtmlUtils.htmlEscape(nome)) + .replace("{{expiraEm}}", expiraEm.format(DateTimeFormatter.ofPattern("dd/MM/yyyy 'às' HH:mm"))) + .replace("{{logoUrl}}", HtmlUtils.htmlEscape(url.replaceAll("/+$", "") + "/images/acta-logo.png")) + .replace("{{codigo}}", HtmlUtils.htmlEscape(token)); + + enviarEmail(email, html); + } + + private void enviarEmail(String email, String html) { + Map mensagem = montarMensagem(email, html); + + try { + REST_CLIENT.post() + .uri("https://api.brevo.com/v3/smtp/email").contentType(MediaType.APPLICATION_JSON) + .header("api-key", apiKey) + .body(mensagem).retrieve().toBodilessEntity(); + } catch (RestClientException rce) { + throw new BusinessRuleException("Não foi possível enviar o convite por e-mail"); + } + } + + private Map montarMensagem(String email, String html) { + return Map.of( + "sender", Map.of("name", "ACTA", "email", senderEmail), + "to", List.of(Map.of("email", email)), + "subject", "Convite para acessar o ACTA", + "htmlContent", html + ); + } + + private String carregarTemplate() { + try (InputStream input = new ClassPathResource("templates/email/" + "convite-usuario.html").getInputStream()) { + return new String(input.readAllBytes(), StandardCharsets.UTF_8); + } catch (IOException ioe) { + throw new BusinessRuleException("Não foi possível carregar o e-mail de convite"); + } + } +} \ No newline at end of file diff --git a/src/main/resources/static/images/acta-logo.png b/src/main/resources/static/images/acta-logo.png new file mode 100644 index 0000000..3dda846 Binary files /dev/null and b/src/main/resources/static/images/acta-logo.png differ diff --git a/src/main/resources/templates/email/convite-usuario.html b/src/main/resources/templates/email/convite-usuario.html new file mode 100644 index 0000000..3b83436 --- /dev/null +++ b/src/main/resources/templates/email/convite-usuario.html @@ -0,0 +1,301 @@ + + + + + + + Convite para acessar o ACTA + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + +
+
+ + \ No newline at end of file