Skip to content

Commit

Permalink
fix(mail): secure imap server, no request if no mail
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihoub2 committed Oct 8, 2024
1 parent 36c204e commit dcbf3d6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 55 deletions.
24 changes: 16 additions & 8 deletions client/src/components/items/staff-action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,14 @@ const StaffActions = ({
<>
{data?.threads?.length > 0 && (
<Col className={contributorClassName}>
<Toggle
label="Marquer toutes les réponses comme lues"
checked={isAllRead}
onChange={handleToggleChange}
/>
{/* Afficher le toggle uniquement s'il y a plus de 2 objets dans threads */}
{data.threads.length > 2 && (
<Toggle
label="Marquer toutes les réponses comme lues"
checked={isAllRead}
onChange={handleToggleChange}
/>
)}

{data.threads.map((thread, threadIndex) =>
thread.responses.map((response, index) => {
Expand Down Expand Up @@ -159,9 +162,14 @@ const StaffActions = ({
response.team.includes("user") ? "user-side" : "staffSide"
}
>
Réponse apportée par {responder} le {responseDate} à{" "}
{responseTime} :<br />
{response.responseMessage}
{responder && (
<>
Réponse apportée par {responder} le {responseDate} à{" "}
{responseTime} :
<br />
{response.responseMessage}
</>
)}
</Text>
</div>
);
Expand Down
107 changes: 60 additions & 47 deletions server/routes/receive-email/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ async function sendNotificationEmail(
);
}
}

export async function fetchEmails() {
const client = new ImapFlow({
host: mailHost!,
Expand All @@ -159,55 +158,69 @@ export async function fetchEmails() {
});

const emails = [];

for await (let message of messages) {
const messageSource = message.source.toString();
const date = message.envelope.date?.toISOString() || null;

const subject = message.envelope.subject || null;

const referenceMatch = subject?.match(
/référence\s+([a-zA-Z0-9_-]+)-([a-zA-Z0-9]+)/
);

let referenceId = null;
let collectionPrefix = null;

if (referenceMatch) {
collectionPrefix = referenceMatch[1];
referenceId = referenceMatch[2];
}

const startMarker = "Content-Transfer-Encoding: quoted-printable";
const endMarker = "Le ";

const startIndex =
messageSource.indexOf(startMarker) + startMarker.length;
const endIndex = messageSource.indexOf(endMarker);

let extractedContent = null;

if (startIndex !== -1 && endIndex !== -1) {
extractedContent = messageSource.substring(startIndex, endIndex).trim();
} else {
console.log("Les marqueurs de début ou de fin n'ont pas été trouvés.");
}

emails.push({
content: extractedContent,
date: formatDate(date),
referenceId,
});

if (referenceId && extractedContent) {
const collectionName = determineCollectionName(
collectionPrefix || "default"
try {
if (!message.envelope || !message.source) {
console.warn("Message sans enveloppe ou contenu, ignoré.");
continue;
}

const messageSource = message.source.toString();
const date = message.envelope.date?.toISOString() || null;
const subject = message.envelope.subject || null;

const referenceMatch = subject?.match(
/référence\s+([a-zA-Z0-9_-]+)-([a-zA-Z0-9]+)/
);
await updateContribution(
let referenceId = null;
let collectionPrefix = null;

if (referenceMatch) {
collectionPrefix = referenceMatch[1];
referenceId = referenceMatch[2];
}

const startMarker = "Content-Transfer-Encoding: quoted-printable";
const endMarker = "Le ";
const startIndex =
messageSource.indexOf(startMarker) + startMarker.length;
const endIndex = messageSource.indexOf(endMarker);

let extractedContent = null;

// Vérifie que les marqueurs existent bien
if (startIndex !== -1 && endIndex !== -1) {
extractedContent = messageSource
.substring(startIndex, endIndex)
.trim();
} else {
console.log(
"Les marqueurs de début ou de fin n'ont pas été trouvés. Email ignoré."
);
continue;
}

emails.push({
content: extractedContent,
date: formatDate(date),
referenceId,
extractedContent,
formatDate(date),
collectionName
});

if (referenceId && extractedContent) {
const collectionName = determineCollectionName(
collectionPrefix || "default"
);
await updateContribution(
referenceId,
extractedContent,
formatDate(date),
collectionName
);
}
} catch (emailProcessingError) {
console.error(
"Erreur lors du traitement de l'email :",
emailProcessingError
);
}
}
Expand Down

0 comments on commit dcbf3d6

Please sign in to comment.