Some checks failed
CI / E2E Tests (push) Has been skipped
CodeQL Analysis / CodeQL (javascript-typescript) (push) Failing after 1m23s
Deploy / Build API Image (push) Failing after 33s
Deploy / Deploy to Staging (push) Has been skipped
CI / Lint → Typecheck → Test → Build (22) (push) Failing after 9s
Deploy / Build Web Image (push) Failing after 11s
Deploy / Build AI Services Image (push) Failing after 9s
E2E Tests / Playwright E2E (push) Failing after 18s
Security Scanning / Dependency Audit (pnpm) (push) Failing after 2s
Security Scanning / Trivy Scan — API Image (push) Failing after 59s
Security Scanning / Trivy Scan — Web Image (push) Failing after 51s
Security Scanning / Trivy Scan — AI Services Image (push) Failing after 34s
Security Scanning / Trivy Filesystem Scan (push) Failing after 24s
Deploy / Smoke Test Staging (push) Has been skipped
Deploy / Deploy to Production (push) Has been skipped
Security Scanning / Security Gate (push) Failing after 1s
Deploy / Rollback Production (push) Has been skipped
Deploy / Smoke Test Production (push) Has been skipped
Deploy / Rollback Staging (push) Has been skipped
Foundation for Phase E (AI advisor / AI valuation on listing detail).
An admin sets the Anthropic Claude credentials once in the new
"/admin/settings/ai" page; downstream features read them via
SystemSettingsService.
Database
--------
- New Prisma model SystemSetting { key @id, value Text, valueType,
isSecret, updatedAt, updatedBy }. db:push applied cleanly.
Backend
-------
- SystemSettingsService — canonical getter/setter for
ai.api_url / ai.api_key / ai.model. maskApiKey() returns the last 4
chars prefixed with "sk-ant-...". Exposes unmasked getAiSettings()
for server-side consumers (AI advisor handlers).
- GET /admin/settings/ai — returns { apiUrl, apiKeyMasked, model,
hasApiKey, updatedAt }. Never emits the raw key.
- PATCH /admin/settings/ai — body accepts partial { apiUrl, apiKey,
model }. apiKey sentinel "__UNCHANGED__" preserves the stored value;
empty string clears it; any other value overwrites.
- CQRS: get-ai-settings query + update-ai-settings command. Registered
in admin.module.ts; service exported via modules/admin/index.ts so
Phase E can inject it.
Frontend
--------
- adminApi.getAiSettings() / updateAiSettings() added to
lib/admin-api.ts with shared AiSettings + UpdateAiSettingsPayload
types.
- New Lucide-only nav entry "Cài đặt AI" (Sparkles) in admin layout.
- /admin/settings/ai/page.tsx — Card with API URL input, masked API
key input with Eye/EyeOff toggle, "Xoá key" button, model Select
(claude-opus-4-5 / sonnet-4-5 / haiku-4-5 + custom input), save
button with inline success/error banners, "last updated" timestamp.
- i18n keys adminNav.settings + adminNav.aiSettings in vi.json/en.json.
Constraints
-----------
- No new packages. Runtime imports for NestJS-DI classes preserved.
- Key NOT encrypted at rest (MVP); documented in service comment as
future hardening.
- Page inherits existing admin auth guard via (admin) layout.
Verification
------------
- API typecheck clean.
- Web typecheck clean in touched files.
- API suite: 1975 / 1975 pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
200 lines
4.9 KiB
TypeScript
200 lines
4.9 KiB
TypeScript
import { apiClient } from './api-client';
|
|
|
|
// ── Types ──
|
|
|
|
export interface DashboardStats {
|
|
totalUsers: number;
|
|
totalListings: number;
|
|
activeListings: number;
|
|
pendingModerationCount: number;
|
|
totalAgents: number;
|
|
verifiedAgents: number;
|
|
totalTransactions: number;
|
|
newUsersLast30Days: number;
|
|
newListingsLast30Days: number;
|
|
}
|
|
|
|
export interface RevenueStatsItem {
|
|
period: string;
|
|
totalRevenue: number;
|
|
subscriptionRevenue: number;
|
|
listingFeeRevenue: number;
|
|
featuredListingRevenue: number;
|
|
transactionCount: number;
|
|
}
|
|
|
|
export interface ModerationQueueItem {
|
|
listingId: string;
|
|
propertyTitle: string;
|
|
propertyType: string;
|
|
transactionType: string;
|
|
priceVND: number;
|
|
sellerName: string;
|
|
sellerId: string;
|
|
moderationScore: number | null;
|
|
createdAt: string;
|
|
}
|
|
|
|
export interface PaginatedResult<T> {
|
|
data: T[];
|
|
total: number;
|
|
page: number;
|
|
limit: number;
|
|
totalPages: number;
|
|
}
|
|
|
|
export interface UserListItem {
|
|
id: string;
|
|
email: string | null;
|
|
phone: string;
|
|
fullName: string;
|
|
role: string;
|
|
kycStatus: string;
|
|
isActive: boolean;
|
|
createdAt: string;
|
|
}
|
|
|
|
export interface UserDetail {
|
|
id: string;
|
|
email: string | null;
|
|
phone: string;
|
|
fullName: string;
|
|
avatarUrl: string | null;
|
|
role: string;
|
|
kycStatus: string;
|
|
kycData: unknown;
|
|
isActive: boolean;
|
|
createdAt: string;
|
|
updatedAt: string;
|
|
listingsCount: number;
|
|
activeListingsCount: number;
|
|
transactionsCount: number;
|
|
subscription: {
|
|
planTier: string;
|
|
status: string;
|
|
currentPeriodEnd: string;
|
|
} | null;
|
|
recentActivity: Array<{
|
|
type: string;
|
|
description: string;
|
|
createdAt: string;
|
|
}>;
|
|
}
|
|
|
|
export interface AiSettings {
|
|
apiUrl: string;
|
|
apiKeyMasked: string | null;
|
|
model: string;
|
|
hasApiKey: boolean;
|
|
updatedAt: string | null;
|
|
}
|
|
|
|
export interface UpdateAiSettingsPayload {
|
|
apiUrl?: string;
|
|
apiKey?: string;
|
|
model?: string;
|
|
}
|
|
|
|
export interface KycQueueItem {
|
|
userId: string;
|
|
fullName: string;
|
|
email: string | null;
|
|
phone: string;
|
|
role: string;
|
|
kycStatus: string;
|
|
kycData: unknown;
|
|
createdAt: string;
|
|
}
|
|
|
|
// ── API ──
|
|
|
|
export const adminApi = {
|
|
// Dashboard
|
|
getDashboardStats: () =>
|
|
apiClient.get<DashboardStats>('/admin/dashboard'),
|
|
|
|
getRevenueStats: (startDate: string, endDate: string, groupBy: 'day' | 'month' = 'month') =>
|
|
apiClient.get<RevenueStatsItem[]>(
|
|
`/admin/revenue?startDate=${startDate}&endDate=${endDate}&groupBy=${groupBy}`,
|
|
),
|
|
|
|
// Moderation
|
|
getModerationQueue: (page = 1, limit = 20) =>
|
|
apiClient.get<PaginatedResult<ModerationQueueItem>>(
|
|
`/admin/moderation?page=${page}&limit=${limit}`,
|
|
),
|
|
|
|
approveListing: (listingId: string, moderationNotes?: string) =>
|
|
apiClient.post<{ success: boolean }>('/admin/moderation/approve', {
|
|
listingId,
|
|
moderationNotes,
|
|
}),
|
|
|
|
rejectListing: (listingId: string, reason: string) =>
|
|
apiClient.post<{ success: boolean }>('/admin/moderation/reject', {
|
|
listingId,
|
|
reason,
|
|
}),
|
|
|
|
bulkModerate: (listingIds: string[], action: 'approve' | 'reject', reason?: string) =>
|
|
apiClient.post<{ success: boolean }>('/admin/moderation/bulk', {
|
|
listingIds,
|
|
action,
|
|
reason,
|
|
}),
|
|
|
|
// Users
|
|
getUsers: (params: { page?: number; limit?: number; role?: string; isActive?: boolean; search?: string } = {}) => {
|
|
const query = new URLSearchParams();
|
|
if (params.page) query.set('page', String(params.page));
|
|
if (params.limit) query.set('limit', String(params.limit));
|
|
if (params.role) query.set('role', params.role);
|
|
if (params.isActive !== undefined) query.set('isActive', String(params.isActive));
|
|
if (params.search) query.set('search', params.search);
|
|
return apiClient.get<PaginatedResult<UserListItem>>(
|
|
`/admin/users?${query.toString()}`,
|
|
);
|
|
},
|
|
|
|
getUserDetail: (userId: string) =>
|
|
apiClient.get<UserDetail>(`/admin/users/${userId}`),
|
|
|
|
updateUserStatus: (userId: string, isActive: boolean, reason?: string) =>
|
|
apiClient.post<{ success: boolean }>('/admin/users/status', {
|
|
userId,
|
|
isActive,
|
|
reason,
|
|
}),
|
|
|
|
banUser: (userId: string, reason: string, unban = false) =>
|
|
apiClient.post<{ success: boolean }>('/admin/users/ban', {
|
|
userId,
|
|
reason,
|
|
unban,
|
|
}),
|
|
|
|
// KYC
|
|
getKycQueue: (page = 1, limit = 20) =>
|
|
apiClient.get<PaginatedResult<KycQueueItem>>(
|
|
`/admin/kyc?page=${page}&limit=${limit}`,
|
|
),
|
|
|
|
approveKyc: (userId: string, notes?: string) =>
|
|
apiClient.post<{ success: boolean }>('/admin/kyc/approve', {
|
|
userId,
|
|
notes,
|
|
}),
|
|
|
|
rejectKyc: (userId: string, reason: string) =>
|
|
apiClient.post<{ success: boolean }>('/admin/kyc/reject', {
|
|
userId,
|
|
reason,
|
|
}),
|
|
|
|
// AI Settings
|
|
getAiSettings: () => apiClient.get<AiSettings>('/admin/settings/ai'),
|
|
|
|
updateAiSettings: (body: UpdateAiSettingsPayload) =>
|
|
apiClient.patch<AiSettings>('/admin/settings/ai', body),
|
|
};
|