Add dailyAverage when providing complete session
This commit is contained in:
parent
856f8c753d
commit
c36391321c
@ -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
|
||||
- `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
|
@ -5,6 +5,7 @@ const cors = require('cors');
|
||||
const authRouter = require('../services/auth/authRouter');
|
||||
const usersRouter = require('../services/users/usersRouter');
|
||||
const sessionsRouter = require('../services/sessions/sessionsRouter');
|
||||
const dailyAveragesRouter = require('../services/dailyAverages/dailyAveragesRouter');
|
||||
|
||||
const server = express();
|
||||
|
||||
@ -15,5 +16,6 @@ server.use(cors());
|
||||
server.use('/api/auth', authRouter);
|
||||
server.use('/api/users', usersRouter);
|
||||
server.use('/api/users', sessionsRouter);
|
||||
server.use('/api/users', dailyAveragesRouter);
|
||||
|
||||
module.exports = server;
|
||||
|
Binary file not shown.
10
services/dailyAverages/dailyAveragesModel.js
Normal file
10
services/dailyAverages/dailyAveragesModel.js
Normal file
@ -0,0 +1,10 @@
|
||||
const db = require('../../data/dbConfig');
|
||||
|
||||
module.exports = {
|
||||
getDailyAverageBy
|
||||
}
|
||||
|
||||
function getDailyAverageBy(id) {
|
||||
return db('dailyAverages')
|
||||
.where({});
|
||||
}
|
18
services/dailyAverages/dailyAveragesRouter.js
Normal file
18
services/dailyAverages/dailyAveragesRouter.js
Normal 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;
|
@ -5,12 +5,15 @@ module.exports = {
|
||||
findUserSessions,
|
||||
updateSession,
|
||||
removeSession
|
||||
};
|
||||
}
|
||||
|
||||
async function addSession(session) {
|
||||
const [id] = await db('sessions')
|
||||
.insert(session);
|
||||
return findSessionById(id);
|
||||
|
||||
const newSession = await findSessionById(id)
|
||||
computeSession(newSession);
|
||||
return newSession;
|
||||
}
|
||||
|
||||
function findUserSessions(user_id) {
|
||||
@ -29,7 +32,9 @@ async function updateSession(id, changes) {
|
||||
await db('sessions')
|
||||
.where({ id })
|
||||
.update(changes, '*');
|
||||
return findSessionById(id);
|
||||
const updatedSession = await findSessionById(id);
|
||||
computeSession(updatedSession);
|
||||
return updatedSession;
|
||||
}
|
||||
|
||||
async function removeSession(id) {
|
||||
@ -37,3 +42,27 @@ async function removeSession(id) {
|
||||
.where({ id })
|
||||
.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));
|
||||
}
|
||||
}
|
@ -8,7 +8,10 @@ router.post('/sessions', authenticate, (req, res) => {
|
||||
.then(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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user