Some checks failed
CI / Lint → Typecheck → Test → Build (22) (push) Failing after 16s
CI / E2E Tests (push) Has been skipped
CodeQL Analysis / CodeQL (javascript-typescript) (push) Failing after 50s
Deploy / Build API Image (push) Failing after 25s
Deploy / Build Web Image (push) Failing after 11s
Deploy / Build AI Services Image (push) Failing after 10s
E2E Tests / Playwright E2E (push) Failing after 12s
Security Scanning / Dependency Audit (pnpm) (push) Failing after 4s
Security Scanning / Trivy Scan — API Image (push) Failing after 1m16s
Security Scanning / Trivy Scan — Web Image (push) Failing after 1m2s
Security Scanning / Trivy Scan — AI Services Image (push) Failing after 50s
Security Scanning / Trivy Filesystem Scan (push) Failing after 38s
Deploy / Deploy to Staging (push) Has been skipped
Deploy / Smoke Test Staging (push) Has been skipped
Deploy / Deploy to Production (push) Has been skipped
Deploy / Smoke Test Production (push) Has been skipped
Security Scanning / Security Gate (push) Failing after 0s
Deploy / Rollback Production (push) Has been skipped
Deploy / Rollback Staging (push) Failing after 10m50s
Two new B2B roles for CĐT (project developers) and KCN operators, provisioned by
admin. Each account owns a subset of ProjectDevelopment / IndustrialPark records
and can CRUD them from the dashboard; admin retains full access.
Phase 1 — Schema
- Extend UserRole enum with DEVELOPER + PARK_OPERATOR (before ADMIN)
- ProjectDevelopment.ownerId FK (User, ON DELETE SET NULL) + index
- IndustrialPark.ownerId FK + index
- Migration 20260420030000
Phase 2a — Backend authorization
- CreateProjectCommand + CreateIndustrialParkCommand accept ownerId; controllers
auto-set it to the caller's user id when role=DEVELOPER / PARK_OPERATOR
- Update + Delete commands gain (requesterUserId, requesterRole) and enforce
ADMIN-or-owner via ForbiddenException; reassigning ownerId is admin-only
- Search params gain optional ownerId filter wired through Prisma repos
- New endpoints: GET /projects/mine/list, GET /industrial/parks/mine/list
- user-rate-limit guard: add DEVELOPER + PARK_OPERATOR entries (300/window)
Phase 2b — Admin provision
- ProvisionDeveloperCommand/Handler: create user (role=DEVELOPER), pre-validate
target projects have no existing owner, batch-assign ownerId
- ProvisionParkOperatorCommand/Handler: same for PARK_OPERATOR + IndustrialPark
- POST /admin/accounts/developers, POST /admin/accounts/park-operators (admin-only)
- DTOs with phone/password/fullName/email + optional {project,park}Ids[]
Phase 2c — Project stats for developer dashboard
- GetProjectStatsQuery + handler: aggregates linkedListingCount, activeListingCount,
totalInquiries, unreadInquiries, savedByUsers via Property → Listing → Inquiry chain
- GET /projects/:id/stats — admin sees all, DEVELOPER only their own (403 otherwise)
Phase 3 — Frontend
- Dashboard layout role-aware: DEVELOPER sees "Dự án của tôi" + CRM + Profile (hides
listings/analytics/subscription); PARK_OPERATOR sees "KCN của tôi" equivalent
- /projects dashboard page switches to duAnApi.searchMine() when role=DEVELOPER
- /industrial-parks page switches to industrialApi.searchMine() when role=PARK_OPERATOR
- Admin nav gains "Tài khoản CĐT" + "Tài khoản KCN" entries
- New pages /admin/accounts/developers + /admin/accounts/park-operators with
checkbox-based multi-select for linking entities
- adminApi.provisionDeveloper + provisionParkOperator + types
- duAnApi.searchMine + getStats; industrialApi.searchMine
- Login demo accounts list includes CĐT Vingroup + KCN VSIP
Phase 4 — Seed (prisma/seed-b2b-accounts.ts)
- DEVELOPER "CĐT Vingroup" (+84912000001) owns 4 projects
- DEVELOPER "CĐT Masterise Homes" (+84912000003) owns 2 projects
- PARK_OPERATOR "Vận hành KCN VSIP" (+84912000002) owns 2 seeded KCN
- Password Velik@2026 for all
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
278 lines
10 KiB
TypeScript
278 lines
10 KiB
TypeScript
'use client';
|
|
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { Pencil, Plus, Trash2 } from 'lucide-react';
|
|
import Image from 'next/image';
|
|
import Link from 'next/link';
|
|
import * as React from 'react';
|
|
import { Badge } from '@/components/ui/badge';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Card, CardContent } from '@/components/ui/card';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Select } from '@/components/ui/select';
|
|
import { useAuthStore } from '@/lib/auth-store';
|
|
import { formatPrice } from '@/lib/currency';
|
|
import {
|
|
duAnApi,
|
|
PROJECT_STATUS_COLORS,
|
|
PROJECT_STATUS_LABELS,
|
|
type ProjectStatus,
|
|
type SearchProjectsParams,
|
|
} from '@/lib/du-an-api';
|
|
import { shimmerBlurDataURL } from '@/lib/image-blur';
|
|
|
|
const STATUS_OPTIONS: { value: ProjectStatus; label: string }[] = [
|
|
{ value: 'PLANNING', label: PROJECT_STATUS_LABELS.PLANNING },
|
|
{ value: 'UNDER_CONSTRUCTION', label: PROJECT_STATUS_LABELS.UNDER_CONSTRUCTION },
|
|
{ value: 'HANDOVER', label: PROJECT_STATUS_LABELS.HANDOVER },
|
|
{ value: 'COMPLETED', label: PROJECT_STATUS_LABELS.COMPLETED },
|
|
];
|
|
|
|
const INITIAL_FILTERS = {
|
|
q: '',
|
|
status: '' as '' | ProjectStatus,
|
|
city: '',
|
|
page: 1,
|
|
};
|
|
|
|
export default function ProjectsAdminPage() {
|
|
const [filters, setFilters] = React.useState(INITIAL_FILTERS);
|
|
const role = useAuthStore((s) => s.user?.role);
|
|
const isDeveloper = role === 'DEVELOPER';
|
|
|
|
const queryParams = React.useMemo<SearchProjectsParams>(() => {
|
|
const params: SearchProjectsParams = { page: filters.page, limit: 12 };
|
|
if (filters.q) params.q = filters.q;
|
|
if (filters.status) params.status = filters.status;
|
|
if (filters.city) params.city = filters.city;
|
|
return params;
|
|
}, [filters]);
|
|
|
|
const { data: result, isLoading } = useQuery({
|
|
queryKey: ['admin-projects', { mine: isDeveloper, ...queryParams }],
|
|
// DEVELOPER sees only their own projects; ADMIN sees all.
|
|
queryFn: () => (isDeveloper ? duAnApi.searchMine(queryParams) : duAnApi.search(queryParams)),
|
|
staleTime: 30_000,
|
|
});
|
|
|
|
const queryClient = useQueryClient();
|
|
const deleteMutation = useMutation({
|
|
mutationFn: (id: string) => duAnApi.delete(id),
|
|
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['admin-projects'] }),
|
|
});
|
|
|
|
const handleDelete = (id: string, name: string) => {
|
|
if (!window.confirm(`Xoá dự án "${name}"? Thao tác này không thể hoàn tác.`)) return;
|
|
deleteMutation.mutate(id);
|
|
};
|
|
|
|
const resetFilters = () => setFilters(INITIAL_FILTERS);
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
{/* Header */}
|
|
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
|
<div>
|
|
<h1 className="text-2xl font-bold">Quản lý dự án</h1>
|
|
<p className="text-sm text-muted-foreground">
|
|
Tạo, chỉnh sửa và xoá dự án bất động sản.
|
|
</p>
|
|
</div>
|
|
<Link href="/projects/new">
|
|
<Button>
|
|
<Plus className="mr-2 h-4 w-4" />
|
|
Thêm dự án
|
|
</Button>
|
|
</Link>
|
|
</div>
|
|
|
|
{/* Filters */}
|
|
<div className="flex flex-wrap items-center gap-3">
|
|
<Input
|
|
value={filters.q}
|
|
onChange={(e) => setFilters((f) => ({ ...f, q: e.target.value, page: 1 }))}
|
|
placeholder="Tìm theo tên dự án..."
|
|
className="w-64"
|
|
/>
|
|
<Select
|
|
value={filters.status}
|
|
onChange={(e) =>
|
|
setFilters((f) => ({
|
|
...f,
|
|
status: e.target.value as '' | ProjectStatus,
|
|
page: 1,
|
|
}))
|
|
}
|
|
className="w-48"
|
|
>
|
|
<option value="">Tất cả trạng thái</option>
|
|
{STATUS_OPTIONS.map((opt) => (
|
|
<option key={opt.value} value={opt.value}>
|
|
{opt.label}
|
|
</option>
|
|
))}
|
|
</Select>
|
|
<Input
|
|
value={filters.city}
|
|
onChange={(e) => setFilters((f) => ({ ...f, city: e.target.value, page: 1 }))}
|
|
placeholder="Thành phố"
|
|
className="w-48"
|
|
/>
|
|
<Button variant="outline" size="sm" onClick={resetFilters}>
|
|
Đặt lại
|
|
</Button>
|
|
</div>
|
|
|
|
{/* Error banner */}
|
|
{deleteMutation.isError && (
|
|
<div className="flex items-center justify-between gap-2 rounded-md bg-destructive/10 px-3 py-2 text-sm text-destructive">
|
|
<p>Không thể xoá dự án. Vui lòng thử lại.</p>
|
|
<button
|
|
type="button"
|
|
onClick={() => deleteMutation.reset()}
|
|
className="text-xs underline"
|
|
>
|
|
Đóng
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
{/* Content */}
|
|
{isLoading ? (
|
|
<div className="flex min-h-[300px] items-center justify-center">
|
|
<div className="h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent" />
|
|
</div>
|
|
) : !result || result.data.length === 0 ? (
|
|
<div className="flex min-h-[300px] flex-col items-center justify-center text-muted-foreground">
|
|
<p>Chưa có dự án nào</p>
|
|
<Link href="/projects/new" className="mt-2">
|
|
<Button variant="outline" size="sm">
|
|
Thêm dự án đầu tiên
|
|
</Button>
|
|
</Link>
|
|
</div>
|
|
) : (
|
|
<Card>
|
|
<CardContent className="p-0">
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-sm">
|
|
<thead>
|
|
<tr className="border-b text-left">
|
|
<th className="p-3 font-medium">Ảnh</th>
|
|
<th className="p-3 font-medium">Tên dự án</th>
|
|
<th className="p-3 font-medium">Chủ đầu tư</th>
|
|
<th className="p-3 font-medium">Thành phố / Quận</th>
|
|
<th className="p-3 font-medium text-right">Tổng căn</th>
|
|
<th className="p-3 font-medium text-right">Giá từ</th>
|
|
<th className="p-3 font-medium text-center">Trạng thái</th>
|
|
<th className="p-3 font-medium text-right">Thao tác</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{result.data.map((project) => (
|
|
<tr
|
|
key={project.id}
|
|
className="border-b last:border-0 transition-colors hover:bg-accent/50"
|
|
>
|
|
<td className="p-3">
|
|
<div className="relative h-10 w-14 flex-shrink-0 overflow-hidden rounded bg-muted">
|
|
{project.thumbnailUrl ? (
|
|
<Image
|
|
src={project.thumbnailUrl}
|
|
alt={project.name}
|
|
fill
|
|
sizes="56px"
|
|
className="object-cover"
|
|
placeholder="blur"
|
|
blurDataURL={shimmerBlurDataURL()}
|
|
/>
|
|
) : (
|
|
<div className="flex h-full items-center justify-center text-xs text-muted-foreground">
|
|
N/A
|
|
</div>
|
|
)}
|
|
</div>
|
|
</td>
|
|
<td className="p-3">
|
|
<a
|
|
href={`/du-an/${project.slug}`}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="font-medium hover:text-primary hover:underline"
|
|
>
|
|
{project.name}
|
|
</a>
|
|
</td>
|
|
<td className="p-3 text-muted-foreground">
|
|
{project.developer.name}
|
|
</td>
|
|
<td className="p-3 text-muted-foreground">
|
|
{project.city}
|
|
{project.district ? ` / ${project.district}` : ''}
|
|
</td>
|
|
<td className="p-3 text-right">{project.totalUnits}</td>
|
|
<td className="p-3 text-right font-medium text-primary">
|
|
{project.minPrice ? formatPrice(project.minPrice) : '—'}
|
|
</td>
|
|
<td className="p-3 text-center">
|
|
<Badge className={PROJECT_STATUS_COLORS[project.status]}>
|
|
{PROJECT_STATUS_LABELS[project.status]}
|
|
</Badge>
|
|
</td>
|
|
<td className="p-3 text-right">
|
|
<div className="flex justify-end gap-2">
|
|
<Link href={`/projects/${project.id}/edit`}>
|
|
<Button variant="outline" size="sm">
|
|
<Pencil className="mr-1 h-3.5 w-3.5" />
|
|
Sửa
|
|
</Button>
|
|
</Link>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => handleDelete(project.id, project.name)}
|
|
disabled={deleteMutation.isPending}
|
|
className="text-destructive hover:text-destructive"
|
|
>
|
|
<Trash2 className="mr-1 h-3.5 w-3.5" />
|
|
Xoá
|
|
</Button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
)}
|
|
|
|
{/* Pagination */}
|
|
{result && result.totalPages > 1 && (
|
|
<div className="flex items-center justify-center gap-2">
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
disabled={filters.page <= 1}
|
|
onClick={() => setFilters((f) => ({ ...f, page: f.page - 1 }))}
|
|
>
|
|
Trước
|
|
</Button>
|
|
<span className="text-sm text-muted-foreground">
|
|
Trang {result.page} / {result.totalPages}
|
|
</span>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
disabled={filters.page >= result.totalPages}
|
|
onClick={() => setFilters((f) => ({ ...f, page: f.page + 1 }))}
|
|
>
|
|
Tiếp
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|