Sistemato magazzino
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import { Categoria, StatoCategoria } from '@prisma/client';
|
||||
import { categorieRepository } from '../repositories/categorie.repository';
|
||||
import { HttpError } from '../errors';
|
||||
import { toHttpError } from '../utils/prisma-errors';
|
||||
import { creaNotificaModerazione } from './notifiche.service';
|
||||
|
||||
export function listCategorie(): Promise<Categoria[]> {
|
||||
return categorieRepository.findAll();
|
||||
}
|
||||
|
||||
export function listCategorieConfermate(nome?: string): Promise<Categoria[]> {
|
||||
return categorieRepository.findConfermate(nome);
|
||||
}
|
||||
|
||||
export function creaCategoria(nome: string): Promise<Categoria> {
|
||||
return categorieRepository.create({ nome, stato: StatoCategoria.confermata });
|
||||
}
|
||||
|
||||
export async function aggiornaCategoria(id: string, nome: string): Promise<Categoria> {
|
||||
try {
|
||||
return await categorieRepository.update(id, nome);
|
||||
} catch (err) {
|
||||
throw toHttpError(err, 'Categoria non trovata', "Conflitto durante l'aggiornamento della categoria");
|
||||
}
|
||||
}
|
||||
|
||||
export async function eliminaCategoria(id: string): Promise<void> {
|
||||
try {
|
||||
await categorieRepository.delete(id);
|
||||
} catch (err) {
|
||||
throw toHttpError(err, 'Categoria non trovata', 'Impossibile eliminare la categoria: è referenziata altrove');
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProponiCategoriaInput {
|
||||
nome: string;
|
||||
orgId: string;
|
||||
}
|
||||
|
||||
export async function proponiCategoria(input: ProponiCategoriaInput): Promise<Categoria> {
|
||||
const categoria = await categorieRepository.create({
|
||||
nome: input.nome,
|
||||
stato: StatoCategoria.da_approvare,
|
||||
creatoDaOrgId: input.orgId,
|
||||
});
|
||||
await creaNotificaModerazione(
|
||||
'CATEGORIA_PROPOSTA',
|
||||
`Nuova categoria proposta: "${categoria.nome}"`,
|
||||
'/tassonomie?tab=categorie',
|
||||
);
|
||||
return categoria;
|
||||
}
|
||||
|
||||
export async function approvaCategoria(id: string): Promise<Categoria> {
|
||||
const categoria = await categorieRepository.findById(id);
|
||||
if (!categoria) {
|
||||
throw new HttpError(404, 'Categoria non trovata');
|
||||
}
|
||||
if (categoria.stato !== StatoCategoria.da_approvare) {
|
||||
throw new HttpError(409, 'La categoria è già confermata');
|
||||
}
|
||||
|
||||
return categorieRepository.updateStato(id, StatoCategoria.confermata);
|
||||
}
|
||||
|
||||
export async function rifiutaCategoria(id: string): Promise<void> {
|
||||
const categoria = await categorieRepository.findById(id);
|
||||
if (!categoria) {
|
||||
throw new HttpError(404, 'Categoria non trovata');
|
||||
}
|
||||
if (categoria.stato !== StatoCategoria.da_approvare) {
|
||||
throw new HttpError(409, 'Solo le proposte in attesa possono essere rifiutate');
|
||||
}
|
||||
|
||||
await categorieRepository.delete(id);
|
||||
}
|
||||
Reference in New Issue
Block a user