From 72096f09594ab5a4155e234ff164895a256ef7f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Thu, 7 Nov 2024 17:03:53 +0100 Subject: [PATCH] feat: send email at first signal on an order --- webapp/src/server/api/routers/order.ts | 18 +++++++ webapp/src/utils/emailHtml.ts | 73 ++++++++++++++++++++++++-- webapp/src/utils/tools.ts | 11 ++++ 3 files changed, 99 insertions(+), 3 deletions(-) diff --git a/webapp/src/server/api/routers/order.ts b/webapp/src/server/api/routers/order.ts index 5abab6e1..4218ae4b 100644 --- a/webapp/src/server/api/routers/order.ts +++ b/webapp/src/server/api/routers/order.ts @@ -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 }; @@ -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, }; diff --git a/webapp/src/utils/emailHtml.ts b/webapp/src/utils/emailHtml.ts index 84c7e37c..2c0e6d58 100644 --- a/webapp/src/utils/emailHtml.ts +++ b/webapp/src/utils/emailHtml.ts @@ -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) => { @@ -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; @@ -68,3 +69,69 @@ export const getHtmlLoginByEmail = (user: User, userAuthToken: string) => { `; }; + +export const getHtmlSignalOrder = (user: User) => { + const now = new Date(); + return ` + + + + + + +
+
+ + +
+ +

Nous avons bien reçu votre demande d’aide ${user.firstName}

+ +

Vérifiez vos mails nous allons bientôt vous écrire pour trouver la solution à votre problème.

+ +

L'équipe Carte "jeune engagé"

+ +
+ +

Date de votre demande : ${formatDateTime(now)} +

+ + + `; +}; diff --git a/webapp/src/utils/tools.ts b/webapp/src/utils/tools.ts index 1f937415..76bc6465 100644 --- a/webapp/src/utils/tools.ts +++ b/webapp/src/utils/tools.ts @@ -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);