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 } from 'arktype';
import type { NextFunction, Request, Response } from 'express';
import { $logger } from '../../../modules/logget';
import { GoalPermissions } from '../../../types/auth.types';
import { CollaborationArkTypeDeleteRoleFromGoal } from '../../collaboration/collaboration.server.types';

export const CanDeleteCollaborationRoles = async (req: Request, res: Response, next: NextFunction) => {
    const args = CollaborationArkTypeDeleteRoleFromGoal(req.body);

    if (args instanceof type.errors) {
        return res.status(400).send(args.summary);
    }

    const goalId = args.goalId;

    const goal = await req.appUser.goalsManager.goalsRepository.fetchGoalById(+goalId);

    if (!goal) {
        $logger.error('Can not get goal for CanDeleteCollaborationRoles middleware');
        return res.status(500).end();
    }

    //Only owners can manage roles itself
    if (goal.owner !== req.appUser.getUserData()?.id) {
        return res.status(403).end();
    }

    const permissions = await req.appUser.permissionsFetcher.getCheckerForGoal(goal.id);
    if (permissions.hasPermissions(GoalPermissions.GOAL_CAN_MANAGE_USERS)) {
        return next();
    }

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