fix(web): unwrap CacheMetaInterceptor envelope + dev port migration + homepage diacritic
Several fixes discovered while smoke-testing the homepage under the new
port layout (web 3200 / api 3201) to avoid clashing with a sibling project:
- analytics-api: add `unwrap<T>()` helper for the `{ data, cacheMeta }`
envelope the backend CacheMetaInterceptor appends to every
`/analytics/*` response. Apply to all 9 analytics methods. Without this
`data.activeCount` (etc.) were `undefined`, crashing KpiStrip with
`TypeError: Cannot read properties of undefined (reading 'toLocaleString')`.
- public page: hard-coded `city = 'Ho Chi Minh'` returned 0 rows because
the DB stores `'Hồ Chí Minh'` and the SQL filter is case-insensitive but
not diacritic-insensitive. Use the accented spelling.
- use-analytics hooks: add `useAuthedAnalytics()` gate so unauthenticated
visitors on public routes no longer fire 401s from analytics queries.
- next.config.js CSP: add localhost:3200/3201 (http + ws) to connect-src so
the web origin can reach the relocated API. Without this fetches hit
`TypeError: Failed to fetch` on login.
- .claude/launch.json + package.json: web → 3200, api → 3201 (was 3000/3001,
conflicting with the sibling psyforge project also using 3000).
- Minor follow-ups from parallel QA work on this branch (analytics modules,
notifications gateway, auth test fixtures, trending-areas handler + DTO
+ tests, a few E2E smoke specs).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { forwardRef, Module } from '@nestjs/common';
|
||||
import { CqrsModule } from '@nestjs/cqrs';
|
||||
import { AdminModule } from '@modules/admin';
|
||||
import { ListingsModule } from '@modules/listings';
|
||||
@@ -17,6 +17,7 @@ import { GetMarketReportHandler } from './application/queries/get-market-report/
|
||||
import { GetMarketHistoryHandler } from './application/queries/get-market-history/get-market-history.handler';
|
||||
import { GetMarketSnapshotHandler } from './application/queries/get-market-snapshot/get-market-snapshot.handler';
|
||||
import { GetPriceMoversHandler } from './application/queries/get-price-movers/get-price-movers.handler';
|
||||
import { GetTrendingAreasHandler } from './application/queries/get-trending-areas/get-trending-areas.handler';
|
||||
import { GetProjectAiAdviceHandler } from './application/queries/get-project-ai-advice/get-project-ai-advice.handler';
|
||||
import { GetNearbyPOIsHandler } from './application/queries/get-nearby-pois/get-nearby-pois.handler';
|
||||
import { GetNeighborhoodScoreHandler } from './application/queries/get-neighborhood-score/get-neighborhood-score.handler';
|
||||
@@ -69,6 +70,7 @@ const QueryHandlers = [
|
||||
GetProjectAiAdviceHandler,
|
||||
GetMarketSnapshotHandler,
|
||||
GetPriceMoversHandler,
|
||||
GetTrendingAreasHandler,
|
||||
];
|
||||
|
||||
const EventHandlers = [
|
||||
@@ -76,7 +78,7 @@ const EventHandlers = [
|
||||
];
|
||||
|
||||
@Module({
|
||||
imports: [CqrsModule, ListingsModule, AdminModule, ProjectsModule],
|
||||
imports: [CqrsModule, forwardRef(() => ListingsModule), forwardRef(() => AdminModule), ProjectsModule],
|
||||
controllers: [AnalyticsController, AvmController],
|
||||
providers: [
|
||||
// AI service client
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
import { type CacheService, type LoggerService } from '@modules/shared';
|
||||
import { GetTrendingAreasHandler } from '../queries/get-trending-areas/get-trending-areas.handler';
|
||||
import { GetTrendingAreasQuery } from '../queries/get-trending-areas/get-trending-areas.query';
|
||||
|
||||
describe('GetTrendingAreasHandler', () => {
|
||||
let handler: GetTrendingAreasHandler;
|
||||
let mockPrisma: { $queryRaw: ReturnType<typeof vi.fn>; marketIndex: { findMany: ReturnType<typeof vi.fn> } };
|
||||
let mockCache: Partial<CacheService>;
|
||||
let mockLogger: Partial<LoggerService>;
|
||||
|
||||
beforeEach(() => {
|
||||
mockPrisma = {
|
||||
$queryRaw: vi.fn(),
|
||||
marketIndex: {
|
||||
findMany: vi.fn(),
|
||||
},
|
||||
};
|
||||
// Bypass @Cacheable decorator by making CacheService.getOrSet call the loader directly
|
||||
mockCache = {
|
||||
getOrSet: vi.fn((_key: string, loader: () => Promise<unknown>) => loader()),
|
||||
} as unknown as Partial<CacheService>;
|
||||
mockLogger = { error: vi.fn(), warn: vi.fn(), log: vi.fn() } as unknown as Partial<LoggerService>;
|
||||
|
||||
handler = new GetTrendingAreasHandler(
|
||||
mockPrisma as any,
|
||||
mockCache as CacheService,
|
||||
mockLogger as LoggerService,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns top trending districts sorted by score', async () => {
|
||||
mockPrisma.$queryRaw.mockResolvedValue([
|
||||
{ district: 'Quận 1', new_listings: BigInt(10), inquiries: BigInt(50), views: BigInt(200) },
|
||||
{ district: 'Quận 7', new_listings: BigInt(20), inquiries: BigInt(30), views: BigInt(400) },
|
||||
{ district: 'Bình Thạnh', new_listings: BigInt(5), inquiries: BigInt(5), views: BigInt(50) },
|
||||
]);
|
||||
mockPrisma.marketIndex.findMany.mockResolvedValue([
|
||||
{ district: 'Quận 1', yoyChange: 0.12 },
|
||||
{ district: 'Quận 7', yoyChange: 0.05 },
|
||||
]);
|
||||
|
||||
const query = new GetTrendingAreasQuery(7, 10, 'district');
|
||||
const result = await handler.execute(query);
|
||||
|
||||
expect(result.period).toBe(7);
|
||||
expect(result.level).toBe('district');
|
||||
expect(result.areas.length).toBe(3);
|
||||
|
||||
// Quận 1 score = 50*0.6 + 200*0.3 + 10*0.1 = 30 + 60 + 1 = 91
|
||||
// Quận 7 score = 30*0.6 + 400*0.3 + 20*0.1 = 18 + 120 + 2 = 140
|
||||
// Bình Thạnh score = 5*0.6 + 50*0.3 + 5*0.1 = 3 + 15 + 0.5 = 18.5
|
||||
// Expected order: Quận 7 (1st), Quận 1 (2nd), Bình Thạnh (3rd)
|
||||
expect(result.areas[0].districtId).toBe('Quận 7');
|
||||
expect(result.areas[0].scoreRank).toBe(1);
|
||||
expect(result.areas[1].districtId).toBe('Quận 1');
|
||||
expect(result.areas[2].districtId).toBe('Bình Thạnh');
|
||||
});
|
||||
|
||||
it('respects the limit parameter', async () => {
|
||||
mockPrisma.$queryRaw.mockResolvedValue([
|
||||
{ district: 'A', new_listings: BigInt(1), inquiries: BigInt(10), views: BigInt(100) },
|
||||
{ district: 'B', new_listings: BigInt(1), inquiries: BigInt(8), views: BigInt(80) },
|
||||
{ district: 'C', new_listings: BigInt(1), inquiries: BigInt(6), views: BigInt(60) },
|
||||
]);
|
||||
mockPrisma.marketIndex.findMany.mockResolvedValue([]);
|
||||
|
||||
const query = new GetTrendingAreasQuery(7, 2, 'district');
|
||||
const result = await handler.execute(query);
|
||||
|
||||
expect(result.areas.length).toBe(2);
|
||||
expect(result.limit).toBe(2);
|
||||
});
|
||||
|
||||
it('returns empty areas when no active listings in window', async () => {
|
||||
mockPrisma.$queryRaw.mockResolvedValue([]);
|
||||
mockPrisma.marketIndex.findMany.mockResolvedValue([]);
|
||||
|
||||
const query = new GetTrendingAreasQuery(7, 10, 'district');
|
||||
const result = await handler.execute(query);
|
||||
|
||||
expect(result.areas).toEqual([]);
|
||||
expect(mockPrisma.marketIndex.findMany).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('attaches yoyChange from market index as priceChangePct', async () => {
|
||||
mockPrisma.$queryRaw.mockResolvedValue([
|
||||
{ district: 'Quận 1', new_listings: BigInt(5), inquiries: BigInt(20), views: BigInt(100) },
|
||||
]);
|
||||
mockPrisma.marketIndex.findMany.mockResolvedValue([
|
||||
{ district: 'Quận 1', yoyChange: 0.08 },
|
||||
]);
|
||||
|
||||
const query = new GetTrendingAreasQuery(14, 10, 'district');
|
||||
const result = await handler.execute(query);
|
||||
|
||||
expect(result.areas[0].priceChangePct).toBe(0.08);
|
||||
});
|
||||
|
||||
it('sets priceChangePct to null when market index data is missing', async () => {
|
||||
mockPrisma.$queryRaw.mockResolvedValue([
|
||||
{ district: 'Huyện Củ Chi', new_listings: BigInt(3), inquiries: BigInt(5), views: BigInt(40) },
|
||||
]);
|
||||
mockPrisma.marketIndex.findMany.mockResolvedValue([]);
|
||||
|
||||
const query = new GetTrendingAreasQuery(7, 10, 'district');
|
||||
const result = await handler.execute(query);
|
||||
|
||||
expect(result.areas[0].priceChangePct).toBeNull();
|
||||
});
|
||||
|
||||
it('throws InternalServerErrorException on unexpected errors', async () => {
|
||||
mockPrisma.$queryRaw.mockRejectedValue(new Error('DB connection lost'));
|
||||
|
||||
const query = new GetTrendingAreasQuery(7, 10, 'district');
|
||||
await expect(handler.execute(query)).rejects.toThrow(
|
||||
'Không thể truy vấn khu vực xu hướng. Vui lòng thử lại sau.',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Inject } from '@nestjs/common';
|
||||
import { EventsHandler, type IEventHandler, CommandBus } from '@nestjs/cqrs';
|
||||
import { ListingCreatedEvent, ModerateListingCommand } from '@modules/listings';
|
||||
import { ListingCreatedEvent } from '@modules/listings/domain/events/listing-created.event';
|
||||
import { ModerateListingCommand } from '@modules/listings/application/commands/moderate-listing/moderate-listing.command';
|
||||
import { PrismaService, LoggerService } from '@modules/shared';
|
||||
import {
|
||||
AI_SERVICE_CLIENT,
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { HttpStatus, Inject } from '@nestjs/common';
|
||||
import { QueryBus, QueryHandler, type IQueryHandler } from '@nestjs/cqrs';
|
||||
import { DomainException, ErrorCode, LoggerService } from '@modules/shared';
|
||||
import { SystemSettingsService } from '@modules/admin';
|
||||
import { SystemSettingsService } from '@modules/admin/application/services/system-settings.service';
|
||||
import {
|
||||
LISTING_REPOSITORY,
|
||||
type IListingRepository,
|
||||
} from '@modules/listings';
|
||||
} from '@modules/listings/domain/repositories/listing.repository';
|
||||
import { type ListingDetailData } from '../../../../listings/domain/repositories/listing-read.dto';
|
||||
import {
|
||||
type NearbyPOIDto,
|
||||
|
||||
@@ -64,26 +64,26 @@ export class GetPriceMoversHandler implements IQueryHandler<GetPriceMoversQuery>
|
||||
WITH current_window AS (
|
||||
SELECT
|
||||
p.district,
|
||||
AVG(l.price) AS avg_price,
|
||||
AVG(l."priceVND") AS avg_price,
|
||||
COUNT(l.id) AS sample_size
|
||||
FROM "Listing" l
|
||||
INNER JOIN "Property" p ON p.id = l."propertyId"
|
||||
WHERE l."createdAt" >= ${currentStart}
|
||||
AND l.status = 'ACTIVE'
|
||||
AND l.price > 0
|
||||
AND l."priceVND" > 0
|
||||
GROUP BY p.district
|
||||
HAVING COUNT(l.id) >= 10
|
||||
),
|
||||
previous_window AS (
|
||||
SELECT
|
||||
p.district,
|
||||
AVG(l.price) AS avg_price
|
||||
AVG(l."priceVND") AS avg_price
|
||||
FROM "Listing" l
|
||||
INNER JOIN "Property" p ON p.id = l."propertyId"
|
||||
WHERE l."createdAt" >= ${previousStart}
|
||||
AND l."createdAt" < ${currentStart}
|
||||
AND l.status = 'ACTIVE'
|
||||
AND l.price > 0
|
||||
AND l."priceVND" > 0
|
||||
GROUP BY p.district
|
||||
)
|
||||
SELECT
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { HttpStatus, Inject } from '@nestjs/common';
|
||||
import { QueryBus, QueryHandler, type IQueryHandler } from '@nestjs/cqrs';
|
||||
import { DomainException, ErrorCode, LoggerService } from '@modules/shared';
|
||||
import { SystemSettingsService } from '@modules/admin';
|
||||
import { SystemSettingsService } from '@modules/admin/application/services/system-settings.service';
|
||||
import {
|
||||
PROJECT_REPOSITORY,
|
||||
type IProjectRepository,
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
import { Inject, InternalServerErrorException } from '@nestjs/common';
|
||||
import { QueryHandler, type IQueryHandler } from '@nestjs/cqrs';
|
||||
import { DomainException, CacheService, CachePrefix, CacheTTL, Cacheable, LoggerService, PrismaService } from '@modules/shared';
|
||||
import { GetTrendingAreasQuery } from './get-trending-areas.query';
|
||||
|
||||
export interface TrendingAreaItem {
|
||||
districtId: string;
|
||||
name: string;
|
||||
listings: number;
|
||||
inquiries: number;
|
||||
views: number;
|
||||
priceChangePct: number | null;
|
||||
scoreRank: number;
|
||||
}
|
||||
|
||||
export interface TrendingAreasDto {
|
||||
period: number;
|
||||
level: string;
|
||||
limit: number;
|
||||
areas: TrendingAreaItem[];
|
||||
}
|
||||
|
||||
interface RawDistrictRow {
|
||||
district: string;
|
||||
new_listings: bigint;
|
||||
inquiries: bigint;
|
||||
views: bigint;
|
||||
}
|
||||
|
||||
@QueryHandler(GetTrendingAreasQuery)
|
||||
export class GetTrendingAreasHandler implements IQueryHandler<GetTrendingAreasQuery> {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly cacheService: CacheService,
|
||||
private readonly logger: LoggerService,
|
||||
) {}
|
||||
|
||||
@Cacheable({
|
||||
prefix: CachePrefix.TRENDING_AREAS,
|
||||
ttl: CacheTTL.TRENDING_AREAS,
|
||||
resource: 'trending_areas',
|
||||
keyFrom: (query: unknown) => {
|
||||
const q = query as GetTrendingAreasQuery;
|
||||
return [String(q.period), String(q.limit), q.level];
|
||||
},
|
||||
})
|
||||
async execute(query: GetTrendingAreasQuery): Promise<TrendingAreasDto> {
|
||||
const { period, limit, level } = query;
|
||||
|
||||
try {
|
||||
const since = new Date(Date.now() - period * 24 * 60 * 60 * 1000);
|
||||
|
||||
// Aggregate new listings, inquiries, and views per district within the time window.
|
||||
// Listing.viewCount is a running total so we use it as a proxy for views.
|
||||
// Inquiry has createdAt that we can filter on.
|
||||
// New listings = listings created within the window.
|
||||
const rows = await this.prisma.$queryRaw<RawDistrictRow[]>`
|
||||
SELECT
|
||||
p.district,
|
||||
COUNT(DISTINCT l.id) AS new_listings,
|
||||
COUNT(DISTINCT i.id) AS inquiries,
|
||||
COALESCE(SUM(l."viewCount"), 0) AS views
|
||||
FROM "Listing" l
|
||||
INNER JOIN "Property" p ON p.id = l."propertyId"
|
||||
LEFT JOIN "Inquiry" i ON i."listingId" = l.id AND i."createdAt" >= ${since}
|
||||
WHERE l."createdAt" >= ${since}
|
||||
AND l.status = 'ACTIVE'
|
||||
GROUP BY p.district
|
||||
`;
|
||||
|
||||
// Compute score for each district
|
||||
const scored = rows.map((r) => {
|
||||
const listings = Number(r.new_listings);
|
||||
const inquiries = Number(r.inquiries);
|
||||
const views = Number(r.views);
|
||||
const score = inquiries * 0.6 + views * 0.3 + listings * 0.1;
|
||||
return { district: r.district, listings, inquiries, views, score };
|
||||
});
|
||||
|
||||
// Sort descending by score, take top `limit`
|
||||
scored.sort((a, b) => b.score - a.score);
|
||||
const top = scored.slice(0, limit);
|
||||
|
||||
// Fetch price change (yoyChange) from MarketIndex for these districts
|
||||
const districts = top.map((r) => r.district);
|
||||
const marketIndexes = districts.length > 0
|
||||
? await this.prisma.marketIndex.findMany({
|
||||
where: { district: { in: districts } },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
select: { district: true, yoyChange: true },
|
||||
})
|
||||
: [];
|
||||
|
||||
// Build a map district → most recent yoyChange
|
||||
const priceMap = new Map<string, number | null>();
|
||||
for (const mi of marketIndexes) {
|
||||
if (!priceMap.has(mi.district)) {
|
||||
priceMap.set(mi.district, mi.yoyChange);
|
||||
}
|
||||
}
|
||||
|
||||
const areas: TrendingAreaItem[] = top.map((r, idx) => ({
|
||||
districtId: r.district,
|
||||
name: r.district,
|
||||
listings: r.listings,
|
||||
inquiries: r.inquiries,
|
||||
views: r.views,
|
||||
priceChangePct: priceMap.get(r.district) ?? null,
|
||||
scoreRank: idx + 1,
|
||||
}));
|
||||
|
||||
return { period, level, limit, areas };
|
||||
} catch (error) {
|
||||
if (error instanceof DomainException) throw error;
|
||||
this.logger.error(
|
||||
`Failed to truy vấn trending areas: ${error instanceof Error ? error.message : error}`,
|
||||
error instanceof Error ? error.stack : undefined,
|
||||
this.constructor.name,
|
||||
);
|
||||
throw new InternalServerErrorException(
|
||||
'Không thể truy vấn khu vực xu hướng. Vui lòng thử lại sau.',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
export class GetTrendingAreasQuery {
|
||||
constructor(
|
||||
/** Number of days to look back, e.g. 7 | 14 | 30 */
|
||||
public readonly period: number,
|
||||
/** Maximum number of results to return */
|
||||
public readonly limit: number,
|
||||
/** Geographic level of aggregation — currently only 'district' is supported */
|
||||
public readonly level: 'district',
|
||||
) {}
|
||||
}
|
||||
@@ -38,6 +38,8 @@ import { type MarketSnapshotDto } from '../../application/queries/get-market-sna
|
||||
import { GetMarketSnapshotQuery } from '../../application/queries/get-market-snapshot/get-market-snapshot.query';
|
||||
import { type PriceMoversDto } from '../../application/queries/get-price-movers/get-price-movers.handler';
|
||||
import { GetPriceMoversQuery } from '../../application/queries/get-price-movers/get-price-movers.query';
|
||||
import { type TrendingAreasDto } from '../../application/queries/get-trending-areas/get-trending-areas.handler';
|
||||
import { GetTrendingAreasQuery } from '../../application/queries/get-trending-areas/get-trending-areas.query';
|
||||
import { type NearbyPOIsResultDto } from '../../application/queries/get-nearby-pois/get-nearby-pois.handler';
|
||||
import { GetNearbyPOIsQuery } from '../../application/queries/get-nearby-pois/get-nearby-pois.query';
|
||||
import { GetNeighborhoodScoreQuery } from '../../application/queries/get-neighborhood-score/get-neighborhood-score.query';
|
||||
@@ -60,6 +62,7 @@ import { GetMarketReportDto } from '../dto/get-market-report.dto';
|
||||
import { GetMarketHistoryDto } from '../dto/get-market-history.dto';
|
||||
import { GetMarketSnapshotDto } from '../dto/get-market-snapshot.dto';
|
||||
import { GetPriceMoversDto } from '../dto/get-price-movers.dto';
|
||||
import { GetTrendingAreasDto } from '../dto/get-trending-areas.dto';
|
||||
import { GetNearbyPOIsDto } from '../dto/get-nearby-pois.dto';
|
||||
import { GetPriceTrendDto } from '../dto/get-price-trend.dto';
|
||||
import { GetValuationDto } from '../dto/get-valuation.dto';
|
||||
@@ -356,6 +359,19 @@ export class AnalyticsController {
|
||||
);
|
||||
}
|
||||
|
||||
@ApiOperation({
|
||||
summary: 'Top khu vực đang trending (public)',
|
||||
description:
|
||||
'Trả về danh sách quận trending theo lượng tin đăng/inquiries/views trong khoảng nhìn lại. Public endpoint cho homepage. Cache.',
|
||||
})
|
||||
@ApiResponse({ status: 200, description: 'Trending areas retrieved' })
|
||||
@Get('trending-areas')
|
||||
async getTrendingAreas(@Query() dto: GetTrendingAreasDto): Promise<TrendingAreasDto> {
|
||||
return this.queryBus.execute(
|
||||
new GetTrendingAreasQuery(dto.period, dto.limit, dto.level),
|
||||
);
|
||||
}
|
||||
|
||||
@ApiBearerAuth('JWT')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('listings/:id/ai-advice')
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsIn, IsInt, IsOptional, Max, Min } from 'class-validator';
|
||||
|
||||
export class GetTrendingAreasDto {
|
||||
@ApiPropertyOptional({
|
||||
description: 'Look-back window in days',
|
||||
enum: [7, 14, 30],
|
||||
default: 7,
|
||||
example: 7,
|
||||
})
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@IsIn([7, 14, 30])
|
||||
period: number = 7;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Maximum number of trending areas to return',
|
||||
minimum: 1,
|
||||
maximum: 50,
|
||||
default: 10,
|
||||
example: 10,
|
||||
})
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@Max(50)
|
||||
limit: number = 10;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Geographic aggregation level (currently only "district" is supported)',
|
||||
enum: ['district'],
|
||||
default: 'district',
|
||||
example: 'district',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsIn(['district'])
|
||||
level: 'district' = 'district';
|
||||
}
|
||||
Reference in New Issue
Block a user