Skip to content

Commit

Permalink
Support sorting parameter to diary api (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
oluizeduardo authored Oct 27, 2024
1 parent 08b7176 commit 626f5f9
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 99 deletions.
62 changes: 28 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "glicocheck-api",
"version": "1.1.6",
"version": "1.1.7",
"description": "API for diabetes management.",
"main": "index.js",
"type": "module",
Expand Down Expand Up @@ -34,7 +34,6 @@
"helmet": "^7.0.0",
"jsonwebtoken": "^9.0.0",
"knex": "^3.1.0",
"moment": "^2.29.4",
"node-cron": "^3.0.3",
"nodemailer": "^6.9.15",
"pg": "^8.13.0",
Expand Down
58 changes: 13 additions & 45 deletions src/controllers/diaryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import logger from '../loggerUtil/logger.js';
import Messages from '../utils/messages.js';
import DiaryDAO from '../dao/DiaryDAO.js';
import UserDAO from '../dao/UserDAO.js';
import moment from 'moment';
import DateTimeUtil from '../utils/dateTimeUtil.js';
import GlycemiaStatistics from '../utils/GlycemiaStatistics.js';

Expand Down Expand Up @@ -65,18 +64,10 @@ class DiaryController {
logger.info('Executing DiaryController.getAll');
try {
const { start, end } = req.query;

const result = await DiaryDAO.getAll();
const result = await DiaryDAO.getAll({ start, end });

if (result.success) {
let diary = result.diary;

if (start && end) {
diary = diary.filter((record) => {
return this.isDateBetween(start, end, record.dateTime);
});
}
return res.status(OK).json(diary);
return res.status(OK).json(result.diary);
} else {
return res.status(CLIENT_ERROR).json({ message: result.message });
}
Expand All @@ -88,22 +79,6 @@ class DiaryController {
}
};

/**
* Checks if a date is between two given dates, inclusive of the boundaries.
* @param {string} start - The start date in the format 'YYYY-MM-DD'.
* @param {string} end - The end date in the format 'YYYY-MM-DD'.
* @param {string} dateToCheck - The date to be checked in the format 'YYYY-MM-DD'.
* @return {boolean} True if the date is between the start and end dates, inclusive; otherwise, false.
*/
static isDateBetween(start, end, dateToCheck) {
const dateFormat = 'YYYY-MM-DD';
const inclusivity = '[]';
const startDate = moment(start, dateFormat);
const endDate = moment(end, dateFormat);
const date = moment(dateToCheck, dateFormat);
return date.isBetween(startDate, endDate, null, inclusivity);
}

static getById = async (req, res) => {
logger.info('Executing DiaryController.getById');
try {
Expand Down Expand Up @@ -232,23 +207,16 @@ class DiaryController {
logger.info('Executing DiaryController.getByUserCode');
try {
const userCode = req.params.usercode;
let { start, end, orderBy, sort } = req.query;

if (!userCode) {
return res.status(CLIENT_ERROR).json({ message: Messages.INCOMPLETE_DATA_PROVIDED });
}

const result = await DiaryDAO.getByUserCode(userCode);
const result = await DiaryDAO.getByUserCode(userCode, { start, end, orderBy, sort });

if (result.success) {
const { start, end } = req.query;
let diary = result.diary;

if (start && end) {
diary = diary.filter((record) => {
return this.isDateBetween(start, end, record.dateTime);
});
}
return res.status(OK).json(diary);
return res.status(OK).json(result.diary);
} else {
return res.status(NOT_FOUND).json({ message: result.message });
}
Expand All @@ -267,12 +235,10 @@ class DiaryController {
return res.status(CLIENT_ERROR).json({ message: Messages.INCOMPLETE_DATA_PROVIDED });
}

const result = await DiaryDAO.getByUserCode(userCode);
const result = await DiaryDAO.getByUserCode(userCode, {});

if (result.success) {
const glucoseReadings = result.diary.map(entry => entry.glucose);
const statistics = this.calculateStatistics(glucoseReadings);
res.status(OK).json(statistics);
res.status(OK).json(this.calculateStatistics(result.diary));
} else {
res.status(NOT_FOUND).json({ message: result.message });
}
Expand All @@ -282,10 +248,12 @@ class DiaryController {
}
};

static calculateStatistics(readings) {
const date_start = new Date().toLocaleDateString();
const date_end = new Date().toLocaleDateString();
const stats = new GlycemiaStatistics(readings).getAllStatistics();
static calculateStatistics(readings) {
const date_start = new Date(Math.min(...readings.map(item => new Date(item.dateTime))));
const date_end = new Date(Math.max(...readings.map(item => new Date(item.dateTime))));

const glucoseReadings = readings.map(entry => entry.glucose);
const stats = new GlycemiaStatistics(glucoseReadings).getAllStatistics();
return {
date_start,
date_end,
Expand Down
Loading

0 comments on commit 626f5f9

Please sign in to comment.