feat(config): alta de usuarios desde Configuraciones (POST /config/usuarios, solo admin)
This commit is contained in:
@@ -7,6 +7,10 @@
|
||||
*/
|
||||
|
||||
import { prisma } from './prisma.js'
|
||||
import bcrypt from 'bcryptjs'
|
||||
import { Prisma } from '@prisma/client'
|
||||
import { normalizarUsername } from './auth.service.js'
|
||||
import { BadRequestError } from '../utils/errors.js'
|
||||
import { accionesPorRol, type AccionClave } from '../config/acciones.js'
|
||||
import { MATRIZ } from '../config/roles.js'
|
||||
import type { Modulo } from '../config/roles.js'
|
||||
@@ -192,3 +196,44 @@ export async function actualizarUsuario(
|
||||
if (data.activo !== undefined) clean.activo = data.activo
|
||||
await prisma.usuario.update({ where: { id }, data: clean as never })
|
||||
}
|
||||
|
||||
/** Crea un usuario (solo admin): username derivado del nombre, bcrypt cost 12 */
|
||||
export async function crearUsuario(datos: {
|
||||
nombre: string
|
||||
rol: string
|
||||
asignacion: string | null
|
||||
password: string
|
||||
}): Promise<UsuarioListado> {
|
||||
const username = normalizarUsername(datos.nombre)
|
||||
const passwordHash = bcrypt.hashSync(datos.password, 12)
|
||||
try {
|
||||
const u = await prisma.usuario.create({
|
||||
data: {
|
||||
username,
|
||||
nombre: datos.nombre.trim().replace(/\s+/g, ' '),
|
||||
passwordHash,
|
||||
rol: datos.rol,
|
||||
asignacion: datos.asignacion,
|
||||
activo: true,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
nombre: true,
|
||||
rol: true,
|
||||
asignacion: true,
|
||||
activo: true,
|
||||
mondayUserId: true,
|
||||
},
|
||||
})
|
||||
return u
|
||||
} catch (err) {
|
||||
if (
|
||||
err instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
err.code === 'P2002'
|
||||
) {
|
||||
throw new BadRequestError(`El usuario "${username}" ya existe`)
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user