Files
goodgo-platform/apps/web/app/[locale]/(dashboard)/industrial-parks/page.tsx
Ho Ngoc Hai 33a5ff407b
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
feat(auth): add DEVELOPER + PARK_OPERATOR roles with owner scoping (B2B accounts)
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>
2026-04-20 22:12:16 +07:00

284 lines
10 KiB
TypeScript

'use client';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { ExternalLink, Pencil, Trash2 } from 'lucide-react';
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 {
industrialApi,
PARK_STATUS_COLORS,
PARK_STATUS_LABELS,
REGION_LABELS,
type IndustrialParkStatus,
type SearchIndustrialParksParams,
type VietnamRegion,
} from '@/lib/khu-cong-nghiep-api';
const STATUS_OPTIONS: IndustrialParkStatus[] = [
'PLANNING',
'UNDER_CONSTRUCTION',
'OPERATIONAL',
'FULL',
];
const REGION_OPTIONS: VietnamRegion[] = ['NORTH', 'CENTRAL', 'SOUTH'];
interface FiltersState {
q: string;
province: string;
status: IndustrialParkStatus | '';
region: VietnamRegion | '';
page: number;
}
const INITIAL_FILTERS: FiltersState = {
q: '',
province: '',
status: '',
region: '',
page: 1,
};
export default function IndustrialParksListPage() {
const [filters, setFilters] = React.useState<FiltersState>(INITIAL_FILTERS);
const role = useAuthStore((s) => s.user?.role);
const isOperator = role === 'PARK_OPERATOR';
const queryParams = React.useMemo<SearchIndustrialParksParams>(() => {
const p: SearchIndustrialParksParams = { page: filters.page, limit: 12 };
if (filters.q) p.q = filters.q;
if (filters.province) p.province = filters.province;
if (filters.status) p.status = filters.status;
if (filters.region) p.region = filters.region;
return p;
}, [filters]);
const { data: result, isLoading } = useQuery({
queryKey: ['admin-industrial-parks', { mine: isOperator, ...queryParams }],
queryFn: () =>
isOperator ? industrialApi.searchMine(queryParams) : industrialApi.search(queryParams),
staleTime: 30_000,
});
const queryClient = useQueryClient();
const deleteMutation = useMutation({
mutationFn: (id: string) => industrialApi.deletePark(id),
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['admin-industrial-parks'] }),
});
const handleDelete = (id: string, name: string) => {
if (!window.confirm(`Xoá KCN "${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 KCN</h1>
<p className="text-sm text-muted-foreground">
Quản danh sách khu công nghiệp, chủ đu tình trạng lấp đy
</p>
</div>
<Link href="/industrial-parks/new">
<Button>Thêm KCN</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, chủ đầu tư..."
className="w-64"
/>
<Input
value={filters.province}
onChange={(e) => setFilters((f) => ({ ...f, province: e.target.value, page: 1 }))}
placeholder="Tỉnh/Thành phố"
className="w-48"
/>
<Select
value={filters.region}
onChange={(e) =>
setFilters((f) => ({ ...f, region: e.target.value as VietnamRegion | '', page: 1 }))
}
className="w-40"
>
<option value="">Tất cả vùng</option>
{REGION_OPTIONS.map((r) => (
<option key={r} value={r}>
{REGION_LABELS[r]}
</option>
))}
</Select>
<Select
value={filters.status}
onChange={(e) =>
setFilters((f) => ({
...f,
status: e.target.value as IndustrialParkStatus | '',
page: 1,
}))
}
className="w-48"
>
<option value="">Tất cả trạng thái</option>
{STATUS_OPTIONS.map((s) => (
<option key={s} value={s}>
{PARK_STATUS_LABELS[s]}
</option>
))}
</Select>
<Button variant="outline" size="sm" onClick={resetFilters}>
Đt lại
</Button>
</div>
{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á KCN. 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 KCN nào</p>
<Link href="/industrial-parks/new" className="mt-2">
<Button variant="outline" size="sm">
Thêm KCN đ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">Tên KCN</th>
<th className="p-3 font-medium">Chủ đu </th>
<th className="p-3 font-medium">Tỉnh</th>
<th className="p-3 font-medium">Vùng</th>
<th className="p-3 font-medium text-right">Diện tích (ha)</th>
<th className="p-3 font-medium text-right">Tỉ lệ lấp đy</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((park) => (
<tr
key={park.id}
className="border-b last:border-0 transition-colors hover:bg-accent/50"
>
<td className="p-3">
<Link
href={`/khu-cong-nghiep/${park.slug}`}
target="_blank"
rel="noreferrer"
className="group inline-flex items-center gap-1 font-medium hover:text-primary"
>
<span className="truncate">{park.name}</span>
<ExternalLink className="h-3 w-3 opacity-60" />
</Link>
{park.nameEn && (
<p className="truncate text-xs text-muted-foreground">{park.nameEn}</p>
)}
</td>
<td className="p-3 text-muted-foreground">{park.developer}</td>
<td className="p-3">{park.province}</td>
<td className="p-3 text-xs text-muted-foreground">
{REGION_LABELS[park.region]}
</td>
<td className="p-3 text-right">{park.totalAreaHa.toLocaleString('vi-VN')}</td>
<td className="p-3 text-right">
{park.occupancyRate.toFixed(1)}%
</td>
<td className="p-3 text-center">
<Badge className={PARK_STATUS_COLORS[park.status]} variant="outline">
{PARK_STATUS_LABELS[park.status]}
</Badge>
</td>
<td className="p-3 text-right">
<div className="flex justify-end gap-1">
<Link href={`/industrial-parks/${park.id}/edit`}>
<Button variant="ghost" size="sm" aria-label="Sửa KCN">
<Pencil className="h-3.5 w-3.5" />
</Button>
</Link>
<Button
variant="ghost"
size="sm"
aria-label="Xoá KCN"
className="text-destructive"
disabled={
deleteMutation.isPending &&
deleteMutation.variables === park.id
}
onClick={() => handleDelete(park.id, park.name)}
>
<Trash2 className="h-3.5 w-3.5" />
</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>
);
}