feat(search): enhance geo-search and listing-approved handlers
Improve geo-search handler with better query processing and update listing-approved event handler with enhanced indexing logic. Tests updated accordingly. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -17,6 +17,9 @@ function createMockSearchResult(overrides?: Partial<SearchResult>): SearchResult
|
||||
describe('GeoSearchHandler', () => {
|
||||
let handler: GeoSearchHandler;
|
||||
let mockSearchRepo: { [K in keyof ISearchRepository]: ReturnType<typeof vi.fn> };
|
||||
let mockCache: {
|
||||
getOrSet: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockSearchRepo = {
|
||||
@@ -27,7 +30,10 @@ describe('GeoSearchHandler', () => {
|
||||
ensureCollection: vi.fn(),
|
||||
dropCollection: vi.fn(),
|
||||
};
|
||||
handler = new GeoSearchHandler(mockSearchRepo as any);
|
||||
mockCache = {
|
||||
getOrSet: vi.fn().mockImplementation((_key: string, loader: () => Promise<unknown>) => loader()),
|
||||
};
|
||||
handler = new GeoSearchHandler(mockSearchRepo as any, mockCache as any);
|
||||
});
|
||||
|
||||
it('performs geo search with basic parameters', async () => {
|
||||
@@ -47,6 +53,21 @@ describe('GeoSearchHandler', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('uses cache with correct TTL and resource label', async () => {
|
||||
const expected = createMockSearchResult({ totalFound: 5 });
|
||||
mockSearchRepo.search.mockResolvedValue(expected);
|
||||
|
||||
const query = new GeoSearchQuery(10.7769, 106.7009, 5);
|
||||
await handler.execute(query);
|
||||
|
||||
expect(mockCache.getOrSet).toHaveBeenCalledWith(
|
||||
expect.stringContaining('cache:geo_search:'),
|
||||
expect.any(Function),
|
||||
60,
|
||||
'geo_search',
|
||||
);
|
||||
});
|
||||
|
||||
it('caps radius at 100km', async () => {
|
||||
mockSearchRepo.search.mockResolvedValue(createMockSearchResult());
|
||||
|
||||
@@ -97,4 +118,15 @@ describe('GeoSearchHandler', () => {
|
||||
const searchCall = mockSearchRepo.search.mock.calls[0]![0];
|
||||
expect(searchCall.filterBy).toContain('priceVND:<=3000000000');
|
||||
});
|
||||
|
||||
it('returns cached result without calling search repo', async () => {
|
||||
const cachedResult = createMockSearchResult({ totalFound: 42 });
|
||||
mockCache.getOrSet.mockResolvedValue(cachedResult);
|
||||
|
||||
const query = new GeoSearchQuery(10.7769, 106.7009, 5);
|
||||
const result = await handler.execute(query);
|
||||
|
||||
expect(result).toEqual(cachedResult);
|
||||
expect(mockSearchRepo.search).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Inject } from '@nestjs/common';
|
||||
import { type IQueryHandler, QueryHandler } from '@nestjs/cqrs';
|
||||
import { CacheService, CachePrefix, CacheTTL } from '@modules/shared/infrastructure/cache.service';
|
||||
import {
|
||||
SEARCH_REPOSITORY,
|
||||
type ISearchRepository,
|
||||
@@ -11,33 +12,53 @@ import { GeoSearchQuery } from './geo-search.query';
|
||||
export class GeoSearchHandler implements IQueryHandler<GeoSearchQuery> {
|
||||
constructor(
|
||||
@Inject(SEARCH_REPOSITORY) private readonly searchRepo: ISearchRepository,
|
||||
private readonly cache: CacheService,
|
||||
) {}
|
||||
|
||||
async execute(query: GeoSearchQuery): Promise<SearchResult> {
|
||||
const filters: string[] = ['status:=ACTIVE'];
|
||||
const cacheKey = CacheService.buildKey(
|
||||
CachePrefix.GEO_SEARCH,
|
||||
`${query.lat}_${query.lng}_${query.radiusKm}`,
|
||||
query.propertyType,
|
||||
query.transactionType,
|
||||
query.priceMin,
|
||||
query.priceMax,
|
||||
query.sortBy,
|
||||
query.page,
|
||||
query.perPage,
|
||||
);
|
||||
|
||||
if (query.propertyType) {
|
||||
filters.push(`propertyType:=${query.propertyType}`);
|
||||
}
|
||||
if (query.transactionType) {
|
||||
filters.push(`transactionType:=${query.transactionType}`);
|
||||
}
|
||||
if (query.priceMin !== undefined && query.priceMax !== undefined) {
|
||||
filters.push(`priceVND:[${query.priceMin}..${query.priceMax}]`);
|
||||
} else if (query.priceMin !== undefined) {
|
||||
filters.push(`priceVND:>=${query.priceMin}`);
|
||||
} else if (query.priceMax !== undefined) {
|
||||
filters.push(`priceVND:<=${query.priceMax}`);
|
||||
}
|
||||
return this.cache.getOrSet(
|
||||
cacheKey,
|
||||
async () => {
|
||||
const filters: string[] = ['status:=ACTIVE'];
|
||||
|
||||
return this.searchRepo.search({
|
||||
query: '*',
|
||||
filterBy: filters.join(' && '),
|
||||
sortBy: query.sortBy,
|
||||
page: query.page,
|
||||
perPage: query.perPage,
|
||||
geoPoint: { lat: query.lat, lng: query.lng },
|
||||
geoRadiusKm: Math.min(query.radiusKm, 100),
|
||||
});
|
||||
if (query.propertyType) {
|
||||
filters.push(`propertyType:=${query.propertyType}`);
|
||||
}
|
||||
if (query.transactionType) {
|
||||
filters.push(`transactionType:=${query.transactionType}`);
|
||||
}
|
||||
if (query.priceMin !== undefined && query.priceMax !== undefined) {
|
||||
filters.push(`priceVND:[${query.priceMin}..${query.priceMax}]`);
|
||||
} else if (query.priceMin !== undefined) {
|
||||
filters.push(`priceVND:>=${query.priceMin}`);
|
||||
} else if (query.priceMax !== undefined) {
|
||||
filters.push(`priceVND:<=${query.priceMax}`);
|
||||
}
|
||||
|
||||
return this.searchRepo.search({
|
||||
query: '*',
|
||||
filterBy: filters.join(' && '),
|
||||
sortBy: query.sortBy,
|
||||
page: query.page,
|
||||
perPage: query.perPage,
|
||||
geoPoint: { lat: query.lat, lng: query.lng },
|
||||
geoRadiusKm: Math.min(query.radiusKm, 100),
|
||||
});
|
||||
},
|
||||
CacheTTL.SEARCH_RESULTS,
|
||||
'geo_search',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user