| 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 { findUsersElligibleForPush } from '@app/services/user.service';
|
| 12 | import { sendPushNotification } from '@app/lib/sendPushNotification';
|
| 13 |
|
| 14 | import {
|
| 15 | CreateMoodSchema
|
| 16 | } from '@app/schemas'
|
| 17 |
|
| 18 | export const MoodsController = {
|
| 19 | create: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
|
| 20 | try {
|
| 21 | const parsed = CreateMoodSchema.safeParse(req.body.mood);
|
| 22 |
|
| 23 | if (!parsed.success) {
|
| 24 | return res.status(400).json({
|
| 25 | errors: parsed.error!.issues
|
| 26 | });
|
| 27 | }
|
| 28 |
|
| 29 | const mood = await createMood(req.userId!, parsed.data);
|
| 30 |
|
| 31 | return res.status(201).json({ mood });
|
| 32 | } catch (err) {
|
| 33 | next(err);
|
| 34 | }
|
| 35 | },
|
| 36 |
|
| 37 | index: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
|
| 38 | try {
|
| 39 | const result = await getMoodsByUserId(Number(req.userId), {
|
| 40 | limit: req.query.limit ? Number(req.query.limit) : undefined,
|
| 41 | page: req.query.page ? Number(req.query.page) : undefined,
|
| 42 | from: req.query.from as string | undefined,
|
| 43 | to: req.query.to as string | undefined,
|
| 44 | });
|
| 45 |
|
| 46 | return res.status(200).json(
|
| 47 | result
|
| 48 | )
|
| 49 | } catch (err) {
|
| 50 | next(err);
|
| 51 | }
|
| 52 | },
|
| 53 |
|
| 54 | show: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
|
| 55 | try {
|
| 56 | const mood = await getMoodById(Number(req.params.id));
|
| 57 |
|
| 58 | if (!mood) {
|
| 59 | return res.status(404).json({ errors: "Mood was not found" });
|
| 60 | }
|
| 61 |
|
| 62 | return res.status(200).json(
|
| 63 | mood
|
| 64 | );
|
| 65 | } catch (err) {
|
| 66 | next(err);
|
| 67 | }
|
| 68 | },
|
| 69 |
|
| 70 | destroy: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
|
| 71 | try {
|
| 72 | const result = await destroyMoodById(req.userId!, Number(req.params.id!));
|
| 73 |
|
| 74 | return res.status(200).json({})
|
| 75 | } catch (err) {
|
| 76 | next(err);
|
| 77 | }
|
| 78 | },
|
| 79 |
|
| 80 | sendNotification: async (req: Request, res: Response, next: NextFunction) => {
|
| 81 | try {
|
| 82 | const users = await findUsersElligibleForPush();
|
| 83 |
|
| 84 | if (users) {
|
| 85 | await Promise.all(
|
| 86 | users
|
| 87 | .filter((user: User) => user.pushToken)
|
| 88 | .map((user: User) =>
|
| 89 | sendPushNotification({
|
| 90 | token: user.pushToken!,
|
| 91 | title: 'Como você está?',
|
| 92 | body: 'Adicione mais registros de humor.',
|
| 93 | data: { screen: 'notifications' },
|
| 94 | })
|
| 95 | )
|
| 96 | );
|
| 97 | }
|
| 98 |
|
| 99 | return res.status(200).json(users)
|
| 100 | } catch (err) {
|
| 101 | next(err);
|
| 102 | }
|
| 103 | }
|
| 104 | };
|