create-nextdevtpl 2.6.0
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 +29 -0
- package/catalog.json +385 -0
- package/dist/catalog.d.ts +3 -0
- package/dist/catalog.js +112 -0
- package/dist/catalog.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +115 -0
- package/dist/cli.js.map +1 -0
- package/dist/filesystem.d.ts +10 -0
- package/dist/filesystem.js +86 -0
- package/dist/filesystem.js.map +1 -0
- package/dist/generate.d.ts +5 -0
- package/dist/generate.js +758 -0
- package/dist/generate.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/selection.d.ts +2 -0
- package/dist/selection.js +111 -0
- package/dist/selection.js.map +1 -0
- package/dist/service-templates.d.ts +4 -0
- package/dist/service-templates.js +180 -0
- package/dist/service-templates.js.map +1 -0
- package/dist/types.d.ts +65 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +54 -0
- package/template/.dockerignore +14 -0
- package/template/.env.example +282 -0
- package/template/.gitattributes +17 -0
- package/template/.node-version +1 -0
- package/template/Dockerfile +45 -0
- package/template/LICENSE +21 -0
- package/template/README.md +267 -0
- package/template/biome.json +68 -0
- package/template/cloudflare/templates/logger.ts +162 -0
- package/template/cloudflare/templates/mail/server.ts +6 -0
- package/template/cloudflare/templates/mail/templates.ts +87 -0
- package/template/cloudflare/templates/mail/utils.ts +69 -0
- package/template/cloudflare/worker.mjs +25 -0
- package/template/components.json +22 -0
- package/template/compose.yaml +74 -0
- package/template/deploy/check-health.mjs +25 -0
- package/template/deploy/docker/README.md +14 -0
- package/template/deploy/server/README.md +24 -0
- package/template/deploy/server/build.sh +24 -0
- package/template/deploy/server/nextdevtpl.service +19 -0
- package/template/deploy/server/start.sh +19 -0
- package/template/deploy/vercel/README.md +16 -0
- package/template/deploy/verify-production-health.mjs +86 -0
- package/template/drizzle.config.ts +47 -0
- package/template/gitignore +62 -0
- package/template/manifest.json +35 -0
- package/template/messages/en.json +766 -0
- package/template/messages/zh.json +762 -0
- package/template/next.config.mjs +28 -0
- package/template/open-next.config.ts +3 -0
- package/template/package.json +125 -0
- package/template/pnpm-lock.yaml +15901 -0
- package/template/postcss.config.mjs +7 -0
- package/template/public/file.svg +1 -0
- package/template/public/globe.svg +1 -0
- package/template/public/logo.svg +6 -0
- package/template/public/next.svg +1 -0
- package/template/public/vercel.svg +1 -0
- package/template/public/window.svg +1 -0
- package/template/sentry.client.config.ts +10 -0
- package/template/sentry.edge.config.ts +10 -0
- package/template/sentry.server.config.ts +10 -0
- package/template/source.config.ts +59 -0
- package/template/src/adapters/ai/anthropic.ts +113 -0
- package/template/src/adapters/ai/index.ts +6 -0
- package/template/src/adapters/ai/openai-compatible.ts +88 -0
- package/template/src/adapters/ai/workers-ai.ts +63 -0
- package/template/src/adapters/index.ts +10 -0
- package/template/src/adapters/jobs/cloudflare-workflows.ts +46 -0
- package/template/src/adapters/jobs/index.ts +5 -0
- package/template/src/adapters/jobs/inngest/adapter.ts +29 -0
- package/template/src/adapters/jobs/inngest/client.ts +8 -0
- package/template/src/adapters/jobs/inngest/functions.ts +17 -0
- package/template/src/adapters/jobs/inngest/handler.ts +6 -0
- package/template/src/adapters/jobs/inngest/index.ts +4 -0
- package/template/src/adapters/mail/cloudflare-email.ts +50 -0
- package/template/src/adapters/mail/disabled.ts +17 -0
- package/template/src/adapters/mail/index.ts +7 -0
- package/template/src/adapters/mail/resend.ts +76 -0
- package/template/src/adapters/mail/smtp.ts +82 -0
- package/template/src/adapters/payment/creem.ts +269 -0
- package/template/src/adapters/payment/index.ts +2 -0
- package/template/src/adapters/payment/stripe.ts +302 -0
- package/template/src/adapters/rate-limit/cloudflare.ts +42 -0
- package/template/src/adapters/rate-limit/index.ts +6 -0
- package/template/src/adapters/rate-limit/noop.ts +27 -0
- package/template/src/adapters/rate-limit/upstash.ts +113 -0
- package/template/src/adapters/registry.ts +228 -0
- package/template/src/adapters/storage/index.ts +3 -0
- package/template/src/adapters/storage/r2-binding.ts +105 -0
- package/template/src/adapters/storage/s3-compatible.ts +149 -0
- package/template/src/app/[locale]/(admin)/admin/layout.tsx +36 -0
- package/template/src/app/[locale]/(admin)/admin/page.tsx +345 -0
- package/template/src/app/[locale]/(admin)/admin/tickets/[id]/page.tsx +261 -0
- package/template/src/app/[locale]/(admin)/admin/tickets/page.tsx +236 -0
- package/template/src/app/[locale]/(admin)/admin/users/page.tsx +557 -0
- package/template/src/app/[locale]/(auth)/forgot-password/page.tsx +9 -0
- package/template/src/app/[locale]/(auth)/layout.tsx +28 -0
- package/template/src/app/[locale]/(auth)/sign-in/page.tsx +9 -0
- package/template/src/app/[locale]/(auth)/sign-up/page.tsx +9 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/credits/buy/buy-credits-view.tsx +170 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/credits/buy/page.tsx +12 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/credits/page.tsx +5 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/loading.tsx +41 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/page.tsx +299 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/settings/page.tsx +38 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/support/[id]/page.tsx +220 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/support/new/page.tsx +183 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/support/page.tsx +145 -0
- package/template/src/app/[locale]/(dashboard)/layout.tsx +35 -0
- package/template/src/app/[locale]/(marketing)/blog/[slug]/page.tsx +178 -0
- package/template/src/app/[locale]/(marketing)/blog/blog-post-card.tsx +95 -0
- package/template/src/app/[locale]/(marketing)/blog/page.tsx +127 -0
- package/template/src/app/[locale]/(marketing)/demo/plan-badges/page.tsx +36 -0
- package/template/src/app/[locale]/(marketing)/layout.tsx +15 -0
- package/template/src/app/[locale]/(marketing)/legal/[slug]/page.tsx +130 -0
- package/template/src/app/[locale]/(marketing)/page.tsx +94 -0
- package/template/src/app/[locale]/(marketing)/pseo/[slug]/page.tsx +68 -0
- package/template/src/app/[locale]/(marketing)/pseo/page.tsx +137 -0
- package/template/src/app/[locale]/docs/[[...slug]]/page.tsx +63 -0
- package/template/src/app/[locale]/docs/layout.tsx +39 -0
- package/template/src/app/[locale]/layout.tsx +80 -0
- package/template/src/app/[locale]/opengraph-image.tsx +20 -0
- package/template/src/app/[locale]/twitter-image.tsx +20 -0
- package/template/src/app/api/auth/[...all]/route.ts +20 -0
- package/template/src/app/api/health/route.ts +45 -0
- package/template/src/app/api/inngest/route.ts +12 -0
- package/template/src/app/api/jobs/credits/expire/route.ts +102 -0
- package/template/src/app/api/search/route.ts +17 -0
- package/template/src/app/api/upload/presigned/route.ts +98 -0
- package/template/src/app/api/webhooks/payment/route.ts +363 -0
- package/template/src/app/globals.css +406 -0
- package/template/src/app/icon.svg +6 -0
- package/template/src/app/image-proxy/[...path]/route.ts +137 -0
- package/template/src/app/layout.tsx +73 -0
- package/template/src/app/robots.ts +29 -0
- package/template/src/app/sitemap.ts +68 -0
- package/template/src/components/motion/reveal.tsx +49 -0
- package/template/src/components/seo/json-ld.tsx +87 -0
- package/template/src/components/seo/og-image-template.tsx +118 -0
- package/template/src/components/ui/accordion.tsx +66 -0
- package/template/src/components/ui/alert-dialog.tsx +195 -0
- package/template/src/components/ui/avatar.tsx +53 -0
- package/template/src/components/ui/badge.tsx +46 -0
- package/template/src/components/ui/button.tsx +65 -0
- package/template/src/components/ui/card.tsx +92 -0
- package/template/src/components/ui/checkbox.tsx +32 -0
- package/template/src/components/ui/collapsible.tsx +33 -0
- package/template/src/components/ui/dialog.tsx +124 -0
- package/template/src/components/ui/dropdown-menu.tsx +257 -0
- package/template/src/components/ui/form.tsx +233 -0
- package/template/src/components/ui/index.ts +2 -0
- package/template/src/components/ui/input.tsx +21 -0
- package/template/src/components/ui/label.tsx +24 -0
- package/template/src/components/ui/navigation-menu.tsx +167 -0
- package/template/src/components/ui/pagination.tsx +125 -0
- package/template/src/components/ui/popover.tsx +48 -0
- package/template/src/components/ui/progress.tsx +27 -0
- package/template/src/components/ui/radio-group.tsx +45 -0
- package/template/src/components/ui/select.tsx +190 -0
- package/template/src/components/ui/separator.tsx +28 -0
- package/template/src/components/ui/sheet.tsx +140 -0
- package/template/src/components/ui/switch.tsx +31 -0
- package/template/src/components/ui/tabs.tsx +75 -0
- package/template/src/components/ui/textarea.tsx +18 -0
- package/template/src/config/index.ts +31 -0
- package/template/src/config/nav.ts +238 -0
- package/template/src/config/payment.ts +282 -0
- package/template/src/config/site.ts +53 -0
- package/template/src/config/subscription-plan.ts +164 -0
- package/template/src/content/blog/en/hello-world.mdx +48 -0
- package/template/src/content/blog/zh/hello-world.mdx +48 -0
- package/template/src/content/blog/zh/nextdevtpl-linuxdo-intro.mdx +98 -0
- package/template/src/content/docs/en/admin-and-support.mdx +65 -0
- package/template/src/content/docs/en/authentication.mdx +71 -0
- package/template/src/content/docs/en/background-jobs.mdx +53 -0
- package/template/src/content/docs/en/configuration.mdx +71 -0
- package/template/src/content/docs/en/credits.mdx +69 -0
- package/template/src/content/docs/en/customization.mdx +95 -0
- package/template/src/content/docs/en/database.mdx +66 -0
- package/template/src/content/docs/en/deployment.mdx +62 -0
- package/template/src/content/docs/en/email.mdx +48 -0
- package/template/src/content/docs/en/index.mdx +48 -0
- package/template/src/content/docs/en/internationalization.mdx +78 -0
- package/template/src/content/docs/en/meta.json +22 -0
- package/template/src/content/docs/en/observability.mdx +69 -0
- package/template/src/content/docs/en/payments.mdx +64 -0
- package/template/src/content/docs/en/project-structure.mdx +82 -0
- package/template/src/content/docs/en/quick-start.mdx +113 -0
- package/template/src/content/docs/en/storage.mdx +51 -0
- package/template/src/content/docs/zh/admin-and-support.mdx +65 -0
- package/template/src/content/docs/zh/authentication.mdx +71 -0
- package/template/src/content/docs/zh/background-jobs.mdx +53 -0
- package/template/src/content/docs/zh/configuration.mdx +70 -0
- package/template/src/content/docs/zh/credits.mdx +69 -0
- package/template/src/content/docs/zh/customization.mdx +95 -0
- package/template/src/content/docs/zh/database.mdx +66 -0
- package/template/src/content/docs/zh/deployment.mdx +61 -0
- package/template/src/content/docs/zh/email.mdx +47 -0
- package/template/src/content/docs/zh/index.mdx +48 -0
- package/template/src/content/docs/zh/internationalization.mdx +78 -0
- package/template/src/content/docs/zh/meta.json +22 -0
- package/template/src/content/docs/zh/observability.mdx +68 -0
- package/template/src/content/docs/zh/payments.mdx +64 -0
- package/template/src/content/docs/zh/project-structure.mdx +82 -0
- package/template/src/content/docs/zh/quick-start.mdx +110 -0
- package/template/src/content/docs/zh/storage.mdx +51 -0
- package/template/src/content/legal/en/cookie-policy.mdx +78 -0
- package/template/src/content/legal/en/privacy.mdx +131 -0
- package/template/src/content/legal/en/terms.mdx +93 -0
- package/template/src/content/legal/zh/cookie-policy.mdx +78 -0
- package/template/src/content/legal/zh/privacy.mdx +131 -0
- package/template/src/content/legal/zh/terms.mdx +92 -0
- package/template/src/core/modules/contract.ts +225 -0
- package/template/src/core/modules/index.ts +1 -0
- package/template/src/core/services/ai.ts +31 -0
- package/template/src/core/services/common.ts +112 -0
- package/template/src/core/services/index.ts +8 -0
- package/template/src/core/services/jobs.ts +24 -0
- package/template/src/core/services/mail.ts +35 -0
- package/template/src/core/services/manifest.ts +51 -0
- package/template/src/core/services/payment.ts +92 -0
- package/template/src/core/services/rate-limit.ts +47 -0
- package/template/src/core/services/storage.ts +42 -0
- package/template/src/db/index.ts +71 -0
- package/template/src/db/schema/auth.ts +67 -0
- package/template/src/db/schema/credits.ts +94 -0
- package/template/src/db/schema/index.ts +5 -0
- package/template/src/db/schema/mail.ts +14 -0
- package/template/src/db/schema/subscription.ts +21 -0
- package/template/src/db/schema/support.ts +56 -0
- package/template/src/db/schema.ts +2 -0
- package/template/src/features/admin/components/admin-sidebar.tsx +213 -0
- package/template/src/features/admin/components/index.ts +2 -0
- package/template/src/features/admin/index.ts +3 -0
- package/template/src/features/admin/manifest.ts +41 -0
- package/template/src/features/analytics/components/analytics.tsx +62 -0
- package/template/src/features/analytics/components/index.ts +2 -0
- package/template/src/features/analytics/index.ts +3 -0
- package/template/src/features/analytics/manifest.ts +13 -0
- package/template/src/features/auth/components/auth-brand-panel.tsx +82 -0
- package/template/src/features/auth/components/auth-error-alert.tsx +28 -0
- package/template/src/features/auth/components/auth-footer.tsx +52 -0
- package/template/src/features/auth/components/auth-logo.tsx +30 -0
- package/template/src/features/auth/components/forgot-password-form.tsx +146 -0
- package/template/src/features/auth/components/index.ts +9 -0
- package/template/src/features/auth/components/sign-in-form.tsx +241 -0
- package/template/src/features/auth/components/sign-up-form.tsx +307 -0
- package/template/src/features/auth/index.ts +3 -0
- package/template/src/features/auth/manifest.ts +30 -0
- package/template/src/features/blog/components/blog-post-item.tsx +72 -0
- package/template/src/features/blog/components/index.ts +1 -0
- package/template/src/features/blog/data/mock-posts.ts +62 -0
- package/template/src/features/blog/index.ts +3 -0
- package/template/src/features/blog/manifest.ts +22 -0
- package/template/src/features/credits/account.ts +76 -0
- package/template/src/features/credits/actions.ts +427 -0
- package/template/src/features/credits/components/credit-balance-badge.tsx +54 -0
- package/template/src/features/credits/components/credit-usage-section.tsx +160 -0
- package/template/src/features/credits/components/index.ts +7 -0
- package/template/src/features/credits/components/transaction-history.tsx +346 -0
- package/template/src/features/credits/config.ts +70 -0
- package/template/src/features/credits/consume.ts +140 -0
- package/template/src/features/credits/core.ts +23 -0
- package/template/src/features/credits/errors.ts +26 -0
- package/template/src/features/credits/expire.ts +112 -0
- package/template/src/features/credits/grant.ts +168 -0
- package/template/src/features/credits/index.ts +6 -0
- package/template/src/features/credits/manifest.ts +36 -0
- package/template/src/features/credits/query.ts +65 -0
- package/template/src/features/credits/types.ts +48 -0
- package/template/src/features/dashboard/components/index.ts +3 -0
- package/template/src/features/dashboard/components/main-wrapper.tsx +100 -0
- package/template/src/features/dashboard/components/sidebar.tsx +341 -0
- package/template/src/features/dashboard/context/index.ts +2 -0
- package/template/src/features/dashboard/context/sidebar-context.tsx +72 -0
- package/template/src/features/dashboard/index.ts +4 -0
- package/template/src/features/dashboard/manifest.ts +28 -0
- package/template/src/features/mail/actions.ts +175 -0
- package/template/src/features/mail/index.ts +5 -0
- package/template/src/features/mail/manifest.ts +13 -0
- package/template/src/features/mail/server.ts +8 -0
- package/template/src/features/mail/templates/index.ts +11 -0
- package/template/src/features/mail/templates/primary-action-email.tsx +224 -0
- package/template/src/features/mail/templates/welcome-email.tsx +121 -0
- package/template/src/features/mail/utils.ts +190 -0
- package/template/src/features/marketing/components/animated-price.tsx +51 -0
- package/template/src/features/marketing/components/cookie-consent.tsx +255 -0
- package/template/src/features/marketing/components/cta-section.tsx +50 -0
- package/template/src/features/marketing/components/faq-section.tsx +59 -0
- package/template/src/features/marketing/components/feature-grid.tsx +80 -0
- package/template/src/features/marketing/components/footer.tsx +128 -0
- package/template/src/features/marketing/components/header.tsx +255 -0
- package/template/src/features/marketing/components/hero-section.tsx +323 -0
- package/template/src/features/marketing/components/how-it-works.tsx +115 -0
- package/template/src/features/marketing/components/index.ts +14 -0
- package/template/src/features/marketing/components/nav-menu.tsx +236 -0
- package/template/src/features/marketing/components/pricing-section.tsx +446 -0
- package/template/src/features/marketing/components/testimonials.tsx +59 -0
- package/template/src/features/marketing/components/use-cases-section.tsx +78 -0
- package/template/src/features/marketing/index.ts +3 -0
- package/template/src/features/marketing/manifest.ts +35 -0
- package/template/src/features/payment/actions.ts +182 -0
- package/template/src/features/payment/index.ts +4 -0
- package/template/src/features/payment/manifest.ts +18 -0
- package/template/src/features/payment/server.ts +7 -0
- package/template/src/features/payment/types.ts +202 -0
- package/template/src/features/pseo/components/index.ts +6 -0
- package/template/src/features/pseo/components/pseo-cta.tsx +51 -0
- package/template/src/features/pseo/components/pseo-faq.tsx +40 -0
- package/template/src/features/pseo/components/pseo-feature-grid.tsx +74 -0
- package/template/src/features/pseo/components/pseo-hero.tsx +106 -0
- package/template/src/features/pseo/components/pseo-related.tsx +62 -0
- package/template/src/features/pseo/components/pseo-use-cases.tsx +50 -0
- package/template/src/features/pseo/data/pseo-pages.json +534 -0
- package/template/src/features/pseo/index.ts +4 -0
- package/template/src/features/pseo/lib/pseo-data.ts +140 -0
- package/template/src/features/pseo/manifest.ts +22 -0
- package/template/src/features/settings/actions/delete-account.ts +50 -0
- package/template/src/features/settings/actions/index.ts +3 -0
- package/template/src/features/settings/actions/update-profile.ts +49 -0
- package/template/src/features/settings/components/billing-section.tsx +283 -0
- package/template/src/features/settings/components/index.ts +3 -0
- package/template/src/features/settings/components/profile-form.tsx +154 -0
- package/template/src/features/settings/components/security-section.tsx +158 -0
- package/template/src/features/settings/components/settings-profile-view.tsx +506 -0
- package/template/src/features/settings/index.ts +5 -0
- package/template/src/features/settings/manifest.ts +28 -0
- package/template/src/features/settings/schemas/index.ts +2 -0
- package/template/src/features/settings/schemas/update-profile.ts +21 -0
- package/template/src/features/shared/components/cookie-settings-dialog.tsx +282 -0
- package/template/src/features/shared/components/index.ts +5 -0
- package/template/src/features/shared/components/language-switcher.tsx +82 -0
- package/template/src/features/shared/components/max-width-wrapper.tsx +49 -0
- package/template/src/features/shared/components/mode-toggle.tsx +119 -0
- package/template/src/features/shared/icons/google-icon.tsx +39 -0
- package/template/src/features/shared/icons/index.ts +7 -0
- package/template/src/features/shared/index.ts +5 -0
- package/template/src/features/shared/manifest.ts +13 -0
- package/template/src/features/shared/providers.tsx +59 -0
- package/template/src/features/storage/actions.ts +107 -0
- package/template/src/features/storage/index.ts +6 -0
- package/template/src/features/storage/manifest.ts +22 -0
- package/template/src/features/storage/types.ts +143 -0
- package/template/src/features/storage/utils.ts +85 -0
- package/template/src/features/storage/validation.ts +151 -0
- package/template/src/features/subscription/actions/get-user-plan.ts +26 -0
- package/template/src/features/subscription/actions/index.ts +5 -0
- package/template/src/features/subscription/components/current-plan-badge.tsx +18 -0
- package/template/src/features/subscription/components/index.ts +6 -0
- package/template/src/features/subscription/components/plan-badge.tsx +98 -0
- package/template/src/features/subscription/index.ts +25 -0
- package/template/src/features/subscription/manifest.ts +13 -0
- package/template/src/features/subscription/services/user-plan.ts +181 -0
- package/template/src/features/support/actions/admin-ticket.ts +172 -0
- package/template/src/features/support/actions/admin-users.ts +295 -0
- package/template/src/features/support/actions/index.ts +22 -0
- package/template/src/features/support/actions/ticket.ts +12 -0
- package/template/src/features/support/actions/user-ticket.ts +153 -0
- package/template/src/features/support/components/admin-ticket-reply-form.tsx +109 -0
- package/template/src/features/support/components/admin-ticket-status-select.tsx +119 -0
- package/template/src/features/support/components/index.ts +6 -0
- package/template/src/features/support/components/ticket-message-form.tsx +99 -0
- package/template/src/features/support/components/user-role-select.tsx +107 -0
- package/template/src/features/support/index.ts +5 -0
- package/template/src/features/support/manifest.ts +53 -0
- package/template/src/features/support/schemas/index.ts +15 -0
- package/template/src/features/support/schemas/ticket.ts +81 -0
- package/template/src/hooks/index.ts +2 -0
- package/template/src/i18n/index.ts +12 -0
- package/template/src/i18n/request.ts +24 -0
- package/template/src/i18n/routing.ts +23 -0
- package/template/src/instrumentation.ts +19 -0
- package/template/src/lib/ai/index.ts +16 -0
- package/template/src/lib/api-logger.ts +24 -0
- package/template/src/lib/auth/admin.ts +54 -0
- package/template/src/lib/auth/api.ts +17 -0
- package/template/src/lib/auth/client.ts +227 -0
- package/template/src/lib/auth/edge.ts +84 -0
- package/template/src/lib/auth/errors.ts +99 -0
- package/template/src/lib/auth/index.ts +159 -0
- package/template/src/lib/auth/server.ts +46 -0
- package/template/src/lib/auth/session-context.tsx +96 -0
- package/template/src/lib/cloudflare/bindings.ts +31 -0
- package/template/src/lib/cookie-consent.ts +33 -0
- package/template/src/lib/file-utils.ts +45 -0
- package/template/src/lib/fingerprint/index.ts +73 -0
- package/template/src/lib/health/checks.ts +6 -0
- package/template/src/lib/health/core.ts +86 -0
- package/template/src/lib/health/index.ts +11 -0
- package/template/src/lib/health/types.ts +13 -0
- package/template/src/lib/index.ts +2 -0
- package/template/src/lib/logger/index.ts +258 -0
- package/template/src/lib/monitoring/index.ts +384 -0
- package/template/src/lib/rate-limit/anonymous.ts +85 -0
- package/template/src/lib/rate-limit/index.ts +111 -0
- package/template/src/lib/safe-action.ts +127 -0
- package/template/src/lib/seo/json-ld.ts +188 -0
- package/template/src/lib/source.ts +122 -0
- package/template/src/lib/utils.ts +6 -0
- package/template/src/middleware.ts +162 -0
- package/template/src/modules/index.ts +10 -0
- package/template/src/modules/presets.ts +8 -0
- package/template/src/modules/registry.ts +34 -0
- package/template/src/modules/selection.ts +45 -0
- package/template/src/services/ai.ts +49 -0
- package/template/src/services/index.ts +6 -0
- package/template/src/services/jobs.ts +3 -0
- package/template/src/services/mail.ts +7 -0
- package/template/src/services/payment.ts +3 -0
- package/template/src/services/rate-limit.ts +12 -0
- package/template/src/services/storage.ts +3 -0
- package/template/src/types/fumadocs-source.d.ts +14 -0
- package/template/src/types/index.ts +2 -0
- package/template/tsconfig.json +53 -0
- package/template/vercel.json +11 -0
- package/template/vitest.config.ts +51 -0
- package/template/wrangler.jsonc +30 -0
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 积分交易历史组件
|
|
5
|
+
*
|
|
6
|
+
* 显示用户的积分交易记录表格
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
ChevronLeft,
|
|
11
|
+
ChevronRight,
|
|
12
|
+
ChevronsLeft,
|
|
13
|
+
ChevronsRight,
|
|
14
|
+
Inbox,
|
|
15
|
+
} from "lucide-react";
|
|
16
|
+
import { useLocale, useTranslations } from "next-intl";
|
|
17
|
+
import { useAction } from "next-safe-action/hooks";
|
|
18
|
+
import { useEffect, useState } from "react";
|
|
19
|
+
|
|
20
|
+
import { Badge } from "@/components/ui/badge";
|
|
21
|
+
import { Button } from "@/components/ui/button";
|
|
22
|
+
import {
|
|
23
|
+
Select,
|
|
24
|
+
SelectContent,
|
|
25
|
+
SelectItem,
|
|
26
|
+
SelectTrigger,
|
|
27
|
+
SelectValue,
|
|
28
|
+
} from "@/components/ui/select";
|
|
29
|
+
import { Separator } from "@/components/ui/separator";
|
|
30
|
+
import { getMyTransactions } from "@/features/credits/actions";
|
|
31
|
+
import { cn } from "@/lib/utils";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 交易类型键
|
|
35
|
+
*/
|
|
36
|
+
type TransactionTypeKey =
|
|
37
|
+
| "registration_bonus"
|
|
38
|
+
| "admin_grant"
|
|
39
|
+
| "monthly_grant"
|
|
40
|
+
| "purchase"
|
|
41
|
+
| "consumption"
|
|
42
|
+
| "expiration"
|
|
43
|
+
| "refund";
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 交易类型变体映射
|
|
47
|
+
*/
|
|
48
|
+
const TRANSACTION_TYPE_VARIANTS: Record<
|
|
49
|
+
TransactionTypeKey,
|
|
50
|
+
"default" | "secondary" | "destructive" | "outline"
|
|
51
|
+
> = {
|
|
52
|
+
registration_bonus: "default",
|
|
53
|
+
admin_grant: "default",
|
|
54
|
+
monthly_grant: "default",
|
|
55
|
+
purchase: "secondary",
|
|
56
|
+
consumption: "destructive",
|
|
57
|
+
expiration: "outline",
|
|
58
|
+
refund: "secondary",
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 格式化日期时间
|
|
63
|
+
*/
|
|
64
|
+
function formatDateTime(date: Date | string, locale: string): string {
|
|
65
|
+
const d = new Date(date);
|
|
66
|
+
return d.toLocaleString(locale === "zh" ? "zh-CN" : "en-US", {
|
|
67
|
+
year: "numeric",
|
|
68
|
+
month: "2-digit",
|
|
69
|
+
day: "2-digit",
|
|
70
|
+
hour: "2-digit",
|
|
71
|
+
minute: "2-digit",
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 判断是否为收入类型交易
|
|
77
|
+
*/
|
|
78
|
+
function isIncomeType(type: string): boolean {
|
|
79
|
+
return [
|
|
80
|
+
"registration_bonus",
|
|
81
|
+
"admin_grant",
|
|
82
|
+
"monthly_grant",
|
|
83
|
+
"purchase",
|
|
84
|
+
"refund",
|
|
85
|
+
].includes(type);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 交易历史组件
|
|
90
|
+
*/
|
|
91
|
+
export function TransactionHistory() {
|
|
92
|
+
const t = useTranslations("Settings.usage.transactions");
|
|
93
|
+
const locale = useLocale();
|
|
94
|
+
const [page, setPage] = useState(1);
|
|
95
|
+
const [pageSize, setPageSize] = useState(10);
|
|
96
|
+
const { execute, result, isPending } = useAction(getMyTransactions);
|
|
97
|
+
|
|
98
|
+
// 获取交易记录
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
execute({ limit: pageSize, offset: (page - 1) * pageSize });
|
|
101
|
+
}, [execute, page, pageSize]);
|
|
102
|
+
|
|
103
|
+
const transactions = result.data?.transactions ?? [];
|
|
104
|
+
const totalCount = result.data?.totalCount ?? 0;
|
|
105
|
+
const totalPages = Math.max(1, Math.ceil(totalCount / pageSize));
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 获取交易类型标签
|
|
109
|
+
*/
|
|
110
|
+
const getTypeLabel = (type: string): string => {
|
|
111
|
+
const typeKey = type as TransactionTypeKey;
|
|
112
|
+
if (typeKey in TRANSACTION_TYPE_VARIANTS) {
|
|
113
|
+
return t(`types.${typeKey}`);
|
|
114
|
+
}
|
|
115
|
+
return type;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 获取本地化的交易描述
|
|
120
|
+
*/
|
|
121
|
+
const getLocalizedDescription = (tx: {
|
|
122
|
+
type: string;
|
|
123
|
+
description: string | null;
|
|
124
|
+
metadata?: Record<string, unknown> | null;
|
|
125
|
+
}): string => {
|
|
126
|
+
const meta = tx.metadata;
|
|
127
|
+
switch (tx.type) {
|
|
128
|
+
case "registration_bonus":
|
|
129
|
+
return t("descriptions.registration_bonus");
|
|
130
|
+
case "monthly_grant": {
|
|
131
|
+
const planType = (meta?.planType as string) ?? "";
|
|
132
|
+
const plan = planType.charAt(0).toUpperCase() + planType.slice(1);
|
|
133
|
+
const interval = meta?.interval as string;
|
|
134
|
+
const monthlyCredits = meta?.planType
|
|
135
|
+
? ({ starter: "3000", pro: "8000", ultra: "16000" }[planType] ?? "")
|
|
136
|
+
: "";
|
|
137
|
+
if (interval === "year") {
|
|
138
|
+
return t("descriptions.yearly_grant", {
|
|
139
|
+
plan,
|
|
140
|
+
monthly: monthlyCredits,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return t("descriptions.monthly_grant", { plan });
|
|
144
|
+
}
|
|
145
|
+
case "consumption":
|
|
146
|
+
return tx.description ?? t("descriptions.consumption");
|
|
147
|
+
case "expiration":
|
|
148
|
+
return t("descriptions.expiration");
|
|
149
|
+
case "admin_grant":
|
|
150
|
+
return t("descriptions.admin_grant");
|
|
151
|
+
case "refund":
|
|
152
|
+
return t("descriptions.refund");
|
|
153
|
+
default:
|
|
154
|
+
return tx.description ?? "-";
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 获取交易类型变体
|
|
160
|
+
*/
|
|
161
|
+
const getTypeVariant = (
|
|
162
|
+
type: string
|
|
163
|
+
): "default" | "secondary" | "destructive" | "outline" => {
|
|
164
|
+
const typeKey = type as TransactionTypeKey;
|
|
165
|
+
return TRANSACTION_TYPE_VARIANTS[typeKey] ?? "outline";
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// 空状态
|
|
169
|
+
if (!isPending && transactions.length === 0) {
|
|
170
|
+
return (
|
|
171
|
+
<div className="space-y-4">
|
|
172
|
+
{/* 标题 */}
|
|
173
|
+
<div>
|
|
174
|
+
<h3 className="text-lg font-semibold">{t("title")}</h3>
|
|
175
|
+
<p className="text-sm text-muted-foreground">{t("description")}</p>
|
|
176
|
+
</div>
|
|
177
|
+
|
|
178
|
+
{/* 空状态 */}
|
|
179
|
+
<div className="flex flex-col items-center justify-center py-12 text-center border rounded-lg">
|
|
180
|
+
<Inbox className="h-12 w-12 text-muted-foreground/50 mb-4" />
|
|
181
|
+
<p className="text-muted-foreground">{t("noTransactions")}</p>
|
|
182
|
+
<p className="text-sm text-muted-foreground/70">
|
|
183
|
+
{t("noTransactionsHint")}
|
|
184
|
+
</p>
|
|
185
|
+
</div>
|
|
186
|
+
</div>
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return (
|
|
191
|
+
<div className="space-y-4">
|
|
192
|
+
{/* 标题 */}
|
|
193
|
+
<div>
|
|
194
|
+
<h3 className="text-lg font-semibold">{t("title")}</h3>
|
|
195
|
+
<p className="text-sm text-muted-foreground">{t("description")}</p>
|
|
196
|
+
</div>
|
|
197
|
+
|
|
198
|
+
{/* 表格 */}
|
|
199
|
+
<div className="rounded-lg border">
|
|
200
|
+
{/* 表头 */}
|
|
201
|
+
<div className="grid grid-cols-12 gap-4 px-4 py-3 bg-muted/50 text-sm font-medium text-muted-foreground">
|
|
202
|
+
<div className="col-span-3">{t("date")}</div>
|
|
203
|
+
<div className="col-span-2">{t("type")}</div>
|
|
204
|
+
<div className="col-span-5">{t("transactionDescription")}</div>
|
|
205
|
+
<div className="col-span-2 text-right">{t("transactionAmount")}</div>
|
|
206
|
+
</div>
|
|
207
|
+
|
|
208
|
+
<Separator />
|
|
209
|
+
|
|
210
|
+
{/* 加载状态 */}
|
|
211
|
+
{isPending ? (
|
|
212
|
+
<div className="flex items-center justify-center py-8">
|
|
213
|
+
<div className="animate-pulse text-muted-foreground">
|
|
214
|
+
{t("loading")}
|
|
215
|
+
</div>
|
|
216
|
+
</div>
|
|
217
|
+
) : (
|
|
218
|
+
/* 表格内容 */
|
|
219
|
+
<div className="divide-y">
|
|
220
|
+
{transactions.map((tx) => {
|
|
221
|
+
const isIncome = isIncomeType(tx.type);
|
|
222
|
+
|
|
223
|
+
return (
|
|
224
|
+
<div
|
|
225
|
+
key={tx.id}
|
|
226
|
+
className="grid grid-cols-12 gap-4 px-4 py-3 text-sm hover:bg-muted/30 transition-colors"
|
|
227
|
+
>
|
|
228
|
+
{/* 日期 */}
|
|
229
|
+
<div className="col-span-3 text-muted-foreground">
|
|
230
|
+
{formatDateTime(tx.createdAt, locale)}
|
|
231
|
+
</div>
|
|
232
|
+
|
|
233
|
+
{/* 类型 */}
|
|
234
|
+
<div className="col-span-2">
|
|
235
|
+
<Badge
|
|
236
|
+
variant={getTypeVariant(tx.type)}
|
|
237
|
+
className="text-xs"
|
|
238
|
+
>
|
|
239
|
+
{getTypeLabel(tx.type)}
|
|
240
|
+
</Badge>
|
|
241
|
+
</div>
|
|
242
|
+
|
|
243
|
+
{/* 描述 */}
|
|
244
|
+
<div
|
|
245
|
+
className="col-span-5 truncate"
|
|
246
|
+
title={getLocalizedDescription(tx)}
|
|
247
|
+
>
|
|
248
|
+
{getLocalizedDescription(tx)}
|
|
249
|
+
</div>
|
|
250
|
+
|
|
251
|
+
{/* 金额 */}
|
|
252
|
+
<div
|
|
253
|
+
className={cn(
|
|
254
|
+
"col-span-2 text-right font-medium",
|
|
255
|
+
isIncome ? "text-success" : "text-destructive"
|
|
256
|
+
)}
|
|
257
|
+
>
|
|
258
|
+
{isIncome ? "+" : "-"}
|
|
259
|
+
{tx.amount}
|
|
260
|
+
</div>
|
|
261
|
+
</div>
|
|
262
|
+
);
|
|
263
|
+
})}
|
|
264
|
+
</div>
|
|
265
|
+
)}
|
|
266
|
+
</div>
|
|
267
|
+
|
|
268
|
+
{/* 分页 */}
|
|
269
|
+
<div className="flex items-center justify-between text-sm text-muted-foreground">
|
|
270
|
+
<div>
|
|
271
|
+
{t("showing", {
|
|
272
|
+
from: Math.min((page - 1) * pageSize + 1, totalCount),
|
|
273
|
+
to: Math.min(page * pageSize, totalCount),
|
|
274
|
+
total: totalCount,
|
|
275
|
+
})}
|
|
276
|
+
</div>
|
|
277
|
+
|
|
278
|
+
<div className="flex items-center gap-4">
|
|
279
|
+
{/* 每页数量选择 */}
|
|
280
|
+
<div className="flex items-center gap-2">
|
|
281
|
+
<span>{t("rowsPerPage")}</span>
|
|
282
|
+
<Select
|
|
283
|
+
value={String(pageSize)}
|
|
284
|
+
onValueChange={(v) => {
|
|
285
|
+
setPageSize(Number(v));
|
|
286
|
+
setPage(1);
|
|
287
|
+
}}
|
|
288
|
+
>
|
|
289
|
+
<SelectTrigger className="w-16 h-8">
|
|
290
|
+
<SelectValue />
|
|
291
|
+
</SelectTrigger>
|
|
292
|
+
<SelectContent>
|
|
293
|
+
<SelectItem value="10">10</SelectItem>
|
|
294
|
+
<SelectItem value="20">20</SelectItem>
|
|
295
|
+
<SelectItem value="50">50</SelectItem>
|
|
296
|
+
</SelectContent>
|
|
297
|
+
</Select>
|
|
298
|
+
</div>
|
|
299
|
+
|
|
300
|
+
{/* 页码信息 */}
|
|
301
|
+
<span>{t("pageOf", { page, total: totalPages })}</span>
|
|
302
|
+
|
|
303
|
+
{/* 分页按钮 */}
|
|
304
|
+
<div className="flex items-center gap-1">
|
|
305
|
+
<Button
|
|
306
|
+
variant="outline"
|
|
307
|
+
size="icon"
|
|
308
|
+
className="h-8 w-8"
|
|
309
|
+
onClick={() => setPage(1)}
|
|
310
|
+
disabled={page === 1}
|
|
311
|
+
>
|
|
312
|
+
<ChevronsLeft className="h-4 w-4" />
|
|
313
|
+
</Button>
|
|
314
|
+
<Button
|
|
315
|
+
variant="outline"
|
|
316
|
+
size="icon"
|
|
317
|
+
className="h-8 w-8"
|
|
318
|
+
onClick={() => setPage((p) => Math.max(1, p - 1))}
|
|
319
|
+
disabled={page === 1}
|
|
320
|
+
>
|
|
321
|
+
<ChevronLeft className="h-4 w-4" />
|
|
322
|
+
</Button>
|
|
323
|
+
<Button
|
|
324
|
+
variant="outline"
|
|
325
|
+
size="icon"
|
|
326
|
+
className="h-8 w-8"
|
|
327
|
+
onClick={() => setPage((p) => Math.min(totalPages, p + 1))}
|
|
328
|
+
disabled={page === totalPages}
|
|
329
|
+
>
|
|
330
|
+
<ChevronRight className="h-4 w-4" />
|
|
331
|
+
</Button>
|
|
332
|
+
<Button
|
|
333
|
+
variant="outline"
|
|
334
|
+
size="icon"
|
|
335
|
+
className="h-8 w-8"
|
|
336
|
+
onClick={() => setPage(totalPages)}
|
|
337
|
+
disabled={page === totalPages}
|
|
338
|
+
>
|
|
339
|
+
<ChevronsRight className="h-4 w-4" />
|
|
340
|
+
</Button>
|
|
341
|
+
</div>
|
|
342
|
+
</div>
|
|
343
|
+
</div>
|
|
344
|
+
</div>
|
|
345
|
+
);
|
|
346
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 积分系统配置
|
|
3
|
+
*
|
|
4
|
+
* 定义积分系统的常量和套餐配置
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// ============================================
|
|
8
|
+
// 积分配置常量
|
|
9
|
+
// ============================================
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 注册奖励积分数量
|
|
13
|
+
*/
|
|
14
|
+
export const REGISTRATION_BONUS_CREDITS = 200;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 订阅用户每月赠送积分数量(默认值,实际按套餐档位)
|
|
18
|
+
* @deprecated 使用 SUBSCRIPTION_MONTHLY_CREDITS from payment.ts
|
|
19
|
+
*/
|
|
20
|
+
export const MONTHLY_SUBSCRIPTION_CREDITS = 7500;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 积分过期天数(从发放日起)
|
|
24
|
+
* null 表示永不过期
|
|
25
|
+
*
|
|
26
|
+
* 设计理念:Pay as you use - 积分永久有效,用户无需担心过期
|
|
27
|
+
*/
|
|
28
|
+
export const CREDITS_EXPIRY_DAYS = null;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 积分包配置(一次性购买)
|
|
32
|
+
*
|
|
33
|
+
* 定价策略:比订阅略贵,鼓励订阅
|
|
34
|
+
* - Starter 订阅 $2.99 = 4000 积分 (1338 积分/$)
|
|
35
|
+
* - 积分包约 1000 积分/$(比订阅贵 25%)
|
|
36
|
+
*/
|
|
37
|
+
export const CREDIT_PACKAGES = [
|
|
38
|
+
{
|
|
39
|
+
id: "lite",
|
|
40
|
+
name: "Lite",
|
|
41
|
+
credits: 3000,
|
|
42
|
+
price: 3,
|
|
43
|
+
description: "For a quick study session",
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: "standard",
|
|
47
|
+
name: "Standard",
|
|
48
|
+
credits: 8000,
|
|
49
|
+
price: 8,
|
|
50
|
+
popular: true,
|
|
51
|
+
description: "Most popular top-up",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
id: "pro",
|
|
55
|
+
name: "Pro",
|
|
56
|
+
credits: 20000,
|
|
57
|
+
price: 18,
|
|
58
|
+
description: "Best value credit pack",
|
|
59
|
+
},
|
|
60
|
+
] as const;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 积分套餐类型
|
|
64
|
+
*/
|
|
65
|
+
export type CreditPackage = (typeof CREDIT_PACKAGES)[number];
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 套餐 ID 类型
|
|
69
|
+
*/
|
|
70
|
+
export type CreditPackageId = CreditPackage["id"];
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 积分消费逻辑 (FIFO)
|
|
3
|
+
*
|
|
4
|
+
* 核心 FIFO 逻辑:
|
|
5
|
+
* 1. 获取所有活跃批次,按过期时间升序排列(最早过期的优先消费)
|
|
6
|
+
* 2. 循环扣除直到满足需求
|
|
7
|
+
* 3. 更新批次状态和余额
|
|
8
|
+
* 4. 记录交易
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { and, asc, eq, gt, isNull, or, sql } from "drizzle-orm";
|
|
12
|
+
|
|
13
|
+
import { db } from "@/db";
|
|
14
|
+
import {
|
|
15
|
+
creditsBalance,
|
|
16
|
+
creditsBatch,
|
|
17
|
+
creditsTransaction,
|
|
18
|
+
} from "@/db/schema/credits";
|
|
19
|
+
|
|
20
|
+
import { AccountFrozenError, InsufficientCreditsError } from "./errors";
|
|
21
|
+
import type { ConsumeCreditsParams, ConsumeCreditsResult } from "./types";
|
|
22
|
+
|
|
23
|
+
export async function consumeCredits(
|
|
24
|
+
params: ConsumeCreditsParams
|
|
25
|
+
): Promise<ConsumeCreditsResult> {
|
|
26
|
+
const { userId, amount, serviceName, description, metadata } = params;
|
|
27
|
+
|
|
28
|
+
if (amount <= 0) {
|
|
29
|
+
throw new Error("消费数量必须大于 0");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return await db.transaction(async (tx) => {
|
|
33
|
+
const [balanceRecord] = await tx
|
|
34
|
+
.select()
|
|
35
|
+
.from(creditsBalance)
|
|
36
|
+
.where(eq(creditsBalance.userId, userId))
|
|
37
|
+
.limit(1);
|
|
38
|
+
|
|
39
|
+
if (!balanceRecord) {
|
|
40
|
+
throw new InsufficientCreditsError(amount, 0);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (balanceRecord.status === "frozen") {
|
|
44
|
+
throw new AccountFrozenError(userId);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (balanceRecord.balance < amount) {
|
|
48
|
+
throw new InsufficientCreditsError(amount, balanceRecord.balance);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const now = new Date();
|
|
52
|
+
const activeBatches = await tx
|
|
53
|
+
.select()
|
|
54
|
+
.from(creditsBatch)
|
|
55
|
+
.where(
|
|
56
|
+
and(
|
|
57
|
+
eq(creditsBatch.userId, userId),
|
|
58
|
+
eq(creditsBatch.status, "active"),
|
|
59
|
+
gt(creditsBatch.remaining, 0),
|
|
60
|
+
or(isNull(creditsBatch.expiresAt), gt(creditsBatch.expiresAt, now))
|
|
61
|
+
)
|
|
62
|
+
)
|
|
63
|
+
.orderBy(asc(creditsBatch.expiresAt), asc(creditsBatch.issuedAt));
|
|
64
|
+
|
|
65
|
+
let remainingToConsume = amount;
|
|
66
|
+
const consumedBatches: Array<{
|
|
67
|
+
batchId: string;
|
|
68
|
+
consumedFromBatch: number;
|
|
69
|
+
}> = [];
|
|
70
|
+
|
|
71
|
+
for (const batch of activeBatches) {
|
|
72
|
+
if (remainingToConsume <= 0) {
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const consumeFromThisBatch = Math.min(
|
|
77
|
+
batch.remaining,
|
|
78
|
+
remainingToConsume
|
|
79
|
+
);
|
|
80
|
+
const newRemaining = batch.remaining - consumeFromThisBatch;
|
|
81
|
+
|
|
82
|
+
await tx
|
|
83
|
+
.update(creditsBatch)
|
|
84
|
+
.set({
|
|
85
|
+
remaining: newRemaining,
|
|
86
|
+
status: newRemaining === 0 ? "consumed" : "active",
|
|
87
|
+
updatedAt: new Date(),
|
|
88
|
+
})
|
|
89
|
+
.where(eq(creditsBatch.id, batch.id));
|
|
90
|
+
|
|
91
|
+
consumedBatches.push({
|
|
92
|
+
batchId: batch.id,
|
|
93
|
+
consumedFromBatch: consumeFromThisBatch,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
remainingToConsume -= consumeFromThisBatch;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (remainingToConsume > 0) {
|
|
100
|
+
throw new InsufficientCreditsError(amount, amount - remainingToConsume);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const transactionId = crypto.randomUUID();
|
|
104
|
+
const debitAccount = `WALLET:${userId}`;
|
|
105
|
+
const creditAccount = `SERVICE:${serviceName}`;
|
|
106
|
+
|
|
107
|
+
await tx.insert(creditsTransaction).values({
|
|
108
|
+
id: transactionId,
|
|
109
|
+
userId,
|
|
110
|
+
type: "consumption",
|
|
111
|
+
amount,
|
|
112
|
+
debitAccount,
|
|
113
|
+
creditAccount,
|
|
114
|
+
description: description ?? `消费于 ${serviceName}`,
|
|
115
|
+
metadata: {
|
|
116
|
+
...metadata,
|
|
117
|
+
serviceName,
|
|
118
|
+
consumedBatches,
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const newBalance = balanceRecord.balance - amount;
|
|
123
|
+
await tx
|
|
124
|
+
.update(creditsBalance)
|
|
125
|
+
.set({
|
|
126
|
+
balance: newBalance,
|
|
127
|
+
totalSpent: sql`${creditsBalance.totalSpent} + ${amount}`,
|
|
128
|
+
updatedAt: new Date(),
|
|
129
|
+
})
|
|
130
|
+
.where(eq(creditsBalance.userId, userId));
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
success: true,
|
|
134
|
+
consumedAmount: amount,
|
|
135
|
+
remainingBalance: newBalance,
|
|
136
|
+
transactionId,
|
|
137
|
+
consumedBatches,
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export {
|
|
2
|
+
ensureCreditsBalance,
|
|
3
|
+
freezeCreditsAccount,
|
|
4
|
+
getCreditsBalance,
|
|
5
|
+
unfreezeCreditsAccount,
|
|
6
|
+
} from "./account";
|
|
7
|
+
export { consumeCredits } from "./consume";
|
|
8
|
+
export {
|
|
9
|
+
AccountFrozenError,
|
|
10
|
+
InsufficientCreditsError,
|
|
11
|
+
} from "./errors";
|
|
12
|
+
export { processExpiredBatches } from "./expire";
|
|
13
|
+
export { ensureRegistrationBonus, grantCredits } from "./grant";
|
|
14
|
+
export {
|
|
15
|
+
getUserActiveBatches,
|
|
16
|
+
getUserTransactions,
|
|
17
|
+
getUserTransactionsCount,
|
|
18
|
+
} from "./query";
|
|
19
|
+
export type {
|
|
20
|
+
ConsumeCreditsParams,
|
|
21
|
+
ConsumeCreditsResult,
|
|
22
|
+
GrantCreditsParams,
|
|
23
|
+
} from "./types";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 积分系统错误类型
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 积分余额错误
|
|
7
|
+
*/
|
|
8
|
+
export class InsufficientCreditsError extends Error {
|
|
9
|
+
constructor(
|
|
10
|
+
public required: number,
|
|
11
|
+
public available: number
|
|
12
|
+
) {
|
|
13
|
+
super(`积分不足: 需要 ${required},可用 ${available}`);
|
|
14
|
+
this.name = "InsufficientCreditsError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 账户冻结错误
|
|
20
|
+
*/
|
|
21
|
+
export class AccountFrozenError extends Error {
|
|
22
|
+
constructor(userId: string) {
|
|
23
|
+
super(`用户 ${userId} 的积分账户已被冻结`);
|
|
24
|
+
this.name = "AccountFrozenError";
|
|
25
|
+
}
|
|
26
|
+
}
|