-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'registration-notification-email' into develop
- Loading branch information
Showing
12 changed files
with
192 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import console from "console"; | ||
import { createTransport } from "nodemailer"; | ||
import { AppDataSource } from "../data-source.js"; | ||
import { UserEntity } from "../entity/User.entity.js"; | ||
import logger from "../logger.js"; | ||
import { ClientSettings, EmailSettings } from "../settings.js"; | ||
|
||
export function sendNotificationEmailAboutNewRegistration(newUsername: string) { | ||
if (!canSendEmail()) { | ||
console.info("Could not send email notification about new user registration to admins. No SMTP server is configured in `.env` file."); | ||
return; | ||
} | ||
|
||
AppDataSource.manager | ||
.getRepository(UserEntity) | ||
.createQueryBuilder("find all admins") | ||
.where("roles @> :role", { role: ["ADMIN"] }) // https://www.postgresql.org/docs/16/functions-array.html#ARRAY-OPERATORS-TABLE | ||
.getMany() | ||
.then((users) => { | ||
console.log("Found users", users); | ||
return users.map((user) => user.email); | ||
}) | ||
.catch((e) => { | ||
console.error("Failed to get admins", e); | ||
return []; | ||
}) | ||
.then((adminEmails) => { | ||
if (adminEmails.length <= 0) { | ||
logger.warn(`There are no admins yet. No notification email is sent about new user '${newUsername}'.`); | ||
} else { | ||
logger.debug(`Sending notification email about registration of new user '${newUsername}' to ${adminEmails.join(", ")}`); | ||
|
||
sendEmail( | ||
adminEmails, | ||
"New user registered", | ||
`Hi admins, | ||
a new user '${encodeURIComponent(newUsername)}' registered at ${ClientSettings.BASE_URL} . | ||
Visit ${ClientSettings.BASE_URL}/administration in order to give them some permissions. | ||
Kind regards, | ||
Your fuBlog`, | ||
); | ||
} | ||
}); | ||
} | ||
|
||
function canSendEmail(): boolean { | ||
return !!(EmailSettings.SMTP_HOST && EmailSettings.SMTP_PORT && EmailSettings.SMTP_FROM); | ||
} | ||
|
||
function sendEmail(to: string[], subject: string, text: string) { | ||
createTransport(EmailSettings.SMTP_OPTIONS) | ||
.sendMail({ | ||
from: EmailSettings.SMTP_FROM, | ||
to, | ||
subject, | ||
text, | ||
}) | ||
.catch(() => { | ||
logger.error("Failed to send email notification!"); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters