Merge pull request #307 from Ohtuilmo/fix/display-instructor-timelogs

Fetch all time logs for instructors
This commit is contained in:
Regularmute
2024-05-03 14:45:06 +03:00
committed by GitHub
+4 -31
View File
@@ -1,5 +1,5 @@
const instructorTimeLogsRouter = require('express').Router()
const { checkLogin } = require('../middleware')
const { checkInstructor } = require('../middleware')
const db = require('../models/index')
const { Sequelize } = require('sequelize')
@@ -71,37 +71,10 @@ const fetchAllFromDb = async () => {
}
}
const fetchGroupIdsFromLatestConfiguration = async () => {
instructorTimeLogsRouter.get('/', checkInstructor, async (req, res) => {
try {
const latestConfigurationId = await db.Configuration.findOne({
order: [['created_at', 'DESC']],
attributes: ['id']
})
const groups = await db.Group.findAll({
where: {
configuration_id: latestConfigurationId.id
}
})
return groups.map(group => group.id)
} catch (error) {
console.error('Error fetching groups:', error)
return false
}
}
instructorTimeLogsRouter.get('/', checkLogin, async (req, res) => {
try {
if (req.user.admin ) {
const timeLogs = await fetchAllFromDb()
return res.status(200).json(timeLogs)
} else if (req.user.instructor) {
const timeLogs = await fetchAllFromDb()
const activeGroupIds = await fetchGroupIdsFromLatestConfiguration()
const filteredTimeLogs = timeLogs.filter(log => activeGroupIds.includes(log.groupId))
return res.status(200).json(filteredTimeLogs)
}
return res.status(302).json({ error: 'Not authorized.' })
const timeLogs = await fetchAllFromDb()
return res.status(200).json(timeLogs)
} catch (error) {
return res.status(500).json({ error: 'Error fetching time logs.' })
}