Add scouthub-home-be
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user