Roles section

This commit is contained in:
2026-01-29 16:41:21 -06:00
parent 13cc4528ff
commit 33b072436d
3 changed files with 315 additions and 97 deletions

View File

@@ -1,5 +1,5 @@
import { Response } from 'express';
import { AuthenticatedRequest } from '../middleware/auth.middleware';
import { AuthenticatedRequest } from '../types';
import * as roleService from '../services/role.service';
import { CreateRoleInput, UpdateRoleInput } from '../validators/role.validator';
@@ -37,9 +37,10 @@ export async function getRoleById(
res: Response
): Promise<void> {
try {
const roleId = parseInt(req.params.id, 10);
const roleId = req.params.id;
if (isNaN(roleId)) {
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
if (!uuidRegex.test(roleId)) {
res.status(400).json({
success: false,
error: 'Invalid role ID',
@@ -120,9 +121,10 @@ export async function updateRole(
res: Response
): Promise<void> {
try {
const roleId = parseInt(req.params.id, 10);
const roleId = req.params.id;
if (isNaN(roleId)) {
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
if (!uuidRegex.test(roleId)) {
res.status(400).json({
success: false,
error: 'Invalid role ID',
@@ -178,9 +180,10 @@ export async function deleteRole(
res: Response
): Promise<void> {
try {
const roleId = parseInt(req.params.id, 10);
const roleId = req.params.id;
if (isNaN(roleId)) {
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
if (!uuidRegex.test(roleId)) {
res.status(400).json({
success: false,
error: 'Invalid role ID',

View File

@@ -32,10 +32,10 @@ export async function getAll(): Promise<Role[]> {
/**
* Get a single role by ID with user count
* @param id - Role ID
* @param id - Role ID (UUID)
* @returns Role with user count or null if not found
*/
export async function getById(id: number): Promise<RoleWithUserCount | null> {
export async function getById(id: string): Promise<RoleWithUserCount | null> {
const result = await query(
`
SELECT
@@ -123,12 +123,12 @@ export async function create(data: {
/**
* Update a role
* @param id - Role ID
* @param id - Role ID (UUID)
* @param data - Fields to update
* @returns Updated role or null if not found
*/
export async function update(
id: number,
id: string,
data: {
name?: string;
description?: string | null;
@@ -194,11 +194,11 @@ export async function update(
/**
* Delete a role (only if no users assigned)
* @param id - Role ID
* @param id - Role ID (UUID)
* @returns True if deleted, false if role not found
* @throws Error if users are assigned to the role
*/
export async function deleteRole(id: number): Promise<boolean> {
export async function deleteRole(id: string): Promise<boolean> {
// Check if role exists and get user count
const role = await getById(id);