openadminjs 0.1.24 → 0.3.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +117 -81
- package/dist/adapt-package-manager.d.ts +3 -0
- package/dist/adapt-package-manager.js +118 -0
- package/dist/create-project.d.ts +77 -2
- package/dist/create-project.js +685 -224
- package/dist/db-commands.d.ts +20 -0
- package/dist/db-commands.js +75 -0
- package/dist/detect-package-manager.d.ts +8 -0
- package/dist/detect-package-manager.js +37 -0
- package/dist/doctor.d.ts +26 -0
- package/dist/doctor.js +232 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +476 -166
- package/dist/render-schema.d.ts +3 -0
- package/dist/render-schema.js +33 -0
- package/dist/resource-slug.d.ts +1 -0
- package/dist/resource-slug.js +7 -0
- package/dist/runtime-mode.d.ts +16 -0
- package/dist/runtime-mode.js +138 -0
- package/dist/secrets.d.ts +24 -0
- package/dist/secrets.js +93 -0
- package/dist/security.d.ts +13 -0
- package/dist/security.js +168 -0
- package/dist/upgrade.d.ts +10 -0
- package/dist/upgrade.js +133 -0
- package/dist/validate-name.d.ts +33 -0
- package/dist/validate-name.js +206 -0
- package/package.json +39 -11
- package/template/.dockerignore +15 -0
- package/template/.env.production.example +5 -0
- package/template/Dockerfile +12 -0
- package/template/README.md +23 -3
- package/template/apps/admin/app/api/auth/login/route.ts +9 -9
- package/template/apps/admin/app/api/auth/logout/route.ts +11 -3
- package/template/apps/admin/app/api/auth/oidc/callback/route.ts +19 -0
- package/template/apps/admin/app/api/auth/oidc/start/route.ts +11 -0
- package/template/apps/admin/app/api/auth/oidc/status/route.ts +6 -0
- package/template/apps/admin/app/api/auth/refresh/route.ts +18 -11
- package/template/apps/admin/app/api/backend/[...path]/route.ts +48 -0
- package/template/apps/admin/app/api-tokens/page.tsx +39 -39
- package/template/apps/admin/app/assistant/page.tsx +11 -0
- package/template/apps/admin/app/dashboard/page.tsx +45 -462
- package/template/apps/admin/app/globals.css +52 -16
- package/template/apps/admin/app/layout.tsx +1 -1
- package/template/apps/admin/app/login/page.tsx +13 -145
- package/template/apps/admin/app/plugins/page.tsx +16 -402
- package/template/apps/admin/app/profile/page.tsx +5 -2
- package/template/apps/admin/app/settings/page.tsx +14 -265
- package/template/apps/admin/components/ai-assistant-widget.tsx +3 -6
- package/template/apps/admin/components/app-shell.tsx +25 -373
- package/template/apps/admin/components/auth-guard.tsx +8 -28
- package/template/apps/admin/components/logo.tsx +1 -30
- package/template/apps/admin/components/resource-form.tsx +3 -3
- package/template/apps/admin/components/resource-table.tsx +12 -8
- package/template/apps/admin/lib/api.ts +45 -107
- package/template/apps/admin/lib/resource-table-prefs.ts +1 -57
- package/template/apps/admin/lib/server-api.ts +7 -0
- package/template/apps/admin/lib/tokens.ts +9 -81
- package/template/apps/admin/middleware.ts +9 -50
- package/template/apps/admin/next.config.mjs +4 -0
- package/template/apps/admin/package.json +2 -1
- package/template/apps/admin/public/brand/favicon.svg +4 -1
- package/template/apps/admin/tailwind.config.ts +9 -8
- package/template/apps/api/package.json +13 -6
- package/template/apps/api/src/access/catalog.ts +4 -0
- package/template/apps/api/src/admin/admin-ai.controller.ts +15 -125
- package/template/apps/api/src/admin/admin-analytics.service.ts +16 -148
- package/template/apps/api/src/admin/admin-logs.controller.ts +4 -1
- package/template/apps/api/src/admin/admin-plugins.controller.ts +8 -8
- package/template/apps/api/src/admin/admin-plugins.service.ts +15 -259
- package/template/apps/api/src/admin/admin-resource.service.ts +105 -254
- package/template/apps/api/src/admin/admin.controller.ts +17 -4
- package/template/apps/api/src/admin/admin.module.ts +2 -2
- package/template/apps/api/src/admin/resource-policy.ts +1 -0
- package/template/apps/api/src/admin/resource-query.ts +1 -0
- package/template/apps/api/src/admin/safe-ai.service.ts +43 -0
- package/template/apps/api/src/admin/validate-resource-payload.ts +2 -0
- package/template/apps/api/src/auth/auth.controller.ts +4 -2
- package/template/apps/api/src/auth/auth.service.ts +68 -83
- package/template/apps/api/src/auth/sso-oidc.controller.ts +12 -74
- package/template/apps/api/src/auth/sso-oidc.service.ts +51 -268
- package/template/apps/api/src/common/api-exception.filter.ts +1 -0
- package/template/apps/api/src/common/auth.guard.ts +66 -39
- package/template/apps/api/src/common/json-field-codec.ts +138 -0
- package/template/apps/api/src/common/prisma.service.ts +12 -7
- package/template/apps/api/src/common/request-logging.interceptor.ts +8 -3
- package/template/apps/api/src/graphql/graphql.module.ts +14 -1
- package/template/apps/api/src/health/health.controller.ts +6 -0
- package/template/apps/api/src/main.ts +11 -2
- package/template/apps/api/src/plugins/plugin-loader.ts +5 -3
- package/template/apps/api/src/resources/commerce.resource.ts +3 -34
- package/template/apps/api/src/resources/operations.resource.ts +1 -0
- package/template/apps/api/src/store/payment/payment.controller.ts +15 -92
- package/template/apps/api/src/store/payment/payment.module.ts +2 -3
- package/template/apps/api/src/store/payment/payment.service.ts +121 -227
- package/template/apps/api/webpack.config.cjs +2 -1
- package/template/apps/web/next.config.mjs +4 -0
- package/template/apps/web/public/brand/favicon.svg +4 -1
- package/template/compose.yaml +56 -0
- package/template/examples/crm-admin/README.md +31 -0
- package/template/examples/crm-admin/package.json +17 -0
- package/template/examples/crm-admin/prisma/models.fragment.prisma +50 -0
- package/template/examples/crm-admin/src/index.ts +7 -0
- package/template/examples/crm-admin/src/resources/activities.resource.ts +29 -0
- package/template/examples/crm-admin/src/resources/companies.resource.ts +21 -0
- package/template/examples/crm-admin/src/resources/contacts.resource.ts +24 -0
- package/template/examples/crm-admin/src/resources/deals.resource.ts +30 -0
- package/template/examples/crm-admin/tsconfig.json +7 -0
- package/template/package.json +8 -1
- package/template/packages/admin-client/package.json +18 -0
- package/template/packages/admin-client/src/contracts.ts +29 -0
- package/template/packages/admin-client/src/index.ts +2 -0
- package/template/packages/admin-client/src/preferences.ts +57 -0
- package/template/packages/admin-client/tsconfig.json +12 -0
- package/template/packages/auth/package.json +1 -1
- package/template/packages/brand/site/assets/brand/favicon.svg +4 -1
- package/template/packages/brand/src/generate-assets.mjs +17 -186
- package/template/packages/core/package.json +1 -1
- package/template/packages/mail/package.json +1 -1
- package/template/packages/nest/package.json +22 -0
- package/template/packages/nest/src/api-exception.filter.ts +23 -0
- package/template/packages/nest/src/index.ts +3 -0
- package/template/packages/nest/src/resource-policy.ts +64 -0
- package/template/packages/nest/src/resource-query.ts +103 -0
- package/template/packages/nest/tsconfig.json +12 -0
- package/template/packages/notifications/package.json +1 -1
- package/template/packages/payments/package.json +1 -1
- package/template/packages/permissions/package.json +1 -1
- package/template/packages/permissions/src/index.ts +2 -2
- package/template/packages/plugin-sdk/package.json +1 -1
- package/template/packages/plugin-sdk/src/index.ts +2 -2
- package/template/packages/prisma/package.json +21 -0
- package/template/packages/prisma/schema/ai.prisma +36 -0
- package/template/packages/prisma/schema/audit.prisma +13 -0
- package/template/packages/prisma/schema/commerce.prisma +79 -0
- package/template/packages/prisma/schema/content.prisma +27 -0
- package/template/packages/prisma/schema/core.prisma +106 -0
- package/template/packages/prisma/schema/header.prisma +8 -0
- package/template/packages/prisma/schema/jobs.prisma +9 -0
- package/template/packages/prisma/schema/media.prisma +9 -0
- package/template/packages/prisma/schema/modules.json +47 -0
- package/template/packages/prisma/schema/notifications.prisma +9 -0
- package/template/packages/prisma/schema/settings.prisma +8 -0
- package/template/packages/prisma/schema/tokens.prisma +10 -0
- package/template/packages/prisma/src/index.d.ts +12 -0
- package/template/packages/prisma/src/index.mjs +138 -0
- package/template/packages/queue/package.json +1 -1
- package/template/packages/resource/package.json +1 -1
- package/template/packages/resource/src/index.ts +1 -0
- package/template/packages/storage/package.json +1 -1
- package/template/packages/storage/src/index.ts +37 -0
- package/template/prisma/migrations/20260713120000_add_user_oidc_key_and_category_order/migration.sql +11 -0
- package/template/prisma/migrations/20261126130000_add_sessions/migration.sql +26 -0
- package/template/prisma/migrations/20261126140000_api_token_hash/migration.sql +3 -0
- package/template/prisma/migrations/20261126150000_payment_idempotency/migration.sql +6 -0
- package/template/prisma/schema.prisma +82 -54
- package/template/prisma/seed.ts +14 -38
- package/template/runtime-versions.json +15 -0
- package/template/scripts/db-init.mjs +33 -0
- package/template/scripts/install-crm.mjs +50 -0
- package/template/scripts/migrate-existing.mjs +65 -0
- package/template/scripts/schema.mjs +48 -0
- package/template/tsconfig.base.json +23 -5
- package/LICENSE +0 -21
- package/dist/create-project.test.d.ts +0 -1
- package/dist/create-project.test.js +0 -99
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -7
- package/dist/resource-slug.test.d.ts +0 -1
- package/dist/resource-slug.test.js +0 -9
- package/template/apps/admin/.storybook/main.ts +0 -12
- package/template/apps/admin/.storybook/preview.ts +0 -10
- package/template/apps/admin/app/login/page.test.tsx +0 -18
- package/template/apps/admin/components/auth-guard.test.tsx +0 -55
- package/template/apps/admin/components/logo.stories.tsx +0 -24
- package/template/apps/admin/lib/api.test.ts +0 -118
- package/template/apps/admin/lib/auth-flow.e2e.test.ts +0 -79
- package/template/apps/admin/vitest.config.ts +0 -7
- package/template/apps/api/src/access/derive-permissions.test.ts +0 -26
- package/template/apps/api/src/admin/admin-resource.service.test.ts +0 -118
- package/template/apps/api/src/admin/validate-resource-payload.test.ts +0 -44
- package/template/apps/api/src/app.module.test.ts +0 -12
- package/template/apps/api/src/auth/auth.service.test.ts +0 -78
- package/template/apps/api/src/auth/sso-oidc.service.test.ts +0 -84
- package/template/apps/api/src/common/auth.guard.test.ts +0 -62
- package/template/apps/api/src/common/casl-ability.factory.test.ts +0 -33
- package/template/apps/api/src/common/request-logging.interceptor.test.ts +0 -39
- package/template/apps/api/src/graphql/admin-resources.resolver.test.ts +0 -45
- package/template/apps/api/src/graphql/ensure-gql-paramtypes.test.ts +0 -44
- package/template/apps/api/src/graphql/graphql-schema.test.ts +0 -26
- package/template/apps/api/src/plugins/manifest.schema.test.ts +0 -36
- package/template/apps/api/src/plugins/plugin-runtime.test.ts +0 -24
- package/template/apps/api/src/queue/queue.service.test.ts +0 -33
- package/template/apps/api/src/resources/resource-hooks.registry.test.ts +0 -72
- package/template/apps/api/vitest.config.ts +0 -21
- package/template/apps/web/lib/seo.test.ts +0 -41
- package/template/apps/web/vitest.config.ts +0 -19
- package/template/packages/auth/src/index.test.ts +0 -10
- package/template/packages/brand/src/index.test.ts +0 -9
- package/template/packages/core/src/index.test.ts +0 -27
- package/template/packages/core/vitest.config.ts +0 -9
- package/template/packages/mail/src/index.test.ts +0 -23
- package/template/packages/notifications/src/index.test.ts +0 -9
- package/template/packages/permissions/src/blueprints.test.ts +0 -62
- package/template/packages/permissions/src/index.test.ts +0 -34
- package/template/packages/plugin-sdk/src/sdk.test.ts +0 -13
- package/template/packages/plugin-sdk/vitest.config.ts +0 -5
- package/template/packages/queue/src/index.test.ts +0 -12
- package/template/packages/resource/src/index.test.ts +0 -196
- package/template/packages/storage/src/index.test.ts +0 -10
|
@@ -1,139 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
Controller,
|
|
4
|
-
Delete,
|
|
5
|
-
Get,
|
|
6
|
-
Inject,
|
|
7
|
-
Param,
|
|
8
|
-
Patch,
|
|
9
|
-
Post,
|
|
10
|
-
Put,
|
|
11
|
-
Query,
|
|
12
|
-
Res,
|
|
13
|
-
UseGuards
|
|
14
|
-
} from "@nestjs/common";
|
|
1
|
+
import { Body, Controller, ForbiddenException, Get, Inject, Post, Put, UseGuards, BadRequestException } from "@nestjs/common";
|
|
2
|
+
import { Throttle } from "@nestjs/throttler";
|
|
15
3
|
import { AuthGuard } from "../common/auth.guard";
|
|
16
4
|
import { RequireAuthRealm } from "../common/auth-realm.decorator";
|
|
17
5
|
import { CurrentUser } from "../common/current-user.decorator";
|
|
18
|
-
import {
|
|
19
|
-
import type { AiStreamErrorEvent } from "./admin-ai.events";
|
|
6
|
+
import { SafeAiService } from "./safe-ai.service";
|
|
20
7
|
|
|
21
|
-
type
|
|
8
|
+
type User={permissions:string[]};
|
|
9
|
+
const requirePermission=(user:User,p:string)=>{if(!user.permissions.includes("*")&&!user.permissions.includes(p))throw new ForbiddenException("Missing permission");};
|
|
22
10
|
|
|
23
11
|
@UseGuards(AuthGuard)
|
|
24
12
|
@RequireAuthRealm("admin")
|
|
25
13
|
@Controller("admin/ai")
|
|
26
14
|
export class AdminAiController {
|
|
27
|
-
constructor(@Inject(
|
|
28
|
-
|
|
15
|
+
constructor(@Inject(SafeAiService) private readonly ai:SafeAiService){}
|
|
16
|
+
@Get("status")
|
|
17
|
+
status(@CurrentUser() user:User){return {enabled:this.ai.enabled()&&(user.permissions.includes("*")||user.permissions.includes("ai.chat"))};}
|
|
29
18
|
@Get("config")
|
|
30
|
-
config() {
|
|
31
|
-
return this.ai.getConfig();
|
|
32
|
-
}
|
|
33
|
-
|
|
19
|
+
config(@CurrentUser() user:User){requirePermission(user,"ai.config.read");return this.ai.config();}
|
|
34
20
|
@Put("config")
|
|
35
|
-
updateConfig(
|
|
36
|
-
|
|
37
|
-
body: {
|
|
38
|
-
providers?: Array<{ id: string; label: string; kind: "openai" | "anthropic" | "gemini"; baseUrl?: string; model: string }>;
|
|
39
|
-
activeProvider?: string;
|
|
40
|
-
tokenProviderId?: string;
|
|
41
|
-
token?: string;
|
|
42
|
-
systemPrompt?: string;
|
|
43
|
-
}
|
|
44
|
-
) {
|
|
45
|
-
return this.ai.updateConfig(body);
|
|
46
|
-
}
|
|
47
|
-
|
|
21
|
+
updateConfig(@CurrentUser() user:User){requirePermission(user,"ai.config.update");throw new ForbiddenException("Provider and credentials are managed by the deployment operator");}
|
|
22
|
+
@Throttle({default:{limit:8,ttl:60000}})
|
|
48
23
|
@Post("chat")
|
|
49
|
-
chat(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
body
|
|
53
|
-
message: string;
|
|
54
|
-
pagePath?: string;
|
|
55
|
-
attachments?: Array<{ name: string; mimeType: string; contentBase64: string }>;
|
|
56
|
-
}
|
|
57
|
-
) {
|
|
58
|
-
return this.ai.chat(user, body);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
@Post("chat/stream")
|
|
62
|
-
async chatStream(
|
|
63
|
-
@CurrentUser() user: UserContext,
|
|
64
|
-
@Body()
|
|
65
|
-
body: {
|
|
66
|
-
message: string;
|
|
67
|
-
pagePath?: string;
|
|
68
|
-
conversationId?: string;
|
|
69
|
-
pendingAction?: Record<string, unknown>;
|
|
70
|
-
confirmAction?: boolean;
|
|
71
|
-
attachments?: Array<{ name: string; mimeType: string; contentBase64: string }>;
|
|
72
|
-
},
|
|
73
|
-
@Res() res: { setHeader: (name: string, value: string) => void; write: (chunk: string) => void; end: () => void }
|
|
74
|
-
) {
|
|
75
|
-
res.setHeader("content-type", "text/event-stream");
|
|
76
|
-
res.setHeader("cache-control", "no-cache");
|
|
77
|
-
res.setHeader("connection", "keep-alive");
|
|
78
|
-
try {
|
|
79
|
-
for await (const event of this.ai.chatStream(user, body)) {
|
|
80
|
-
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
|
81
|
-
}
|
|
82
|
-
} catch (error) {
|
|
83
|
-
const err = error as { response?: { message?: string; code?: string; details?: unknown } };
|
|
84
|
-
const errorCode = err.response?.code ?? "AI_STREAM_FAILED";
|
|
85
|
-
const payload: AiStreamErrorEvent = {
|
|
86
|
-
type: "error",
|
|
87
|
-
message: "Chat stream failed",
|
|
88
|
-
code: errorCode,
|
|
89
|
-
details: err.response?.details ?? null
|
|
90
|
-
};
|
|
91
|
-
res.write(`data: ${JSON.stringify(payload)}\n\n`);
|
|
92
|
-
}
|
|
93
|
-
res.end();
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
@Get("conversations")
|
|
97
|
-
conversations(
|
|
98
|
-
@CurrentUser() user: UserContext,
|
|
99
|
-
@Query("q") q?: string,
|
|
100
|
-
@Query("page") page?: string,
|
|
101
|
-
@Query("limit") limit?: string
|
|
102
|
-
) {
|
|
103
|
-
return this.ai.listConversations(user, q, Number(page ?? 1), Number(limit ?? 20));
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
@Get("conversations/:id")
|
|
107
|
-
conversation(@CurrentUser() user: UserContext, @Param("id") id: string) {
|
|
108
|
-
return this.ai.getConversation(user, id);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
@Patch("conversations/:id")
|
|
112
|
-
renameConversation(
|
|
113
|
-
@CurrentUser() user: UserContext,
|
|
114
|
-
@Param("id") id: string,
|
|
115
|
-
@Body() body: { title: string }
|
|
116
|
-
) {
|
|
117
|
-
return this.ai.renameConversation(user, id, body.title);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
@Delete("conversations/:id")
|
|
121
|
-
deleteConversation(@CurrentUser() user: UserContext, @Param("id") id: string) {
|
|
122
|
-
return this.ai.deleteConversation(user, id);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
@Get("artifacts/:id/download")
|
|
126
|
-
async downloadArtifact(
|
|
127
|
-
@CurrentUser() user: UserContext,
|
|
128
|
-
@Param("id") id: string,
|
|
129
|
-
@Res() res: {
|
|
130
|
-
setHeader: (name: string, value: string) => void;
|
|
131
|
-
send: (body: Buffer) => void;
|
|
132
|
-
}
|
|
133
|
-
) {
|
|
134
|
-
const artifact = await this.ai.getArtifact(user, id);
|
|
135
|
-
res.setHeader("content-type", artifact.mimeType);
|
|
136
|
-
res.setHeader("content-disposition", `attachment; filename="${artifact.filename.replace(/"/g, "")}"`);
|
|
137
|
-
res.send(Buffer.from(artifact.contentBase64, "base64"));
|
|
24
|
+
chat(@CurrentUser() user:User,@Body() body:unknown){
|
|
25
|
+
requirePermission(user,"ai.chat");
|
|
26
|
+
if(!body||typeof body!=="object"||Array.isArray(body)||Object.keys(body).some(k=>k!=="message"))throw new BadRequestException("Only a message is accepted");
|
|
27
|
+
return this.ai.chat((body as {message?:unknown}).message);
|
|
138
28
|
}
|
|
139
29
|
}
|
|
@@ -9,156 +9,24 @@ export class AdminAnalyticsService {
|
|
|
9
9
|
constructor(@Inject(PrismaService) private readonly prisma: PrismaService) {}
|
|
10
10
|
|
|
11
11
|
async overview(user: UserContext, period: Period = "30d") {
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.prisma.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
this.prisma.post.findMany({ where: { createdAt: { gte: from } }, select: { createdAt: true, status: true } }),
|
|
24
|
-
this.prisma.user.findMany({ where: { createdAt: { gte: from } }, select: { createdAt: true } }),
|
|
25
|
-
this.prisma.jobLog.findMany({ where: { createdAt: { gte: from } }, select: { status: true } }),
|
|
26
|
-
this.prisma.auditLog.findMany({
|
|
27
|
-
take: 15,
|
|
28
|
-
orderBy: { createdAt: "desc" },
|
|
29
|
-
select: { id: true, action: true, resource: true, resourceId: true, createdAt: true, user: { select: { email: true, name: true } } }
|
|
30
|
-
}),
|
|
31
|
-
this.health()
|
|
12
|
+
const has = (permission: string) => user.permissions.includes("*") || user.permissions.includes(permission);
|
|
13
|
+
const from = new Date(Date.now() - (period === "7d" ? 7 : period === "90d" ? 90 : 30) * 86400000);
|
|
14
|
+
const [usersTotal, postsTotal, publishedPosts, filesTotal, recentActivity] = await Promise.all([
|
|
15
|
+
has("users.read") ? this.prisma.user.count() : null,
|
|
16
|
+
has("posts.read") ? this.prisma.post.count() : null,
|
|
17
|
+
has("posts.read") ? this.prisma.post.count({ where: { status: "published" } }) : null,
|
|
18
|
+
has("files.read") ? this.prisma.fileAsset.count() : null,
|
|
19
|
+
has("audit-logs.read") ? this.prisma.auditLog.findMany({
|
|
20
|
+
where: { createdAt: { gte: from } }, take: 15, orderBy: { createdAt: "desc" },
|
|
21
|
+
select: { id: true, action: true, resource: true, createdAt: true }
|
|
22
|
+
}) : []
|
|
32
23
|
]);
|
|
33
|
-
|
|
34
|
-
const revenueData = this.buildTimeSeries(period, recentPosts, recentUsers);
|
|
35
|
-
const trafficChannels = this.buildTrafficChannels(recentPosts, recentUsers);
|
|
36
|
-
const crmPipeline = this.buildCrmPipeline(recentPosts);
|
|
37
|
-
const ecommerceRevenue = this.buildEcommerceRevenue(revenueData);
|
|
38
|
-
const marketingMix = this.buildMarketingMix(trafficChannels);
|
|
39
|
-
const conversion = this.buildConversion(recentUsers, publishedPosts);
|
|
40
|
-
|
|
41
24
|
return {
|
|
42
|
-
metrics: {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
},
|
|
48
|
-
charts: {
|
|
49
|
-
revenue: revenueData,
|
|
50
|
-
trafficChannels,
|
|
51
|
-
conversion,
|
|
52
|
-
crmPipeline,
|
|
53
|
-
ecommerceRevenue,
|
|
54
|
-
marketingMix
|
|
55
|
-
},
|
|
56
|
-
jobs: this.buildJobStats(jobsRecent),
|
|
57
|
-
recentActivity: recentActivity.map((entry) => ({
|
|
58
|
-
id: entry.id,
|
|
59
|
-
action: entry.action,
|
|
60
|
-
resource: entry.resource,
|
|
61
|
-
resourceId: entry.resourceId,
|
|
62
|
-
createdAt: entry.createdAt,
|
|
63
|
-
user: entry.user ? (entry.user.name ?? entry.user.email ?? "system") : "system"
|
|
64
|
-
})),
|
|
65
|
-
health
|
|
25
|
+
metrics: { usersTotal, postsTotal, publishedPosts, filesTotal },
|
|
26
|
+
recentActivity,
|
|
27
|
+
period,
|
|
28
|
+
// No fabricated revenue, traffic, conversion or job statistics.
|
|
29
|
+
health: { api: "ok" }
|
|
66
30
|
};
|
|
67
31
|
}
|
|
68
|
-
|
|
69
|
-
private buildTimeSeries(period: Period, posts: Array<{ createdAt: Date; status: string }>, users: Array<{ createdAt: Date }>) {
|
|
70
|
-
const bucketCount = period === "7d" ? 7 : period === "30d" ? 4 : 3;
|
|
71
|
-
const labels = period === "7d" ? ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] : period === "30d" ? ["W1", "W2", "W3", "W4"] : ["M1", "M2", "M3"];
|
|
72
|
-
const buckets = Array.from({ length: bucketCount }, (_, index) => ({ name: labels[index] ?? `P${index + 1}`, revenue: 0, signups: 0 }));
|
|
73
|
-
|
|
74
|
-
for (const user of users) {
|
|
75
|
-
const index = this.bucketIndex(user.createdAt, bucketCount);
|
|
76
|
-
const bucket = buckets[index];
|
|
77
|
-
if (!bucket) continue;
|
|
78
|
-
bucket.signups += 1;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
for (const post of posts) {
|
|
82
|
-
const index = this.bucketIndex(post.createdAt, bucketCount);
|
|
83
|
-
const multiplier = post.status === "published" ? 12 : 6;
|
|
84
|
-
const bucket = buckets[index];
|
|
85
|
-
if (!bucket) continue;
|
|
86
|
-
bucket.revenue += multiplier;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return buckets.map((bucket) => ({ ...bucket, revenue: Math.max(bucket.revenue, bucket.signups * 4) }));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
private buildTrafficChannels(posts: Array<{ status: string }>, users: Array<{ createdAt: Date }>) {
|
|
93
|
-
const base = Math.max(users.length, 1);
|
|
94
|
-
const organic = Math.min(55, 30 + Math.round((posts.filter((post) => post.status === "published").length / base) * 100));
|
|
95
|
-
const paid = 25;
|
|
96
|
-
const referral = 12;
|
|
97
|
-
const direct = Math.max(8, 100 - organic - paid - referral);
|
|
98
|
-
return [
|
|
99
|
-
{ label: "Organic", value: organic, color: "bg-[#7B8CFF]" },
|
|
100
|
-
{ label: "Paid", value: paid, color: "bg-[#20D2C2]" },
|
|
101
|
-
{ label: "Referral", value: referral, color: "bg-[#7A5AF8]" },
|
|
102
|
-
{ label: "Direct", value: direct, color: "bg-[#98A2B3]" }
|
|
103
|
-
];
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
private buildCrmPipeline(posts: Array<{ status: string }>) {
|
|
107
|
-
const total = Math.max(posts.length, 8);
|
|
108
|
-
return [
|
|
109
|
-
{ stage: "New", value: total },
|
|
110
|
-
{ stage: "Qualified", value: Math.max(Math.round(total * 0.68), 1) },
|
|
111
|
-
{ stage: "Proposal", value: Math.max(Math.round(total * 0.42), 1) },
|
|
112
|
-
{ stage: "Won", value: Math.max(posts.filter((post) => post.status === "published").length, 1) }
|
|
113
|
-
];
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
private buildEcommerceRevenue(revenueData: Array<{ name: string; revenue: number }>) {
|
|
117
|
-
return revenueData.map((point) => ({ month: point.name, value: point.revenue }));
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
private buildMarketingMix(channels: Array<{ label: string; value: number }>) {
|
|
121
|
-
return channels.map((channel) => ({
|
|
122
|
-
name: channel.label,
|
|
123
|
-
value: channel.value,
|
|
124
|
-
color: channel.label === "Organic" ? "#7B8CFF" : channel.label === "Paid" ? "#20D2C2" : channel.label === "Referral" ? "#7A5AF8" : "#98A2B3"
|
|
125
|
-
}));
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
private buildConversion(users: Array<{ createdAt: Date }>, publishedPosts: number) {
|
|
129
|
-
const visits = Math.max(users.length * 20, 100);
|
|
130
|
-
const signups = Math.max(users.length, 1);
|
|
131
|
-
const trials = Math.max(Math.round(signups * 0.55), 1);
|
|
132
|
-
const paid = Math.max(Math.min(publishedPosts, trials), 1);
|
|
133
|
-
return [
|
|
134
|
-
{ label: "Visits", value: visits, percent: 100 },
|
|
135
|
-
{ label: "Signups", value: signups, percent: Math.max(Math.round((signups / visits) * 100), 1) },
|
|
136
|
-
{ label: "Trials", value: trials, percent: Math.max(Math.round((trials / visits) * 100), 1) },
|
|
137
|
-
{ label: "Paid", value: paid, percent: Math.max(Math.round((paid / visits) * 100), 1) }
|
|
138
|
-
];
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
private buildJobStats(rows: Array<{ status: string }>) {
|
|
142
|
-
return rows.reduce(
|
|
143
|
-
(acc, row) => {
|
|
144
|
-
if (row.status === "completed") acc.completed += 1;
|
|
145
|
-
else if (row.status === "failed") acc.failed += 1;
|
|
146
|
-
else if (row.status === "processing") acc.processing += 1;
|
|
147
|
-
else acc.queued += 1;
|
|
148
|
-
return acc;
|
|
149
|
-
},
|
|
150
|
-
{ queued: 0, processing: 0, completed: 0, failed: 0 }
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
private bucketIndex(date: Date, bucketCount: number) {
|
|
155
|
-
const day = date.getDay();
|
|
156
|
-
return bucketCount === 7 ? (day + 6) % 7 : Math.min(Math.floor(((date.getDate() - 1) / 31) * bucketCount), bucketCount - 1);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
private async health() {
|
|
160
|
-
const db = await this.prisma.$queryRaw`SELECT 1`;
|
|
161
|
-
void db;
|
|
162
|
-
return { api: "ok", db: "ok", redis: process.env.REDIS_URL ? "configured" : "unavailable" };
|
|
163
|
-
}
|
|
164
32
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Controller, Get, Inject, Query, UseGuards } from "@nestjs/common";
|
|
1
|
+
import { Controller, Get, Inject, Query, UseGuards, ForbiddenException } from "@nestjs/common";
|
|
2
2
|
import { AuthGuard } from "../common/auth.guard";
|
|
3
3
|
import { RequireAuthRealm } from "../common/auth-realm.decorator";
|
|
4
|
+
import { CurrentUser } from "../common/current-user.decorator";
|
|
4
5
|
import { AppLoggerService } from "../common/app-logger.service";
|
|
5
6
|
|
|
6
7
|
@UseGuards(AuthGuard)
|
|
@@ -11,6 +12,7 @@ export class AdminLogsController {
|
|
|
11
12
|
|
|
12
13
|
@Get()
|
|
13
14
|
list(
|
|
15
|
+
@CurrentUser() user: {permissions:string[]},
|
|
14
16
|
@Query("level") level: "info" | "error" | undefined,
|
|
15
17
|
@Query("search") search: string | undefined,
|
|
16
18
|
@Query("from") from: string | undefined,
|
|
@@ -18,6 +20,7 @@ export class AdminLogsController {
|
|
|
18
20
|
@Query("page") page: string | undefined,
|
|
19
21
|
@Query("limit") limit: string | undefined
|
|
20
22
|
) {
|
|
23
|
+
if (!user.permissions.includes("*") && !user.permissions.includes("system-logs.read")) throw new ForbiddenException("Missing permission");
|
|
21
24
|
return this.logs.query({
|
|
22
25
|
level,
|
|
23
26
|
search,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Controller, Delete, ForbiddenException, Get, Inject, Patch, Post, UseGuards } from "@nestjs/common";
|
|
2
2
|
import { AuthGuard } from "../common/auth.guard";
|
|
3
3
|
import { RequireAuthRealm } from "../common/auth-realm.decorator";
|
|
4
|
-
import { AddPluginDto, PatchPluginDto } from "./dto/plugins.dto";
|
|
5
4
|
import { AdminPluginsService } from "./admin-plugins.service";
|
|
6
5
|
|
|
7
6
|
@UseGuards(AuthGuard)
|
|
@@ -20,18 +19,19 @@ export class AdminPluginsController {
|
|
|
20
19
|
return this.plugins.getDashboardWidgets();
|
|
21
20
|
}
|
|
22
21
|
|
|
22
|
+
// Plugin installation and manifest changes are deployment-operator actions.
|
|
23
23
|
@Post()
|
|
24
|
-
add(
|
|
25
|
-
|
|
24
|
+
add() {
|
|
25
|
+
throw new ForbiddenException("Install plugins during build or deployment");
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
@Patch(":id")
|
|
29
|
-
patch(
|
|
30
|
-
|
|
29
|
+
patch() {
|
|
30
|
+
throw new ForbiddenException("Configure plugins in the deployment manifest");
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
@Delete(":id")
|
|
34
|
-
remove(
|
|
35
|
-
|
|
34
|
+
remove() {
|
|
35
|
+
throw new ForbiddenException("Configure plugins in the deployment manifest");
|
|
36
36
|
}
|
|
37
37
|
}
|