Skip to content

TaskView is a self-hosted project and task management platform focused on clarity, ownership, and control. TaskView is built for teams that want a transparent, self-hosted alternative to SaaS task managers.

1.2 KB
Raw Blame
import type { NextFunction, Request, Response } from 'express'
import { $logger } from '../../../modules/logget'
import { GoalPermissions } from '../../../types/auth.types'
import { logError } from '../../../utils/api'

type Action = 'view' | 'edit'

export const canAccessTimeEntry = (action: Action) => async (req: Request, res: Response, next: NextFunction) => {
    const entryId = Number(req.params.id)
    if (!entryId) return res.status(400).end()

    const entry = await req.appUser.timeTrackingManager.repository.findById(entryId).catch(logError)
    if (!entry) return res.status(404).end()

    const checker = await req.appUser.permissionsFetcher
        .getCheckerForGoal(entry.goalId)
        .catch(logError)

    if (!checker) {
        $logger.error('Can not get permissions for canAccessTimeEntry middleware')
        return res.status(500).end()
    }

    if (action === 'view') {
        if (
            checker.hasPermissions(GoalPermissions.TIMETRACKING_CAN_VIEW)
        ) {
            return next()
        }
        return res.status(403).end()
    }

    if (checker.hasPermissions(GoalPermissions.TIMETRACKING_CAN_MANAGE_ALL)) return next()

    return res.status(403).end()
}