Add scouthub-home-be

This commit is contained in:
Lorenzo Sanesi
2026-07-23 19:36:55 +02:00
parent cfd2de4ca3
commit 566754f825
45 changed files with 8233 additions and 0 deletions
@@ -0,0 +1,42 @@
import { Request, Response, NextFunction } from 'express';
import { createGruppo } from '../services/gruppi.service';
import { HttpError } from '../errors';
interface PostGruppoBody {
nome?: unknown;
regione?: unknown;
ruoliDefault?: unknown;
}
function parseBody(body: PostGruppoBody): { nome: string; regione?: string; ruoliDefault?: string[] } {
if (typeof body.nome !== 'string' || body.nome.trim().length === 0) {
throw new HttpError(400, "Il campo 'nome' è obbligatorio ed è una stringa non vuota");
}
if (body.regione !== undefined && typeof body.regione !== 'string') {
throw new HttpError(400, "Il campo 'regione', se presente, deve essere una stringa");
}
if (body.ruoliDefault !== undefined) {
const isArrayOfStrings = Array.isArray(body.ruoliDefault) && body.ruoliDefault.every((r) => typeof r === 'string');
if (!isArrayOfStrings) {
throw new HttpError(400, "Il campo 'ruoliDefault', se presente, deve essere un array di stringhe");
}
}
return {
nome: body.nome,
regione: body.regione as string | undefined,
ruoliDefault: body.ruoliDefault as string[] | undefined,
};
}
export async function postGruppo(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const input = parseBody(req.body ?? {});
const result = await createGruppo({ ...input, userId: req.auth!.userId });
res.status(201).json(result);
} catch (err) {
next(err);
}
}
@@ -0,0 +1,11 @@
import { Request, Response, NextFunction } from 'express';
import { healthService } from '../services/health.service';
export async function getHealth(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
await healthService.checkDatabase();
res.json({ status: 'ok', database: 'up' });
} catch (err) {
next(err);
}
}
@@ -0,0 +1,65 @@
import { Request, Response, NextFunction } from 'express';
import { creaInvito, getInvitoPubblico, accettaInvito } from '../services/inviti.service';
import { HttpError } from '../errors';
interface PostInvitoBody {
email?: unknown;
ruolo?: unknown;
}
function parseCreaInvitoBody(body: PostInvitoBody): { 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 postInvito(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const { orgId } = req.params;
// Un capo gruppo può invitare solo all'interno della propria organization.
if (req.auth?.organizationId !== orgId) {
throw new HttpError(403, "Non puoi invitare persone in un'organizzazione diversa dalla tua");
}
const { email, ruolo } = parseCreaInvitoBody(req.body ?? {});
const result = await creaInvito({ orgId, email, ruolo });
res.status(201).json(result);
} catch (err) {
next(err);
}
}
export async function getInvito(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const { token } = req.params;
const invito = await getInvitoPubblico(token);
if (!invito) {
throw new HttpError(404, 'Invito non trovato');
}
res.json(invito);
} catch (err) {
next(err);
}
}
export async function postAccettaInvito(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const { token } = req.params;
const result = await accettaInvito(token, {
userId: req.auth!.userId,
email: req.auth!.email,
});
res.status(200).json(result);
} catch (err) {
next(err);
}
}
@@ -0,0 +1,50 @@
import { Request, Response, NextFunction } from 'express';
import { listaMembri, cambiaRuoloMembro, rimuoviMembro } 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");
}
return orgId;
}
export async function getMembri(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const orgId = checkOrgAccess(req);
const membri = await listaMembri(orgId);
res.json(membri);
} catch (err) {
next(err);
}
}
export async function putRuoloMembro(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const orgId = checkOrgAccess(req);
const { userId } = req.params;
const { ruolo } = req.body ?? {};
if (typeof ruolo !== 'string' || ruolo.trim().length === 0) {
throw new HttpError(400, "Il campo 'ruolo' è obbligatorio ed è una stringa non vuota");
}
await cambiaRuoloMembro(orgId, userId, ruolo);
res.status(200).json({ userId, ruolo });
} catch (err) {
next(err);
}
}
export async function deleteMembro(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const orgId = checkOrgAccess(req);
const { userId } = req.params;
await rimuoviMembro(orgId, userId);
res.status(204).send();
} catch (err) {
next(err);
}
}