eletrotupi / tcc/ commit / 2963d71

api: testing endpoint for notifications

Pedro Lucas Porcellis porcellis@eletrotupi.com 1 month ago 2963d715f84fdcecd3d9590259af9c13403c40a2
Parents: 424c22f
3 file(s) changed
  • api/src/controllers/moods.ts +36 -0
  • api/src/routes/moods.ts +1 -0
  • api/src/services/user.service.ts +10 -0
api/src/controllers/moods.ts
1 1
import { Request, Response, NextFunction } from 'express';
2 2
import { AuthenticatedRequest } from '@app/middleware/auth';
3 +
import { User } from '@prisma/client';
3 4
import {
4 5
  createMood,
5 6
  getMoodsByUserId,
6 7
  getMoodById,
7 8
  destroyMoodById
8 9
} from '@app/services/mood.service';
10 +

11 +
import {
12 +
  findUsersElligibleForPush
13 +
} from '@app/services/user.service';
9 14

10 15
import {
11 16
  CreateMoodSchema
12 17
} from '@app/schemas'
13 18

▸ 52 unchanged lines
66 71
  destroy: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
67 72
    try {
68 73
      const result = await destroyMoodById(req.userId!, Number(req.params.id!));
69 74

70 75
      return res.status(200).json({})
76 +
    } catch (err) {
77 +
      next(err);
78 +
    }
79 +
  },
80 +

81 +
  sendNotification: async (req: Request, res: Response, next: NextFunction) => {
82 +
    try {
83 +
      const users = await findUsersElligibleForPush();
84 +

85 +
      if (users) {
86 +
        users.forEach(async (user: User) => {
87 +
          const response = await fetch('https://exp.host/--/api/v2/push/send', {
88 +
            method: 'POST', headers: {
89 +
              'Content-Type': 'application/json'
90 +
            },
91 +
            body: JSON.stringify({
92 +
              to: user.pushToken,
93 +
              title: 'Como você está?',
94 +
              body: 'Adicione mais registros de humor.',
95 +
              data: { screen: 'notifications' }, // Deep link payload
96 +
            })
97 +
          });
98 +

99 +
          const data = await response.json();
100 +

101 +
          req.log.info("Resposta %s", data)
102 +
          console.log("Resposta ", data)
103 +
        })
104 +
      }
105 +

106 +
      return res.status(200).json(users)
71 107
    } catch (err) {
72 108
      next(err);
73 109
    }
74 110
  }
75 111
};
api/src/routes/moods.ts
1 1
import { Router } from "express";
2 2
import { MoodsController } from '@app/controllers/moods'
3 3
import { requireAuth } from '@app/middleware/auth';
4 4

5 5
const router = Router();
6 6

7 7
router.get('/', requireAuth, MoodsController.index);
8 8
router.get('/:id', requireAuth, MoodsController.show);
9 9
router.post('/', requireAuth, MoodsController.create);
10 10
router.delete('/:id', requireAuth, MoodsController.destroy);
11 +
router.post('/notify', MoodsController.sendNotification);
11 12

12 13
export default router;
api/src/services/user.service.ts
1 1
import { prisma } from '@app/lib/prisma';
2 2
import { User } from '@prisma/client';
3 3
import bcrypt from 'bcryptjs';
4 4

5 5
import {
▸ 49 unchanged lines
55 55
      id: input.id
56 56
    },
57 57
    data: updateData
58 58
  })
59 59
}
60 +

61 +
export const findUsersElligibleForPush = async (): Promise<User[]> => {
62 +
  return await prisma.user.findMany({
63 +
    where: {
64 +
      pushToken: {
65 +
        not: null
66 +
      }
67 +
    }
68 +
  });
69 +
}