Skip to content

Commit

Permalink
fix(mailer): rework mailer code using brevo base code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihoub2 committed May 21, 2024
1 parent 8260ce1 commit c07df8b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 44 deletions.
7 changes: 7 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ server {
proxy_set_header Content-Type application/json;
client_max_body_size 10M;
}
location /mailer/ {
proxy_set_header accept application/json;
proxy_set_header api-key $VITE_BREVO_API_AUTHORIZATION;
proxy_set_header content-type application/json;
proxy_ssl_server_name on;
proxy_pass https://api.brevo.com/v3/smtp/email;
}
}
1 change: 0 additions & 1 deletion sib-api-v3-sdk.d.ts

This file was deleted.

79 changes: 36 additions & 43 deletions src/api/send-mail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import {
Text,
TextArea,
} from "@dataesr/dsfr-plus";
import SibApiV3Sdk from "sib-api-v3-sdk";
import { Contribution } from "../../types";
import { postHeaders } from "../../config/api";

function EmailSender({ contribution }: { contribution: Contribution }) {
const [emailSent, setEmailSent] = useState(false);
const [response, setResponse] = useState("");
const { VITE_BREVO_API_AUTHORIZATION } = import.meta.env;
const contributorName = contribution.name;
const [userResponse, setUserResponse] = useState("");
const basePath = window.location.pathname.includes("contact")
? "contact"
: "contribute";
Expand All @@ -28,60 +25,56 @@ function EmailSender({ contribution }: { contribution: Contribution }) {
setSelectedProfile(profileFromLocalStorage);
}
}, []);

const sendEmail = async () => {
try {
var defaultClient = SibApiV3Sdk.ApiClient.instance;
var apiKey = defaultClient.authentications["api-key"];
apiKey.apiKey = VITE_BREVO_API_AUTHORIZATION;

var apiInstance = new SibApiV3Sdk.TransactionalEmailsApi();
var sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail();

sendSmtpEmail.sender = {
name: `${selectedProfile} - de l'équipe scanR`,
email: "[email protected]",
};
sendSmtpEmail.to = [
const data = {
sender: {
email: "[email protected]",
name: "Debache ",
},
to: [
{
name: contributorName,
email: "debache.mihoub@gmail.com",
email: "[email protected]",
name: "Mihoub mihoub",
},
];
sendSmtpEmail.subject = "Réponse à votre contribution";
sendSmtpEmail.htmlContent = response;
],
subject: `${selectedProfile} de l'équipe scanR`,
htmlContent: userResponse,
};

await apiInstance.sendTransacEmail(sendSmtpEmail);
const response = await fetch("https://api.brevo.com/v3/smtp/email", {
method: "POST",
headers: {
"api-key": import.meta.env.VITE_BREVO_API_AUTHORIZATION,
},
body: JSON.stringify(data),
});

const data = {
comment: response,
};
const responsePatch = await fetch(`${window.location.origin}/api/${basePath}/${contribution._id}`, {
method: "PATCH",
headers: postHeaders,
body: JSON.stringify(data),
});
if (!responsePatch.ok) {
console.log("Erreur lors de la mise à jour de la contribution");
return;
}
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const responsePatch = await fetch(`/api/${basePath}/${contribution._id}`, {
method: "PATCH",
headers: postHeaders,
body: JSON.stringify(data),
});

setEmailSent(true);
} catch (error) {
console.error(
"Erreur lors de l'envoi de l'email ou de la mise à jour de la contribution:",
error
if (!responsePatch.ok) {
throw new Error(
`Erreur pendant la mise à jour de l'api ! status: ${response.status}`
);
}

setEmailSent(true);
};

return (
<Container>
<Row gutters>
<Col offsetMd="2" md="8">
<TextArea
value={response}
onChange={(e) => setResponse(e.target.value)}
value={userResponse}
onChange={(e) => setUserResponse(e.target.value)}
placeholder="Votre réponse..."
rows={2}
/>
Expand Down

0 comments on commit c07df8b

Please sign in to comment.