-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Innovix-Matrix-Systems/IMS-19
minor bugs fix and improvements
- Loading branch information
Showing
18 changed files
with
562 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
import { EntityRepository } from '@mikro-orm/core'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { FilterService } from './filter.service'; | ||
import { MiscModule } from './misc.module'; | ||
import { PaginationService } from './pagination.service'; | ||
|
||
describe('FilterService', () => { | ||
let service: FilterService; | ||
let mockPaginationService: jest.Mocked<PaginationService>; | ||
let mockRepository: jest.Mocked<EntityRepository<any>>; | ||
|
||
beforeEach(async () => { | ||
mockRepository = { | ||
findAndCount: jest.fn(), | ||
} as any; | ||
|
||
mockPaginationService = { | ||
buildPaginationOptions: jest.fn().mockReturnValue({ | ||
limit: 10, | ||
offset: 0, | ||
}), | ||
buildPaginationMeta: jest.fn().mockReturnValue({ | ||
currentPage: 1, | ||
from: 1, | ||
lastPage: 1, | ||
perPage: 10, | ||
to: 10, | ||
total: 10, | ||
}), | ||
} as any; | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [MiscModule], | ||
providers: [ | ||
FilterService, | ||
{ | ||
provide: PaginationService, | ||
useValue: mockPaginationService, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<FilterService>(FilterService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('filter', () => { | ||
it('should return paginated results with default parameters', async () => { | ||
const mockData = [{ id: 1, name: 'Test' }]; | ||
mockRepository.findAndCount.mockResolvedValue([mockData, 1]); | ||
|
||
const result = await service.filter( | ||
mockRepository, | ||
{}, | ||
['name'], | ||
['roles'], | ||
); | ||
|
||
expect(result).toEqual({ | ||
data: mockData, | ||
meta: expect.any(Object), | ||
}); | ||
expect(mockRepository.findAndCount).toHaveBeenCalledWith( | ||
{}, | ||
expect.objectContaining({ | ||
limit: 10, | ||
offset: 0, | ||
orderBy: { createdAt: 'ASC' }, | ||
}), | ||
); | ||
}); | ||
|
||
it('should apply search filters correctly', async () => { | ||
const mockData = [{ id: 1, name: 'Test' }]; | ||
mockRepository.findAndCount.mockResolvedValue([mockData, 1]); | ||
|
||
await service.filter( | ||
mockRepository, | ||
{ | ||
search: 'test', | ||
searchFields: ['name', 'email'], | ||
}, | ||
['name'], | ||
['roles'], | ||
); | ||
|
||
expect(mockRepository.findAndCount).toHaveBeenCalledWith( | ||
{ | ||
$or: [ | ||
{ name: { $ilike: '%test%' } }, | ||
{ email: { $ilike: '%test%' } }, | ||
], | ||
}, | ||
expect.any(Object), | ||
); | ||
}); | ||
|
||
it('should apply select filters correctly', async () => { | ||
const mockData = [{ id: 1, name: 'Test' }]; | ||
mockRepository.findAndCount.mockResolvedValue([mockData, 1]); | ||
|
||
await service.filter( | ||
mockRepository, | ||
{ | ||
selectFields: [{ status: 'active' }], | ||
}, | ||
['name'], | ||
['roles'], | ||
); | ||
|
||
expect(mockRepository.findAndCount).toHaveBeenCalledWith( | ||
{ status: 'active' }, | ||
expect.any(Object), | ||
); | ||
}); | ||
|
||
it('should apply ordering correctly', async () => { | ||
const mockData = [{ id: 1, name: 'Test' }]; | ||
mockRepository.findAndCount.mockResolvedValue([mockData, 1]); | ||
|
||
await service.filter( | ||
mockRepository, | ||
{ | ||
orderBy: 'name', | ||
orderDirection: 'DESC', | ||
}, | ||
['name'], | ||
['roles'], | ||
); | ||
|
||
expect(mockRepository.findAndCount).toHaveBeenCalledWith( | ||
expect.any(Object), | ||
expect.objectContaining({ | ||
orderBy: { name: 'DESC' }, | ||
}), | ||
); | ||
}); | ||
|
||
it('should use default ordering when invalid order field is provided', async () => { | ||
const mockData = [{ id: 1, name: 'Test' }]; | ||
mockRepository.findAndCount.mockResolvedValue([mockData, 1]); | ||
|
||
await service.filter( | ||
mockRepository, | ||
{ | ||
orderBy: 'invalid_field', | ||
orderDirection: 'DESC', | ||
}, | ||
['name'], | ||
['roles'], | ||
); | ||
|
||
expect(mockRepository.findAndCount).toHaveBeenCalledWith( | ||
expect.any(Object), | ||
expect.objectContaining({ | ||
orderBy: { createdAt: 'ASC' }, | ||
}), | ||
); | ||
}); | ||
|
||
it('should combine search and select filters with $and', async () => { | ||
const mockData = [{ id: 1, name: 'Test' }]; | ||
mockRepository.findAndCount.mockResolvedValue([mockData, 1]); | ||
|
||
await service.filter( | ||
mockRepository, | ||
{ | ||
search: 'test', | ||
searchFields: ['name'], | ||
selectFields: [{ status: 'active' }], | ||
}, | ||
['name'], | ||
['roles'], | ||
); | ||
|
||
expect(mockRepository.findAndCount).toHaveBeenCalledWith( | ||
{ | ||
$and: [ | ||
{ $or: [{ name: { $ilike: '%test%' } }] }, | ||
{ status: 'active' }, | ||
], | ||
}, | ||
expect.any(Object), | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { | ||
EntityRepository, | ||
FilterQuery, | ||
OrderDefinition, | ||
Populate, | ||
} from '@mikro-orm/core'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { PaginationService } from './pagination.service'; | ||
|
||
@Injectable() | ||
export class FilterService { | ||
constructor(private readonly paginationService: PaginationService) {} | ||
|
||
async filter<T extends object>( | ||
repository: EntityRepository<T>, | ||
params: FilterWithPaginationParams, | ||
validOrderFields: string[] = [], | ||
populate: string[] = [], | ||
): Promise<PaginatedResult<T>> { | ||
const where = this.buildWhereClause<T>( | ||
params.search, | ||
params.searchFields, | ||
params.selectFields, | ||
); | ||
|
||
const orderBy = this.buildOrderOptions( | ||
validOrderFields, | ||
params.orderBy, | ||
params.orderDirection, | ||
); | ||
|
||
const { limit, offset } = this.paginationService.buildPaginationOptions({ | ||
page: params.page, | ||
perPage: params.perPage, | ||
}); | ||
|
||
const [results, totalCount] = await repository.findAndCount(where, { | ||
populate: populate as unknown as Populate<T>, | ||
limit, | ||
offset, | ||
orderBy, | ||
}); | ||
|
||
const meta = this.paginationService.buildPaginationMeta( | ||
params.page || 1, | ||
params.perPage || 10, | ||
totalCount, | ||
); | ||
|
||
return { | ||
data: results, | ||
meta, | ||
}; | ||
} | ||
|
||
private buildWhereClause<T>( | ||
search?: string, | ||
searchFields?: string[], | ||
selectFields?: Array<{ [key: string]: boolean | number | string }>, | ||
): FilterQuery<T> { | ||
if (!search && (!selectFields || selectFields.length === 0)) { | ||
return {} as FilterQuery<T>; | ||
} | ||
|
||
const conditions: any[] = []; | ||
|
||
if (search && searchFields?.length > 0) { | ||
const searchConditions = searchFields.map((field) => ({ | ||
[field]: { $ilike: `%${search}%` }, | ||
})); | ||
conditions.push({ $or: searchConditions }); | ||
} | ||
|
||
if (selectFields?.length > 0) { | ||
selectFields.forEach((field) => { | ||
conditions.push(field); | ||
}); | ||
} | ||
|
||
if (conditions.length > 1) { | ||
return { $and: conditions } as FilterQuery<T>; | ||
} | ||
|
||
if (conditions.length === 1) { | ||
return conditions[0] as FilterQuery<T>; | ||
} | ||
|
||
return {} as FilterQuery<T>; | ||
} | ||
|
||
private buildOrderOptions<T>( | ||
validOrderFields: string[], | ||
orderBy?: string, | ||
orderDirection: 'ASC' | 'DESC' = 'ASC', | ||
): OrderDefinition<T> { | ||
const defaultOrderField = 'createdAt'; | ||
const defaultOrderDirection = 'ASC' as const; | ||
|
||
if (orderBy && validOrderFields.includes(orderBy)) { | ||
return { [orderBy]: orderDirection } as OrderDefinition<T>; | ||
} | ||
|
||
return { [defaultOrderField]: defaultOrderDirection } as OrderDefinition<T>; | ||
} | ||
} |
Oops, something went wrong.