Skip to content

Commit

Permalink
feat: send email at first signal on an order
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNumericite committed Nov 7, 2024
1 parent 7799dd5 commit 72096f0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
18 changes: 18 additions & 0 deletions webapp/src/server/api/routers/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fs from "fs/promises";
import os from "os";
import { Where } from "payload/types";
import { PDFDocument } from "pdf-lib";
import { getHtmlSignalOrder } from "~/utils/emailHtml";

export interface OrderIncluded extends Order {
offer: Offer & { partner: Partner & { icon: Media } } & { image: Media };
Expand Down Expand Up @@ -387,6 +388,23 @@ export const orderRouter = createTRPCRouter({
},
});

const users = await ctx.payload.find({
collection: "users",
limit: 1,
page: 1,
where: {
id: { equals: ctx.session.id },
},
});
const currentUser = users.docs[0];

ctx.payload.sendEmail({
from: process.env.SMTP_FROM_ADDRESS,
to: currentUser.userEmail,
subject: 'Signalement d\'une commande sur la carte "jeune engagé"',
html: getHtmlSignalOrder(currentUser),
});

return {
data: orderSignal,
};
Expand Down
73 changes: 70 additions & 3 deletions webapp/src/utils/emailHtml.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getBaseUrl } from "./tools";
import { formatDateTime, getBaseUrl } from "./tools";
import { User } from "~/payload/payload-types";

export const getHtmlLoginByEmail = (user: User, userAuthToken: string) => {
Expand All @@ -24,10 +24,11 @@ export const getHtmlLoginByEmail = (user: User, userAuthToken: string) => {
}
.logo {
height: 48px;
width: 48px;
margin: 0 8px;
}
.logo.logo-cje {
height: 25px;
.logo-cje {
width: 80px;
}
h1 {
font-size: 28px;
Expand Down Expand Up @@ -68,3 +69,69 @@ export const getHtmlLoginByEmail = (user: User, userAuthToken: string) => {
</html>
`;
};

export const getHtmlSignalOrder = (user: User) => {
const now = new Date();
return `
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
line-height: 1.6;
color: #333333;
text-align: center;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 32px;
text-align: center;
}
.logo-container {
margin-bottom: 48px;
}
.logo {
height: 48px;
width: 48px;
margin: 0 8px;
}
.logo-cje {
width: 80px;
}
h1 {
font-size: 28px;
font-weight: bolder;
margin-bottom: 30px;
}
hr {
border-color: #E2E8F0;
border-width: 1px;
}
.bottom-container {
font-size: 14px;
}
</style>
</head>
<body>
<div class="container">
<div class="logo-container">
<img src="${getBaseUrl()}/images/landing/ministere-travail.png" alt="Ministère" class="logo">
<img src="${getBaseUrl()}/images/cje-logo.png" width={} alt="Jeune Engagé" class="logo logo-cje">
</div>
<h1>Nous avons bien reçu votre demande d’aide ${user.firstName}</h1>
<p>Vérifiez vos mails nous allons bientôt vous écrire pour trouver la solution à votre problème.</p>
<p>L'équipe Carte "jeune engagé"</p>
<hr/>
<p class="bottom-container"><b>Date de votre demande : ${formatDateTime(now)}</b>
</div>
</body>
</html>
`;
};
11 changes: 11 additions & 0 deletions webapp/src/utils/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ export const formatDateToDDMMYYYY = (isoDate: string | Date): string => {
return `${day}/${month}/${year}`;
};

export const formatDateTime = (date: Date) => {
const day = date.getDate().toString().padStart(2, "0");
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const year = date.getFullYear();
const hours = date.getHours().toString().padStart(2, "0");
const minutes = date.getMinutes().toString().padStart(2, "0");
const seconds = date.getSeconds().toString().padStart(2, "0");

return `${day}/${month}/${year} à ${hours}:${minutes}:${seconds}`;
};

export const isOlderThan24Hours = (dateString: string): boolean => {
const inputDate = new Date(dateString);

Expand Down

0 comments on commit 72096f0

Please sign in to comment.