Skip to content

Commit 8a53c2b

Browse files
committed
feat: add from field in email, linked to the conciergerie
1 parent a2d78ed commit 8a53c2b

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

app/actions/email.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const transporter = nodemailer.createTransport({
1717

1818
interface Email {
1919
to: string;
20+
from?: string;
2021
subject: string;
2122
html: string;
2223
}
@@ -25,7 +26,7 @@ async function sendEmail(email: Email): Promise<boolean> {
2526
try {
2627
await transporter.sendMail({
2728
...email,
28-
from: `"Job Conciergerie" <${process.env.SMTP_FROM_EMAIL}>`,
29+
from: `"Job Conciergerie" <${email.from || process.env.SMTP_FROM_EMAIL}>`,
2930
});
3031

3132
return true;
@@ -98,13 +99,14 @@ export async function sendEmployeeRegistrationEmail(conciergerie: Conciergerie,
9899
*/
99100
export async function sendEmployeeAcceptanceEmail(
100101
employee: Employee,
101-
conciergerieName: string,
102+
conciergerie: Conciergerie,
102103
missionsCount: number,
103104
isAccepted: boolean,
104105
): Promise<boolean> {
105106
const wasAccepted = employee.status === 'accepted';
106107
return await sendEmail({
107108
to: employee.email,
109+
from: conciergerie.email,
108110
subject: 'Information concernant votre inscription à Job Conciergerie',
109111
html: `
110112
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
@@ -114,7 +116,7 @@ export async function sendEmployeeAcceptanceEmail(
114116
<p>Bonjour ${employee.firstName} ${employee.familyName},</p>
115117
<p>Nous vous informons que votre inscription a été ${
116118
isAccepted ? 'retenue' : wasAccepted ? 'arrêtée' : 'refusée'
117-
} par la conciergerie ${conciergerieName}.</p>
119+
} par la conciergerie ${conciergerie.name}.</p>
118120
${
119121
missionsCount > 0
120122
? `<p>Vos ${missionsCount} mission${missionsCount > 1 ? 's' : ''} ont été annulée${
@@ -276,7 +278,7 @@ export async function sendMissionAcceptanceToEmployeeEmail(
276278
mission: Mission,
277279
home: Home,
278280
employee: Employee,
279-
conciergerieName: string,
281+
conciergerie: Conciergerie,
280282
): Promise<boolean> {
281283
// Format dates
282284
const startDate = formatDateTime(mission.startDateTime);
@@ -288,6 +290,7 @@ export async function sendMissionAcceptanceToEmployeeEmail(
288290
// Generate email content
289291
return await sendEmail({
290292
to: employee.email,
293+
from: conciergerie.email,
291294
subject: `🔔 Confirmation de mission - ${home.title}`,
292295
html: `
293296
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
@@ -298,7 +301,7 @@ export async function sendMissionAcceptanceToEmployeeEmail(
298301
<div style="background-color: #f5f7ff; padding: 15px; border-radius: 5px; margin: 15px 0; border: 1px solid #4F46E5;">
299302
<h3 style="margin-top: 0; color: #4F46E5;">Récapitulatif de la mission</h3>
300303
<p><strong>Bien:</strong> ${home.title}</p>
301-
<p><strong>Conciergerie:</strong> ${conciergerieName}</p>
304+
<p><strong>Conciergerie:</strong> ${conciergerie.name}</p>
302305
<p><strong>Date de début:</strong> ${startDate}</p>
303306
<p><strong>Date de fin:</strong> ${endDate}</p>
304307
<p><strong>Durée estimée:</strong> ${hours}</p>
@@ -333,7 +336,7 @@ export async function sendMissionUpdatedToEmployeeEmail(
333336
mission: Mission,
334337
home: Home,
335338
employee: Employee,
336-
conciergerieName: string,
339+
conciergerie: Conciergerie,
337340
changes: string[],
338341
): Promise<boolean> {
339342
// Format dates
@@ -346,14 +349,15 @@ export async function sendMissionUpdatedToEmployeeEmail(
346349
// Generate email content
347350
return await sendEmail({
348351
to: employee.email,
352+
from: conciergerie.email,
349353
subject: `🔄 Mise à jour de mission - ${home.title}`,
350354
html: `
351355
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
352356
<h2 style="color: #d97706;">Mise à jour de mission</h2>
353357
<p>Bonjour ${employee.firstName},</p>
354-
<p>Une mission que vous avez acceptée pour <strong>${
355-
home.title
356-
}</strong> a été modifiée par la conciergerie ${conciergerieName}.</p>
358+
<p>Une mission que vous avez acceptée pour <strong>${home.title}</strong> a été modifiée par la conciergerie ${
359+
conciergerie.name
360+
}.</p>
357361
358362
<div style="background-color: #fffbeb; padding: 15px; border-radius: 5px; margin: 15px 0; border: 1px solid #d97706;">
359363
<h3 style="margin-top: 0; color: #d97706;">Modifications apportées</h3>
@@ -365,7 +369,7 @@ export async function sendMissionUpdatedToEmployeeEmail(
365369
<div style="background-color: #f9fafb; padding: 15px; border-radius: 5px; margin: 15px 0;">
366370
<h3 style="margin-top: 0; color: #4b5563;">Détails mis à jour de la mission</h3>
367371
<p><strong>Bien:</strong> ${home.title}</p>
368-
<p><strong>Conciergerie:</strong> ${conciergerieName}</p>
372+
<p><strong>Conciergerie:</strong> ${conciergerie.name}</p>
369373
<p><strong>Date de début:</strong> ${startDate}</p>
370374
<p><strong>Date de fin:</strong> ${endDate}</p>
371375
<p><strong>Durée estimée:</strong> ${hours}</p>
@@ -395,7 +399,7 @@ export async function sendMissionRemovedToEmployeeEmail(
395399
mission: Mission,
396400
home: Home,
397401
employee: Employee,
398-
conciergerieName: string,
402+
conciergerie: Conciergerie,
399403
type: 'deleted' | 'canceled',
400404
): Promise<boolean> {
401405
// Format dates
@@ -425,21 +429,22 @@ export async function sendMissionRemovedToEmployeeEmail(
425429
// Generate email content
426430
return await sendEmail({
427431
to: employee.email,
432+
from: conciergerie.email,
428433
subject: `${config.emoji} ${config.title} - ${home.title}`,
429434
html: `
430435
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
431436
<h2 style="color: ${config.color};">${config.title}</h2>
432437
<p>Bonjour ${employee.firstName},</p>
433438
<p>Nous vous informons que la mission pour <strong>${home.title}</strong> a été ${
434439
config.action
435-
} par la conciergerie ${conciergerieName}.</p>
440+
} par la conciergerie ${conciergerie.name}.</p>
436441
437442
<div style="background-color: ${
438443
config.bgColor
439444
}; padding: 15px; border-radius: 5px; margin: 15px 0; border: 1px solid ${config.color};">
440445
<h3 style="margin-top: 0; color: ${config.color};">Détails de la mission ${config.action}</h3>
441446
<p><strong>Bien:</strong> ${home.title}</p>
442-
<p><strong>Conciergerie:</strong> ${conciergerieName}</p>
447+
<p><strong>Conciergerie:</strong> ${conciergerie.name}</p>
443448
<p><strong>Date de début:</strong> ${startDate}</p>
444449
<p><strong>Date de fin:</strong> ${endDate}</p>
445450
<p><strong>Tâches:</strong> ${mission.tasks.join(', ')}</p>

app/contexts/missionsProvider.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,9 @@ function MissionsProvider({ children }: { children: ReactNode }) {
146146
if (!success) return false;
147147

148148
// If successful and there's an employee assigned who has notifications enabled, send email
149-
if (employee?.notificationSettings?.missionChanged && home && changes.length > 0)
150-
await sendMissionUpdatedToEmployeeEmail(updatedMission, home, employee, conciergerieName, changes);
149+
const conciergerie = conciergeries?.find(c => c.name === updatedMission.conciergerieName);
150+
if (employee?.notificationSettings?.missionChanged && home && changes.length > 0 && conciergerie)
151+
await sendMissionUpdatedToEmployeeEmail(updatedMission, home, employee, conciergerie, changes);
151152

152153
return true;
153154
};
@@ -165,10 +166,11 @@ function MissionsProvider({ children }: { children: ReactNode }) {
165166
// Check if we need to notify an employee about the deletion
166167
const employee = employees.find(e => e.id === missionToDelete.employeeId);
167168
const home = homes.find(h => h.id === missionToDelete.homeId);
169+
const conciergerie = conciergeries?.find(c => c.name === missionToDelete.conciergerieName);
168170

169171
// Notify employee before deleting the mission
170-
if (employee?.notificationSettings?.missionDeleted && home && conciergerieName)
171-
sendMissionRemovedToEmployeeEmail(missionToDelete, home, employee, conciergerieName, 'deleted');
172+
if (employee?.notificationSettings?.missionDeleted && home && conciergerie)
173+
sendMissionRemovedToEmployeeEmail(missionToDelete, home, employee, conciergerie, 'deleted');
172174

173175
return true;
174176
};
@@ -184,10 +186,11 @@ function MissionsProvider({ children }: { children: ReactNode }) {
184186
// Check if we need to notify an employee about the cancellation
185187
const employee = employees.find(e => e.id === missionToCancel.employeeId);
186188
const home = homes.find(h => h.id === missionToCancel.homeId);
189+
const conciergerie = conciergeries?.find(c => c.name === missionToCancel.conciergerieName);
187190

188191
// Send notification to employee about cancellation
189-
if (employee?.notificationSettings?.missionsCanceled && home && conciergerieName)
190-
sendMissionRemovedToEmployeeEmail(missionToCancel, home, employee, conciergerieName, 'canceled');
192+
if (employee?.notificationSettings?.missionsCanceled && home && conciergerie)
193+
sendMissionRemovedToEmployeeEmail(missionToCancel, home, employee, conciergerie, 'canceled');
191194

192195
return true;
193196
};
@@ -207,10 +210,10 @@ function MissionsProvider({ children }: { children: ReactNode }) {
207210

208211
// Send confirmation email to the employee
209212
const home = homes.find(h => h.id === missionToAccept.homeId);
210-
const conciergerieName = missionToAccept.conciergerieName;
213+
const conciergerie = conciergeries?.find(c => c.name === missionToAccept.conciergerieName);
211214
const employee = employees.find(e => e.id === employeeData.id);
212-
if (home && conciergerieName && employee?.notificationSettings?.acceptedMissions)
213-
sendMissionAcceptanceToEmployeeEmail(missionToAccept, home, employee, conciergerieName);
215+
if (employee?.notificationSettings?.acceptedMissions && home && conciergerie)
216+
sendMissionAcceptanceToEmployeeEmail(missionToAccept, home, employee, conciergerie);
214217

215218
return true;
216219
};

app/employees/page.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { useAuth } from '@/app/contexts/authProvider';
1010
import { useMenuContext } from '@/app/contexts/menuProvider';
1111
import { useMissions } from '@/app/contexts/missionsProvider';
1212
import EmployeeDetails from '@/app/employees/components/employeeDetails';
13-
import { Employee } from '@/app/types/dataTypes';
13+
import { Conciergerie, Employee } from '@/app/types/dataTypes';
1414
import { filterEmployees, filterEmployeesByConciergerie, sortEmployees } from '@/app/utils/employee';
1515
import { Page } from '@/app/utils/navigation';
1616
import { IconCheck, IconUser, IconUserCheck, IconUserX, IconX } from '@tabler/icons-react';
@@ -23,6 +23,7 @@ export default function EmployeesList() {
2323
isLoading: authLoading,
2424
employees: authEmployees,
2525
fetchDataFromDatabase,
26+
getUserData,
2627
updateUserData,
2728
} = useAuth();
2829
const { currentPage } = useMenuContext();
@@ -105,9 +106,12 @@ export default function EmployeesList() {
105106
// Update local state
106107
updateUserData(updatedEmployee, 'employee');
107108

109+
const conciergerie = getUserData<Conciergerie>();
110+
if (!conciergerie) throw new Error('Conciergerie non trouvée');
111+
108112
sendEmployeeAcceptanceEmail(
109113
employee,
110-
conciergerieName || '',
114+
conciergerie,
111115
countEmployeeMissions(employee.id),
112116
newStatus === 'accepted',
113117
)

0 commit comments

Comments
 (0)