Skip to content

Commit

Permalink
[back]🐛fix: fix categories return
Browse files Browse the repository at this point in the history
  • Loading branch information
0thigs committed Nov 30, 2024
1 parent 7e9990d commit 721c645
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export class PrismaCategoriesRepository implements ICategoriesRepository {
}
}

async findMany({ page, companyId, name }: CategoriesListParams): Promise<Category[]> {
async findMany({
page,
companyId,
name,
}: CategoriesListParams): Promise<{ categories: Category[]; count: number }> {
try {
const prismaCategories = await prisma.category.findMany({
take: PAGINATION.itemsPerPage,
Expand All @@ -83,7 +87,7 @@ export class PrismaCategoriesRepository implements ICategoriesRepository {
},
})

return prismaCategories.map(this.mapper.toDomain)
return { categories: prismaCategories.map(this.mapper.toDomain), count }
} catch (error) {
throw new PrismaError(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ export class CategoriesRepositoryMock implements ICategoriesRepository {
return category
}

async findMany({ page }: CategoriesListParams): Promise<Category[]> {
const startIndex = (page - 1) * 10 // 10 pode ser o valor do PAGINATION.itemsPerPage
return this.categories.slice(startIndex, startIndex + 10)
async findMany({
page,
}: CategoriesListParams): Promise<{ categories: Category[]; count: number }> {
const startIndex = (page - 1) * 10
return {
categories: this.categories.slice(startIndex, startIndex + 10),
count: this.categories.length,
}
}

async addMany(categories: Category[]): Promise<void> {
this.categories.push(...categories)
}

async add(category: Category): Promise<void> {
Expand All @@ -26,24 +35,23 @@ export class CategoriesRepositoryMock implements ICategoriesRepository {

async update(category: Category): Promise<void> {
this.categories = this.categories.map((existingCategory) =>
existingCategory.id === category.id ? category : existingCategory
existingCategory.id === category.id ? category : existingCategory,
)
}

async deleteById(categoryId: string): Promise<void> {
const categoryToDelete = this.categories.find((cat) => cat.id === categoryId);
const categoryToDelete = this.categories.find((cat) => cat.id === categoryId)

if (!categoryToDelete) {
throw new NotFoundError();
throw new NotFoundError()
}
const subCategoryIds = categoryToDelete.dto.subCategories.map(subCat => subCat.id);
const subCategoryIds = categoryToDelete.dto.subCategories.map((subCat) => subCat.id)
if (subCategoryIds) {
this.categories = this.categories.filter(cat => {
return !subCategoryIds.includes(cat.id);
});
this.categories = this.categories.filter((cat) => {
return !subCategoryIds.includes(cat.id)
})
}

this.categories = this.categories.filter(cat => cat.id !== categoryId);


this.categories = this.categories.filter((cat) => cat.id !== categoryId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import type { CategoriesListParams } from '../../types'

export interface ICategoriesRepository {
findById(categoryId: string): Promise<Category | null>
findMany(params: CategoriesListParams): Promise<Category[]>
findMany(
params: CategoriesListParams,
): Promise<{ categories: Category[]; count: number }>
add(category: Category): Promise<void>
addMany(suppliers: Category[]): Promise<void>
count(): Promise<number>
Expand Down
11 changes: 3 additions & 8 deletions packages/core/src/use-cases/categories/list-category-use-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@ type Request = {
}

export class ListCategoryUseCase {
private readonly categoriesRepository: ICategoriesRepository

constructor(categoriesRepository: ICategoriesRepository) {
this.categoriesRepository = categoriesRepository
}
constructor(private readonly categoriesRepository: ICategoriesRepository) {}

async execute({ page, companyId, name }: Request) {
const categories = await this.categoriesRepository.findMany({
const { categories, count } = await this.categoriesRepository.findMany({
page,
companyId,
name,
})

const categoriesCount = await this.categoriesRepository.count()
return new PaginationResponse({
items: categories.map((category) => category.dto),
itemsCount: categoriesCount,
itemsCount: count,
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('List notification use case', () => {
await notificationsRepository.addStockLevelNotification(fakeNotification)
}

const { stockLevelNotifications } = await useCase.execute(companyId)
const { stockLevelNotifications } = await useCase.execute({ companyId })
expect(stockLevelNotifications).toEqual(
fakeStockLevelNotifications.map((notification) => notification.dto),
)
Expand All @@ -40,7 +40,7 @@ describe('List notification use case', () => {
fakeExpirationDateNotifications,
)

const { expirationDateNotifications } = await useCase.execute(companyId)
const { expirationDateNotifications } = await useCase.execute({ companyId })
expect(expirationDateNotifications).toEqual(
fakeExpirationDateNotifications.map((notification) => notification.dto),
)
Expand All @@ -51,7 +51,7 @@ describe('List notification use case', () => {
const useCase = new ListNotificationsUseCase(notificationsRepository)

const { stockLevelNotifications, expirationDateNotifications } =
await useCase.execute('non-existent-company-id')
await useCase.execute({ companyId: 'non-existent-company-id' })
expect(stockLevelNotifications).toEqual([])
expect(expirationDateNotifications).toEqual([])
})
Expand Down

0 comments on commit 721c645

Please sign in to comment.