Création de l'extension 'directus-extension-new-user'
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
export default ({filter}, {services, env}) => {
|
||||
const checkEmailExists = async (email, database) => {
|
||||
const user = await database('directus_users')
|
||||
.select('id')
|
||||
.where({email})
|
||||
.first()
|
||||
|
||||
return user !== undefined
|
||||
}
|
||||
|
||||
filter('users.create', async (input, {schema}, {database}) => {
|
||||
if (!services.MailService) {
|
||||
console.error('Le service MailService est manquant.')
|
||||
return input
|
||||
}
|
||||
|
||||
const adminEmail = env.EMAIL_NEW_USER
|
||||
|
||||
if (!adminEmail) {
|
||||
console.error('La variable EMAIL_NEW_USER est manquante.')
|
||||
return input
|
||||
}
|
||||
|
||||
const emailExists = await checkEmailExists(input.email, database)
|
||||
|
||||
if (emailExists) {
|
||||
console.error('L’adresse e-mail est déjà utilisée.')
|
||||
return input
|
||||
}
|
||||
|
||||
try {
|
||||
const mailService = new services.MailService({schema})
|
||||
mailService.send({
|
||||
to: adminEmail,
|
||||
subject: `Nouvel utilisateur : ${input.email}`,
|
||||
text: `Un nouvel utilisateur a été créé :\n Nom: ${input.first_name || 'N/A'} Email: ${input.email || 'N/A'}\n `,
|
||||
html: `
|
||||
<p>Un nouvel utilisateur a été créé :</p>
|
||||
<ul>
|
||||
<li><strong>Nom:</strong> ${input.first_name || 'N/A'} </li>
|
||||
<li><strong>Email:</strong> ${input.email || 'N/A'}</li>
|
||||
</ul>
|
||||
`,
|
||||
})
|
||||
|
||||
console.log('Email envoyé avec succès à', adminEmail)
|
||||
} catch (err) {
|
||||
console.error("Erreur lors de l’envoi de l’e-mail via MailService:", err)
|
||||
}
|
||||
|
||||
return input
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user