feat: add updateCategoryMine endpoint to manage product category updates
Test Suite / test (24.15.0) (push) Has been cancelled
Test Suite / test (24.15.0) (push) Has been cancelled
- Implemented a new PATCH endpoint in ProductsController to update the category of a user's product. - Added corresponding service method in ProductsService to handle business logic and validation. - Created UpdateCategoryMineDto for request validation. - Enhanced error handling for forbidden actions and not found resources. - Updated API error mapping in Flutter to handle specific forbidden messages. - Modified ProductPickerField to allow product creation directly from the picker. - Added tests for the new endpoint and service method to ensure proper functionality and error handling.
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import { ForbiddenException, NotFoundException } from '@nestjs/common';
|
||||
import { ProductsService } from './products.service';
|
||||
|
||||
describe('ProductsService.updateCategoryMine', () => {
|
||||
const prismaMock = {
|
||||
product: {
|
||||
findUnique: jest.fn(),
|
||||
update: jest.fn(),
|
||||
},
|
||||
category: {
|
||||
findUnique: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
const aiServiceMock = {
|
||||
suggestCategory: jest.fn(),
|
||||
};
|
||||
|
||||
const categoriesServiceMock = {
|
||||
findFlattened: jest.fn(),
|
||||
};
|
||||
|
||||
const service = new ProductsService(
|
||||
prismaMock as any,
|
||||
aiServiceMock as any,
|
||||
categoriesServiceMock as any,
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('kastar NotFound om produkt saknas', async () => {
|
||||
prismaMock.product.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(service.updateCategoryMine(42, 100, 5)).rejects.toBeInstanceOf(NotFoundException);
|
||||
});
|
||||
|
||||
it('kastar Forbidden om produkten inte ägs av användaren', async () => {
|
||||
prismaMock.product.findUnique.mockResolvedValue({
|
||||
id: 100,
|
||||
ownerId: 7,
|
||||
isPrivate: true,
|
||||
});
|
||||
|
||||
await expect(service.updateCategoryMine(42, 100, 5)).rejects.toBeInstanceOf(ForbiddenException);
|
||||
});
|
||||
|
||||
it('kastar Forbidden för globala produkter', async () => {
|
||||
prismaMock.product.findUnique.mockResolvedValue({
|
||||
id: 100,
|
||||
ownerId: 42,
|
||||
isPrivate: false,
|
||||
});
|
||||
|
||||
await expect(service.updateCategoryMine(42, 100, 5)).rejects.toBeInstanceOf(ForbiddenException);
|
||||
expect(prismaMock.product.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('kastar NotFound om kategori saknas', async () => {
|
||||
prismaMock.product.findUnique.mockResolvedValue({
|
||||
id: 100,
|
||||
ownerId: 42,
|
||||
isPrivate: true,
|
||||
});
|
||||
prismaMock.category.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(service.updateCategoryMine(42, 100, 9999)).rejects.toBeInstanceOf(NotFoundException);
|
||||
expect(prismaMock.product.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('uppdaterar kategori för ägd icke-global produkt', async () => {
|
||||
prismaMock.product.findUnique.mockResolvedValue({
|
||||
id: 100,
|
||||
ownerId: 42,
|
||||
isPrivate: true,
|
||||
});
|
||||
prismaMock.category.findUnique.mockResolvedValue({ id: 5 });
|
||||
prismaMock.product.update.mockResolvedValue({ id: 100, categoryId: 5 });
|
||||
|
||||
await expect(service.updateCategoryMine(42, 100, 5)).resolves.toEqual({
|
||||
id: 100,
|
||||
categoryId: 5,
|
||||
});
|
||||
expect(prismaMock.product.update).toHaveBeenCalledWith({
|
||||
where: { id: 100 },
|
||||
data: { categoryId: 5 },
|
||||
select: { id: true, categoryId: true },
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user