Add dailyAverage when providing complete session

This commit is contained in:
ruihildt 2019-08-01 20:43:43 +02:00
parent 856f8c753d
commit c36391321c
7 changed files with 67 additions and 5 deletions

View File

@ -60,4 +60,4 @@ Sleep Tracker is intended for anyone interested in improving their sleep and hea
- `PUT /api/users/sessions/:id` - update a session by id - `PUT /api/users/sessions/:id` - update a session by id
- `DELETE /api/users/sessions/:id` - delete a session by id - `DELETE /api/users/sessions/:id` - delete a session by id
- `GET /api/users/:id/dailyaverages` - fetch a list of last 30 daily averages by user id - `GET /api/users/:id/averages` - fetch a list of last 30 daily averages by user id

View File

@ -5,6 +5,7 @@ const cors = require('cors');
const authRouter = require('../services/auth/authRouter'); const authRouter = require('../services/auth/authRouter');
const usersRouter = require('../services/users/usersRouter'); const usersRouter = require('../services/users/usersRouter');
const sessionsRouter = require('../services/sessions/sessionsRouter'); const sessionsRouter = require('../services/sessions/sessionsRouter');
const dailyAveragesRouter = require('../services/dailyAverages/dailyAveragesRouter');
const server = express(); const server = express();
@ -15,5 +16,6 @@ server.use(cors());
server.use('/api/auth', authRouter); server.use('/api/auth', authRouter);
server.use('/api/users', usersRouter); server.use('/api/users', usersRouter);
server.use('/api/users', sessionsRouter); server.use('/api/users', sessionsRouter);
server.use('/api/users', dailyAveragesRouter);
module.exports = server; module.exports = server;

Binary file not shown.

View File

@ -0,0 +1,10 @@
const db = require('../../data/dbConfig');
module.exports = {
getDailyAverageBy
}
function getDailyAverageBy(id) {
return db('dailyAverages')
.where({});
}

View File

@ -0,0 +1,18 @@
require("dotenv").config();
const router = require('express').Router();
const Averages = require('./dailyAveragesModel');
router.get('/:id/averages', (req, res) => {
Averages.findUserBy(req.params.id)
.first()
.then(response => {
res.status(200).json(response);
})
.catch(error => {
res.status(500).json(error);
});
});
module.exports = router;

View File

@ -5,12 +5,15 @@ module.exports = {
findUserSessions, findUserSessions,
updateSession, updateSession,
removeSession removeSession
}; }
async function addSession(session) { async function addSession(session) {
const [id] = await db('sessions') const [id] = await db('sessions')
.insert(session); .insert(session);
return findSessionById(id);
const newSession = await findSessionById(id)
computeSession(newSession);
return newSession;
} }
function findUserSessions(user_id) { function findUserSessions(user_id) {
@ -29,11 +32,37 @@ async function updateSession(id, changes) {
await db('sessions') await db('sessions')
.where({ id }) .where({ id })
.update(changes, '*'); .update(changes, '*');
return findSessionById(id); const updatedSession = await findSessionById(id);
computeSession(updatedSession);
return updatedSession;
} }
async function removeSession(id) { async function removeSession(id) {
await db('sessions') await db('sessions')
.where({ id }) .where({ id })
.del(); .del();
}
async function computeSession(session) {
if ( session.bed_tiredness && session.day_mood && session.wake_mood) {
// Calculate the sleep duration
let date1 = new Date(session.bed_time);
let date2 = new Date(session.wake_time);
let diffTime = Math.abs(date1.getTime() - date2.getTime());
diffTime = diffTime/(1000*60*60);
let sleep_duration = Math.round( diffTime * 100) / 100;
// Calculate the average mood
let average_mood = (session.bed_tiredness + session.wake_mood + session.day_mood) / 3;
let new_average_mood = Math.round( average_mood * 100) / 100;
await db('dailyAverages')
.insert({
session_id: session.id,
user_id: session.user_id,
sleep_duration: sleep_duration,
average_mood: new_average_mood,
})
.catch(err => console.log(err));
}
} }

View File

@ -8,7 +8,10 @@ router.post('/sessions', authenticate, (req, res) => {
.then(response => { .then(response => {
res.status(200).json(response); res.status(200).json(response);
}) })
.catch(err => res.send(err)); .catch(err => {
console.log(err);
res.send(err)
});
}); });
router.get('/:id/sessions', authenticate, (req, res) => { router.get('/:id/sessions', authenticate, (req, res) => {