-
Notifications
You must be signed in to change notification settings - Fork 2
(SBL-34-583) Feature: Invite email sending with a cron job #413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hrsy
wants to merge
4
commits into
feature/SBL-34/SBL-582/route_to_generate_link
Choose a base branch
from
feature/SBL-34/SBL-583/research_email_job_scheduling
base: feature/SBL-34/SBL-582/route_to_generate_link
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dfdc132
feat: invite email sending with a cron job
hrsy e159ff3
Merge branch 'feature/SBL-34/SBL-582/route_to_generate_link' into fea…
hrsy 6d01e78
fix: PR comments
hrsy ab90ba9
Merge branch 'feature/SBL-34/SBL-582/route_to_generate_link' into fea…
hrsy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
api/migrations/20211027105721_alter_invites_table_add_email_status.js
This file contains hidden or 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,9 @@ | ||
| export const up = (knex) => | ||
| knex.schema.alterTable('invites', (table) => { | ||
| table.enum('email_status', ['error', 'pending', 'success']); | ||
| }); | ||
|
|
||
| export const down = (knex) => | ||
| knex.schema.alterTable('invites', (table) => { | ||
| table.dropColumn('email_status'); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
| FROM node:14-alpine | ||
|
|
||
| WORKDIR /scheduler | ||
|
|
||
| COPY package.json yarn.lock ./ | ||
| RUN yarn | ||
| COPY . . |
This file contains hidden or 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,37 @@ | ||
| import mailer from 'nodemailer'; | ||
| import { DEFAULT_LANGUAGE } from './scheduler.js'; | ||
|
|
||
| export class Email { | ||
| constructor({ i18n }) { | ||
| this.t = i18n.t.bind(i18n); | ||
| this.transporter = mailer.createTransport({ | ||
| pool: true, | ||
| host: 'smtp.gmail.com', | ||
| port: process.env.SMTP_PORT || 465, | ||
| secure: true, | ||
| auth: { | ||
| user: process.env.SB_MAIL_USER, | ||
| pass: process.env.SB_MAIL_PASSWORD, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| static getMailMocked({ ...params }) { | ||
| return { | ||
| from: 'StudyBites', | ||
| ...params, | ||
| }; | ||
| } | ||
|
|
||
| async sendMailWithLogging(params) { | ||
| console.log(Email.getMailMocked(params)); | ||
| } | ||
|
|
||
| async sendInvite({ email, link, language = DEFAULT_LANGUAGE }) { | ||
| return this.sendMailWithLogging({ | ||
| to: email, | ||
| subject: this.t('email:invite.subject', { lng: language }), | ||
| html: this.t('email:invite.html', { lng: language, link }), | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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,18 @@ | ||
| { | ||
| "password_reset": { | ||
| "subject": "Password reset", | ||
| "html": "Reset password link {{link}}" | ||
| }, | ||
| "password_changed": { | ||
| "subject": "Password changed", | ||
| "html": "Your password were successfully changed" | ||
| }, | ||
| "email_confirmation": { | ||
| "subject": "Email confirmation", | ||
| "html": "Confirm email link {{link}}" | ||
| }, | ||
| "invite": { | ||
| "subject": "You were invited to start learning at StudyBites", | ||
| "html": "Follow the link to start: {{link}}" | ||
| } | ||
| } |
This file contains hidden or 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,18 @@ | ||
| { | ||
| "password_reset": { | ||
| "subject": "Восстановление пароля", | ||
| "html": "Ссылка для восстановления пароля {{link}}" | ||
| }, | ||
| "password_changed": { | ||
| "subject": "Пароль изменен", | ||
| "html": "Ваш пароль был успешно изменен." | ||
| }, | ||
| "email_confirmation": { | ||
| "subject": "Email подтверждение", | ||
| "html": "Подтвердите ссылку на электронную почту {{link}}" | ||
| }, | ||
| "invite": { | ||
| "subject": "Вас пригласили начать обучение на StudyBites", | ||
| "html": "Пройдите по ссылке, чтобы начать: {{link}}" | ||
| } | ||
| } |
This file contains hidden or 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,21 @@ | ||
| { | ||
| "name": "scheduler", | ||
| "version": "0.1.0", | ||
| "main": "scheduler.js", | ||
| "type": "module", | ||
| "license": "Apache-2.0", | ||
| "scripts": { | ||
| "run:dev": "yarn && nodemon scheduler.js" | ||
| }, | ||
| "dependencies": { | ||
| "i18next": "^21.3.3", | ||
| "i18next-fs-backend": "^1.1.1", | ||
| "knex": "^0.95.11", | ||
| "node-cron": "^3.0.0", | ||
| "nodemailer": "^6.7.0", | ||
| "pg": "^8.7.1" | ||
| }, | ||
| "devDependencies": { | ||
| "nodemon": "^2.0.14" | ||
| } | ||
| } |
This file contains hidden or 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,75 @@ | ||
| import knex from 'knex'; | ||
| import cron from 'node-cron'; | ||
| import i18next from 'i18next'; | ||
| import Backend from 'i18next-fs-backend'; | ||
| import { Email } from './email.js'; | ||
|
|
||
| export const DEFAULT_LANGUAGE = 'en'; | ||
| export const LANGUAGES_LIST = ['en', 'ru']; | ||
|
|
||
| class Scheduler { | ||
| constructor({ i18n }) { | ||
| this.mailer = new Email({ i18n }); | ||
| } | ||
|
|
||
| async start() { | ||
| try { | ||
| await this.connDB(); | ||
| // invite job will run every one minute | ||
| cron.schedule('* * * * *', this.inviteJob, {}); | ||
| console.log('scheduler started successfully'); | ||
| } catch (e) { | ||
| console.log(e); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| async connDB() { | ||
| this.db = knex({ | ||
| client: 'pg', | ||
| connection: process.env.DATABASE_URL, | ||
| }); | ||
| await this.db.raw('SELECT 1'); | ||
| } | ||
|
|
||
| inviteJob = async () => { | ||
| try { | ||
| const pendingInvites = await this.db('invites').where({ | ||
| status: 'pending', | ||
| email_status: 'pending', | ||
| }); | ||
| pendingInvites.map(async (invite) => { | ||
| await this.mailer.sendInvite({ | ||
| email: invite.email, | ||
| link: `${process.env.HOST}/invite/${invite.id}`, | ||
| }); | ||
| await this.db('invites').where({ id: invite.id }).update({ | ||
| email_status: 'success', | ||
| }); | ||
| }); | ||
| } catch (e) { | ||
| console.log('invite job failed:', e); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| (async () => { | ||
| await i18next.use(Backend).init({ | ||
| initImmediate: false, | ||
| lng: DEFAULT_LANGUAGE, | ||
| fallbackLng: DEFAULT_LANGUAGE, | ||
| preload: LANGUAGES_LIST, | ||
| ns: ['email'], | ||
| keySeparator: '.', | ||
| interpolation: { | ||
| escapeValue: false, | ||
| }, | ||
| backend: { | ||
| loadPath: `locales/{{lng}}/{{ns}}.json`, | ||
| }, | ||
| }); | ||
| const scheduler = new Scheduler({ | ||
| i18n: i18next, | ||
| }); | ||
| await scheduler.start(); | ||
| })(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.