feat: implement project development module, transfer management features, and industrial AVM model integration

This commit is contained in:
Ho Ngoc Hai
2026-04-18 20:34:35 +07:00
parent 0f3b4d7b0d
commit 38b9def99a
66 changed files with 9051 additions and 17 deletions

View File

@@ -0,0 +1,31 @@
import type { ProjectDevelopmentStatus } from '@prisma/client';
export class CreateProjectCommand {
constructor(
public readonly name: string,
public readonly slug: string,
public readonly developer: string,
public readonly developerLogo: string | null,
public readonly totalUnits: number,
public readonly status: ProjectDevelopmentStatus,
public readonly latitude: number,
public readonly longitude: number,
public readonly address: string,
public readonly ward: string,
public readonly district: string,
public readonly city: string,
public readonly description: string | null,
public readonly amenities: Record<string, unknown> | null,
public readonly masterPlanUrl: string | null,
public readonly minPrice: bigint | null,
public readonly maxPrice: bigint | null,
public readonly pricePerM2Range: Record<string, unknown> | null,
public readonly totalArea: number | null,
public readonly buildingCount: number | null,
public readonly floorCount: number | null,
public readonly unitTypes: Record<string, unknown> | null,
public readonly tags: string[],
public readonly startDate: Date | null,
public readonly completionDate: Date | null,
) {}
}

View File

@@ -0,0 +1,66 @@
import { Inject } from '@nestjs/common';
import { type ICommandHandler, CommandHandler } from '@nestjs/cqrs';
import { createId } from '@paralleldrive/cuid2';
import { ConflictException } from '@modules/shared';
import { ProjectDevelopmentEntity } from '../../../domain/entities/project-development.entity';
import {
PROJECT_REPOSITORY,
type IProjectRepository,
} from '../../../domain/repositories/project-development.repository';
import { CreateProjectCommand } from './create-project.command';
@CommandHandler(CreateProjectCommand)
export class CreateProjectHandler implements ICommandHandler<CreateProjectCommand> {
constructor(
@Inject(PROJECT_REPOSITORY)
private readonly repo: IProjectRepository,
) {}
async execute(cmd: CreateProjectCommand): Promise<{ id: string; slug: string }> {
const existing = await this.repo.findBySlug(cmd.slug);
if (existing) {
throw new ConflictException(`Dự án với slug "${cmd.slug}" đã tồn tại`);
}
const now = new Date();
const entity = new ProjectDevelopmentEntity(
createId(),
{
name: cmd.name,
slug: cmd.slug,
developer: cmd.developer,
developerLogo: cmd.developerLogo,
totalUnits: cmd.totalUnits,
completedUnits: 0,
status: cmd.status,
startDate: cmd.startDate,
completionDate: cmd.completionDate,
description: cmd.description,
amenities: cmd.amenities,
masterPlanUrl: cmd.masterPlanUrl,
latitude: cmd.latitude,
longitude: cmd.longitude,
address: cmd.address,
ward: cmd.ward,
district: cmd.district,
city: cmd.city,
minPrice: cmd.minPrice,
maxPrice: cmd.maxPrice,
pricePerM2Range: cmd.pricePerM2Range,
totalArea: cmd.totalArea,
buildingCount: cmd.buildingCount,
floorCount: cmd.floorCount,
unitTypes: cmd.unitTypes,
media: null,
documents: null,
tags: cmd.tags,
isVerified: false,
},
now,
now,
);
await this.repo.save(entity);
return { id: entity.id, slug: entity.slug };
}
}

View File

@@ -0,0 +1,29 @@
import type { ProjectDevelopmentStatus } from '@prisma/client';
export class UpdateProjectCommand {
constructor(
public readonly id: string,
public readonly name?: string,
public readonly developer?: string,
public readonly developerLogo?: string | null,
public readonly totalUnits?: number,
public readonly completedUnits?: number,
public readonly status?: ProjectDevelopmentStatus,
public readonly description?: string | null,
public readonly amenities?: Record<string, unknown> | null,
public readonly masterPlanUrl?: string | null,
public readonly minPrice?: bigint | null,
public readonly maxPrice?: bigint | null,
public readonly pricePerM2Range?: Record<string, unknown> | null,
public readonly totalArea?: number | null,
public readonly buildingCount?: number | null,
public readonly floorCount?: number | null,
public readonly unitTypes?: Record<string, unknown> | null,
public readonly media?: Record<string, unknown>[] | null,
public readonly documents?: Record<string, unknown>[] | null,
public readonly tags?: string[],
public readonly isVerified?: boolean,
public readonly startDate?: Date | null,
public readonly completionDate?: Date | null,
) {}
}

View File

@@ -0,0 +1,51 @@
import { Inject } from '@nestjs/common';
import { type ICommandHandler, CommandHandler } from '@nestjs/cqrs';
import { NotFoundException } from '@modules/shared';
import {
PROJECT_REPOSITORY,
type IProjectRepository,
} from '../../../domain/repositories/project-development.repository';
import { UpdateProjectCommand } from './update-project.command';
@CommandHandler(UpdateProjectCommand)
export class UpdateProjectHandler implements ICommandHandler<UpdateProjectCommand> {
constructor(
@Inject(PROJECT_REPOSITORY)
private readonly repo: IProjectRepository,
) {}
async execute(cmd: UpdateProjectCommand): Promise<{ id: string }> {
const entity = await this.repo.findById(cmd.id);
if (!entity) {
throw new NotFoundException('Dự án', cmd.id);
}
entity.updateDetails({
...(cmd.name !== undefined && { name: cmd.name }),
...(cmd.developer !== undefined && { developer: cmd.developer }),
...(cmd.developerLogo !== undefined && { developerLogo: cmd.developerLogo }),
...(cmd.totalUnits !== undefined && { totalUnits: cmd.totalUnits }),
...(cmd.completedUnits !== undefined && { completedUnits: cmd.completedUnits }),
...(cmd.status !== undefined && { status: cmd.status }),
...(cmd.description !== undefined && { description: cmd.description }),
...(cmd.amenities !== undefined && { amenities: cmd.amenities }),
...(cmd.masterPlanUrl !== undefined && { masterPlanUrl: cmd.masterPlanUrl }),
...(cmd.minPrice !== undefined && { minPrice: cmd.minPrice }),
...(cmd.maxPrice !== undefined && { maxPrice: cmd.maxPrice }),
...(cmd.pricePerM2Range !== undefined && { pricePerM2Range: cmd.pricePerM2Range }),
...(cmd.totalArea !== undefined && { totalArea: cmd.totalArea }),
...(cmd.buildingCount !== undefined && { buildingCount: cmd.buildingCount }),
...(cmd.floorCount !== undefined && { floorCount: cmd.floorCount }),
...(cmd.unitTypes !== undefined && { unitTypes: cmd.unitTypes }),
...(cmd.media !== undefined && { media: cmd.media }),
...(cmd.documents !== undefined && { documents: cmd.documents }),
...(cmd.tags !== undefined && { tags: cmd.tags }),
...(cmd.isVerified !== undefined && { isVerified: cmd.isVerified }),
...(cmd.startDate !== undefined && { startDate: cmd.startDate }),
...(cmd.completionDate !== undefined && { completionDate: cmd.completionDate }),
});
await this.repo.update(entity);
return { id: entity.id };
}
}

View File

@@ -0,0 +1,23 @@
import { Inject } from '@nestjs/common';
import { type IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import {
PROJECT_REPOSITORY,
type IProjectRepository,
type ProjectDetailData,
} from '../../../domain/repositories/project-development.repository';
import { GetProjectQuery } from './get-project.query';
@QueryHandler(GetProjectQuery)
export class GetProjectHandler implements IQueryHandler<GetProjectQuery> {
constructor(
@Inject(PROJECT_REPOSITORY)
private readonly repo: IProjectRepository,
) {}
async execute(query: GetProjectQuery): Promise<ProjectDetailData | null> {
// Try slug first, then ID
const bySlug = await this.repo.findDetailBySlug(query.slugOrId);
if (bySlug) return bySlug;
return this.repo.findDetailById(query.slugOrId);
}
}

View File

@@ -0,0 +1,3 @@
export class GetProjectQuery {
constructor(public readonly slugOrId: string) {}
}

View File

@@ -0,0 +1,30 @@
import { Inject } from '@nestjs/common';
import { type IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import {
PROJECT_REPOSITORY,
type IProjectRepository,
type PaginatedResult,
type ProjectListItem,
} from '../../../domain/repositories/project-development.repository';
import { ListProjectsQuery } from './list-projects.query';
@QueryHandler(ListProjectsQuery)
export class ListProjectsHandler implements IQueryHandler<ListProjectsQuery> {
constructor(
@Inject(PROJECT_REPOSITORY)
private readonly repo: IProjectRepository,
) {}
async execute(query: ListProjectsQuery): Promise<PaginatedResult<ProjectListItem>> {
return this.repo.search({
query: query.query,
status: query.status,
city: query.city,
district: query.district,
developer: query.developer,
isVerified: query.isVerified,
page: query.page,
limit: query.limit,
});
}
}

View File

@@ -0,0 +1,14 @@
import type { ProjectDevelopmentStatus } from '@prisma/client';
export class ListProjectsQuery {
constructor(
public readonly query: string | undefined,
public readonly status: ProjectDevelopmentStatus | undefined,
public readonly city: string | undefined,
public readonly district: string | undefined,
public readonly developer: string | undefined,
public readonly isVerified: boolean | undefined,
public readonly page: number,
public readonly limit: number,
) {}
}

View File

@@ -0,0 +1,155 @@
import { type ProjectDevelopmentStatus } from '@prisma/client';
import { AggregateRoot } from '@modules/shared';
export interface ProjectDevelopmentProps {
name: string;
slug: string;
developer: string;
developerLogo: string | null;
totalUnits: number;
completedUnits: number;
status: ProjectDevelopmentStatus;
startDate: Date | null;
completionDate: Date | null;
description: string | null;
amenities: Record<string, unknown> | null;
masterPlanUrl: string | null;
latitude: number;
longitude: number;
address: string;
ward: string;
district: string;
city: string;
minPrice: bigint | null;
maxPrice: bigint | null;
pricePerM2Range: Record<string, unknown> | null;
totalArea: number | null;
buildingCount: number | null;
floorCount: number | null;
unitTypes: Record<string, unknown> | null;
media: Record<string, unknown>[] | null;
documents: Record<string, unknown>[] | null;
tags: string[];
isVerified: boolean;
}
export class ProjectDevelopmentEntity extends AggregateRoot<string> {
private _name: string;
private _slug: string;
private _developer: string;
private _developerLogo: string | null;
private _totalUnits: number;
private _completedUnits: number;
private _status: ProjectDevelopmentStatus;
private _startDate: Date | null;
private _completionDate: Date | null;
private _description: string | null;
private _amenities: Record<string, unknown> | null;
private _masterPlanUrl: string | null;
private _latitude: number;
private _longitude: number;
private _address: string;
private _ward: string;
private _district: string;
private _city: string;
private _minPrice: bigint | null;
private _maxPrice: bigint | null;
private _pricePerM2Range: Record<string, unknown> | null;
private _totalArea: number | null;
private _buildingCount: number | null;
private _floorCount: number | null;
private _unitTypes: Record<string, unknown> | null;
private _media: Record<string, unknown>[] | null;
private _documents: Record<string, unknown>[] | null;
private _tags: string[];
private _isVerified: boolean;
constructor(id: string, props: ProjectDevelopmentProps, createdAt: Date, updatedAt: Date) {
super(id, createdAt, updatedAt);
this._name = props.name;
this._slug = props.slug;
this._developer = props.developer;
this._developerLogo = props.developerLogo;
this._totalUnits = props.totalUnits;
this._completedUnits = props.completedUnits;
this._status = props.status;
this._startDate = props.startDate;
this._completionDate = props.completionDate;
this._description = props.description;
this._amenities = props.amenities;
this._masterPlanUrl = props.masterPlanUrl;
this._latitude = props.latitude;
this._longitude = props.longitude;
this._address = props.address;
this._ward = props.ward;
this._district = props.district;
this._city = props.city;
this._minPrice = props.minPrice;
this._maxPrice = props.maxPrice;
this._pricePerM2Range = props.pricePerM2Range;
this._totalArea = props.totalArea;
this._buildingCount = props.buildingCount;
this._floorCount = props.floorCount;
this._unitTypes = props.unitTypes;
this._media = props.media;
this._documents = props.documents;
this._tags = props.tags;
this._isVerified = props.isVerified;
}
get name() { return this._name; }
get slug() { return this._slug; }
get developer() { return this._developer; }
get developerLogo() { return this._developerLogo; }
get totalUnits() { return this._totalUnits; }
get completedUnits() { return this._completedUnits; }
get status() { return this._status; }
get startDate() { return this._startDate; }
get completionDate() { return this._completionDate; }
get description() { return this._description; }
get amenities() { return this._amenities; }
get masterPlanUrl() { return this._masterPlanUrl; }
get latitude() { return this._latitude; }
get longitude() { return this._longitude; }
get address() { return this._address; }
get ward() { return this._ward; }
get district() { return this._district; }
get city() { return this._city; }
get minPrice() { return this._minPrice; }
get maxPrice() { return this._maxPrice; }
get pricePerM2Range() { return this._pricePerM2Range; }
get totalArea() { return this._totalArea; }
get buildingCount() { return this._buildingCount; }
get floorCount() { return this._floorCount; }
get unitTypes() { return this._unitTypes; }
get media() { return this._media; }
get documents() { return this._documents; }
get tags() { return this._tags; }
get isVerified() { return this._isVerified; }
updateDetails(props: Partial<ProjectDevelopmentProps>): void {
if (props.name !== undefined) this._name = props.name;
if (props.developer !== undefined) this._developer = props.developer;
if (props.developerLogo !== undefined) this._developerLogo = props.developerLogo;
if (props.totalUnits !== undefined) this._totalUnits = props.totalUnits;
if (props.completedUnits !== undefined) this._completedUnits = props.completedUnits;
if (props.status !== undefined) this._status = props.status;
if (props.startDate !== undefined) this._startDate = props.startDate;
if (props.completionDate !== undefined) this._completionDate = props.completionDate;
if (props.description !== undefined) this._description = props.description;
if (props.amenities !== undefined) this._amenities = props.amenities;
if (props.masterPlanUrl !== undefined) this._masterPlanUrl = props.masterPlanUrl;
if (props.minPrice !== undefined) this._minPrice = props.minPrice;
if (props.maxPrice !== undefined) this._maxPrice = props.maxPrice;
if (props.pricePerM2Range !== undefined) this._pricePerM2Range = props.pricePerM2Range;
if (props.totalArea !== undefined) this._totalArea = props.totalArea;
if (props.buildingCount !== undefined) this._buildingCount = props.buildingCount;
if (props.floorCount !== undefined) this._floorCount = props.floorCount;
if (props.unitTypes !== undefined) this._unitTypes = props.unitTypes;
if (props.media !== undefined) this._media = props.media;
if (props.documents !== undefined) this._documents = props.documents;
if (props.tags !== undefined) this._tags = props.tags;
if (props.isVerified !== undefined) this._isVerified = props.isVerified;
this.updatedAt = new Date();
}
}

View File

@@ -0,0 +1,72 @@
import type { ProjectDevelopmentStatus } from '@prisma/client';
import type { ProjectDevelopmentEntity } from '../entities/project-development.entity';
export const PROJECT_REPOSITORY = Symbol('PROJECT_REPOSITORY');
export interface ProjectSearchParams {
query?: string;
status?: ProjectDevelopmentStatus;
city?: string;
district?: string;
developer?: string;
isVerified?: boolean;
page?: number;
limit?: number;
}
export interface PaginatedResult<T> {
data: T[];
total: number;
page: number;
limit: number;
totalPages: number;
}
export interface ProjectListItem {
id: string;
name: string;
slug: string;
developer: string;
developerLogo: string | null;
status: ProjectDevelopmentStatus;
totalUnits: number;
completedUnits: number;
address: string;
ward: string;
district: string;
city: string;
minPrice: bigint | null;
maxPrice: bigint | null;
totalArea: number | null;
tags: string[];
isVerified: boolean;
latitude: number;
longitude: number;
propertyCount: number;
createdAt: Date;
}
export interface ProjectDetailData extends ProjectListItem {
startDate: Date | null;
completionDate: Date | null;
description: string | null;
amenities: Record<string, unknown> | null;
masterPlanUrl: string | null;
pricePerM2Range: Record<string, unknown> | null;
buildingCount: number | null;
floorCount: number | null;
unitTypes: Record<string, unknown> | null;
media: Record<string, unknown>[] | null;
documents: Record<string, unknown>[] | null;
updatedAt: Date;
}
export interface IProjectRepository {
findById(id: string): Promise<ProjectDevelopmentEntity | null>;
findBySlug(slug: string): Promise<ProjectDevelopmentEntity | null>;
findDetailBySlug(slug: string): Promise<ProjectDetailData | null>;
findDetailById(id: string): Promise<ProjectDetailData | null>;
save(entity: ProjectDevelopmentEntity): Promise<void>;
update(entity: ProjectDevelopmentEntity): Promise<void>;
search(params: ProjectSearchParams): Promise<PaginatedResult<ProjectListItem>>;
}

View File

@@ -0,0 +1,7 @@
export { ProjectsModule } from './projects.module';
export { PROJECT_REPOSITORY } from './domain/repositories/project-development.repository';
export type {
IProjectRepository,
ProjectDetailData,
ProjectListItem,
} from './domain/repositories/project-development.repository';

View File

@@ -0,0 +1,304 @@
import { Injectable } from '@nestjs/common';
import type { Prisma } from '@prisma/client';
import { type PrismaService } from '@modules/shared';
import { ProjectDevelopmentEntity } from '../../domain/entities/project-development.entity';
import type {
IProjectRepository,
ProjectSearchParams,
PaginatedResult,
ProjectListItem,
ProjectDetailData,
} from '../../domain/repositories/project-development.repository';
@Injectable()
export class PrismaProjectDevelopmentRepository implements IProjectRepository {
constructor(private readonly prisma: PrismaService) {}
async findById(id: string): Promise<ProjectDevelopmentEntity | null> {
const row = await this.prisma.$queryRaw<RawProject[]>`
SELECT *, ST_Y(location::geometry) as lat, ST_X(location::geometry) as lng
FROM "ProjectDevelopment" WHERE id = ${id} LIMIT 1
`;
return row[0] ? this.toDomain(row[0]) : null;
}
async findBySlug(slug: string): Promise<ProjectDevelopmentEntity | null> {
const row = await this.prisma.$queryRaw<RawProject[]>`
SELECT *, ST_Y(location::geometry) as lat, ST_X(location::geometry) as lng
FROM "ProjectDevelopment" WHERE slug = ${slug} LIMIT 1
`;
return row[0] ? this.toDomain(row[0]) : null;
}
async findDetailBySlug(slug: string): Promise<ProjectDetailData | null> {
const rows = await this.prisma.$queryRaw<RawProjectDetail[]>`
SELECT p.*,
ST_Y(p.location::geometry) as lat,
ST_X(p.location::geometry) as lng,
COUNT(pr.id)::int as "propertyCount"
FROM "ProjectDevelopment" p
LEFT JOIN "Property" pr ON pr."projectId" = p.id
WHERE p.slug = ${slug}
GROUP BY p.id
LIMIT 1
`;
return rows[0] ? this.toDetail(rows[0]) : null;
}
async findDetailById(id: string): Promise<ProjectDetailData | null> {
const rows = await this.prisma.$queryRaw<RawProjectDetail[]>`
SELECT p.*,
ST_Y(p.location::geometry) as lat,
ST_X(p.location::geometry) as lng,
COUNT(pr.id)::int as "propertyCount"
FROM "ProjectDevelopment" p
LEFT JOIN "Property" pr ON pr."projectId" = p.id
WHERE p.id = ${id}
GROUP BY p.id
LIMIT 1
`;
return rows[0] ? this.toDetail(rows[0]) : null;
}
async save(entity: ProjectDevelopmentEntity): Promise<void> {
await this.prisma.$executeRaw`
INSERT INTO "ProjectDevelopment" (
id, name, slug, developer, "developerLogo", "totalUnits", "completedUnits",
status, "startDate", "completionDate", description, amenities, "masterPlanUrl",
location, address, ward, district, city,
"minPrice", "maxPrice", "pricePerM2Range", "totalArea",
"buildingCount", "floorCount", "unitTypes", media, documents,
tags, "isVerified", "createdAt", "updatedAt"
) VALUES (
${entity.id}, ${entity.name}, ${entity.slug}, ${entity.developer},
${entity.developerLogo}, ${entity.totalUnits}, ${entity.completedUnits},
${entity.status}::"ProjectDevelopmentStatus",
${entity.startDate}, ${entity.completionDate},
${entity.description},
${entity.amenities ? JSON.stringify(entity.amenities) : null}::jsonb,
${entity.masterPlanUrl},
ST_SetSRID(ST_MakePoint(${entity.longitude}, ${entity.latitude}), 4326),
${entity.address}, ${entity.ward}, ${entity.district}, ${entity.city},
${entity.minPrice}, ${entity.maxPrice},
${entity.pricePerM2Range ? JSON.stringify(entity.pricePerM2Range) : null}::jsonb,
${entity.totalArea}, ${entity.buildingCount}, ${entity.floorCount},
${entity.unitTypes ? JSON.stringify(entity.unitTypes) : null}::jsonb,
${entity.media ? JSON.stringify(entity.media) : null}::jsonb,
${entity.documents ? JSON.stringify(entity.documents) : null}::jsonb,
${entity.tags}::text[],
${entity.isVerified}, ${entity.createdAt}, ${entity.updatedAt}
)
`;
}
async update(entity: ProjectDevelopmentEntity): Promise<void> {
await this.prisma.$executeRaw`
UPDATE "ProjectDevelopment" SET
name = ${entity.name}, developer = ${entity.developer},
"developerLogo" = ${entity.developerLogo},
"totalUnits" = ${entity.totalUnits}, "completedUnits" = ${entity.completedUnits},
status = ${entity.status}::"ProjectDevelopmentStatus",
"startDate" = ${entity.startDate}, "completionDate" = ${entity.completionDate},
description = ${entity.description},
amenities = ${entity.amenities ? JSON.stringify(entity.amenities) : null}::jsonb,
"masterPlanUrl" = ${entity.masterPlanUrl},
"minPrice" = ${entity.minPrice}, "maxPrice" = ${entity.maxPrice},
"pricePerM2Range" = ${entity.pricePerM2Range ? JSON.stringify(entity.pricePerM2Range) : null}::jsonb,
"totalArea" = ${entity.totalArea},
"buildingCount" = ${entity.buildingCount}, "floorCount" = ${entity.floorCount},
"unitTypes" = ${entity.unitTypes ? JSON.stringify(entity.unitTypes) : null}::jsonb,
media = ${entity.media ? JSON.stringify(entity.media) : null}::jsonb,
documents = ${entity.documents ? JSON.stringify(entity.documents) : null}::jsonb,
tags = ${entity.tags}::text[],
"isVerified" = ${entity.isVerified},
"updatedAt" = ${entity.updatedAt}
WHERE id = ${entity.id}
`;
}
async search(params: ProjectSearchParams): Promise<PaginatedResult<ProjectListItem>> {
const page = params.page ?? 1;
const limit = params.limit ?? 20;
const offset = (page - 1) * limit;
const conditions: string[] = ['1=1'];
const values: unknown[] = [];
let paramIndex = 1;
if (params.status) {
conditions.push(`status = $${paramIndex++}::"ProjectDevelopmentStatus"`);
values.push(params.status);
}
if (params.city) {
conditions.push(`city = $${paramIndex++}`);
values.push(params.city);
}
if (params.district) {
conditions.push(`district = $${paramIndex++}`);
values.push(params.district);
}
if (params.developer) {
conditions.push(`developer ILIKE $${paramIndex++}`);
values.push(`%${params.developer}%`);
}
if (params.isVerified !== undefined) {
conditions.push(`"isVerified" = $${paramIndex++}`);
values.push(params.isVerified);
}
if (params.query) {
conditions.push(`(name ILIKE $${paramIndex} OR developer ILIKE $${paramIndex} OR district ILIKE $${paramIndex} OR city ILIKE $${paramIndex})`);
values.push(`%${params.query}%`);
paramIndex++;
}
const where = conditions.join(' AND ');
const countResult = await this.prisma.$queryRawUnsafe<[{ count: bigint }]>(
`SELECT COUNT(*)::bigint as count FROM "ProjectDevelopment" WHERE ${where}`,
...values,
);
const total = Number(countResult[0].count);
const rows = await this.prisma.$queryRawUnsafe<RawProjectDetail[]>(
`SELECT p.*, ST_Y(p.location::geometry) as lat, ST_X(p.location::geometry) as lng,
COUNT(pr.id)::int as "propertyCount"
FROM "ProjectDevelopment" p
LEFT JOIN "Property" pr ON pr."projectId" = p.id
WHERE ${where.replace(/\b(\$\d+)/g, (_, m) => m)}
GROUP BY p.id
ORDER BY p."createdAt" DESC
LIMIT $${paramIndex++} OFFSET $${paramIndex}`,
...values, limit, offset,
);
return {
data: rows.map((r) => this.toListItem(r)),
total,
page,
limit,
totalPages: Math.ceil(total / limit),
};
}
private toDomain(row: RawProject): ProjectDevelopmentEntity {
return new ProjectDevelopmentEntity(
row.id,
{
name: row.name,
slug: row.slug,
developer: row.developer,
developerLogo: row.developerLogo,
totalUnits: row.totalUnits,
completedUnits: row.completedUnits,
status: row.status,
startDate: row.startDate,
completionDate: row.completionDate,
description: row.description,
amenities: row.amenities as Record<string, unknown> | null,
masterPlanUrl: row.masterPlanUrl,
latitude: Number(row.lat),
longitude: Number(row.lng),
address: row.address,
ward: row.ward,
district: row.district,
city: row.city,
minPrice: row.minPrice,
maxPrice: row.maxPrice,
pricePerM2Range: row.pricePerM2Range as Record<string, unknown> | null,
totalArea: row.totalArea,
buildingCount: row.buildingCount,
floorCount: row.floorCount,
unitTypes: row.unitTypes as Record<string, unknown> | null,
media: row.media as Record<string, unknown>[] | null,
documents: row.documents as Record<string, unknown>[] | null,
tags: row.tags ?? [],
isVerified: row.isVerified,
},
row.createdAt,
row.updatedAt,
);
}
private toListItem(row: RawProjectDetail): ProjectListItem {
return {
id: row.id,
name: row.name,
slug: row.slug,
developer: row.developer,
developerLogo: row.developerLogo,
status: row.status,
totalUnits: row.totalUnits,
completedUnits: row.completedUnits,
address: row.address,
ward: row.ward,
district: row.district,
city: row.city,
minPrice: row.minPrice,
maxPrice: row.maxPrice,
totalArea: row.totalArea,
tags: row.tags ?? [],
isVerified: row.isVerified,
latitude: Number(row.lat),
longitude: Number(row.lng),
propertyCount: row.propertyCount ?? 0,
createdAt: row.createdAt,
};
}
private toDetail(row: RawProjectDetail): ProjectDetailData {
return {
...this.toListItem(row),
startDate: row.startDate,
completionDate: row.completionDate,
description: row.description,
amenities: row.amenities as Record<string, unknown> | null,
masterPlanUrl: row.masterPlanUrl,
pricePerM2Range: row.pricePerM2Range as Record<string, unknown> | null,
buildingCount: row.buildingCount,
floorCount: row.floorCount,
unitTypes: row.unitTypes as Record<string, unknown> | null,
media: row.media as Record<string, unknown>[] | null,
documents: row.documents as Record<string, unknown>[] | null,
updatedAt: row.updatedAt,
};
}
}
interface RawProject {
id: string;
name: string;
slug: string;
developer: string;
developerLogo: string | null;
totalUnits: number;
completedUnits: number;
status: 'PLANNING' | 'UNDER_CONSTRUCTION' | 'COMPLETED' | 'HANDOVER';
startDate: Date | null;
completionDate: Date | null;
description: string | null;
amenities: Prisma.JsonValue;
masterPlanUrl: string | null;
lat: number;
lng: number;
address: string;
ward: string;
district: string;
city: string;
minPrice: bigint | null;
maxPrice: bigint | null;
pricePerM2Range: Prisma.JsonValue;
totalArea: number | null;
buildingCount: number | null;
floorCount: number | null;
unitTypes: Prisma.JsonValue;
media: Prisma.JsonValue;
documents: Prisma.JsonValue;
tags: string[] | null;
isVerified: boolean;
createdAt: Date;
updatedAt: Date;
}
interface RawProjectDetail extends RawProject {
propertyCount: number;
}

View File

@@ -0,0 +1,130 @@
import { Body, Controller, Get, Param, Patch, Post, Query, UseGuards } from '@nestjs/common';
import { type CommandBus, type QueryBus } from '@nestjs/cqrs';
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { UserRole } from '@prisma/client';
import { JwtAuthGuard, Roles, RolesGuard } from '@modules/auth';
import { NotFoundException } from '@modules/shared';
import { CreateProjectCommand } from '../../application/commands/create-project/create-project.command';
import { UpdateProjectCommand } from '../../application/commands/update-project/update-project.command';
import { GetProjectQuery } from '../../application/queries/get-project/get-project.query';
import { ListProjectsQuery } from '../../application/queries/list-projects/list-projects.query';
import { type CreateProjectDto } from '../dto/create-project.dto';
import { type SearchProjectsDto } from '../dto/search-projects.dto';
import { type UpdateProjectDto } from '../dto/update-project.dto';
@ApiTags('projects')
@Controller('projects')
export class ProjectsController {
constructor(
private readonly commandBus: CommandBus,
private readonly queryBus: QueryBus,
) {}
// ── Public endpoints ──────────────────────────────────────────────
@ApiOperation({ summary: 'Danh sách dự án', description: 'Tìm kiếm và lọc dự án bất động sản' })
@ApiResponse({ status: 200, description: 'Danh sách dự án phân trang' })
@Get()
async listProjects(@Query() dto: SearchProjectsDto) {
return this.queryBus.execute(
new ListProjectsQuery(
dto.q,
dto.status,
dto.city,
dto.district,
dto.developer,
dto.isVerified,
dto.page ?? 1,
dto.limit ?? 20,
),
);
}
@ApiOperation({ summary: 'Chi tiết dự án', description: 'Xem chi tiết dự án theo slug hoặc ID' })
@ApiResponse({ status: 200, description: 'Thông tin chi tiết dự án' })
@ApiResponse({ status: 404, description: 'Không tìm thấy dự án' })
@Get(':slugOrId')
async getProject(@Param('slugOrId') slugOrId: string) {
const result = await this.queryBus.execute(new GetProjectQuery(slugOrId));
if (!result) {
throw new NotFoundException('Dự án', slugOrId);
}
return result;
}
// ── Admin endpoints ───────────────────────────────────────────────
@ApiOperation({ summary: 'Tạo dự án (admin)', description: 'Tạo mới dự án bất động sản' })
@ApiResponse({ status: 201, description: 'Dự án đã tạo' })
@ApiBearerAuth('JWT')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@Post()
async createProject(@Body() dto: CreateProjectDto) {
return this.commandBus.execute(
new CreateProjectCommand(
dto.name,
dto.slug,
dto.developer,
dto.developerLogo ?? null,
dto.totalUnits,
dto.status,
dto.latitude,
dto.longitude,
dto.address,
dto.ward,
dto.district,
dto.city,
dto.description ?? null,
dto.amenities ?? null,
dto.masterPlanUrl ?? null,
dto.minPrice ? BigInt(dto.minPrice) : null,
dto.maxPrice ? BigInt(dto.maxPrice) : null,
dto.pricePerM2Range ?? null,
dto.totalArea ?? null,
dto.buildingCount ?? null,
dto.floorCount ?? null,
dto.unitTypes ?? null,
dto.tags ?? [],
dto.startDate ? new Date(dto.startDate) : null,
dto.completionDate ? new Date(dto.completionDate) : null,
),
);
}
@ApiOperation({ summary: 'Cập nhật dự án (admin)', description: 'Cập nhật thông tin dự án' })
@ApiResponse({ status: 200, description: 'Dự án đã cập nhật' })
@ApiBearerAuth('JWT')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@Patch(':id')
async updateProject(@Param('id') id: string, @Body() dto: UpdateProjectDto) {
return this.commandBus.execute(
new UpdateProjectCommand(
id,
dto.name,
dto.developer,
dto.developerLogo,
dto.totalUnits,
dto.completedUnits,
dto.status,
dto.description,
dto.amenities,
dto.masterPlanUrl,
dto.minPrice !== undefined ? (dto.minPrice ? BigInt(dto.minPrice) : null) : undefined,
dto.maxPrice !== undefined ? (dto.maxPrice ? BigInt(dto.maxPrice) : null) : undefined,
dto.pricePerM2Range,
dto.totalArea,
dto.buildingCount,
dto.floorCount,
dto.unitTypes,
dto.media,
dto.documents,
dto.tags,
dto.isVerified,
dto.startDate !== undefined ? (dto.startDate ? new Date(dto.startDate) : null) : undefined,
dto.completionDate !== undefined ? (dto.completionDate ? new Date(dto.completionDate) : null) : undefined,
),
);
}
}

View File

@@ -0,0 +1,146 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { ProjectDevelopmentStatus } from '@prisma/client';
import { Type } from 'class-transformer';
import {
IsString,
IsNumber,
IsEnum,
IsOptional,
IsArray,
IsObject,
Min,
Max,
MaxLength,
IsDateString,
} from 'class-validator';
export class CreateProjectDto {
@ApiProperty({ example: 'Vinhomes Grand Park', description: 'Tên dự án' })
@IsString()
@MaxLength(200)
name!: string;
@ApiProperty({ example: 'vinhomes-grand-park', description: 'URL slug (unique)' })
@IsString()
@MaxLength(100)
slug!: string;
@ApiProperty({ example: 'Vingroup' })
@IsString()
developer!: string;
@ApiPropertyOptional({ example: 'https://example.com/logo.png' })
@IsOptional()
@IsString()
developerLogo?: string;
@ApiProperty({ example: 10000, description: 'Tổng số căn hộ/đơn vị' })
@IsNumber()
@Type(() => Number)
@Min(1)
totalUnits!: number;
@ApiProperty({ enum: ProjectDevelopmentStatus, example: 'UNDER_CONSTRUCTION' })
@IsEnum(ProjectDevelopmentStatus)
status!: ProjectDevelopmentStatus;
@ApiProperty({ example: 10.8231, description: 'Latitude' })
@IsNumber()
@Type(() => Number)
@Min(-90)
@Max(90)
latitude!: number;
@ApiProperty({ example: 106.8368, description: 'Longitude' })
@IsNumber()
@Type(() => Number)
@Min(-180)
@Max(180)
longitude!: number;
@ApiProperty({ example: 'Phường Long Thạnh Mỹ, TP. Thủ Đức' })
@IsString()
address!: string;
@ApiProperty({ example: 'Long Thạnh Mỹ' })
@IsString()
ward!: string;
@ApiProperty({ example: 'Thủ Đức' })
@IsString()
district!: string;
@ApiProperty({ example: 'Hồ Chí Minh' })
@IsString()
city!: string;
@ApiPropertyOptional({ description: 'Mô tả dự án' })
@IsOptional()
@IsString()
description?: string;
@ApiPropertyOptional({ description: 'Tiện ích dự án (JSON)' })
@IsOptional()
@IsObject()
amenities?: Record<string, unknown>;
@ApiPropertyOptional({ example: 'https://example.com/masterplan.jpg' })
@IsOptional()
@IsString()
masterPlanUrl?: string;
@ApiPropertyOptional({ example: '3000000000', description: 'Giá thấp nhất (VND)' })
@IsOptional()
@IsString()
minPrice?: string;
@ApiPropertyOptional({ example: '15000000000', description: 'Giá cao nhất (VND)' })
@IsOptional()
@IsString()
maxPrice?: string;
@ApiPropertyOptional({ description: 'Giá/m² range (JSON)' })
@IsOptional()
@IsObject()
pricePerM2Range?: Record<string, unknown>;
@ApiPropertyOptional({ example: 271, description: 'Tổng diện tích (ha)' })
@IsOptional()
@IsNumber()
@Type(() => Number)
@Min(0)
totalArea?: number;
@ApiPropertyOptional({ example: 14 })
@IsOptional()
@IsNumber()
@Type(() => Number)
buildingCount?: number;
@ApiPropertyOptional({ example: 35 })
@IsOptional()
@IsNumber()
@Type(() => Number)
floorCount?: number;
@ApiPropertyOptional({ description: 'Loại căn hộ (JSON)' })
@IsOptional()
@IsObject()
unitTypes?: Record<string, unknown>;
@ApiPropertyOptional({ example: ['cao-cap', 'can-ho'], description: 'Tags' })
@IsOptional()
@IsArray()
@IsString({ each: true })
tags?: string[];
@ApiPropertyOptional({ example: '2020-06-01', description: 'Ngày khởi công' })
@IsOptional()
@IsDateString()
startDate?: string;
@ApiPropertyOptional({ example: '2025-12-31', description: 'Ngày dự kiến hoàn thành' })
@IsOptional()
@IsDateString()
completionDate?: string;
}

View File

@@ -0,0 +1,52 @@
import { ApiPropertyOptional } from '@nestjs/swagger';
import { ProjectDevelopmentStatus } from '@prisma/client';
import { Type } from 'class-transformer';
import { IsString, IsEnum, IsOptional, IsNumber, IsBoolean, Min, Max } from 'class-validator';
export class SearchProjectsDto {
@ApiPropertyOptional({ description: 'Tìm kiếm theo tên, chủ đầu tư, quận, thành phố' })
@IsOptional()
@IsString()
q?: string;
@ApiPropertyOptional({ enum: ProjectDevelopmentStatus })
@IsOptional()
@IsEnum(ProjectDevelopmentStatus)
status?: ProjectDevelopmentStatus;
@ApiPropertyOptional({ example: 'Hồ Chí Minh' })
@IsOptional()
@IsString()
city?: string;
@ApiPropertyOptional({ example: 'Thủ Đức' })
@IsOptional()
@IsString()
district?: string;
@ApiPropertyOptional({ example: 'Vingroup' })
@IsOptional()
@IsString()
developer?: string;
@ApiPropertyOptional()
@IsOptional()
@IsBoolean()
@Type(() => Boolean)
isVerified?: boolean;
@ApiPropertyOptional({ default: 1 })
@IsOptional()
@IsNumber()
@Type(() => Number)
@Min(1)
page?: number;
@ApiPropertyOptional({ default: 20 })
@IsOptional()
@IsNumber()
@Type(() => Number)
@Min(1)
@Max(100)
limit?: number;
}

View File

@@ -0,0 +1,50 @@
import { ApiPropertyOptional } from '@nestjs/swagger';
import { ProjectDevelopmentStatus } from '@prisma/client';
import { Type } from 'class-transformer';
import {
IsString,
IsNumber,
IsEnum,
IsOptional,
IsArray,
IsObject,
IsBoolean,
Min,
IsDateString,
} from 'class-validator';
export class UpdateProjectDto {
@ApiPropertyOptional() @IsOptional() @IsString() name?: string;
@ApiPropertyOptional() @IsOptional() @IsString() developer?: string;
@ApiPropertyOptional() @IsOptional() @IsString() developerLogo?: string | null;
@ApiPropertyOptional() @IsOptional() @IsNumber() @Type(() => Number) @Min(1)
totalUnits?: number;
@ApiPropertyOptional() @IsOptional() @IsNumber() @Type(() => Number) @Min(0)
completedUnits?: number;
@ApiPropertyOptional({ enum: ProjectDevelopmentStatus })
@IsOptional() @IsEnum(ProjectDevelopmentStatus)
status?: ProjectDevelopmentStatus;
@ApiPropertyOptional() @IsOptional() @IsString() description?: string | null;
@ApiPropertyOptional() @IsOptional() @IsObject() amenities?: Record<string, unknown> | null;
@ApiPropertyOptional() @IsOptional() @IsString() masterPlanUrl?: string | null;
@ApiPropertyOptional() @IsOptional() @IsString() minPrice?: string | null;
@ApiPropertyOptional() @IsOptional() @IsString() maxPrice?: string | null;
@ApiPropertyOptional() @IsOptional() @IsObject() pricePerM2Range?: Record<string, unknown> | null;
@ApiPropertyOptional() @IsOptional() @IsNumber() @Type(() => Number) @Min(0)
totalArea?: number | null;
@ApiPropertyOptional() @IsOptional() @IsNumber() @Type(() => Number) buildingCount?: number | null;
@ApiPropertyOptional() @IsOptional() @IsNumber() @Type(() => Number) floorCount?: number | null;
@ApiPropertyOptional() @IsOptional() @IsObject() unitTypes?: Record<string, unknown> | null;
@ApiPropertyOptional() @IsOptional() @IsArray() media?: Record<string, unknown>[] | null;
@ApiPropertyOptional() @IsOptional() @IsArray() documents?: Record<string, unknown>[] | null;
@ApiPropertyOptional() @IsOptional() @IsArray() @IsString({ each: true }) tags?: string[];
@ApiPropertyOptional() @IsOptional() @IsBoolean() isVerified?: boolean;
@ApiPropertyOptional() @IsOptional() @IsDateString() startDate?: string | null;
@ApiPropertyOptional() @IsOptional() @IsDateString() completionDate?: string | null;
}

View File

@@ -0,0 +1,24 @@
import { Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { CreateProjectHandler } from './application/commands/create-project/create-project.handler';
import { UpdateProjectHandler } from './application/commands/update-project/update-project.handler';
import { GetProjectHandler } from './application/queries/get-project/get-project.handler';
import { ListProjectsHandler } from './application/queries/list-projects/list-projects.handler';
import { PROJECT_REPOSITORY } from './domain/repositories/project-development.repository';
import { PrismaProjectDevelopmentRepository } from './infrastructure/repositories/prisma-project-development.repository';
import { ProjectsController } from './presentation/controllers/projects.controller';
const CommandHandlers = [CreateProjectHandler, UpdateProjectHandler];
const QueryHandlers = [GetProjectHandler, ListProjectsHandler];
@Module({
imports: [CqrsModule],
controllers: [ProjectsController],
providers: [
{ provide: PROJECT_REPOSITORY, useClass: PrismaProjectDevelopmentRepository },
...CommandHandlers,
...QueryHandlers,
],
exports: [PROJECT_REPOSITORY],
})
export class ProjectsModule {}