feat(categories): implement category management with hierarchical structure and update product association

This commit is contained in:
Nils-Johan Gynther
2026-04-17 21:16:58 +02:00
parent a9e83544c5
commit cc8be88462
11 changed files with 286 additions and 42 deletions
+2
View File
@@ -13,6 +13,7 @@ import { ReceiptAliasModule } from './receipt-alias/receipt-alias.module';
import { AuthModule } from './auth/auth.module';
import { UsersModule } from './users/users.module';
import { UserProductsModule } from './user-products/user-products.module';
import { CategoriesModule } from './categories/categories.module';
import { JwtAuthGuard } from './auth/jwt-auth.guard';
@@ -31,6 +32,7 @@ import { JwtAuthGuard } from './auth/jwt-auth.guard';
AuthModule,
UsersModule,
UserProductsModule,
CategoriesModule,
],
providers: [
{
@@ -0,0 +1,20 @@
import { Controller, Get } from '@nestjs/common';
import { CategoriesService } from './categories.service';
import { Public } from '../auth/decorators/public.decorator';
@Controller('api/categories')
export class CategoriesController {
constructor(private readonly categoriesService: CategoriesService) {}
@Public()
@Get()
findAll() {
return this.categoriesService.findAll();
}
@Public()
@Get('tree')
findTree() {
return this.categoriesService.findTree();
}
}
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { CategoriesService } from './categories.service';
import { CategoriesController } from './categories.controller';
import { PrismaModule } from '../prisma/prisma.module';
@Module({
imports: [PrismaModule],
providers: [CategoriesService],
controllers: [CategoriesController],
exports: [CategoriesService],
})
export class CategoriesModule {}
@@ -0,0 +1,33 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
export type CategoryNode = {
id: number;
name: string;
parentId: number | null;
children: CategoryNode[];
};
@Injectable()
export class CategoriesService {
constructor(private readonly prisma: PrismaService) {}
async findAll() {
return this.prisma.category.findMany({ orderBy: { name: 'asc' } });
}
async findTree(): Promise<CategoryNode[]> {
const all = await this.prisma.category.findMany({ orderBy: { name: 'asc' } });
const map = new Map<number, CategoryNode>();
all.forEach((c) => map.set(c.id, { ...c, children: [] }));
const roots: CategoryNode[] = [];
map.forEach((node) => {
if (node.parentId === null) {
roots.push(node);
} else {
map.get(node.parentId)?.children.push(node);
}
});
return roots;
}
}
@@ -1,4 +1,4 @@
import { IsNotEmpty, IsOptional, IsString, MaxLength } from 'class-validator';
import { IsNotEmpty, IsNumber, IsOptional, IsString, MaxLength } from 'class-validator';
export class UpdateProductDto {
@IsOptional()
@@ -26,4 +26,8 @@ export class UpdateProductDto {
@IsString()
@MaxLength(191)
brand?: string;
@IsOptional()
@IsNumber()
categoryId?: number | null;
}
+6
View File
@@ -21,6 +21,7 @@ export class ProductsService {
include: {
tags: { include: { tag: true } },
nutrition: true,
categoryRef: { include: { parent: { include: { parent: true } } } },
},
orderBy: { name: 'asc' },
});
@@ -114,6 +115,7 @@ export class ProductsService {
category?: string | null;
subcategory?: string | null;
brand?: string | null;
categoryId?: number | null;
} = {};
if (typeof data.name === 'string') {
@@ -160,6 +162,10 @@ export class ProductsService {
updateData.brand = data.brand.trim() || null;
}
if ('categoryId' in data) {
updateData.categoryId = data.categoryId ?? null;
}
return this.prisma.product.update({
where: { id },
data: updateData,