feat(products): include ownerId in product creation and enforce its requirement

This commit is contained in:
Nils-Johan Gynther
2026-05-02 19:12:26 +02:00
parent 4e568b4d2e
commit 5842646e77
3 changed files with 15 additions and 10 deletions
+2 -2
View File
@@ -128,8 +128,8 @@ export class ProductsController {
@Roles('admin')
@Post()
create(@Body() body: CreateProductDto) {
return this.productsService.create(body);
create(@Body() body: CreateProductDto, @Request() req: { user: { id: number } }) {
return this.productsService.create(body, req.user.id);
}
// Tillgänglig för alla inloggade användare — req.user.id injiceras av JWT-guard
+6 -1
View File
@@ -116,7 +116,7 @@ export class ProductsService {
return product;
}
async create(data: CreateProductDto) {
async create(data: CreateProductDto, ownerId?: number) {
const name = data.name.trim();
const normalizedName = normalizeName(name);
@@ -140,8 +140,13 @@ export class ProductsService {
return existing;
}
if (!ownerId) {
throw new Error('ownerId är obligatorisk för att skapa en produkt');
}
return this.prisma.product.create({
data: {
ownerId,
name,
normalizedName,
canonicalName: name,