-
Notifications
You must be signed in to change notification settings - Fork 0
/
habits.js
98 lines (84 loc) · 2.56 KB
/
habits.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const habitsRouter = require('express').Router()
const Habit = require('../models/habit')
const User = require('../models/user')
const Action = require('../models/action')
const { endOfDay } = require('date-fns')
const { startOfDay } = require('date-fns')
const { addBusinessDays } = require('date-fns')
habitsRouter.get('/', async (request, response) => {
const { endDate } = request.query;
const { startDate } = request.query;
let habits = await Habit
.find().where('user')
.in(request.userId)
.populate({
path: 'actions',
populate: {
path: 'timeEntries',
match: {
date: {
$gte: startOfDay(new Date(startDate)),
$lte: endOfDay(new Date(endDate))
}
}
}
})
response.json(habits)
})
habitsRouter.get('/:id', async (request, response) => {
const user = await User.findById(request.userId)
let habit = await Habit.findById(request.params.id).lean()
if (habit && user && habit.user.toString() === user.id.toString() ) {
habit = await Habit.findById(request.params.id)
.populate({
path: 'actions',
// TODO: get timeEntries within a period of time
populate: {
path: 'timeEntries'
}
})
response.json(habit)
} else if (!habit) {
return response.status(400).json({ error: 'No habit found having this action' })
}
})
habitsRouter.post('/', async (request, response) => {
const body = request.body
const user = await User.findById(request.userId)
const { name, description } = body
if (!name) {
response.status(400).end()
return
}
const habit = new Habit({
name,
description,
user: user._id
})
const savedHabit = await habit.save()
response.json(savedHabit.toJSON())
})
habitsRouter.put('/:id', (request, response, next) => {
const id = request.params.id;
const { name, description } = request.body
const habit = {
name,
description
}
Habit.findByIdAndUpdate(id, habit, { new: true })
.then(updatedHabit => {
response.json(updatedHabit.toJSON())
})
.catch(error => next(error))
})
habitsRouter.delete('/:id', async (request, response) => {
const user = await User.findById(request.userId)
const habit = await Habit.findById(request.params.id)
if (habit && user && habit.user.toString() === user.id.toString() ) {
await habit.deleteOne({'_id': request.params.id})
response.status(204).end()
} else {
return response.status(400).json({ error: 'Current user does not have right to delete this habit' })
}
})
module.exports = habitsRouter