openadminjs 0.1.7 → 0.1.8
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/dist/create-project.js +27 -4
- package/dist/create-project.test.js +57 -41
- package/dist/index.js +16 -5
- package/package.json +4 -2
- package/template/.env.example +2 -2
- package/template/README.md +24 -3
- package/template/apps/admin/app/api/auth/login/route.ts +51 -0
- package/template/apps/admin/app/api/auth/logout/route.ts +11 -0
- package/template/apps/admin/app/api/auth/refresh/route.ts +46 -0
- package/template/apps/admin/app/api-tokens/page.tsx +253 -0
- package/template/apps/admin/app/audit-logs/page.tsx +5 -0
- package/template/apps/admin/app/dashboard/page.tsx +392 -0
- package/template/apps/admin/app/files/page.tsx +5 -0
- package/template/apps/admin/app/globals.css +206 -0
- package/template/apps/admin/app/jobs/page.tsx +19 -0
- package/template/apps/admin/app/layout.tsx +57 -0
- package/template/apps/admin/app/login/page.test.tsx +18 -0
- package/template/apps/admin/app/login/page.tsx +161 -0
- package/template/apps/admin/app/login/safe-next-param.ts +4 -0
- package/template/apps/admin/app/notifications/page.tsx +146 -0
- package/template/apps/admin/app/page.tsx +5 -0
- package/template/apps/admin/app/plugins/page.tsx +407 -0
- package/template/apps/admin/app/profile/page.tsx +116 -0
- package/template/apps/admin/app/resources/[resource]/[id]/edit/page.tsx +45 -0
- package/template/apps/admin/app/resources/[resource]/[id]/page.tsx +449 -0
- package/template/apps/admin/app/resources/[resource]/create/page.tsx +34 -0
- package/template/apps/admin/app/resources/[resource]/page.tsx +100 -0
- package/template/apps/admin/app/settings/page.tsx +233 -0
- package/template/apps/admin/components/ai-assistant-widget.tsx +684 -0
- package/template/apps/admin/components/app-shell.tsx +305 -0
- package/template/apps/admin/components/auth-guard.test.tsx +55 -0
- package/template/apps/admin/components/auth-guard.tsx +39 -0
- package/template/apps/admin/components/jobs-panel.tsx +103 -0
- package/template/apps/admin/components/logo.tsx +30 -0
- package/template/apps/admin/components/resource-form.tsx +349 -0
- package/template/apps/admin/components/resource-table.tsx +277 -0
- package/template/apps/admin/lib/ai-events.ts +23 -0
- package/template/apps/admin/lib/api.test.ts +118 -0
- package/template/apps/admin/lib/api.ts +135 -0
- package/template/apps/admin/lib/auth-flow.e2e.test.ts +79 -0
- package/template/apps/admin/lib/brand.ts +14 -0
- package/template/apps/admin/lib/locale.ts +49 -0
- package/template/apps/admin/lib/tokens.ts +93 -0
- package/template/apps/admin/middleware.ts +58 -0
- package/template/apps/admin/next-env.d.ts +6 -0
- package/template/apps/admin/next.config.mjs +7 -0
- package/template/apps/admin/package.json +44 -0
- package/template/apps/admin/postcss.config.mjs +6 -0
- package/template/apps/admin/public/brand/apple-touch-icon.png +0 -0
- package/template/apps/admin/public/brand/favicon.svg +1 -0
- package/template/apps/admin/public/brand/og-image.png +0 -0
- package/template/apps/admin/public/brand/openadminjs-icon-new.png +0 -0
- package/template/apps/admin/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/template/apps/admin/public/brand/openadminjs-logo-new.png +0 -0
- package/template/apps/admin/tailwind.config.ts +37 -0
- package/template/apps/admin/tsconfig.json +23 -0
- package/template/apps/admin/vitest.config.ts +7 -0
- package/template/apps/api/package.json +56 -0
- package/template/apps/api/plugins.manifest.example.json +34 -0
- package/template/apps/api/plugins.manifest.json +4 -0
- package/template/apps/api/src/access/catalog.ts +112 -0
- package/template/apps/api/src/access/derive-permissions.test.ts +26 -0
- package/template/apps/api/src/access/derive-permissions.ts +36 -0
- package/template/apps/api/src/access/public-catalog.ts +28 -0
- package/template/apps/api/src/admin/admin-ai.controller.ts +125 -0
- package/template/apps/api/src/admin/admin-ai.events.ts +23 -0
- package/template/apps/api/src/admin/admin-ai.service.ts +1271 -0
- package/template/apps/api/src/admin/admin-analytics.service.ts +164 -0
- package/template/apps/api/src/admin/admin-plugins.controller.ts +32 -0
- package/template/apps/api/src/admin/admin-plugins.service.ts +267 -0
- package/template/apps/api/src/admin/admin-resource.service.test.ts +118 -0
- package/template/apps/api/src/admin/admin-resource.service.ts +375 -0
- package/template/apps/api/src/admin/admin.controller.ts +72 -0
- package/template/apps/api/src/admin/admin.module.ts +30 -0
- package/template/apps/api/src/admin/dto/plugins.dto.ts +55 -0
- package/template/apps/api/src/admin/validate-resource-payload.test.ts +44 -0
- package/template/apps/api/src/admin/validate-resource-payload.ts +103 -0
- package/template/apps/api/src/app.module.ts +45 -0
- package/template/apps/api/src/auth/auth.controller.ts +70 -0
- package/template/apps/api/src/auth/auth.dto.ts +49 -0
- package/template/apps/api/src/auth/auth.module.ts +30 -0
- package/template/apps/api/src/auth/auth.service.test.ts +75 -0
- package/template/apps/api/src/auth/auth.service.ts +231 -0
- package/template/apps/api/src/common/auth-realm.decorator.ts +6 -0
- package/template/apps/api/src/common/auth.guard.test.ts +61 -0
- package/template/apps/api/src/common/auth.guard.ts +78 -0
- package/template/apps/api/src/common/casl-ability.factory.test.ts +33 -0
- package/template/apps/api/src/common/casl-ability.factory.ts +41 -0
- package/template/apps/api/src/common/current-user.decorator.ts +6 -0
- package/template/apps/api/src/common/permission.decorator.ts +7 -0
- package/template/apps/api/src/common/permissions.guard.ts +31 -0
- package/template/apps/api/src/common/prisma.service.ts +13 -0
- package/template/apps/api/src/health/health.controller.ts +25 -0
- package/template/apps/api/src/mail/mail.module.ts +9 -0
- package/template/apps/api/src/mail/mail.service.ts +115 -0
- package/template/apps/api/src/main.ts +37 -0
- package/template/apps/api/src/plugins/bootstrap.ts +4 -0
- package/template/apps/api/src/plugins/bundled/admin-widget.plugin.ts +25 -0
- package/template/apps/api/src/plugins/bundled/hello.plugin.ts +19 -0
- package/template/apps/api/src/plugins/bundled/image-optimizer.plugin.ts +22 -0
- package/template/apps/api/src/plugins/bundled/registry.ts +13 -0
- package/template/apps/api/src/plugins/bundled/seo.plugin.ts +25 -0
- package/template/apps/api/src/plugins/manifest-path.ts +35 -0
- package/template/apps/api/src/plugins/manifest.schema.test.ts +36 -0
- package/template/apps/api/src/plugins/manifest.schema.ts +58 -0
- package/template/apps/api/src/plugins/plugin-api.interceptor.ts +47 -0
- package/template/apps/api/src/plugins/plugin-loader.ts +134 -0
- package/template/apps/api/src/plugins/plugin-runtime.test.ts +24 -0
- package/template/apps/api/src/plugins/plugin-runtime.ts +85 -0
- package/template/apps/api/src/queue/queue.controller.ts +28 -0
- package/template/apps/api/src/queue/queue.module.ts +23 -0
- package/template/apps/api/src/queue/queue.processor.ts +47 -0
- package/template/apps/api/src/queue/queue.service.ts +50 -0
- package/template/apps/api/src/resources/categories.resource.ts +21 -0
- package/template/apps/api/src/resources/commerce.resource.ts +157 -0
- package/template/apps/api/src/resources/operations.resource.ts +111 -0
- package/template/apps/api/src/resources/posts.resource.ts +88 -0
- package/template/apps/api/src/resources/registry.ts +27 -0
- package/template/apps/api/src/resources/resource-hooks.install.ts +18 -0
- package/template/apps/api/src/resources/resource-hooks.registry.test.ts +72 -0
- package/template/apps/api/src/resources/resource-hooks.registry.ts +66 -0
- package/template/apps/api/src/resources/resource-hooks.runner.ts +93 -0
- package/template/apps/api/src/resources/system.resource.ts +51 -0
- package/template/apps/api/src/resources/users.resource.ts +19 -0
- package/template/apps/api/src/store/payment/payment.controller.ts +98 -0
- package/template/apps/api/src/store/payment/payment.module.ts +14 -0
- package/template/apps/api/src/store/payment/payment.provider.ts +38 -0
- package/template/apps/api/src/store/payment/payment.service.ts +235 -0
- package/template/apps/api/src/store/store.controller.ts +26 -0
- package/template/apps/api/src/store/store.module.ts +12 -0
- package/template/apps/api/src/test-setup.ts +1 -0
- package/template/apps/api/tsconfig.json +11 -0
- package/template/apps/api/vitest.config.ts +21 -0
- package/template/apps/web/lib/openadmin-client.ts +11 -0
- package/template/apps/web/lib/seo.test.ts +41 -0
- package/template/apps/web/lib/seo.ts +24 -13
- package/template/apps/web/next-env.d.ts +4 -0
- package/template/apps/web/package.json +2 -1
- package/template/apps/web/test/next-stub.ts +7 -0
- package/template/apps/web/vitest.config.ts +19 -0
- package/template/package.json +18 -11
- package/template/packages/auth/package.json +17 -0
- package/template/packages/auth/src/index.test.ts +10 -0
- package/template/packages/auth/src/index.ts +17 -0
- package/template/packages/auth/tsconfig.json +8 -0
- package/template/packages/brand/apps/admin/public/brand/apple-touch-icon.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/favicon.svg +1 -0
- package/template/packages/brand/apps/admin/public/brand/og-image.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/openadminjs-icon-new.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/openadminjs-logo-new.png +0 -0
- package/template/packages/brand/apps/web/public/brand/apple-touch-icon.png +0 -0
- package/template/packages/brand/apps/web/public/brand/favicon.svg +1 -0
- package/template/packages/brand/apps/web/public/brand/og-image.png +0 -0
- package/template/packages/brand/apps/web/public/brand/openadminjs-icon-new.png +0 -0
- package/template/packages/brand/apps/web/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/template/packages/brand/apps/web/public/brand/openadminjs-logo-new.png +0 -0
- package/template/packages/brand/package.json +19 -0
- package/template/packages/brand/site/assets/brand/favicon.svg +1 -0
- package/template/packages/brand/site/assets/brand/og-image.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-icon.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo-dark.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo-horizontal.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo-monochrome.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo.svg +1 -0
- package/template/packages/brand/src/generate-assets.mjs +182 -0
- package/template/packages/brand/src/index.test.ts +9 -0
- package/template/packages/brand/src/index.ts +7 -0
- package/template/packages/brand/tsconfig.json +7 -0
- package/template/packages/core/package.json +20 -0
- package/template/packages/core/src/index.js +1 -0
- package/template/packages/core/src/index.test.ts +27 -0
- package/template/packages/core/src/index.ts +61 -0
- package/template/packages/core/tsconfig.json +8 -0
- package/template/packages/mail/package.json +18 -0
- package/template/packages/mail/src/index.test.ts +23 -0
- package/template/packages/mail/src/index.ts +246 -0
- package/template/packages/mail/tsconfig.json +5 -0
- package/template/packages/notifications/package.json +16 -0
- package/template/packages/notifications/src/index.test.ts +9 -0
- package/template/packages/notifications/src/index.ts +5 -0
- package/template/packages/notifications/tsconfig.json +5 -0
- package/template/packages/payments/package.json +15 -0
- package/template/packages/payments/src/index.ts +256 -0
- package/template/packages/payments/tsconfig.json +9 -0
- package/template/packages/permissions/package.json +17 -0
- package/template/packages/permissions/src/blueprints.js +57 -0
- package/template/packages/permissions/src/blueprints.test.ts +62 -0
- package/template/packages/permissions/src/blueprints.ts +89 -0
- package/template/packages/permissions/src/index.js +11 -0
- package/template/packages/permissions/src/index.test.ts +34 -0
- package/template/packages/permissions/src/index.ts +22 -0
- package/template/packages/permissions/tsconfig.json +8 -0
- package/template/packages/plugin-sdk/package.json +26 -0
- package/template/packages/plugin-sdk/src/hooks.ts +135 -0
- package/template/packages/plugin-sdk/src/index.ts +42 -0
- package/template/packages/plugin-sdk/src/sdk.test.ts +13 -0
- package/template/packages/plugin-sdk/tsconfig.json +8 -0
- package/template/packages/plugin-sdk/vitest.config.ts +5 -0
- package/template/packages/queue/package.json +16 -0
- package/template/packages/queue/src/index.test.ts +12 -0
- package/template/packages/queue/src/index.ts +6 -0
- package/template/packages/queue/tsconfig.json +5 -0
- package/template/packages/resource/package.json +17 -0
- package/template/packages/resource/src/index.js +177 -0
- package/template/packages/resource/src/index.test.ts +151 -0
- package/template/packages/resource/src/index.ts +344 -0
- package/template/packages/resource/tsconfig.json +8 -0
- package/template/packages/storage/package.json +16 -0
- package/template/packages/storage/src/index.test.ts +10 -0
- package/template/packages/storage/src/index.ts +23 -0
- package/template/packages/storage/tsconfig.json +5 -0
- package/template/prisma/migrations/20260505151751_baseline_init_all_tables/migration.sql +371 -0
- package/template/prisma/migrations/migration_lock.toml +3 -0
- package/template/prisma/schema.prisma +291 -0
- package/template/prisma/seed.ts +217 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Injectable, Logger } from "@nestjs/common";
|
|
2
|
+
import { ConfigService } from "@nestjs/config";
|
|
3
|
+
import {
|
|
4
|
+
createPaymentProvider,
|
|
5
|
+
type PaymentProvider,
|
|
6
|
+
type ProviderName
|
|
7
|
+
} from "@openadminjs/payments";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* NestJS-injectable wrapper around the provider factory.
|
|
11
|
+
*
|
|
12
|
+
* Configure via env vars:
|
|
13
|
+
* PAYMENT_PROVIDER=stripe (default: mock)
|
|
14
|
+
* STRIPE_SECRET_KEY=sk_live_...
|
|
15
|
+
* STRIPE_WEBHOOK_SECRET=whsec_...
|
|
16
|
+
*/
|
|
17
|
+
@Injectable()
|
|
18
|
+
export class PaymentProviderService {
|
|
19
|
+
private readonly provider: PaymentProvider;
|
|
20
|
+
private readonly logger = new Logger(PaymentProviderService.name);
|
|
21
|
+
|
|
22
|
+
constructor(private readonly config: ConfigService) {
|
|
23
|
+
const providerName = (this.config.get<string>("PAYMENT_PROVIDER") ?? "mock") as ProviderName;
|
|
24
|
+
this.provider = createPaymentProvider(providerName, {
|
|
25
|
+
stripeSecretKey: this.config.get<string>("STRIPE_SECRET_KEY"),
|
|
26
|
+
stripeWebhookSecret: this.config.get<string>("STRIPE_WEBHOOK_SECRET")
|
|
27
|
+
});
|
|
28
|
+
this.logger.log(`Payment provider: ${this.provider.name}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get(): PaymentProvider {
|
|
32
|
+
return this.provider;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get name(): string {
|
|
36
|
+
return this.provider.name;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
|
2
|
+
import { PrismaService } from "../../common/prisma.service";
|
|
3
|
+
import { PaymentProviderService } from "./payment.provider";
|
|
4
|
+
|
|
5
|
+
export type CreateOrderInput = {
|
|
6
|
+
items: Array<{
|
|
7
|
+
productId?: string;
|
|
8
|
+
name: string;
|
|
9
|
+
price: number;
|
|
10
|
+
quantity: number;
|
|
11
|
+
}>;
|
|
12
|
+
currency?: string;
|
|
13
|
+
email?: string;
|
|
14
|
+
userId?: string;
|
|
15
|
+
notes?: string;
|
|
16
|
+
successUrl: string;
|
|
17
|
+
cancelUrl: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type OrderStatusResponse = {
|
|
21
|
+
id: string;
|
|
22
|
+
status: string;
|
|
23
|
+
total: number;
|
|
24
|
+
currency: string;
|
|
25
|
+
items: Array<{ name: string; price: number; quantity: number }>;
|
|
26
|
+
createdAt: Date;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
@Injectable()
|
|
30
|
+
export class PaymentService {
|
|
31
|
+
constructor(
|
|
32
|
+
private readonly prisma: PrismaService,
|
|
33
|
+
private readonly paymentProvider: PaymentProviderService
|
|
34
|
+
) {}
|
|
35
|
+
|
|
36
|
+
/** Create a new order and return a checkout URL. */
|
|
37
|
+
async createCheckout(input: CreateOrderInput) {
|
|
38
|
+
const { items, currency = "USD", email, userId, notes, successUrl, cancelUrl } = input;
|
|
39
|
+
|
|
40
|
+
if (!items.length) throw new BadRequestException({ message: "Order must contain at least one item", code: "EMPTY_ORDER" });
|
|
41
|
+
|
|
42
|
+
const total = items.reduce((sum, item) => sum + item.price * item.quantity, 0);
|
|
43
|
+
|
|
44
|
+
const order = await this.prisma.$transaction(async (tx) => {
|
|
45
|
+
const created = await tx.order.create({
|
|
46
|
+
data: {
|
|
47
|
+
userId: userId ?? null,
|
|
48
|
+
email: email ?? null,
|
|
49
|
+
currency,
|
|
50
|
+
total,
|
|
51
|
+
notes: notes ?? null,
|
|
52
|
+
status: "pending"
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
await tx.orderItem.createMany({
|
|
57
|
+
data: items.map((item) => ({
|
|
58
|
+
orderId: created.id,
|
|
59
|
+
productId: item.productId ?? null,
|
|
60
|
+
name: item.name,
|
|
61
|
+
price: item.price,
|
|
62
|
+
quantity: item.quantity
|
|
63
|
+
}))
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return created;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const provider = this.paymentProvider.get();
|
|
70
|
+
const checkout = await provider.createCheckout({
|
|
71
|
+
orderId: order.id,
|
|
72
|
+
lineItems: items.map((item) => ({
|
|
73
|
+
name: item.name,
|
|
74
|
+
unitAmount: item.price,
|
|
75
|
+
quantity: item.quantity
|
|
76
|
+
})),
|
|
77
|
+
currency,
|
|
78
|
+
successUrl,
|
|
79
|
+
cancelUrl,
|
|
80
|
+
customerEmail: email
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Log the initial transaction attempt
|
|
84
|
+
await this.prisma.transaction.create({
|
|
85
|
+
data: {
|
|
86
|
+
orderId: order.id,
|
|
87
|
+
provider: this.paymentProvider.name,
|
|
88
|
+
providerTxId: checkout.providerSessionId,
|
|
89
|
+
status: "pending",
|
|
90
|
+
amount: total,
|
|
91
|
+
currency,
|
|
92
|
+
metadata: { sessionId: checkout.providerSessionId }
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return { orderId: order.id, checkoutUrl: checkout.checkoutUrl };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Process an inbound webhook event from the payment provider. */
|
|
100
|
+
async handleWebhook(provider: string, rawBody: Buffer, signature: string) {
|
|
101
|
+
const logEntry = await this.prisma.webhookLog.create({
|
|
102
|
+
data: { provider, eventType: "unknown", payload: {}, status: "pending" }
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
const event = await this.paymentProvider.get().verifyWebhook(rawBody, signature);
|
|
107
|
+
|
|
108
|
+
await this.prisma.webhookLog.update({
|
|
109
|
+
where: { id: logEntry.id },
|
|
110
|
+
data: { eventType: event.type, payload: event.raw as object, status: "processed" }
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
if (event.orderId) {
|
|
114
|
+
await this.applyWebhookEvent(event.orderId, event.type, event.providerEventId, event.amount, event.currency);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return { received: true, type: event.type };
|
|
118
|
+
} catch (error) {
|
|
119
|
+
await this.prisma.webhookLog.update({
|
|
120
|
+
where: { id: logEntry.id },
|
|
121
|
+
data: { status: "failed", error: error instanceof Error ? error.message : "unknown" }
|
|
122
|
+
});
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private async applyWebhookEvent(
|
|
128
|
+
orderId: string,
|
|
129
|
+
type: string,
|
|
130
|
+
providerEventId: string,
|
|
131
|
+
amount?: number,
|
|
132
|
+
currency?: string
|
|
133
|
+
) {
|
|
134
|
+
const statusMap: Record<string, string> = {
|
|
135
|
+
"payment.succeeded": "paid",
|
|
136
|
+
"payment.failed": "failed",
|
|
137
|
+
"payment.refunded": "refunded",
|
|
138
|
+
"checkout.expired": "cancelled"
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const newStatus = statusMap[type];
|
|
142
|
+
if (!newStatus) return;
|
|
143
|
+
|
|
144
|
+
await this.prisma.$transaction(async (tx) => {
|
|
145
|
+
await tx.order.update({ where: { id: orderId }, data: { status: newStatus } });
|
|
146
|
+
await tx.transaction.updateMany({
|
|
147
|
+
where: { orderId, providerTxId: providerEventId },
|
|
148
|
+
data: {
|
|
149
|
+
status: newStatus === "paid" ? "succeeded" : newStatus === "refunded" ? "refunded" : "failed",
|
|
150
|
+
...(amount ? { amount } : {}),
|
|
151
|
+
...(currency ? { currency } : {})
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Get order status (public — by orderId only, no auth needed). */
|
|
158
|
+
async getOrderStatus(orderId: string): Promise<OrderStatusResponse> {
|
|
159
|
+
const order = await this.prisma.order.findUnique({
|
|
160
|
+
where: { id: orderId },
|
|
161
|
+
include: { items: true }
|
|
162
|
+
});
|
|
163
|
+
if (!order) throw new NotFoundException({ message: "Order not found", code: "ORDER_NOT_FOUND" });
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
id: order.id,
|
|
167
|
+
status: order.status,
|
|
168
|
+
total: order.total,
|
|
169
|
+
currency: order.currency,
|
|
170
|
+
items: order.items.map((item) => ({ name: item.name, price: item.price, quantity: item.quantity })),
|
|
171
|
+
createdAt: order.createdAt
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** List orders for the authenticated user. */
|
|
176
|
+
async getUserOrders(userId: string, page = 1, limit = 20) {
|
|
177
|
+
const [data, total] = await Promise.all([
|
|
178
|
+
this.prisma.order.findMany({
|
|
179
|
+
where: { userId },
|
|
180
|
+
include: { items: true },
|
|
181
|
+
orderBy: { createdAt: "desc" },
|
|
182
|
+
skip: (page - 1) * limit,
|
|
183
|
+
take: limit
|
|
184
|
+
}),
|
|
185
|
+
this.prisma.order.count({ where: { userId } })
|
|
186
|
+
]);
|
|
187
|
+
return { data, meta: { page, limit, total, pages: Math.ceil(total / limit) || 1 } };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Refund an order (admin only). */
|
|
191
|
+
async refundOrder(orderId: string) {
|
|
192
|
+
const order = await this.prisma.order.findUnique({
|
|
193
|
+
where: { id: orderId },
|
|
194
|
+
include: { transactions: true }
|
|
195
|
+
});
|
|
196
|
+
if (!order) throw new NotFoundException({ message: "Order not found", code: "ORDER_NOT_FOUND" });
|
|
197
|
+
if (order.status !== "paid") {
|
|
198
|
+
throw new BadRequestException({ message: `Cannot refund order with status "${order.status}"`, code: "INVALID_ORDER_STATUS" });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const successTx = order.transactions.find((tx) => tx.status === "succeeded");
|
|
202
|
+
if (!successTx?.providerTxId) {
|
|
203
|
+
throw new BadRequestException({ message: "No successful transaction found for this order", code: "NO_TRANSACTION" });
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const refund = await this.paymentProvider.get().refund({
|
|
207
|
+
providerTransactionId: successTx.providerTxId,
|
|
208
|
+
amount: order.total
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
await this.prisma.$transaction(async (tx) => {
|
|
212
|
+
await tx.order.update({ where: { id: orderId }, data: { status: "refunded" } });
|
|
213
|
+
await tx.transaction.create({
|
|
214
|
+
data: {
|
|
215
|
+
orderId,
|
|
216
|
+
provider: this.paymentProvider.name,
|
|
217
|
+
providerTxId: refund.providerRefundId,
|
|
218
|
+
status: "refunded",
|
|
219
|
+
amount: refund.amount,
|
|
220
|
+
currency: order.currency
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
return { ok: true, refundId: refund.providerRefundId };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** List products. */
|
|
229
|
+
async listProducts(onlyActive = true) {
|
|
230
|
+
return this.prisma.product.findMany({
|
|
231
|
+
where: onlyActive ? { active: true } : undefined,
|
|
232
|
+
orderBy: { createdAt: "desc" }
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Controller, Get, UseGuards } from "@nestjs/common";
|
|
2
|
+
import { AuthGuard } from "../common/auth.guard";
|
|
3
|
+
import { RequireAuthRealm } from "../common/auth-realm.decorator";
|
|
4
|
+
import { CurrentUser } from "../common/current-user.decorator";
|
|
5
|
+
import type { RequestUser } from "../common/auth.guard";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Example storefront-scoped routes (`Role.realm === "public"`).
|
|
9
|
+
* Add cart/checkout/etc. here or in a dedicated app — checks are the same: JWT realm + permission slugs.
|
|
10
|
+
*/
|
|
11
|
+
@Controller("store")
|
|
12
|
+
@UseGuards(AuthGuard)
|
|
13
|
+
@RequireAuthRealm("public")
|
|
14
|
+
export class StoreController {
|
|
15
|
+
@Get("session")
|
|
16
|
+
session(@CurrentUser() user: RequestUser) {
|
|
17
|
+
return {
|
|
18
|
+
realm: user.realm,
|
|
19
|
+
id: user.id,
|
|
20
|
+
email: user.email,
|
|
21
|
+
name: user.name,
|
|
22
|
+
roles: user.roles,
|
|
23
|
+
permissions: user.permissions
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Module } from "@nestjs/common";
|
|
2
|
+
import { AuthModule } from "../auth/auth.module";
|
|
3
|
+
import { PrismaService } from "../common/prisma.service";
|
|
4
|
+
import { StoreController } from "./store.controller";
|
|
5
|
+
import { PaymentModule } from "./payment/payment.module";
|
|
6
|
+
|
|
7
|
+
@Module({
|
|
8
|
+
imports: [AuthModule, PaymentModule],
|
|
9
|
+
controllers: [StoreController],
|
|
10
|
+
providers: [PrismaService]
|
|
11
|
+
})
|
|
12
|
+
export class StoreModule {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { dirname, resolve } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { defineConfig } from "vitest/config";
|
|
4
|
+
|
|
5
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
const repoRoot = resolve(__dirname, "../..");
|
|
7
|
+
|
|
8
|
+
export default defineConfig({
|
|
9
|
+
test: {
|
|
10
|
+
environment: "node",
|
|
11
|
+
setupFiles: [resolve(__dirname, "src/test-setup.ts")]
|
|
12
|
+
},
|
|
13
|
+
resolve: {
|
|
14
|
+
alias: {
|
|
15
|
+
"@openadminjs/core": resolve(repoRoot, "packages/core/src/index.ts"),
|
|
16
|
+
"@openadminjs/resource": resolve(repoRoot, "packages/resource/src/index.ts"),
|
|
17
|
+
"@openadminjs/permissions": resolve(repoRoot, "packages/permissions/src/index.ts"),
|
|
18
|
+
"@openadminjs/plugin-sdk": resolve(repoRoot, "packages/plugin-sdk/src/index.ts")
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
});
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { ResourceSeoConfig } from "@openadminjs/core";
|
|
2
|
+
|
|
1
3
|
export type PublicFieldConfig = {
|
|
2
4
|
type: string;
|
|
3
5
|
label?: string;
|
|
@@ -12,6 +14,8 @@ export type PublicResourceConfig = {
|
|
|
12
14
|
titleField: string;
|
|
13
15
|
descriptionField?: string;
|
|
14
16
|
slugField: string;
|
|
17
|
+
/** Aligns with `defineResource` SEO block for hreflang, OG image, custom paths */
|
|
18
|
+
seo?: ResourceSeoConfig;
|
|
15
19
|
fields: Record<string, PublicFieldConfig>;
|
|
16
20
|
};
|
|
17
21
|
|
|
@@ -31,6 +35,13 @@ export const publicResources: PublicResourceConfig[] = [
|
|
|
31
35
|
titleField: "title",
|
|
32
36
|
descriptionField: "excerpt",
|
|
33
37
|
slugField: "slug",
|
|
38
|
+
seo: {
|
|
39
|
+
public: true,
|
|
40
|
+
slugField: "slug",
|
|
41
|
+
pathPattern: "/posts/:slug",
|
|
42
|
+
titleField: "title",
|
|
43
|
+
descriptionField: "excerpt"
|
|
44
|
+
},
|
|
34
45
|
fields: {
|
|
35
46
|
title: { type: "text", label: "Title", public: true, seo: "title" },
|
|
36
47
|
slug: { type: "slug", label: "Slug", public: false, seo: "slug" },
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
describe("seo helpers", () => {
|
|
4
|
+
afterEach(() => {
|
|
5
|
+
vi.unstubAllEnvs();
|
|
6
|
+
vi.resetModules();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("builds absolute URLs from NEXT_PUBLIC_SITE_URL", async () => {
|
|
10
|
+
vi.stubEnv("NEXT_PUBLIC_SITE_URL", "https://example.com");
|
|
11
|
+
const { absoluteUrl } = await import("./seo");
|
|
12
|
+
expect(absoluteUrl("/posts/hello")).toBe("https://example.com/posts/hello");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("metadataForRecord fills canonical and OpenGraph", async () => {
|
|
16
|
+
vi.stubEnv("NEXT_PUBLIC_SITE_URL", "https://example.com");
|
|
17
|
+
const { metadataForRecord } = await import("./seo");
|
|
18
|
+
const meta = metadataForRecord(
|
|
19
|
+
{
|
|
20
|
+
name: "posts",
|
|
21
|
+
label: "Posts",
|
|
22
|
+
basePath: "/posts",
|
|
23
|
+
titleField: "title",
|
|
24
|
+
slugField: "slug",
|
|
25
|
+
fields: {}
|
|
26
|
+
},
|
|
27
|
+
{ id: "1", title: "Hi", slug: "hi", description: "d" }
|
|
28
|
+
);
|
|
29
|
+
expect(meta.title).toBe("Hi");
|
|
30
|
+
expect(meta.alternates?.canonical).toBe("https://example.com/posts/hi");
|
|
31
|
+
expect(meta.openGraph?.url).toBe("https://example.com/posts/hi");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("sitemapXml renders url entries", async () => {
|
|
35
|
+
const { sitemapXml } = await import("./seo");
|
|
36
|
+
const xml = sitemapXml([{ loc: "https://example.com/a", lastmod: "2024-01-01" }]);
|
|
37
|
+
expect(xml).toContain("<loc>https://example.com/a</loc>");
|
|
38
|
+
expect(xml).toContain("<lastmod>2024-01-01</lastmod>");
|
|
39
|
+
expect(xml).toContain("<urlset");
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { buildSeoPagePayload } from "@openadminjs/core";
|
|
1
2
|
import type { Metadata } from "next";
|
|
2
3
|
import type { PublicRecord, PublicResourceConfig } from "./openadmin-client";
|
|
3
4
|
|
|
@@ -8,25 +9,35 @@ export function absoluteUrl(path: string): string {
|
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
export function metadataForRecord(resource: PublicResourceConfig, record: PublicRecord): Metadata {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
const payload = buildSeoPagePayload({
|
|
13
|
+
siteUrl,
|
|
14
|
+
basePath: resource.basePath,
|
|
15
|
+
seo: resource.seo,
|
|
16
|
+
record: record as Record<string, unknown>,
|
|
17
|
+
resourceLabel: resource.label
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const ogImages = payload.openGraph.images?.map((img) => img.url).filter(Boolean);
|
|
15
21
|
|
|
16
22
|
return {
|
|
17
|
-
title,
|
|
18
|
-
description,
|
|
19
|
-
alternates: {
|
|
23
|
+
title: payload.title,
|
|
24
|
+
description: payload.description,
|
|
25
|
+
alternates: {
|
|
26
|
+
canonical: payload.canonicalUrl,
|
|
27
|
+
...(payload.alternates?.languages ? { languages: payload.alternates.languages } : {})
|
|
28
|
+
},
|
|
20
29
|
openGraph: {
|
|
21
|
-
title,
|
|
22
|
-
description,
|
|
23
|
-
url:
|
|
24
|
-
type: "article"
|
|
30
|
+
title: payload.openGraph.title,
|
|
31
|
+
description: payload.openGraph.description,
|
|
32
|
+
url: payload.openGraph.url,
|
|
33
|
+
type: payload.openGraph.type as "article",
|
|
34
|
+
...(ogImages?.length ? { images: ogImages } : {})
|
|
25
35
|
},
|
|
26
36
|
twitter: {
|
|
27
37
|
card: "summary_large_image",
|
|
28
|
-
title,
|
|
29
|
-
description
|
|
38
|
+
title: payload.title,
|
|
39
|
+
description: payload.description,
|
|
40
|
+
...(ogImages?.[0] ? { images: [ogImages[0]] } : {})
|
|
30
41
|
}
|
|
31
42
|
};
|
|
32
43
|
}
|
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
/// <reference types="next" />
|
|
2
2
|
/// <reference types="next/image-types/global" />
|
|
3
|
+
/// <reference path="./.next/types/routes.d.ts" />
|
|
4
|
+
|
|
5
|
+
// NOTE: This file should not be edited
|
|
6
|
+
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|
|
@@ -9,9 +9,10 @@
|
|
|
9
9
|
"start": "next start --port 3001",
|
|
10
10
|
"lint": "tsc -p tsconfig.json --noEmit",
|
|
11
11
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
12
|
-
"test": "vitest run
|
|
12
|
+
"test": "vitest run"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
+
"@openadminjs/core": "workspace:*",
|
|
15
16
|
"next": "^15.1.0",
|
|
16
17
|
"react": "^19.0.0",
|
|
17
18
|
"react-dom": "^19.0.0"
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type Metadata = {
|
|
2
|
+
title?: string | string[];
|
|
3
|
+
description?: string;
|
|
4
|
+
alternates?: { canonical?: string };
|
|
5
|
+
openGraph?: { title?: string; description?: string; url?: string; type?: string };
|
|
6
|
+
twitter?: { card?: string; title?: string; description?: string };
|
|
7
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { dirname, resolve } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { defineConfig } from "vitest/config";
|
|
4
|
+
|
|
5
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
const repoRoot = resolve(__dirname, "../..");
|
|
7
|
+
|
|
8
|
+
export default defineConfig({
|
|
9
|
+
test: {
|
|
10
|
+
environment: "node"
|
|
11
|
+
},
|
|
12
|
+
resolve: {
|
|
13
|
+
alias: {
|
|
14
|
+
next: resolve(__dirname, "test/next-stub.ts"),
|
|
15
|
+
"@openadminjs/core": resolve(repoRoot, "packages/core/src/index.ts"),
|
|
16
|
+
"@openadminjs/resource": resolve(repoRoot, "packages/resource/src/index.ts")
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
});
|
package/template/package.json
CHANGED
|
@@ -2,18 +2,25 @@
|
|
|
2
2
|
"name": "__PACKAGE_NAME__",
|
|
3
3
|
"private": true,
|
|
4
4
|
"version": "0.1.0",
|
|
5
|
+
"packageManager": "pnpm@9.15.0",
|
|
5
6
|
"scripts": {
|
|
6
|
-
"dev": "openadminjs dev",
|
|
7
|
-
"build": "
|
|
8
|
-
"start": "openadminjs start",
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
7
|
+
"dev": "pnpm --parallel --filter @openadminjs/api --filter @openadminjs/admin dev",
|
|
8
|
+
"build": "pnpm -r build",
|
|
9
|
+
"start": "pnpm --parallel --filter @openadminjs/api --filter @openadminjs/admin start",
|
|
10
|
+
"lint": "pnpm -r lint",
|
|
11
|
+
"typecheck": "pnpm -r typecheck",
|
|
12
|
+
"test": "pnpm -r test",
|
|
13
|
+
"db:migrate": "pnpm --filter @openadminjs/api db:migrate",
|
|
14
|
+
"db:seed": "pnpm --filter @openadminjs/api db:seed",
|
|
15
|
+
"db:studio": "pnpm --filter @openadminjs/api db:studio",
|
|
16
|
+
"generate": "pnpm --filter @openadminjs/api generate"
|
|
15
17
|
},
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@prisma/client": "^6.1.0",
|
|
20
|
+
"@types/node": "^22.10.2",
|
|
21
|
+
"prisma": "^6.1.0",
|
|
22
|
+
"tsx": "^4.19.2",
|
|
23
|
+
"typescript": "^5.7.2",
|
|
24
|
+
"vitest": "^2.1.8"
|
|
18
25
|
}
|
|
19
26
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openadminjs/auth",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc -p tsconfig.json",
|
|
9
|
+
"lint": "tsc -p tsconfig.json --noEmit",
|
|
10
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
11
|
+
"test": "vitest run"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"typescript": "^5.7.2",
|
|
15
|
+
"vitest": "^2.1.8"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { publicAuthRoutes } from "./index";
|
|
3
|
+
|
|
4
|
+
describe("auth constants", () => {
|
|
5
|
+
it("lists public auth routes", () => {
|
|
6
|
+
expect(publicAuthRoutes).toContain("/auth/login");
|
|
7
|
+
expect(publicAuthRoutes).toContain("/auth/password/forgot");
|
|
8
|
+
expect(publicAuthRoutes).toContain("/auth/email/verify");
|
|
9
|
+
});
|
|
10
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type AuthSession = {
|
|
2
|
+
accessToken: string;
|
|
3
|
+
refreshToken: string;
|
|
4
|
+
expiresIn: number;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type LoginInput = {
|
|
8
|
+
email: string;
|
|
9
|
+
password: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const publicAuthRoutes = [
|
|
13
|
+
"/auth/login",
|
|
14
|
+
"/auth/password/forgot",
|
|
15
|
+
"/auth/password/reset",
|
|
16
|
+
"/auth/email/verify"
|
|
17
|
+
] as const;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 512 512" role="img" aria-label="OpenAdminJS"><image xlink:href="openadminjs-icon-new.png" href="openadminjs-icon-new.png" width="512" height="512" preserveAspectRatio="xMidYMid meet"/></svg>
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 512 512" role="img" aria-label="OpenAdminJS"><image xlink:href="openadminjs-icon-new.png" href="openadminjs-icon-new.png" width="512" height="512" preserveAspectRatio="xMidYMid meet"/></svg>
|
|
Binary file
|
|
Binary file
|