/* * Copyright (c) 2022. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ import { ForbiddenError, NotFoundError } from '@ebec/http'; import { PermissionName, isRealmResourceWritable } from '@authup/common'; import type { Request, Response } from 'routup'; import { sendAccepted, useRequestParam } from 'routup'; import { useDataSource } from 'typeorm-extension'; import { RobotRoleEntity } from '@authup/server-database'; import { useRequestEnv } from '../../../utils/env'; export async function deleteRobotRoleRouteHandler(req: Request, res: Response) : Promise { const id = useRequestParam(req, 'id'); const ability = useRequestEnv(req, 'ability'); if (!ability.has(PermissionName.ROBOT_ROLE_DROP)) { throw new ForbiddenError(); } const dataSource = await useDataSource(); const repository = dataSource.getRepository(RobotRoleEntity); const entity = await repository.findOneBy({ id }); if (!entity) { throw new NotFoundError(); } if ( !isRealmResourceWritable(useRequestEnv(req, 'realm'), entity.robot_realm_id) || !isRealmResourceWritable(useRequestEnv(req, 'realm'), entity.role_realm_id) ) { throw new ForbiddenError(); } const { id: entityId } = entity; await repository.remove(entity); entity.id = entityId; return sendAccepted(res, entity); }