feat: add functionality for managing deleted products, including restoration and permanent deletion

This commit is contained in:
Nils-Johan Gynther
2026-04-21 13:30:44 +02:00
parent 4074b850cb
commit 87eab4d0ca
7 changed files with 323 additions and 22 deletions
@@ -186,6 +186,18 @@ export class ProductsController {
return this.productsService.update(id, body);
}
@Roles('admin')
@Get('deleted')
findDeleted() {
return this.productsService.findDeleted();
}
@Roles('admin')
@Delete(':id/permanent')
permanentDelete(@Param('id', ParseIntPipe) id: number) {
return this.productsService.permanentDelete(id);
}
@Roles('admin')
@Delete(':id')
remove(@Param('id', ParseIntPipe) id: number) {
+20 -13
View File
@@ -127,19 +127,8 @@ export class ProductsService {
});
if (existing && existing.id !== id) {
if (!existing.isActive) {
return this.prisma.product.update({
where: { id: existing.id },
data: {
isActive: true,
deletedAt: null,
name,
canonicalName: name,
},
});
}
return existing;
// Om en annan produkt har samma namn, returnera ett tydligt fel
throw new Error('Det finns redan en annan produkt med detta namn. Välj ett unikt namn.');
}
updateData.name = name;
@@ -186,6 +175,13 @@ export class ProductsService {
});
}
async findDeleted() {
return this.prisma.product.findMany({
where: { isActive: false },
orderBy: { deletedAt: 'desc' },
});
}
async remove(id: number) {
await this.findOne(id);
@@ -198,6 +194,17 @@ export class ProductsService {
});
}
async permanentDelete(id: number) {
const product = await this.prisma.product.findUnique({ where: { id } });
if (!product) {
throw new NotFoundException(`Product with id ${id} not found`);
}
// Ta bort beroenden först
await this.prisma.productTag.deleteMany({ where: { productId: id } });
await this.prisma.userProduct.deleteMany({ where: { productId: id } });
return this.prisma.product.delete({ where: { id } });
}
async restore(id: number) {
const product = await this.findOne(id);