From fe56b91b76869c08a0068ddd87c3665a0864abc7 Mon Sep 17 00:00:00 2001 From: Janaina <jsk22@inf.ufpr.br> Date: Mon, 24 Mar 2025 11:15:03 -0300 Subject: [PATCH] Issue #23/CREATE-contact-email nter the commit message for your changes. Lines starting --- src/index.ts | 2 + src/routes/contact.route.ts | 26 ++++++++++++ src/services/auth.service.ts | 1 - src/services/contact.service.ts | 74 +++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/routes/contact.route.ts create mode 100644 src/services/contact.service.ts diff --git a/src/index.ts b/src/index.ts index 099d6b7..810bd7f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,6 +45,7 @@ import { publicCommentsReplyRoute, commentReplyRouter } from './routes/comment-r import { publicUserCollectionsRoutes, userCollectionsRoute } from './routes/user-collection.route' import { publicS3, s3Routes } from './routes/s3.route' import { homologationRouter } from './routes/homologation.route' +import { contactRoute } from './routes/contact.route' @@ -122,6 +123,7 @@ app .route('/replyComment', publicCommentsReplyRoute) .route('/userCollections', publicUserCollectionsRoutes) .route('/s3', publicS3) + .route('/contact', contactRoute) //rotas que precisam de token app .basePath('/api') diff --git a/src/routes/contact.route.ts b/src/routes/contact.route.ts new file mode 100644 index 0000000..7905c50 --- /dev/null +++ b/src/routes/contact.route.ts @@ -0,0 +1,26 @@ +import { ContactService } from "@/services/contact.service"; +import { zValidator } from "@hono/zod-validator"; +import { Hono } from "hono"; +import Container from "typedi"; +import { z } from "zod"; + +const contactService = Container.get(ContactService) + +export const contactRoute = new Hono() + .post('/contact', + zValidator('json', z.object({ + name: z.string().min(1, 'Name is required'), + email: z.string().email(), + message: z.string().min(10, 'Message should be at least 10 characters long'), + })) + , async (c) => { + try { + const { name, email, message } = await c.req.json(); + + await contactService.sendContactEmail(email, message, name) + return c.json({ status: 'success', name, email, message }); + } catch (error) { + console.error('Error sending contact email:', error); + return c.json({ error: 'Invalid request data' }, 400); + } + }); \ No newline at end of file diff --git a/src/services/auth.service.ts b/src/services/auth.service.ts index 527e436..30d51df 100644 --- a/src/services/auth.service.ts +++ b/src/services/auth.service.ts @@ -195,7 +195,6 @@ export class AuthService { html: text }); console.log("E-mail enviado:", response); - console.log(userEmail) } catch (error) { console.error("Erro ao enviar e-mail:", error); } diff --git a/src/services/contact.service.ts b/src/services/contact.service.ts new file mode 100644 index 0000000..6c7eccd --- /dev/null +++ b/src/services/contact.service.ts @@ -0,0 +1,74 @@ +import { Service } from "typedi"; +import transporter from "@/mailer"; + +@Service() +export class ContactService { + async sendContactEmail(email: string, text: string, name: string) { + const message = ` + <head> + <style> + + .container { + display: block; + text-align: left; + background-color: rgb(240, 240, 240); + width: 90%; + margin: 0 auto; + padding: 30px 30px; + border-radius: 30%, + font-family: Arial, Helvetica, sans-serif; + } + + button { + background-color: #00bacc; + color: white; + border: none; + border-radius: 5px; + padding: 10px; + font-size: 16px; + cursor: pointer; + } + + .container_msg { + text-align: left; + padding: 0px 20px 0px 20px; + background-color: white; + } + + button:hover { + background-color: rgb(7, 29, 65, 0.8); + } + span { + color: #e74c3c; + font-weight: bold; + } + + </style> + </head> + + <body> + <div class="container" > + <a href="${process.env["URL"]}"></a> + <h1 style="color:#00bacc; font-size: 30px; font-weight: 800; margin-top: 5px; margin-bottom: 5px">Contato de ${name}</h1> + <br> + <p>${text}</p> + </div> + </body> + ` + + try { + const response = await transporter.sendMail({ + from: email, + to: "mecred@c3sl.ufpr.br", + subject: "Contato MECRED", + html: message + }); + console.log("E-mail enviado:", response); + console.log() + } catch (error) { + console.error("Erro ao enviar e-mail:", error); + } + } + + +} \ No newline at end of file -- GitLab