Skip to content

Commit

Permalink
Merge pull request #307 from Ohtuilmo/fix/display-instructor-timelogs
Browse files Browse the repository at this point in the history
Fetch all time logs for instructors
  • Loading branch information
Regularmute authored May 3, 2024
2 parents c3cabb7 + 027afc0 commit 1f059d8
Showing 1 changed file with 4 additions and 31 deletions.
35 changes: 4 additions & 31 deletions backend/controllers/instructorTimeLogs.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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.' })
}
Expand Down

0 comments on commit 1f059d8

Please sign in to comment.