eletrotupi / tcc/ commit / fe72d2d

api: add main controller & also rig mail queues + welcome mail

Pedro Lucas Porcellis porcellis@eletrotupi.com · 5 months ago fe72d2d549b50484e27bf51d0213c33be47596d5 Browse files
Parents 9491cee
Changed files
3
Additions
+26
Deletions
0
/dev/null → api/src/controllers/main.ts
1 +
import { Request, Response, NextFunction } from 'express';
2 +

3 +
export const MainController = {
4 +
  // TODO: replace with a HTML response for the main app
5 +
  index: async (req: Request, res: Response, next: NextFunction) => {
6 +
    try {
7 +
      const { message } = req.params;
8 +
      res.json({ status: 'alive' });
9 +
    } catch (err) {
10 +
      next(err);
11 +
    }
12 +
  }
13 +
};
api/src/controllers/users.ts
1 1
import { Request, Response, NextFunction } from 'express';
2 2
import {
3 3
  createUser,
4 4
  findUserById,
5 5
  findUserByEmail,
9 9

10 10
import {
11 11
  validateRequiredFields
12 12
} from '@app/utils/validators'
13 13

14 +
import { getQueue, MailJobName } from '@app/lib/queue';
15 +

14 16
export const UsersController = {
15 17
  create: async (req: Request, res: Response, next: NextFunction) => {
16 18
    try {
17 19
      const { email, password, firstName, lastName } = req.body;
18 20

31 33
      const user = await createUser(
32 34
        email, firstName, lastName, password
33 35
      );
34 36

35 37
      const jwtToken = generateToken(user.id, user.email);
38 +

39 +
      const mailQueue = getQueue('mail');
40 +
      const job = await mailQueue.add(MailJobName.WelcomeEmail, { userId: user.id });
36 41

37 42
      res.status(201).json({ token: jwtToken });
38 43
    } catch (err) {
39 44
      next(err);
40 45
    }
76 81
    } catch (err) {
77 82
      next(err);
78 83
    }
79 84
  }
80 85
};
/dev/null → api/src/routes/main.ts
1 +
import { Router } from "express";
2 +
import { MainController } from '@app/controllers/main'
3 +

4 +
const router = Router();
5 +

6 +
router.get('/', MainController.index);
7 +

8 +
export default router;