Skip to content

Commit

Permalink
Merge pull request #634 from elmadev/metsavir-stats-date-filter
Browse files Browse the repository at this point in the history
Add time filter to getLevelStatsForPlayer
  • Loading branch information
sunehs authored Jan 4, 2024
2 parents 10edd83 + f75f32b commit 105b25a
Show file tree
Hide file tree
Showing 4 changed files with 589 additions and 189 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
"sib-api-v3-sdk": "^8.4.2"
},
"devDependencies": {
"@babel/eslint-parser": "^7.19.1",
"@babel/core": "^7.23.3",
"@babel/eslint-parser": "^7.23.3",
"@babel/plugin-syntax-import-assertions": "^7.20.0",
"eslint": "latest",
"eslint": "^8.54.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"nodemon": "^2.0.19",
Expand Down
12 changes: 9 additions & 3 deletions src/api/level.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { has } from 'lodash-es';
import { getLevel as getLevelSecure } from '#utils/download';
import { Level, Time, LevelStats, Battle } from '../data/models';
import connection from '../data/sequelize';
import { fromToTime } from '#utils/database';

const router = express.Router();

Expand Down Expand Up @@ -79,7 +80,7 @@ const getLevelData = async LevelIndex => {
}
};

const getLevelStatsForPlayer = async (LevelIndex, KuskiIndex) => {
const getLevelStatsForPlayer = async (LevelIndex, KuskiIndex, from, to) => {
const stats = await Time.findAll({
group: ['Finished'],
attributes: [
Expand All @@ -89,7 +90,7 @@ const getLevelStatsForPlayer = async (LevelIndex, KuskiIndex) => {
[sequelize.fn('min', sequelize.col('Driven')), 'FirstPlayed'],
[sequelize.fn('max', sequelize.col('Driven')), 'LastPlayed'],
],
where: { LevelIndex, KuskiIndex },
where: { LevelIndex, KuskiIndex, ...fromToTime(from, to, 'Driven') },
});

return stats;
Expand Down Expand Up @@ -164,7 +165,12 @@ router.get('/timestats/:LevelIndex', async (req, res) => {
KuskiIndex = auth.userid;
}

const data = await getLevelStatsForPlayer(req.params.LevelIndex, KuskiIndex);
const data = await getLevelStatsForPlayer(
req.params.LevelIndex,
KuskiIndex,
req.query.from,
req.query.to,
);
res.json(data);
});

Expand Down
18 changes: 18 additions & 0 deletions src/utils/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,21 @@ export const fromTo = (from, to, column) => {
}
return where;
};

export const fromToTime = (from, to, column) => {
const where = {};
if (from && to) {
where[column] = {
[Op.between]: [from, to],
};
} else if (from) {
where[column] = {
[Op.gte]: from,
};
} else if (to) {
where[column] = {
[Op.lte]: to,
};
}
return where;
};
Loading

0 comments on commit 105b25a

Please sign in to comment.