From 021e05a8df604e9b764850f4736f8699e447e676 Mon Sep 17 00:00:00 2001 From: Davi Aliaga Date: Mon, 19 Oct 2026 11:37:32 -0300 Subject: [PATCH] feat/email: implementa cliente Brevo e template para envio de convite --- .../com/acta/common/client/BrevoClient.java | 72 +++++ .../resources/static/images/acta-logo.png | Bin 0 -> 1999 bytes .../templates/email/convite-usuario.html | 301 ++++++++++++++++++ 3 files changed, 373 insertions(+) create mode 100644 src/main/java/br/com/acta/common/client/BrevoClient.java create mode 100644 src/main/resources/static/images/acta-logo.png create mode 100644 src/main/resources/templates/email/convite-usuario.html 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 0000000000000000000000000000000000000000..3dda84619ba51c3b27aef16bca037fc0845ef407 GIT binary patch literal 1999 zcmV;=2Qc`FP)jG+@o6bA+qi3#W?Ac%rv z+wW~fMV$bAKgGZHhwxhk~l!T=aESUO{j#6pd`en~3h zW07eYUn~6Fo$5X@^PY#3+CbRRPRj-fG6=m9c!N z$Z><^E;=uqP^(ZC6%9^1+>i_ocZdV|p*jJ6{Xo@`6*AApx!6MM2DQLNHjCjRIQaKdr8YRamfGB1A zMx|V(!1*G6l_a!DP&XR*vX}yxNno99=y+ZhQB%_}NCYiBS+7bsv#qpF!c9Nj$^LSM17)_rVDYGLWI&k$xp*qS`LZL@{-PmGFdzT7@TO$VODoX@J|hn;UHPNh+`$93Ve^Gl=QON?3Qy8wV$ z2Tf@!dMi}`^EBt|+yF}g(}LaRKxN82N3Zc@yBz$A($eSz(!K}m5|~k;ItMsTZwb1# zn{$nla?jCj-*|A`!Iu~UX`KcIzwBE@lEV`mc2^>4XZy@nEEDD#&e{0~OcQi{JSOvr zs+4-PJ_}&F#9l9;W53kBqNHRPz*L38$cF;wsyNOPm^l<@j!RM-gx;HkfDkb4%~zqf zM{wGKhv~H<2{2PQ>a>H6#mt+9N#oNb6+w{U6DVE$sIDg_$WS0KFF19j-Lkc-I` zwx<%vvJ6n*RV;|ZPNM504%kKtIOya!XA8cb;;tE&#<<1=%^w1Gk+Y^s3?$1_6rEE| zES^IbC+KMJSe@IK%{w`{ygWY9Ve8@oL9(b33AL#lbx;^zECiv^0WC>sX=onc`|+73zVsIiq_S9fTJ=2&$)X1xyV4(A?w z!@dlH(gNz7;RUKC#`7u*$>AVQrKG*3GcXSQ#@XHJ3R0;|H_ZxmQd0Om(jc!RIccbUd?CjmTAPPOS>(T4!kw{nUrI5D(8 zUs=KWVZl9LaH!=_W`-ArB5uv6K2MSY#8;Ef}CmvC^tZ~ zNrLE00;lKInsoyc+|U?Gg$jV3L(Aq-<~iWVZCQ?Ey8*(H<=GiG;28*IKEDd0Ay~c< zvQRoIeEkGz)4R<15v>pY2G(Ko2&nUS8cD{$A-5`qp@_fW=eF#_rsR9}A0og1$Z$L0j9ux0LT|r;YQGA);SQc~>V#{TAj{S0 zUBuhj&g~Oa586noESrQH*c6iNJ;3nt^>RpJpoqK3c~Bur^{|dyikJy%Tn~g*yXsLG hfvk5L>~bU@^gq<~l+=WQ#Zv$P002ovPDHLkV1kb`v9ACC literal 0 HcmV?d00001 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