Fix scouthub-home-be

This commit is contained in:
Lorenzo Sanesi
2026-07-25 12:09:12 +02:00
parent ac92ca43ce
commit 5b7c82d359
39 changed files with 2116 additions and 81 deletions
@@ -1,18 +1,36 @@
import { Request, Response, NextFunction } from 'express';
import { listaMembri, cambiaRuoloMembro, rimuoviMembro } from '../services/membri.service';
import { listaMembri, cambiaRuoloMembro, rimuoviMembro, aggiungiMembro } from '../services/membri.service';
import { HttpError } from '../errors';
function checkOrgAccess(req: Request): string {
const { orgId } = req.params;
if (req.auth?.organizationId !== orgId) {
throw new HttpError(403, "Non puoi gestire membri di un'organizzazione diversa dalla tua");
interface PostMembroBody {
email?: unknown;
ruolo?: unknown;
}
function parseAggiungiMembroBody(body: PostMembroBody): { email: string; ruolo: string } {
if (typeof body.email !== 'string' || body.email.trim().length === 0) {
throw new HttpError(400, "Il campo 'email' è obbligatorio ed è una stringa non vuota");
}
if (typeof body.ruolo !== 'string' || body.ruolo.trim().length === 0) {
throw new HttpError(400, "Il campo 'ruolo' è obbligatorio ed è una stringa non vuota");
}
return { email: body.email, ruolo: body.ruolo };
}
export async function postMembro(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const { orgId } = req.params;
const { email, ruolo } = parseAggiungiMembroBody(req.body ?? {});
const result = await aggiungiMembro({ orgId, email, ruolo });
res.status(201).json(result);
} catch (err) {
next(err);
}
return orgId;
}
export async function getMembri(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const orgId = checkOrgAccess(req);
const { orgId } = req.params;
const membri = await listaMembri(orgId);
res.json(membri);
} catch (err) {
@@ -22,8 +40,7 @@ export async function getMembri(req: Request, res: Response, next: NextFunction)
export async function putRuoloMembro(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const orgId = checkOrgAccess(req);
const { userId } = req.params;
const { orgId, userId } = req.params;
const { ruolo } = req.body ?? {};
if (typeof ruolo !== 'string' || ruolo.trim().length === 0) {
@@ -39,8 +56,7 @@ export async function putRuoloMembro(req: Request, res: Response, next: NextFunc
export async function deleteMembro(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const orgId = checkOrgAccess(req);
const { userId } = req.params;
const { orgId, userId } = req.params;
await rimuoviMembro(orgId, userId);
res.status(204).send();