feat(auth): add DEVELOPER + PARK_OPERATOR roles with owner scoping (B2B accounts)
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
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>
This commit is contained in:
225
apps/web/app/[locale]/(admin)/admin/accounts/developers/page.tsx
Normal file
225
apps/web/app/[locale]/(admin)/admin/accounts/developers/page.tsx
Normal file
@@ -0,0 +1,225 @@
|
||||
'use client';
|
||||
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
import { Building2, Check, Loader2 } from 'lucide-react';
|
||||
import * as React from 'react';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import {
|
||||
adminApi,
|
||||
type ProvisionAccountResult,
|
||||
type ProvisionDeveloperPayload,
|
||||
} from '@/lib/admin-api';
|
||||
import { ApiError } from '@/lib/api-client';
|
||||
import {
|
||||
duAnApi,
|
||||
PROJECT_STATUS_LABELS,
|
||||
type ProjectSummary,
|
||||
} from '@/lib/du-an-api';
|
||||
|
||||
/**
|
||||
* Admin → Provision a CĐT (DEVELOPER) account and link existing projects
|
||||
* whose `ownerId` is currently unassigned.
|
||||
*/
|
||||
export default function AdminProvisionDeveloperPage() {
|
||||
const [form, setForm] = React.useState<ProvisionDeveloperPayload>({
|
||||
phone: '',
|
||||
password: '',
|
||||
fullName: '',
|
||||
email: '',
|
||||
projectIds: [],
|
||||
});
|
||||
const [success, setSuccess] = React.useState<ProvisionAccountResult | null>(null);
|
||||
const [error, setError] = React.useState<string | null>(null);
|
||||
|
||||
// List unassigned projects (status PLANNING or UNDER_CONSTRUCTION or any — admin
|
||||
// picks). We show all projects here; the backend rejects ones already owned.
|
||||
const { data: projectsResp } = useQuery({
|
||||
queryKey: ['admin-projects-all'],
|
||||
queryFn: () => duAnApi.search({ limit: 100 }),
|
||||
staleTime: 60_000,
|
||||
});
|
||||
|
||||
const provision = useMutation({
|
||||
mutationFn: (payload: ProvisionDeveloperPayload) => adminApi.provisionDeveloper(payload),
|
||||
onSuccess: (res) => {
|
||||
setSuccess(res);
|
||||
setError(null);
|
||||
setForm({ phone: '', password: '', fullName: '', email: '', projectIds: [] });
|
||||
},
|
||||
onError: (err) => {
|
||||
setError(err instanceof ApiError ? err.message : 'Có lỗi xảy ra');
|
||||
setSuccess(null);
|
||||
},
|
||||
});
|
||||
|
||||
const toggleProject = (id: string) => {
|
||||
setForm((f) => ({
|
||||
...f,
|
||||
projectIds: f.projectIds?.includes(id)
|
||||
? f.projectIds.filter((x) => x !== id)
|
||||
: [...(f.projectIds ?? []), id],
|
||||
}));
|
||||
};
|
||||
|
||||
const onSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
provision.mutate({
|
||||
phone: form.phone.trim(),
|
||||
password: form.password,
|
||||
fullName: form.fullName.trim(),
|
||||
email: form.email?.trim() || undefined,
|
||||
projectIds: form.projectIds && form.projectIds.length > 0 ? form.projectIds : undefined,
|
||||
});
|
||||
};
|
||||
|
||||
const projects: ProjectSummary[] = projectsResp?.data ?? [];
|
||||
|
||||
return (
|
||||
<div className="mx-auto max-w-4xl space-y-6">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold">Tạo tài khoản CĐT (Chủ đầu tư)</h1>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Cấp quyền truy cập dashboard cho một chủ đầu tư. Có thể gán ngay các dự án họ
|
||||
sở hữu (các dự án đã có CĐT khác sẽ bị từ chối).
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{success && (
|
||||
<div className="rounded-md border border-green-500/40 bg-green-500/10 p-4">
|
||||
<p className="flex items-start gap-2 text-sm text-green-700 dark:text-green-400">
|
||||
<Check className="mt-0.5 h-4 w-4 shrink-0" />
|
||||
<span>
|
||||
Đã tạo tài khoản <strong>{success.fullName}</strong> ({success.phone}).
|
||||
{success.linkedProjectIds && success.linkedProjectIds.length > 0 && (
|
||||
<> Gán cho {success.linkedProjectIds.length} dự án.</>
|
||||
)}
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<div className="rounded-md border border-destructive/50 bg-destructive/10 p-3 text-sm text-destructive">
|
||||
{error}
|
||||
<button type="button" className="ml-2 underline" onClick={() => setError(null)}>
|
||||
Đóng
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={onSubmit} className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Thông tin tài khoản</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="grid gap-4 sm:grid-cols-2">
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="fullName">Họ tên *</Label>
|
||||
<Input
|
||||
id="fullName"
|
||||
value={form.fullName}
|
||||
onChange={(e) => setForm({ ...form, fullName: e.target.value })}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="phone">Số điện thoại *</Label>
|
||||
<Input
|
||||
id="phone"
|
||||
placeholder="+84912345678"
|
||||
value={form.phone}
|
||||
onChange={(e) => setForm({ ...form, phone: e.target.value })}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="email">Email (tuỳ chọn)</Label>
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
value={form.email}
|
||||
onChange={(e) => setForm({ ...form, email: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="password">Mật khẩu tạm *</Label>
|
||||
<Input
|
||||
id="password"
|
||||
type="text"
|
||||
placeholder="Tối thiểu 8 ký tự"
|
||||
value={form.password}
|
||||
onChange={(e) => setForm({ ...form, password: e.target.value })}
|
||||
required
|
||||
minLength={8}
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
CĐT đăng nhập lần đầu với mật khẩu này; khuyến nghị đổi sau khi nhận
|
||||
tài khoản.
|
||||
</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Gán dự án (tuỳ chọn)</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<p className="mb-3 text-sm text-muted-foreground">
|
||||
Chọn các dự án sẽ giao cho CĐT này quản lý. Dự án đã có owner khác sẽ bị
|
||||
backend từ chối khi submit.
|
||||
</p>
|
||||
<div className="max-h-80 space-y-1 overflow-y-auto rounded-md border p-2">
|
||||
{projects.length === 0 && (
|
||||
<p className="text-sm text-muted-foreground">Chưa có dự án nào.</p>
|
||||
)}
|
||||
{projects.map((p) => {
|
||||
const checked = form.projectIds?.includes(p.id) ?? false;
|
||||
return (
|
||||
<label
|
||||
key={p.id}
|
||||
className="flex cursor-pointer items-center gap-3 rounded-md px-2 py-2 hover:bg-accent"
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
className="h-4 w-4"
|
||||
checked={checked}
|
||||
onChange={() => toggleProject(p.id)}
|
||||
/>
|
||||
<Building2 className="h-4 w-4 text-primary" />
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="truncate text-sm font-medium">{p.name}</p>
|
||||
<p className="truncate text-xs text-muted-foreground">
|
||||
{p.developer.name} · {p.district}, {p.city}
|
||||
</p>
|
||||
</div>
|
||||
<Badge variant="outline" className="text-xs">
|
||||
{PROJECT_STATUS_LABELS[p.status]}
|
||||
</Badge>
|
||||
</label>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
{form.projectIds && form.projectIds.length > 0 && (
|
||||
<p className="mt-2 text-sm text-muted-foreground">
|
||||
Đã chọn <strong>{form.projectIds.length}</strong> dự án.
|
||||
</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<div className="flex justify-end gap-3">
|
||||
<Button type="submit" disabled={provision.isPending}>
|
||||
{provision.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Tạo tài khoản CĐT
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
'use client';
|
||||
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
import { Check, Factory, Loader2 } from 'lucide-react';
|
||||
import * as React from 'react';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import {
|
||||
adminApi,
|
||||
type ProvisionAccountResult,
|
||||
type ProvisionParkOperatorPayload,
|
||||
} from '@/lib/admin-api';
|
||||
import { ApiError } from '@/lib/api-client';
|
||||
import {
|
||||
industrialApi,
|
||||
PARK_STATUS_LABELS,
|
||||
type IndustrialParkListItem,
|
||||
} from '@/lib/khu-cong-nghiep-api';
|
||||
|
||||
/**
|
||||
* Admin → Provision a PARK_OPERATOR account and link existing KCNs to it.
|
||||
*/
|
||||
export default function AdminProvisionParkOperatorPage() {
|
||||
const [form, setForm] = React.useState<ProvisionParkOperatorPayload>({
|
||||
phone: '',
|
||||
password: '',
|
||||
fullName: '',
|
||||
email: '',
|
||||
parkIds: [],
|
||||
});
|
||||
const [success, setSuccess] = React.useState<ProvisionAccountResult | null>(null);
|
||||
const [error, setError] = React.useState<string | null>(null);
|
||||
|
||||
const { data: parksResp } = useQuery({
|
||||
queryKey: ['admin-parks-all'],
|
||||
queryFn: () => industrialApi.search({ limit: 100 }),
|
||||
staleTime: 60_000,
|
||||
});
|
||||
|
||||
const provision = useMutation({
|
||||
mutationFn: (payload: ProvisionParkOperatorPayload) =>
|
||||
adminApi.provisionParkOperator(payload),
|
||||
onSuccess: (res) => {
|
||||
setSuccess(res);
|
||||
setError(null);
|
||||
setForm({ phone: '', password: '', fullName: '', email: '', parkIds: [] });
|
||||
},
|
||||
onError: (err) => {
|
||||
setError(err instanceof ApiError ? err.message : 'Có lỗi xảy ra');
|
||||
setSuccess(null);
|
||||
},
|
||||
});
|
||||
|
||||
const togglePark = (id: string) => {
|
||||
setForm((f) => ({
|
||||
...f,
|
||||
parkIds: f.parkIds?.includes(id)
|
||||
? f.parkIds.filter((x) => x !== id)
|
||||
: [...(f.parkIds ?? []), id],
|
||||
}));
|
||||
};
|
||||
|
||||
const onSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
provision.mutate({
|
||||
phone: form.phone.trim(),
|
||||
password: form.password,
|
||||
fullName: form.fullName.trim(),
|
||||
email: form.email?.trim() || undefined,
|
||||
parkIds: form.parkIds && form.parkIds.length > 0 ? form.parkIds : undefined,
|
||||
});
|
||||
};
|
||||
|
||||
const parks: IndustrialParkListItem[] = parksResp?.data ?? [];
|
||||
|
||||
return (
|
||||
<div className="mx-auto max-w-4xl space-y-6">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold">Tạo tài khoản vận hành KCN</h1>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Cấp quyền truy cập dashboard cho đơn vị vận hành KCN. Có thể gán ngay các KCN
|
||||
họ quản lý (KCN đã có owner khác sẽ bị từ chối).
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{success && (
|
||||
<div className="rounded-md border border-green-500/40 bg-green-500/10 p-4">
|
||||
<p className="flex items-start gap-2 text-sm text-green-700 dark:text-green-400">
|
||||
<Check className="mt-0.5 h-4 w-4 shrink-0" />
|
||||
<span>
|
||||
Đã tạo tài khoản <strong>{success.fullName}</strong> ({success.phone}).
|
||||
{success.linkedParkIds && success.linkedParkIds.length > 0 && (
|
||||
<> Gán cho {success.linkedParkIds.length} KCN.</>
|
||||
)}
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<div className="rounded-md border border-destructive/50 bg-destructive/10 p-3 text-sm text-destructive">
|
||||
{error}
|
||||
<button type="button" className="ml-2 underline" onClick={() => setError(null)}>
|
||||
Đóng
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={onSubmit} className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Thông tin tài khoản</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="grid gap-4 sm:grid-cols-2">
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="fullName">Họ tên / Đơn vị *</Label>
|
||||
<Input
|
||||
id="fullName"
|
||||
value={form.fullName}
|
||||
onChange={(e) => setForm({ ...form, fullName: e.target.value })}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="phone">Số điện thoại *</Label>
|
||||
<Input
|
||||
id="phone"
|
||||
placeholder="+84912345678"
|
||||
value={form.phone}
|
||||
onChange={(e) => setForm({ ...form, phone: e.target.value })}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="email">Email (tuỳ chọn)</Label>
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
value={form.email}
|
||||
onChange={(e) => setForm({ ...form, email: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="password">Mật khẩu tạm *</Label>
|
||||
<Input
|
||||
id="password"
|
||||
type="text"
|
||||
placeholder="Tối thiểu 8 ký tự"
|
||||
value={form.password}
|
||||
onChange={(e) => setForm({ ...form, password: e.target.value })}
|
||||
required
|
||||
minLength={8}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Gán KCN (tuỳ chọn)</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="max-h-80 space-y-1 overflow-y-auto rounded-md border p-2">
|
||||
{parks.length === 0 && (
|
||||
<p className="text-sm text-muted-foreground">Chưa có KCN nào.</p>
|
||||
)}
|
||||
{parks.map((p) => {
|
||||
const checked = form.parkIds?.includes(p.id) ?? false;
|
||||
return (
|
||||
<label
|
||||
key={p.id}
|
||||
className="flex cursor-pointer items-center gap-3 rounded-md px-2 py-2 hover:bg-accent"
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
className="h-4 w-4"
|
||||
checked={checked}
|
||||
onChange={() => togglePark(p.id)}
|
||||
/>
|
||||
<Factory className="h-4 w-4 text-primary" />
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="truncate text-sm font-medium">{p.name}</p>
|
||||
<p className="truncate text-xs text-muted-foreground">
|
||||
{p.province} · {p.totalAreaHa} ha
|
||||
</p>
|
||||
</div>
|
||||
<Badge variant="outline" className="text-xs">
|
||||
{PARK_STATUS_LABELS[p.status]}
|
||||
</Badge>
|
||||
</label>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
{form.parkIds && form.parkIds.length > 0 && (
|
||||
<p className="mt-2 text-sm text-muted-foreground">
|
||||
Đã chọn <strong>{form.parkIds.length}</strong> KCN.
|
||||
</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<div className="flex justify-end gap-3">
|
||||
<Button type="submit" disabled={provision.isPending}>
|
||||
{provision.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Tạo tài khoản KCN
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import {
|
||||
Users,
|
||||
ClipboardList,
|
||||
ShieldCheck,
|
||||
Building2,
|
||||
Factory,
|
||||
LogOut,
|
||||
Menu,
|
||||
Sparkles,
|
||||
@@ -31,6 +33,8 @@ export default function AdminLayout({ children }: { children: React.ReactNode })
|
||||
{ href: '/admin/users' as const, label: t('adminNav.users'), icon: Users },
|
||||
{ href: '/admin/moderation' as const, label: t('adminNav.moderation'), icon: ClipboardList },
|
||||
{ href: '/admin/kyc' as const, label: t('adminNav.kyc'), icon: ShieldCheck },
|
||||
{ href: '/admin/accounts/developers' as const, label: 'Tài khoản CĐT', icon: Building2 },
|
||||
{ href: '/admin/accounts/park-operators' as const, label: 'Tài khoản KCN', icon: Factory },
|
||||
{ href: '/admin/settings/ai' as const, label: t('adminNav.aiSettings'), icon: Sparkles },
|
||||
];
|
||||
|
||||
|
||||
@@ -18,11 +18,18 @@ import { loginSchema, type LoginFormData } from '@/lib/validations/auth';
|
||||
|
||||
const DEMO_PASSWORD = 'Velik@2026';
|
||||
|
||||
const DEMO_ACCOUNTS: { phone: string; name: string; role: 'ADMIN' | 'AGENT' | 'SELLER' | 'BUYER'; badgeClass: string }[] = [
|
||||
const DEMO_ACCOUNTS: {
|
||||
phone: string;
|
||||
name: string;
|
||||
role: 'ADMIN' | 'AGENT' | 'SELLER' | 'BUYER' | 'DEVELOPER' | 'PARK_OPERATOR';
|
||||
badgeClass: string;
|
||||
}[] = [
|
||||
{ phone: '+84876677771', name: 'Hồ Ngọc Hải', role: 'ADMIN', badgeClass: 'bg-red-500/10 text-red-600 border-red-500/20' },
|
||||
{ phone: '+84900000002', name: 'Nguyễn Văn An', role: 'AGENT', badgeClass: 'bg-blue-500/10 text-blue-600 border-blue-500/20' },
|
||||
{ phone: '+84900000005', name: 'Phạm Đức Dũng', role: 'SELLER', badgeClass: 'bg-amber-500/10 text-amber-600 border-amber-500/20' },
|
||||
{ phone: '+84900000004', name: 'Lê Minh Cường', role: 'BUYER', badgeClass: 'bg-emerald-500/10 text-emerald-600 border-emerald-500/20' },
|
||||
{ phone: '+84912000001', name: 'CĐT Vingroup', role: 'DEVELOPER', badgeClass: 'bg-violet-500/10 text-violet-600 border-violet-500/20' },
|
||||
{ phone: '+84912000002', name: 'KCN VSIP', role: 'PARK_OPERATOR', badgeClass: 'bg-cyan-500/10 text-cyan-600 border-cyan-500/20' },
|
||||
];
|
||||
|
||||
export default function LoginPage() {
|
||||
|
||||
@@ -9,6 +9,7 @@ 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,
|
||||
@@ -46,6 +47,8 @@ const INITIAL_FILTERS: FiltersState = {
|
||||
|
||||
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 };
|
||||
@@ -57,8 +60,9 @@ export default function IndustrialParksListPage() {
|
||||
}, [filters]);
|
||||
|
||||
const { data: result, isLoading } = useQuery({
|
||||
queryKey: ['admin-industrial-parks', queryParams],
|
||||
queryFn: () => industrialApi.search(queryParams),
|
||||
queryKey: ['admin-industrial-parks', { mine: isOperator, ...queryParams }],
|
||||
queryFn: () =>
|
||||
isOperator ? industrialApi.searchMine(queryParams) : industrialApi.search(queryParams),
|
||||
staleTime: 30_000,
|
||||
});
|
||||
|
||||
|
||||
@@ -75,56 +75,115 @@ export default function DashboardLayout({ children }: { children: React.ReactNod
|
||||
);
|
||||
}
|
||||
|
||||
const role = user?.role;
|
||||
const isDeveloper = role === 'DEVELOPER';
|
||||
const isParkOperator = role === 'PARK_OPERATOR';
|
||||
// B2B roles get a focused nav: dashboard + their owned catalog + CRM + profile.
|
||||
// ADMIN / AGENT / SELLER / BUYER keep the full nav.
|
||||
const showListings = !isDeveloper && !isParkOperator;
|
||||
const showProjects = !isParkOperator;
|
||||
const showParks = !isDeveloper;
|
||||
|
||||
const navGroups: NavGroup[] = [
|
||||
{
|
||||
label: t('dashboard.title'),
|
||||
items: [
|
||||
{ href: '/dashboard', label: t('dashboard.title'), icon: Home },
|
||||
{ href: '/listings', label: t('dashboard.listings'), icon: List },
|
||||
{ href: '/listings/new', label: t('dashboard.createListing'), icon: Plus },
|
||||
...(showListings
|
||||
? [
|
||||
{ href: '/listings', label: t('dashboard.listings'), icon: List },
|
||||
{ href: '/listings/new', label: t('dashboard.createListing'), icon: Plus },
|
||||
]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
{
|
||||
label: t('dashboard.catalogs'),
|
||||
items: [
|
||||
{ href: '/projects', label: t('dashboard.manageProjects'), icon: Building2 },
|
||||
{ href: '/industrial-parks', label: t('dashboard.manageIndustrialParks'), icon: Factory },
|
||||
...(showProjects
|
||||
? [
|
||||
{
|
||||
href: '/projects',
|
||||
label: isDeveloper ? 'Dự án của tôi' : t('dashboard.manageProjects'),
|
||||
icon: Building2,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(showParks
|
||||
? [
|
||||
{
|
||||
href: '/industrial-parks',
|
||||
label: isParkOperator
|
||||
? 'KCN của tôi'
|
||||
: t('dashboard.manageIndustrialParks'),
|
||||
icon: Factory,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
{
|
||||
label: 'CRM',
|
||||
items: [
|
||||
{ href: '/inquiries', label: t('dashboard.inquiries'), icon: MessageSquare },
|
||||
{ href: '/leads', label: t('dashboard.leads'), icon: Target },
|
||||
],
|
||||
},
|
||||
{
|
||||
label: t('dashboard.analytics'),
|
||||
items: [
|
||||
{ href: '/analytics', label: t('dashboard.analytics'), icon: BarChart3 },
|
||||
{ href: '/dashboard/reports', label: t('dashboard.reports'), icon: FileText },
|
||||
{ href: '/dashboard/saved-searches', label: t('dashboard.savedSearches'), icon: Bookmark },
|
||||
{ href: '/dashboard/valuation', label: t('dashboard.aiValuation'), icon: Bot },
|
||||
...(showListings
|
||||
? [{ href: '/leads', label: t('dashboard.leads'), icon: Target }]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
...(showListings
|
||||
? [
|
||||
{
|
||||
label: t('dashboard.analytics'),
|
||||
items: [
|
||||
{ href: '/analytics', label: t('dashboard.analytics'), icon: BarChart3 },
|
||||
{ href: '/dashboard/reports', label: t('dashboard.reports'), icon: FileText },
|
||||
{ href: '/dashboard/saved-searches', label: t('dashboard.savedSearches'), icon: Bookmark },
|
||||
{ href: '/dashboard/valuation', label: t('dashboard.aiValuation'), icon: Bot },
|
||||
],
|
||||
},
|
||||
]
|
||||
: []),
|
||||
{
|
||||
label: t('dashboard.profile'),
|
||||
items: [
|
||||
{ href: '/dashboard/profile', label: t('dashboard.profile'), icon: User },
|
||||
{ href: '/dashboard/subscription', label: t('dashboard.subscription'), icon: Gem },
|
||||
{ href: '/dashboard/payments', label: t('dashboard.payments'), icon: CreditCard },
|
||||
...(showListings
|
||||
? [
|
||||
{ href: '/dashboard/subscription', label: t('dashboard.subscription'), icon: Gem },
|
||||
{ href: '/dashboard/payments', label: t('dashboard.payments'), icon: CreditCard },
|
||||
]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
];
|
||||
].filter((g) => g.items.length > 0);
|
||||
|
||||
// Flat list for desktop nav (only primary items shown inline)
|
||||
const primaryNav: NavItem[] = [
|
||||
{ href: '/dashboard', label: t('dashboard.title'), icon: Home },
|
||||
{ href: '/listings', label: t('dashboard.listings'), icon: List },
|
||||
{ href: '/projects', label: t('dashboard.manageProjects'), icon: Building2 },
|
||||
{ href: '/industrial-parks', label: t('dashboard.manageIndustrialParks'), icon: Factory },
|
||||
...(showListings ? [{ href: '/listings', label: t('dashboard.listings'), icon: List }] : []),
|
||||
...(showProjects
|
||||
? [
|
||||
{
|
||||
href: '/projects',
|
||||
label: isDeveloper ? 'Dự án của tôi' : t('dashboard.manageProjects'),
|
||||
icon: Building2,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(showParks
|
||||
? [
|
||||
{
|
||||
href: '/industrial-parks',
|
||||
label: isParkOperator ? 'KCN của tôi' : t('dashboard.manageIndustrialParks'),
|
||||
icon: Factory,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
{ href: '/inquiries', label: t('dashboard.inquiries'), icon: MessageSquare },
|
||||
{ href: '/analytics', label: t('dashboard.analytics'), icon: BarChart3 },
|
||||
...(showListings
|
||||
? [{ href: '/analytics', label: t('dashboard.analytics'), icon: BarChart3 }]
|
||||
: []),
|
||||
];
|
||||
|
||||
const secondaryNav: NavItem[] = [
|
||||
|
||||
@@ -10,6 +10,7 @@ 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,
|
||||
@@ -36,6 +37,8 @@ const INITIAL_FILTERS = {
|
||||
|
||||
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 };
|
||||
@@ -46,8 +49,9 @@ export default function ProjectsAdminPage() {
|
||||
}, [filters]);
|
||||
|
||||
const { data: result, isLoading } = useQuery({
|
||||
queryKey: ['admin-projects', queryParams],
|
||||
queryFn: () => duAnApi.search(queryParams),
|
||||
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,
|
||||
});
|
||||
|
||||
|
||||
@@ -196,4 +196,36 @@ export const adminApi = {
|
||||
|
||||
updateAiSettings: (body: UpdateAiSettingsPayload) =>
|
||||
apiClient.patch<AiSettings>('/admin/settings/ai', body),
|
||||
|
||||
// B2B account provisioning
|
||||
provisionDeveloper: (body: ProvisionDeveloperPayload) =>
|
||||
apiClient.post<ProvisionAccountResult>('/admin/accounts/developers', body),
|
||||
|
||||
provisionParkOperator: (body: ProvisionParkOperatorPayload) =>
|
||||
apiClient.post<ProvisionAccountResult>('/admin/accounts/park-operators', body),
|
||||
};
|
||||
|
||||
export interface ProvisionDeveloperPayload {
|
||||
phone: string;
|
||||
password: string;
|
||||
fullName: string;
|
||||
email?: string;
|
||||
projectIds?: string[];
|
||||
}
|
||||
|
||||
export interface ProvisionParkOperatorPayload {
|
||||
phone: string;
|
||||
password: string;
|
||||
fullName: string;
|
||||
email?: string;
|
||||
parkIds?: string[];
|
||||
}
|
||||
|
||||
export interface ProvisionAccountResult {
|
||||
userId: string;
|
||||
phone: string;
|
||||
email: string | null;
|
||||
fullName: string;
|
||||
linkedProjectIds?: string[];
|
||||
linkedParkIds?: string[];
|
||||
}
|
||||
|
||||
@@ -242,6 +242,29 @@ export const duAnApi = {
|
||||
);
|
||||
},
|
||||
|
||||
/** DEVELOPER / ADMIN only — returns projects owned by the current user. */
|
||||
searchMine: (params: SearchProjectsParams = {}) => {
|
||||
const query = new URLSearchParams();
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== '') query.append(key, String(value));
|
||||
});
|
||||
const qs = query.toString();
|
||||
return apiClient.get<PaginatedResult<ProjectSummary>>(
|
||||
`/projects/mine/list${qs ? `?${qs}` : ''}`,
|
||||
);
|
||||
},
|
||||
|
||||
/** Stats for the "Dự án của tôi" dashboard card — admin + owner only. */
|
||||
getStats: (projectId: string) =>
|
||||
apiClient.get<{
|
||||
projectId: string;
|
||||
linkedListingCount: number;
|
||||
activeListingCount: number;
|
||||
totalInquiries: number;
|
||||
unreadInquiries: number;
|
||||
savedByUsers: number;
|
||||
}>(`/projects/${projectId}/stats`),
|
||||
|
||||
getBySlug: (slug: string) =>
|
||||
apiClient.get<ProjectDetail>(`/projects/${slug}`),
|
||||
|
||||
|
||||
@@ -274,6 +274,18 @@ export const industrialApi = {
|
||||
);
|
||||
},
|
||||
|
||||
/** PARK_OPERATOR / ADMIN only — returns KCN owned by the current user. */
|
||||
searchMine: (params: SearchIndustrialParksParams = {}) => {
|
||||
const query = new URLSearchParams();
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== '') query.append(key, String(value));
|
||||
});
|
||||
const qs = query.toString();
|
||||
return apiClient.get<PaginatedResult<IndustrialParkListItem>>(
|
||||
`/industrial/parks/mine/list${qs ? `?${qs}` : ''}`,
|
||||
);
|
||||
},
|
||||
|
||||
getBySlug: (slug: string) =>
|
||||
apiClient.get<IndustrialParkDetail>(`/industrial/parks/${slug}`),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user