Add dailyAverage when providing complete session
This commit is contained in:
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,11 +32,37 @@ 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) {
|
||||
await db('sessions')
|
||||
.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) => {
|
||||
|
||||
Reference in New Issue
Block a user