Skip to content

Commit

Permalink
add: send onboarding email
Browse files Browse the repository at this point in the history
  • Loading branch information
genzyy committed Nov 11, 2023
1 parent c35ce05 commit 098ab53
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Request, Response } from 'express';
import userRepository from '../repositories/user';
import catchAsync from '../utils/catchAsync';
import JwtService from '../services/jwt.service';
import EmailService from '../services/email.service';
import { NotFound, Unauthorized } from '../utils/ApiError';
import exclude from '../utils/exclude';
import { UserReturn } from '../types/user';
Expand All @@ -12,6 +13,8 @@ const register = catchAsync(async (req: Request, res: Response) => {
const user = await userRepository.createUser(username, email, password);
const tokens = JwtService.generateAuthTokenForUser(user.id);

await EmailService.sendOnboardingEmail(user.email, user.name ?? '');

return res.status(CREATED).send({ user: exclude(user, ['id', 'password', 'signedOut']), tokens });
});

Expand Down
12 changes: 8 additions & 4 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const envVarsSchema = Joi.object()
SHOW_SQL_QUERIES: Joi.bool().default(false),
SENTRY_DSN: Joi.string().default(''),
SENTRY_ENVIRONMENT: Joi.string().default('development'),
COMPANY_SENDER_EMAIL: Joi.string().email().default('[email protected]'),
QUEUE_REDIS_HOST: Joi.string().default(''),
QUEUE_REDIS_PORT: Joi.number().default(6379),
QUEUE_CONCURRENCY: Joi.number().default(1),
Expand Down Expand Up @@ -51,10 +52,13 @@ export default {
dsn: envVars.SENTRY_DSN,
environment: envVars.SENTRY_ENVIRONMENT,
},
queue: {
redisHost: envVars.QUEUE_REDIS_HOST,
redisPort: envVars.QUEUE_REDIS_PORT,
concurrency: envVars.QUEUE_CONCURRENCY,
emailService: {
companySenderEmail: envVars.COMPANY_SENDER_EMAIL,
queue: {
redisHost: envVars.QUEUE_REDIS_HOST,
redisPort: envVars.QUEUE_REDIS_PORT,
concurrency: envVars.QUEUE_CONCURRENCY,
},
},
aws: {
region: envVars.AWS_REGION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const EMAIL_QUEUE_NAME = 'mail-queue';

export const emailQueue = new Queue<Email>(EMAIL_QUEUE_NAME, {
connection: {
host: config.queue.redisHost,
port: config.queue.redisPort,
host: config.emailService.queue.redisHost,
port: config.emailService.queue.redisPort,
},
});
6 changes: 3 additions & 3 deletions src/queues/email/email.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const MAIL_WORKER_NAME = 'MailWorker';

const EmailWorker = new Worker(MAIL_WORKER_NAME, __dirname + 'email.processor.ts', {
connection: {
host: config.queue.redisHost,
port: config.queue.redisPort,
host: config.emailService.queue.redisHost,
port: config.emailService.queue.redisPort,
},
concurrency: config.queue.concurrency,
concurrency: config.emailService.queue.concurrency,
});

export default EmailWorker;
15 changes: 15 additions & 0 deletions src/services/email.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { emailQueue, EMAIL_QUEUE_NAME } from '../queues/email/email.queue';
import config from '../core/config';
import { Email } from '../types/email';

const sendOnboardingEmail = async (recipient: string, name: string) => {
const emailData: Email = {
recipient: recipient,
sender: config.emailService.companySenderEmail,
html: `<h1>Welcome ${name}</h1>`,
subject: `Welcome to the app, ${name}`,
};
await emailQueue.add(EMAIL_QUEUE_NAME, emailData);
};

export default { sendOnboardingEmail };

0 comments on commit 098ab53

Please sign in to comment.