| 1 | import { Request, Response, NextFunction } from 'express';
|
| 2 | import { AuthenticatedRequest } from '@app/middleware/auth';
|
| 3 | import { User } from '@prisma/client';
|
| 4 | import {
|
| 5 | createMood,
|
| 6 | getMoodsByUserId,
|
| 7 | getMoodById,
|
| 8 | destroyMoodById
|
| 9 | } from '@app/services/mood.service';
|
| 10 |
|
| 11 | import {
|
| 12 | findUsersElligibleForPush
|
| 13 | } from '@app/services/user.service';
|
| 14 |
|
| 15 | import {
|
| 16 | CreateMoodSchema
|
| 17 | } from '@app/schemas'
|
| 18 |
|
| 19 | export const MoodsController = {
|
| 20 | create: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
|
| 21 | try {
|
| 22 | const parsed = CreateMoodSchema.safeParse(req.body.mood);
|
| 23 |
|
| 24 | if (!parsed.success) {
|
| 25 | return res.status(400).json({
|
| 26 | errors: parsed.error!.issues
|
| 27 | });
|
| 28 | }
|
| 29 |
|
| 30 | const mood = await createMood(req.userId!, parsed.data);
|
| 31 |
|
| 32 | return res.status(201).json({ mood });
|
| 33 | } catch (err) {
|
| 34 | next(err);
|
| 35 | }
|
| 36 | },
|
| 37 |
|
| 38 | index: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
|
| 39 | try {
|
| 40 | const result = await getMoodsByUserId(Number(req.userId), {
|
| 41 | limit: req.query.limit ? Number(req.query.limit) : undefined,
|
| 42 | page: req.query.page ? Number(req.query.page) : undefined,
|
| 43 | from: req.query.from as string | undefined,
|
| 44 | to: req.query.to as string | undefined,
|
| 45 | });
|
| 46 |
|
| 47 | return res.status(200).json(
|
| 48 | result
|
| 49 | )
|
| 50 | } catch (err) {
|
| 51 | next(err);
|
| 52 | }
|
| 53 | },
|
| 54 |
|
| 55 | show: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
|
| 56 | try {
|
| 57 | const mood = await getMoodById(Number(req.params.id));
|
| 58 |
|
| 59 | if (!mood) {
|
| 60 | return res.status(404).json({ errors: "Mood was not found" });
|
| 61 | }
|
| 62 |
|
| 63 | return res.status(200).json(
|
| 64 | mood
|
| 65 | );
|
| 66 | } catch (err) {
|
| 67 | next(err);
|
| 68 | }
|
| 69 | },
|
| 70 |
|
| 71 | destroy: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
|
| 72 | try {
|
| 73 | const result = await destroyMoodById(req.userId!, Number(req.params.id!));
|
| 74 |
|
| 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)
|
| 107 | } catch (err) {
|
| 108 | next(err);
|
| 109 | }
|
| 110 | }
|
| 111 | };
|