Fix dailyAverages query on session update & test
- Add dailyAverages seeds - Start implementing testing
This commit is contained in:
parent
7bddb9a54e
commit
77a4c28757
32
.eslintrc.js
32
.eslintrc.js
@ -1,17 +1,19 @@
|
||||
module.exports = {
|
||||
"env": {
|
||||
"commonjs": true,
|
||||
"es6": true,
|
||||
"node": true
|
||||
},
|
||||
"extends": "eslint:recommended",
|
||||
"globals": {
|
||||
"Atomics": "readonly",
|
||||
"SharedArrayBuffer": "readonly"
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2018
|
||||
},
|
||||
"rules": {
|
||||
}
|
||||
"env": {
|
||||
"commonjs": true,
|
||||
"es6": true,
|
||||
"node": true,
|
||||
jest: true
|
||||
},
|
||||
"extends": "eslint:recommended",
|
||||
"globals": {
|
||||
"Atomics": "readonly",
|
||||
"SharedArrayBuffer": "readonly"
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2018
|
||||
},
|
||||
"rules": {
|
||||
semi: 1
|
||||
}
|
||||
};
|
@ -3,6 +3,6 @@ require("dotenv").config();
|
||||
const knex = require('knex');
|
||||
const config = require('../knexfile');
|
||||
|
||||
const environment = process.env.DB_ENV || 'development';
|
||||
const environment = process.env.DB_ENV || 'test';
|
||||
|
||||
module.exports = knex(config[environment]);
|
||||
|
@ -52,6 +52,7 @@ exports.up = function(knex) {
|
||||
.unsigned()
|
||||
.notNullable()
|
||||
.references('sessions.id')
|
||||
.unique()
|
||||
.onUpdate('CASCADE')
|
||||
.onDelete('CASCADE');
|
||||
tbl
|
||||
@ -66,7 +67,7 @@ exports.up = function(knex) {
|
||||
.unsigned();
|
||||
tbl
|
||||
.float('average_mood');
|
||||
})
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function(knex) {
|
||||
|
77
data/seeds/addDailyAverages.js
Normal file
77
data/seeds/addDailyAverages.js
Normal file
@ -0,0 +1,77 @@
|
||||
exports.seed = function(knex) {
|
||||
return knex('dailyAverages').truncate()
|
||||
.then(function () {
|
||||
return knex('dailyAverages').insert([
|
||||
{
|
||||
"id": 1,
|
||||
"session_id": 1,
|
||||
"user_id": 1,
|
||||
"sleep_duration": 9,
|
||||
"average_mood": 3
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"session_id": 2,
|
||||
"user_id": 1,
|
||||
"sleep_duration": 7.67,
|
||||
"average_mood": 4
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"session_id": 3,
|
||||
"user_id": 1,
|
||||
"sleep_duration": 7.67,
|
||||
"average_mood": 4
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"session_id": 4,
|
||||
"user_id": 1,
|
||||
"sleep_duration": 6.67,
|
||||
"average_mood": 3
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"session_id": 5,
|
||||
"user_id": 1,
|
||||
"sleep_duration": 9.5,
|
||||
"average_mood": 4
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"session_id": 6,
|
||||
"user_id": 1,
|
||||
"sleep_duration": 7.5,
|
||||
"average_mood": 4
|
||||
},
|
||||
{
|
||||
"id": 7,
|
||||
"session_id": 7,
|
||||
"user_id": 1,
|
||||
"sleep_duration": 8.58,
|
||||
"average_mood": 2.67
|
||||
},
|
||||
{
|
||||
"id": 8,
|
||||
"session_id": 8,
|
||||
"user_id": 1,
|
||||
"sleep_duration": 8.42,
|
||||
"average_mood": 4.33
|
||||
},
|
||||
{
|
||||
"id": 9,
|
||||
"session_id": 9,
|
||||
"user_id": 1,
|
||||
"sleep_duration": 6.25,
|
||||
"average_mood": 3.33
|
||||
},
|
||||
{
|
||||
"id": 10,
|
||||
"session_id": 10,
|
||||
"user_id": 1,
|
||||
"sleep_duration": 11.25,
|
||||
"average_mood": 3.67
|
||||
}
|
||||
]);
|
||||
});
|
||||
};
|
Binary file not shown.
@ -5,7 +5,10 @@
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node index.js",
|
||||
"test": "node test"
|
||||
"test": "jest --watch"
|
||||
},
|
||||
"jest": {
|
||||
"testEnvironment": "node"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
@ -2,9 +2,9 @@ const db = require('../../data/dbConfig');
|
||||
|
||||
module.exports = {
|
||||
getDailyAverages
|
||||
}
|
||||
};
|
||||
|
||||
function getDailyAverages(id) {
|
||||
return db('dailyAverages')
|
||||
.where({ id });
|
||||
.where({user_id : id });
|
||||
}
|
@ -4,7 +4,7 @@ const router = require('express').Router();
|
||||
const Averages = require('./dailyAveragesModel');
|
||||
|
||||
router.get('/:id/averages', (req, res) => {
|
||||
Averages.getDailyAverages(req.params.id)
|
||||
Averages.getDailyAverages(req.params.id)
|
||||
.then(averages => {
|
||||
res.status(200).json(averages);
|
||||
})
|
||||
|
@ -12,7 +12,7 @@ async function addSession(session) {
|
||||
.insert(session);
|
||||
|
||||
const newSession = await findSessionById(id)
|
||||
computeSession(newSession);
|
||||
addDailyAverage(newSession);
|
||||
return newSession;
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ async function updateSession(id, changes) {
|
||||
.where({ id })
|
||||
.update(changes, '*');
|
||||
const updatedSession = await findSessionById(id);
|
||||
computeSession(updatedSession);
|
||||
updateDailyAverage(id, updatedSession);
|
||||
return updatedSession;
|
||||
}
|
||||
|
||||
@ -43,7 +43,8 @@ async function removeSession(id) {
|
||||
.del();
|
||||
}
|
||||
|
||||
async function computeSession(session) {
|
||||
//TODO: Refactor code
|
||||
async function addDailyAverage(session) {
|
||||
if ( session.bed_tiredness && session.day_mood && session.wake_mood) {
|
||||
// Calculate the sleep duration
|
||||
let date1 = new Date(session.bed_time);
|
||||
@ -65,4 +66,29 @@ async function computeSession(session) {
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
}
|
||||
}
|
||||
|
||||
async function updateDailyAverage(id, 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')
|
||||
.where({ session_id: id })
|
||||
.update({
|
||||
session_id: session.id,
|
||||
user_id: session.user_id,
|
||||
sleep_duration: sleep_duration,
|
||||
average_mood: new_average_mood,
|
||||
}, '*')
|
||||
.catch(err => console.log(err));
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ const router = require('express').Router();
|
||||
|
||||
const Sessions = require('../sessions/sessionsModel');
|
||||
|
||||
// TODO: add validation
|
||||
router.post('/sessions', (req, res) => {
|
||||
Sessions.addSession(req.body)
|
||||
.then(response => {
|
||||
|
45
services/users/users.spec.js
Normal file
45
services/users/users.spec.js
Normal file
@ -0,0 +1,45 @@
|
||||
const db = require('../../data/dbConfig');
|
||||
const Users = require('./usersModel');
|
||||
|
||||
beforeEach(async () => {
|
||||
await db('users').truncate();
|
||||
});
|
||||
|
||||
describe('Users.addUser', () => {
|
||||
|
||||
it('is able to insert user to the db', async () => {
|
||||
await Users.addUser({
|
||||
email: "gabetest@ls.com",
|
||||
username: "gabetest",
|
||||
password: "1234"
|
||||
});
|
||||
await Users.addUser({
|
||||
email: "gabetest2@ls.com",
|
||||
username: "gabetest2",
|
||||
password: "1234"
|
||||
});
|
||||
|
||||
let users = await Users.findUserById(2);
|
||||
expect(users.username).toBe('gabetest2');
|
||||
});
|
||||
|
||||
// it('is only able to insert user as a string', async () => {
|
||||
// let users = await Users.addUser({ username: 13 });
|
||||
|
||||
// let user = await Users.findUserById(1);
|
||||
// expect(user.name).toBe('13');
|
||||
// });
|
||||
});
|
||||
|
||||
// describe('Users.remove', () => {
|
||||
// it('is able to remove an user', async () => {
|
||||
// await Users.insert({ name: 'dragon' });
|
||||
// await Users.insert({ name: 'phoenix' });
|
||||
// let users = await Users.get();
|
||||
|
||||
// users = await Users.remove(2);
|
||||
// users = await Users.get();
|
||||
// expect(users).toHaveLength(1);
|
||||
// expect(users[0].name).toBe('dragon');
|
||||
// });
|
||||
// });
|
@ -2,6 +2,7 @@ const router = require('express').Router();
|
||||
|
||||
const Users = require('../users/usersModel');
|
||||
|
||||
// TODO: add validation
|
||||
router.get('/:id', (req, res) => {
|
||||
Users.findUserById(req.params.id)
|
||||
.then(users => {
|
||||
|
Loading…
Reference in New Issue
Block a user