Skip to content

Commit

Permalink
Fix previous pr (#22)
Browse files Browse the repository at this point in the history
* Fix freeze boolean in ranking

* Add usersPoints endpoint

* Fix fetching freeze value
  • Loading branch information
JakubZojdzik authored Apr 20, 2024
1 parent 68b68e9 commit 52e4100
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/controllers/users.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const pool = require('../services/db.service');
const isAdminUtil = require('../utils/isAdminUtil');

dotenv.config();
const competitionConf = yaml.load(fs.readFileSync(process.env.SOK_CONFIG, 'utf8'));

const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
Expand All @@ -23,7 +22,6 @@ const transporter = nodemailer.createTransport({
const signToken = (username, expTime) => jwt.sign(username, process.env.TOKEN_SECRET, { expiresIn: expTime });

const sendMail = (destination, subject, text, html) => {
console.log('wysylam', html);
const message = {
from: process.env.SMTP_FROM,
to: destination,
Expand Down Expand Up @@ -179,9 +177,9 @@ const solves = async (request, response) => {
const ranking = async (request, response) => {
const { id } = request.body;
const admin = await isAdminUtil(id);
const freeze = competitionConf.endTime;
const competitionConf = yaml.load(fs.readFileSync(process.env.SOK_CONFIG, 'utf8'));
let dbRes;
if (freeze === 'true' && !admin) {
if (competitionConf.freeze === 'true' && !admin) {
const freezeTime = new Date(Date.parse(competitionConf.freezeTime));
dbRes = await pool.query(
`
Expand Down Expand Up @@ -231,6 +229,32 @@ const ranking = async (request, response) => {
return response.status(200).send(dbRows);
};

const usersPoints = async (reuqest, response) => {
const { id } = reuqest.body;
if (!id) {
return response.status(403).send('Not permited!');
}
const dbRes = await pool.query(
`SELECT
COALESCE(SUM(CASE WHEN s.correct = true THEN c.points ELSE -1 END), 0) AS points
FROM
users u
JOIN
submits s ON u.id = s.usr_id
JOIN
challenges c ON s.chall_id = c.id
WHERE
admin = 0 AND verified = true AND u.id=$1
ORDER BY
points DESC, MAX(s.sent) ASC;`,
[id],
);
if (!dbRes.rowCount) {
return response.status(200).send(0);
}
return response.status(200).send(dbRes.rows[0].points);
};

const isAdmin = async (request, response) => {
const { id } = request.body;
if (!id) {
Expand All @@ -253,4 +277,5 @@ module.exports = {
verifyRegistration,
changePassword,
verifyPasswordChange,
usersPoints,
};
2 changes: 2 additions & 0 deletions src/routes/users.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ router.use('/solves', authenticateToken);
router.use('/islogged', authenticateToken);
router.use('/ranking', authenticateToken);
router.use('/isAdmin', authenticateToken);
router.use('/usersPoints', authenticateToken);

router.get('/solves', errorHandler(usersController.solves));
router.get('/islogged', errorHandler(usersController.isLogged));
router.get('/ranking', errorHandler(usersController.ranking));
router.get('/isAdmin', errorHandler(usersController.isAdmin));
router.get('/usersPoints', errorHandler(usersController.usersPoints));

router.post('/verify', errorHandler(usersController.verifyRegistration));
router.post('/verifyPass', errorHandler(usersController.verifyPasswordChange));
Expand Down

0 comments on commit 52e4100

Please sign in to comment.