@nextsparkjs/core 0.1.0-beta.83 → 0.1.0-beta.85

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.
Files changed (365) hide show
  1. package/dist/styles/classes.json +1 -1
  2. package/dist/templates/app/(auth)/forgot-password/page.tsx +216 -0
  3. package/dist/templates/app/(auth)/layout.tsx +51 -0
  4. package/dist/templates/app/(auth)/login/page.tsx +21 -0
  5. package/dist/templates/app/(auth)/reset-password/page.tsx +212 -0
  6. package/dist/templates/app/(auth)/signup/page.tsx +21 -0
  7. package/dist/templates/app/(auth)/verify-email/page.tsx +190 -0
  8. package/dist/templates/app/(public)/[...slug]/page.tsx +378 -0
  9. package/dist/templates/app/(public)/docs/[section]/[page]/page.tsx +90 -0
  10. package/dist/templates/app/(public)/docs/layout.tsx +25 -0
  11. package/dist/templates/app/(public)/docs/page.tsx +81 -0
  12. package/dist/templates/app/(public)/layout.tsx +41 -0
  13. package/dist/templates/app/(public)/page.tsx +19 -0
  14. package/dist/templates/app/403/page.tsx +89 -0
  15. package/dist/templates/app/api/auth/[...all]/route.ts +78 -0
  16. package/dist/templates/app/api/cron/billing/lifecycle/route.ts +98 -0
  17. package/dist/templates/app/api/csp-report/route.ts +175 -0
  18. package/dist/templates/app/api/devtools/config/entities/route.ts +108 -0
  19. package/dist/templates/app/api/devtools/config/theme/route.ts +66 -0
  20. package/dist/templates/app/api/devtools/tests/[...path]/route.ts +130 -0
  21. package/dist/templates/app/api/devtools/tests/route.ts +134 -0
  22. package/dist/templates/app/api/health/route.ts +29 -0
  23. package/dist/templates/app/api/internal/user-metadata/route.ts +36 -0
  24. package/dist/templates/app/api/superadmin/subscriptions/route.ts +310 -0
  25. package/dist/templates/app/api/superadmin/teams/[teamId]/route.ts +286 -0
  26. package/dist/templates/app/api/superadmin/teams/route.ts +188 -0
  27. package/dist/templates/app/api/superadmin/users/[userId]/route.ts +540 -0
  28. package/dist/templates/app/api/superadmin/users/route.ts +323 -0
  29. package/dist/templates/app/api/user/delete-account/route.ts +55 -0
  30. package/dist/templates/app/api/user/plan-flags/route.ts +283 -0
  31. package/dist/templates/app/api/user/profile/route.ts +133 -0
  32. package/dist/templates/app/api/v1/[entity]/[id]/child/[childType]/[childId]/route.ts +210 -0
  33. package/dist/templates/app/api/v1/[entity]/[id]/child/[childType]/route.ts +331 -0
  34. package/dist/templates/app/api/v1/[entity]/[id]/route.ts +35 -0
  35. package/dist/templates/app/api/v1/[entity]/docs.md +369 -0
  36. package/dist/templates/app/api/v1/[entity]/presets.ts +194 -0
  37. package/dist/templates/app/api/v1/[entity]/route.ts +31 -0
  38. package/dist/templates/app/api/v1/api-keys/[id]/route.ts +303 -0
  39. package/dist/templates/app/api/v1/api-keys/docs.md +101 -0
  40. package/dist/templates/app/api/v1/api-keys/presets.ts +31 -0
  41. package/dist/templates/app/api/v1/api-keys/route.ts +250 -0
  42. package/dist/templates/app/api/v1/auth/docs.md +184 -0
  43. package/dist/templates/app/api/v1/auth/presets.ts +44 -0
  44. package/dist/templates/app/api/v1/auth/signup-with-invite/route.ts +227 -0
  45. package/dist/templates/app/api/v1/billing/cancel/route.ts +206 -0
  46. package/dist/templates/app/api/v1/billing/change-plan/route.ts +97 -0
  47. package/dist/templates/app/api/v1/billing/check-action/route.ts +81 -0
  48. package/dist/templates/app/api/v1/billing/checkout/route.ts +124 -0
  49. package/dist/templates/app/api/v1/billing/docs.md +209 -0
  50. package/dist/templates/app/api/v1/billing/plans/route.ts +85 -0
  51. package/dist/templates/app/api/v1/billing/portal/route.ts +90 -0
  52. package/dist/templates/app/api/v1/billing/presets.ts +121 -0
  53. package/dist/templates/app/api/v1/billing/webhooks/stripe/route.ts +428 -0
  54. package/dist/templates/app/api/v1/blocks/[slug]/route.ts +29 -0
  55. package/dist/templates/app/api/v1/blocks/docs.md +173 -0
  56. package/dist/templates/app/api/v1/blocks/presets.ts +121 -0
  57. package/dist/templates/app/api/v1/blocks/route.ts +45 -0
  58. package/dist/templates/app/api/v1/blocks/validate/route.ts +45 -0
  59. package/dist/templates/app/api/v1/cron/docs.md +116 -0
  60. package/dist/templates/app/api/v1/cron/presets.ts +26 -0
  61. package/dist/templates/app/api/v1/cron/process/route.ts +108 -0
  62. package/dist/templates/app/api/v1/devtools/blocks/route.ts +82 -0
  63. package/dist/templates/app/api/v1/devtools/docs/route.ts +150 -0
  64. package/dist/templates/app/api/v1/devtools/docs.md +204 -0
  65. package/dist/templates/app/api/v1/devtools/features/route.ts +61 -0
  66. package/dist/templates/app/api/v1/devtools/flows/route.ts +61 -0
  67. package/dist/templates/app/api/v1/devtools/presets.ts +113 -0
  68. package/dist/templates/app/api/v1/devtools/scheduled-actions/route.ts +120 -0
  69. package/dist/templates/app/api/v1/devtools/testing/route.ts +82 -0
  70. package/dist/templates/app/api/v1/media/docs.md +117 -0
  71. package/dist/templates/app/api/v1/media/presets.ts +24 -0
  72. package/dist/templates/app/api/v1/media/upload/route.ts +150 -0
  73. package/dist/templates/app/api/v1/patterns/[id]/usages/route.ts +116 -0
  74. package/dist/templates/app/api/v1/plugin/[...path]/route.ts +373 -0
  75. package/dist/templates/app/api/v1/plugin/docs.md +79 -0
  76. package/dist/templates/app/api/v1/plugin/presets.ts +21 -0
  77. package/dist/templates/app/api/v1/plugin/route.ts +96 -0
  78. package/dist/templates/app/api/v1/post-categories/[id]/route.ts +255 -0
  79. package/dist/templates/app/api/v1/post-categories/docs.md +134 -0
  80. package/dist/templates/app/api/v1/post-categories/presets.ts +78 -0
  81. package/dist/templates/app/api/v1/post-categories/route.ts +119 -0
  82. package/dist/templates/app/api/v1/team-invitations/[token]/accept/route.ts +179 -0
  83. package/dist/templates/app/api/v1/team-invitations/[token]/decline/route.ts +120 -0
  84. package/dist/templates/app/api/v1/team-invitations/[token]/route.ts +89 -0
  85. package/dist/templates/app/api/v1/team-invitations/docs.md +88 -0
  86. package/dist/templates/app/api/v1/team-invitations/presets.ts +43 -0
  87. package/dist/templates/app/api/v1/team-invitations/route.ts +114 -0
  88. package/dist/templates/app/api/v1/teams/[teamId]/invitations/route.ts +171 -0
  89. package/dist/templates/app/api/v1/teams/[teamId]/invoices/[invoiceNumber]/route.ts +105 -0
  90. package/dist/templates/app/api/v1/teams/[teamId]/invoices/route.ts +125 -0
  91. package/dist/templates/app/api/v1/teams/[teamId]/members/[memberId]/route.ts +263 -0
  92. package/dist/templates/app/api/v1/teams/[teamId]/members/route.ts +358 -0
  93. package/dist/templates/app/api/v1/teams/[teamId]/route.ts +322 -0
  94. package/dist/templates/app/api/v1/teams/[teamId]/subscription/route.ts +50 -0
  95. package/dist/templates/app/api/v1/teams/[teamId]/usage/[limitSlug]/route.ts +91 -0
  96. package/dist/templates/app/api/v1/teams/docs.md +320 -0
  97. package/dist/templates/app/api/v1/teams/presets.ts +178 -0
  98. package/dist/templates/app/api/v1/teams/route.ts +293 -0
  99. package/dist/templates/app/api/v1/teams/switch/route.ts +88 -0
  100. package/dist/templates/app/api/v1/theme/[...path]/route.ts +361 -0
  101. package/dist/templates/app/api/v1/theme/docs.md +74 -0
  102. package/dist/templates/app/api/v1/theme/presets.ts +21 -0
  103. package/dist/templates/app/api/v1/theme/route.ts +96 -0
  104. package/dist/templates/app/api/v1/users/[id]/meta/[key]/route.ts +363 -0
  105. package/dist/templates/app/api/v1/users/[id]/route.ts +302 -0
  106. package/dist/templates/app/api/v1/users/docs.md +93 -0
  107. package/dist/templates/app/api/v1/users/presets.ts +59 -0
  108. package/dist/templates/app/api/v1/users/route.ts +197 -0
  109. package/dist/templates/app/dashboard/(main)/[entity]/[id]/edit/page.tsx +117 -0
  110. package/dist/templates/app/dashboard/(main)/[entity]/[id]/page.tsx +103 -0
  111. package/dist/templates/app/dashboard/(main)/[entity]/create/page.tsx +95 -0
  112. package/dist/templates/app/dashboard/(main)/[entity]/error.tsx +51 -0
  113. package/dist/templates/app/dashboard/(main)/[entity]/layout.tsx +113 -0
  114. package/dist/templates/app/dashboard/(main)/[entity]/loading.tsx +61 -0
  115. package/dist/templates/app/dashboard/(main)/[entity]/page.tsx +90 -0
  116. package/dist/templates/app/dashboard/(main)/layout.tsx +98 -0
  117. package/dist/templates/app/dashboard/(main)/loading.tsx +5 -0
  118. package/dist/templates/app/dashboard/(main)/page.tsx +201 -0
  119. package/dist/templates/app/dashboard/(main)/patterns/[id]/edit/page.tsx +114 -0
  120. package/dist/templates/app/dashboard/(main)/patterns/[id]/page.tsx +20 -0
  121. package/dist/templates/app/dashboard/(main)/patterns/[id]/reports/page.tsx +171 -0
  122. package/dist/templates/app/dashboard/(main)/patterns/create/page.tsx +86 -0
  123. package/dist/templates/app/dashboard/(main)/patterns/page.tsx +444 -0
  124. package/dist/templates/app/dashboard/features/analytics/page.tsx +35 -0
  125. package/dist/templates/app/dashboard/features/automation/page.tsx +35 -0
  126. package/dist/templates/app/dashboard/features/layout.tsx +13 -0
  127. package/dist/templates/app/dashboard/features/loading.tsx +5 -0
  128. package/dist/templates/app/dashboard/features/webhooks/page.tsx +35 -0
  129. package/dist/templates/app/dashboard/layout.tsx +86 -0
  130. package/dist/templates/app/dashboard/permission-denied/page.tsx +29 -0
  131. package/dist/templates/app/dashboard/settings/api-keys/loading.tsx +5 -0
  132. package/dist/templates/app/dashboard/settings/api-keys/page.tsx +513 -0
  133. package/dist/templates/app/dashboard/settings/billing/loading.tsx +5 -0
  134. package/dist/templates/app/dashboard/settings/billing/page.tsx +284 -0
  135. package/dist/templates/app/dashboard/settings/invoices/[invoiceNumber]/page.tsx +222 -0
  136. package/dist/templates/app/dashboard/settings/invoices/loading.tsx +5 -0
  137. package/dist/templates/app/dashboard/settings/invoices/page.tsx +82 -0
  138. package/dist/templates/app/dashboard/settings/layout.tsx +151 -0
  139. package/dist/templates/app/dashboard/settings/loading.tsx +5 -0
  140. package/dist/templates/app/dashboard/settings/notifications/loading.tsx +5 -0
  141. package/dist/templates/app/dashboard/settings/notifications/page.tsx +462 -0
  142. package/dist/templates/app/dashboard/settings/page.tsx +92 -0
  143. package/dist/templates/app/dashboard/settings/password/loading.tsx +5 -0
  144. package/dist/templates/app/dashboard/settings/password/page.tsx +306 -0
  145. package/dist/templates/app/dashboard/settings/plans/loading.tsx +5 -0
  146. package/dist/templates/app/dashboard/settings/plans/page.tsx +40 -0
  147. package/dist/templates/app/dashboard/settings/profile/loading.tsx +5 -0
  148. package/dist/templates/app/dashboard/settings/profile/page.tsx +686 -0
  149. package/dist/templates/app/dashboard/settings/security/loading.tsx +5 -0
  150. package/dist/templates/app/dashboard/settings/security/page.tsx +505 -0
  151. package/dist/templates/app/dashboard/settings/teams/loading.tsx +5 -0
  152. package/dist/templates/app/dashboard/settings/teams/page.tsx +272 -0
  153. package/dist/templates/app/dashboard/settings/teams/permissions/page.tsx +92 -0
  154. package/dist/templates/app/devtools/blocks/[slug]/page.tsx +39 -0
  155. package/dist/templates/app/devtools/blocks/page.tsx +31 -0
  156. package/dist/templates/app/devtools/config/page.tsx +31 -0
  157. package/dist/templates/app/devtools/features/page.tsx +31 -0
  158. package/dist/templates/app/devtools/flows/page.tsx +31 -0
  159. package/dist/templates/app/devtools/layout.tsx +58 -0
  160. package/dist/templates/app/devtools/page.tsx +121 -0
  161. package/dist/templates/app/devtools/scheduled-actions/page.tsx +157 -0
  162. package/dist/templates/app/devtools/style/page.tsx +330 -0
  163. package/dist/templates/app/devtools/tags/page.tsx +31 -0
  164. package/dist/templates/app/devtools/tests/[[...path]]/page.tsx +47 -0
  165. package/dist/templates/app/favicon.ico +0 -0
  166. package/dist/templates/app/globals.css +12 -0
  167. package/dist/templates/app/layout.tsx +96 -0
  168. package/dist/templates/app/public/page.tsx +30 -0
  169. package/dist/templates/app/superadmin/docs/[section]/[page]/page.tsx +92 -0
  170. package/dist/templates/app/superadmin/docs/page.tsx +75 -0
  171. package/dist/templates/app/superadmin/layout.tsx +67 -0
  172. package/dist/templates/app/superadmin/page.tsx +149 -0
  173. package/dist/templates/app/superadmin/subscriptions/page.tsx +655 -0
  174. package/dist/templates/app/superadmin/team-roles/page.tsx +493 -0
  175. package/dist/templates/app/superadmin/teams/[teamId]/page.tsx +687 -0
  176. package/dist/templates/app/superadmin/teams/page.tsx +302 -0
  177. package/dist/templates/app/superadmin/users/[userId]/page.tsx +548 -0
  178. package/dist/templates/app/superadmin/users/page.tsx +528 -0
  179. package/package.json +15 -15
  180. package/scripts/build/docs-registry.mjs +0 -0
  181. package/scripts/create-theme.mjs +0 -0
  182. package/scripts/deploy/release-version.mjs +0 -0
  183. package/scripts/deploy/vercel-deploy.mjs +0 -0
  184. package/scripts/dev/watch-plugins.mjs +0 -0
  185. package/scripts/maintenance/update-core.mjs +0 -0
  186. package/scripts/setup/npm-postinstall.mjs +0 -0
  187. package/scripts/setup/setup-claude.mjs +0 -0
  188. package/scripts/validation/check-imports.sh +0 -0
  189. package/templates/app/(auth)/forgot-password/page.tsx +216 -0
  190. package/templates/app/(auth)/layout.tsx +51 -0
  191. package/templates/app/(auth)/login/page.tsx +21 -0
  192. package/templates/app/(auth)/reset-password/page.tsx +212 -0
  193. package/templates/app/(auth)/signup/page.tsx +21 -0
  194. package/templates/app/(auth)/verify-email/page.tsx +190 -0
  195. package/templates/app/(public)/[...slug]/page.tsx +378 -0
  196. package/templates/app/(public)/docs/[section]/[page]/page.tsx +90 -0
  197. package/templates/app/(public)/docs/layout.tsx +25 -0
  198. package/templates/app/(public)/docs/page.tsx +81 -0
  199. package/templates/app/(public)/layout.tsx +41 -0
  200. package/templates/app/(public)/page.tsx +19 -0
  201. package/templates/app/403/page.tsx +89 -0
  202. package/templates/app/api/auth/[...all]/route.ts +78 -0
  203. package/templates/app/api/cron/billing/lifecycle/route.ts +98 -0
  204. package/templates/app/api/csp-report/route.ts +175 -0
  205. package/templates/app/api/devtools/config/entities/route.ts +108 -0
  206. package/templates/app/api/devtools/config/theme/route.ts +66 -0
  207. package/templates/app/api/devtools/tests/[...path]/route.ts +130 -0
  208. package/templates/app/api/devtools/tests/route.ts +134 -0
  209. package/templates/app/api/health/route.ts +29 -0
  210. package/templates/app/api/internal/user-metadata/route.ts +36 -0
  211. package/templates/app/api/superadmin/subscriptions/route.ts +310 -0
  212. package/templates/app/api/superadmin/teams/[teamId]/route.ts +286 -0
  213. package/templates/app/api/superadmin/teams/route.ts +188 -0
  214. package/templates/app/api/superadmin/users/[userId]/route.ts +540 -0
  215. package/templates/app/api/superadmin/users/route.ts +323 -0
  216. package/templates/app/api/user/delete-account/route.ts +55 -0
  217. package/templates/app/api/user/plan-flags/route.ts +283 -0
  218. package/templates/app/api/user/profile/route.ts +133 -0
  219. package/templates/app/api/v1/[entity]/[id]/child/[childType]/[childId]/route.ts +210 -0
  220. package/templates/app/api/v1/[entity]/[id]/child/[childType]/route.ts +331 -0
  221. package/templates/app/api/v1/[entity]/[id]/route.ts +35 -0
  222. package/templates/app/api/v1/[entity]/docs.md +369 -0
  223. package/templates/app/api/v1/[entity]/presets.ts +194 -0
  224. package/templates/app/api/v1/[entity]/route.ts +31 -0
  225. package/templates/app/api/v1/api-keys/[id]/route.ts +303 -0
  226. package/templates/app/api/v1/api-keys/docs.md +101 -0
  227. package/templates/app/api/v1/api-keys/presets.ts +31 -0
  228. package/templates/app/api/v1/api-keys/route.ts +250 -0
  229. package/templates/app/api/v1/auth/docs.md +184 -0
  230. package/templates/app/api/v1/auth/presets.ts +44 -0
  231. package/templates/app/api/v1/auth/signup-with-invite/route.ts +227 -0
  232. package/templates/app/api/v1/billing/cancel/route.ts +206 -0
  233. package/templates/app/api/v1/billing/change-plan/route.ts +97 -0
  234. package/templates/app/api/v1/billing/check-action/route.ts +81 -0
  235. package/templates/app/api/v1/billing/checkout/route.ts +124 -0
  236. package/templates/app/api/v1/billing/docs.md +209 -0
  237. package/templates/app/api/v1/billing/plans/route.ts +85 -0
  238. package/templates/app/api/v1/billing/portal/route.ts +90 -0
  239. package/templates/app/api/v1/billing/presets.ts +121 -0
  240. package/templates/app/api/v1/billing/webhooks/stripe/route.ts +428 -0
  241. package/templates/app/api/v1/blocks/[slug]/route.ts +29 -0
  242. package/templates/app/api/v1/blocks/docs.md +173 -0
  243. package/templates/app/api/v1/blocks/presets.ts +121 -0
  244. package/templates/app/api/v1/blocks/route.ts +45 -0
  245. package/templates/app/api/v1/blocks/validate/route.ts +45 -0
  246. package/templates/app/api/v1/cron/docs.md +116 -0
  247. package/templates/app/api/v1/cron/presets.ts +26 -0
  248. package/templates/app/api/v1/cron/process/route.ts +108 -0
  249. package/templates/app/api/v1/devtools/blocks/route.ts +82 -0
  250. package/templates/app/api/v1/devtools/docs/route.ts +150 -0
  251. package/templates/app/api/v1/devtools/docs.md +204 -0
  252. package/templates/app/api/v1/devtools/features/route.ts +61 -0
  253. package/templates/app/api/v1/devtools/flows/route.ts +61 -0
  254. package/templates/app/api/v1/devtools/presets.ts +113 -0
  255. package/templates/app/api/v1/devtools/scheduled-actions/route.ts +120 -0
  256. package/templates/app/api/v1/devtools/testing/route.ts +82 -0
  257. package/templates/app/api/v1/media/docs.md +117 -0
  258. package/templates/app/api/v1/media/presets.ts +24 -0
  259. package/templates/app/api/v1/media/upload/route.ts +150 -0
  260. package/templates/app/api/v1/patterns/[id]/usages/route.ts +116 -0
  261. package/templates/app/api/v1/plugin/[...path]/route.ts +373 -0
  262. package/templates/app/api/v1/plugin/docs.md +79 -0
  263. package/templates/app/api/v1/plugin/presets.ts +21 -0
  264. package/templates/app/api/v1/plugin/route.ts +96 -0
  265. package/templates/app/api/v1/post-categories/[id]/route.ts +255 -0
  266. package/templates/app/api/v1/post-categories/docs.md +134 -0
  267. package/templates/app/api/v1/post-categories/presets.ts +78 -0
  268. package/templates/app/api/v1/post-categories/route.ts +119 -0
  269. package/templates/app/api/v1/team-invitations/[token]/accept/route.ts +179 -0
  270. package/templates/app/api/v1/team-invitations/[token]/decline/route.ts +120 -0
  271. package/templates/app/api/v1/team-invitations/[token]/route.ts +89 -0
  272. package/templates/app/api/v1/team-invitations/docs.md +88 -0
  273. package/templates/app/api/v1/team-invitations/presets.ts +43 -0
  274. package/templates/app/api/v1/team-invitations/route.ts +114 -0
  275. package/templates/app/api/v1/teams/[teamId]/invitations/route.ts +171 -0
  276. package/templates/app/api/v1/teams/[teamId]/invoices/[invoiceNumber]/route.ts +105 -0
  277. package/templates/app/api/v1/teams/[teamId]/invoices/route.ts +125 -0
  278. package/templates/app/api/v1/teams/[teamId]/members/[memberId]/route.ts +263 -0
  279. package/templates/app/api/v1/teams/[teamId]/members/route.ts +358 -0
  280. package/templates/app/api/v1/teams/[teamId]/route.ts +322 -0
  281. package/templates/app/api/v1/teams/[teamId]/subscription/route.ts +50 -0
  282. package/templates/app/api/v1/teams/[teamId]/usage/[limitSlug]/route.ts +91 -0
  283. package/templates/app/api/v1/teams/docs.md +320 -0
  284. package/templates/app/api/v1/teams/presets.ts +178 -0
  285. package/templates/app/api/v1/teams/route.ts +293 -0
  286. package/templates/app/api/v1/teams/switch/route.ts +88 -0
  287. package/templates/app/api/v1/theme/[...path]/route.ts +361 -0
  288. package/templates/app/api/v1/theme/docs.md +74 -0
  289. package/templates/app/api/v1/theme/presets.ts +21 -0
  290. package/templates/app/api/v1/theme/route.ts +96 -0
  291. package/templates/app/api/v1/users/[id]/meta/[key]/route.ts +363 -0
  292. package/templates/app/api/v1/users/[id]/route.ts +302 -0
  293. package/templates/app/api/v1/users/docs.md +93 -0
  294. package/templates/app/api/v1/users/presets.ts +59 -0
  295. package/templates/app/api/v1/users/route.ts +197 -0
  296. package/templates/app/dashboard/(main)/[entity]/[id]/edit/page.tsx +117 -0
  297. package/templates/app/dashboard/(main)/[entity]/[id]/page.tsx +103 -0
  298. package/templates/app/dashboard/(main)/[entity]/create/page.tsx +95 -0
  299. package/templates/app/dashboard/(main)/[entity]/error.tsx +51 -0
  300. package/templates/app/dashboard/(main)/[entity]/layout.tsx +113 -0
  301. package/templates/app/dashboard/(main)/[entity]/loading.tsx +61 -0
  302. package/templates/app/dashboard/(main)/[entity]/page.tsx +90 -0
  303. package/templates/app/dashboard/(main)/layout.tsx +98 -0
  304. package/templates/app/dashboard/(main)/loading.tsx +5 -0
  305. package/templates/app/dashboard/(main)/page.tsx +201 -0
  306. package/templates/app/dashboard/(main)/patterns/[id]/edit/page.tsx +114 -0
  307. package/templates/app/dashboard/(main)/patterns/[id]/page.tsx +20 -0
  308. package/templates/app/dashboard/(main)/patterns/[id]/reports/page.tsx +171 -0
  309. package/templates/app/dashboard/(main)/patterns/create/page.tsx +86 -0
  310. package/templates/app/dashboard/(main)/patterns/page.tsx +444 -0
  311. package/templates/app/dashboard/features/analytics/page.tsx +35 -0
  312. package/templates/app/dashboard/features/automation/page.tsx +35 -0
  313. package/templates/app/dashboard/features/layout.tsx +13 -0
  314. package/templates/app/dashboard/features/loading.tsx +5 -0
  315. package/templates/app/dashboard/features/webhooks/page.tsx +35 -0
  316. package/templates/app/dashboard/layout.tsx +86 -0
  317. package/templates/app/dashboard/permission-denied/page.tsx +29 -0
  318. package/templates/app/dashboard/settings/api-keys/loading.tsx +5 -0
  319. package/templates/app/dashboard/settings/api-keys/page.tsx +513 -0
  320. package/templates/app/dashboard/settings/billing/loading.tsx +5 -0
  321. package/templates/app/dashboard/settings/billing/page.tsx +284 -0
  322. package/templates/app/dashboard/settings/invoices/[invoiceNumber]/page.tsx +222 -0
  323. package/templates/app/dashboard/settings/invoices/loading.tsx +5 -0
  324. package/templates/app/dashboard/settings/invoices/page.tsx +82 -0
  325. package/templates/app/dashboard/settings/layout.tsx +151 -0
  326. package/templates/app/dashboard/settings/loading.tsx +5 -0
  327. package/templates/app/dashboard/settings/notifications/loading.tsx +5 -0
  328. package/templates/app/dashboard/settings/notifications/page.tsx +462 -0
  329. package/templates/app/dashboard/settings/page.tsx +92 -0
  330. package/templates/app/dashboard/settings/password/loading.tsx +5 -0
  331. package/templates/app/dashboard/settings/password/page.tsx +306 -0
  332. package/templates/app/dashboard/settings/plans/loading.tsx +5 -0
  333. package/templates/app/dashboard/settings/plans/page.tsx +40 -0
  334. package/templates/app/dashboard/settings/profile/loading.tsx +5 -0
  335. package/templates/app/dashboard/settings/profile/page.tsx +686 -0
  336. package/templates/app/dashboard/settings/security/loading.tsx +5 -0
  337. package/templates/app/dashboard/settings/security/page.tsx +505 -0
  338. package/templates/app/dashboard/settings/teams/loading.tsx +5 -0
  339. package/templates/app/dashboard/settings/teams/page.tsx +272 -0
  340. package/templates/app/dashboard/settings/teams/permissions/page.tsx +92 -0
  341. package/templates/app/devtools/blocks/[slug]/page.tsx +39 -0
  342. package/templates/app/devtools/blocks/page.tsx +31 -0
  343. package/templates/app/devtools/config/page.tsx +31 -0
  344. package/templates/app/devtools/features/page.tsx +31 -0
  345. package/templates/app/devtools/flows/page.tsx +31 -0
  346. package/templates/app/devtools/layout.tsx +58 -0
  347. package/templates/app/devtools/page.tsx +121 -0
  348. package/templates/app/devtools/scheduled-actions/page.tsx +157 -0
  349. package/templates/app/devtools/style/page.tsx +330 -0
  350. package/templates/app/devtools/tags/page.tsx +31 -0
  351. package/templates/app/devtools/tests/[[...path]]/page.tsx +47 -0
  352. package/templates/app/favicon.ico +0 -0
  353. package/templates/app/globals.css +12 -0
  354. package/templates/app/layout.tsx +96 -0
  355. package/templates/app/public/page.tsx +30 -0
  356. package/templates/app/superadmin/docs/[section]/[page]/page.tsx +92 -0
  357. package/templates/app/superadmin/docs/page.tsx +75 -0
  358. package/templates/app/superadmin/layout.tsx +67 -0
  359. package/templates/app/superadmin/page.tsx +149 -0
  360. package/templates/app/superadmin/subscriptions/page.tsx +655 -0
  361. package/templates/app/superadmin/team-roles/page.tsx +493 -0
  362. package/templates/app/superadmin/teams/[teamId]/page.tsx +687 -0
  363. package/templates/app/superadmin/teams/page.tsx +302 -0
  364. package/templates/app/superadmin/users/[userId]/page.tsx +548 -0
  365. package/templates/app/superadmin/users/page.tsx +528 -0
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Change Plan Endpoint
3
+ *
4
+ * Allows users to upgrade or downgrade their subscription plan.
5
+ * Handles proration via Stripe automatically.
6
+ *
7
+ * P1-3: Plan Change con Proration
8
+ */
9
+
10
+ import { NextRequest } from 'next/server'
11
+ import { z } from 'zod'
12
+ import { authenticateRequest, createAuthError } from '@nextsparkjs/core/lib/api/auth/dual-auth'
13
+ import { SubscriptionService, MembershipService } from '@nextsparkjs/core/lib/services'
14
+
15
+ const changePlanSchema = z.object({
16
+ planSlug: z.string().min(1, 'Plan slug is required'),
17
+ billingInterval: z.enum(['monthly', 'yearly']).default('monthly'),
18
+ })
19
+
20
+ /**
21
+ * POST /api/v1/billing/change-plan
22
+ * Change the team's subscription plan (upgrade or downgrade)
23
+ *
24
+ * Body:
25
+ * - planSlug: string - Target plan slug
26
+ * - billingInterval: 'monthly' | 'yearly' (default: monthly)
27
+ */
28
+ export async function POST(request: NextRequest) {
29
+ // 1. Dual authentication
30
+ const authResult = await authenticateRequest(request)
31
+
32
+ if (!authResult.success || !authResult.user) {
33
+ return createAuthError('Unauthorized', 401)
34
+ }
35
+
36
+ // 2. Get team context
37
+ const teamId = request.headers.get('x-team-id') || authResult.user.defaultTeamId
38
+
39
+ if (!teamId) {
40
+ return Response.json(
41
+ {
42
+ success: false,
43
+ error: 'No team context available. Please provide x-team-id header.',
44
+ },
45
+ { status: 400 }
46
+ )
47
+ }
48
+
49
+ // 3. Permission check using MembershipService
50
+ const membership = await MembershipService.get(authResult.user.id, teamId)
51
+ const actionResult = membership.canPerformAction('billing.change-plan')
52
+
53
+ if (!actionResult.allowed) {
54
+ return Response.json(
55
+ {
56
+ success: false,
57
+ error: actionResult.message,
58
+ reason: actionResult.reason,
59
+ meta: actionResult.meta,
60
+ },
61
+ { status: 403 }
62
+ )
63
+ }
64
+
65
+ // 4. Parse and validate request body
66
+ let body: Record<string, unknown>
67
+ try {
68
+ body = await request.json()
69
+ } catch {
70
+ return Response.json({ success: false, error: 'Invalid JSON body' }, { status: 400 })
71
+ }
72
+
73
+ const parseResult = changePlanSchema.safeParse(body)
74
+ if (!parseResult.success) {
75
+ return Response.json(
76
+ { success: false, error: 'Invalid request body', details: parseResult.error.issues },
77
+ { status: 400 }
78
+ )
79
+ }
80
+
81
+ const { planSlug, billingInterval } = parseResult.data
82
+
83
+ // 5. Execute plan change
84
+ const result = await SubscriptionService.changePlan(teamId, planSlug, billingInterval)
85
+
86
+ if (!result.success) {
87
+ return Response.json({ success: false, error: result.error }, { status: 400 })
88
+ }
89
+
90
+ return Response.json({
91
+ success: true,
92
+ data: {
93
+ subscription: result.subscription,
94
+ warnings: result.downgradeWarnings,
95
+ },
96
+ })
97
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Check Action Endpoint
3
+ *
4
+ * Verifies if a user can perform a specific action by checking:
5
+ * 1. RBAC permissions (role-based access control)
6
+ * 2. Plan features (subscription-based access)
7
+ * 3. Quota limits (usage-based access)
8
+ *
9
+ * FIX1: This endpoint was missing, causing useMembership.canDo() to fail
10
+ */
11
+
12
+ import { NextRequest } from 'next/server'
13
+ import { authenticateRequest, createAuthError } from '@nextsparkjs/core/lib/api/auth/dual-auth'
14
+ import { MembershipService } from '@nextsparkjs/core/lib/services'
15
+ import { z } from 'zod'
16
+
17
+ const checkActionSchema = z.object({
18
+ action: z.string().min(1, 'Action is required'),
19
+ teamId: z.string().uuid().optional()
20
+ })
21
+
22
+ export async function POST(request: NextRequest) {
23
+ // 1. Dual authentication (API Key OR Session)
24
+ const authResult = await authenticateRequest(request)
25
+
26
+ if (!authResult.success || !authResult.user) {
27
+ return createAuthError('Unauthorized', 401)
28
+ }
29
+
30
+ // 2. Parse and validate request body
31
+ let body
32
+ try {
33
+ body = await request.json()
34
+ } catch {
35
+ return Response.json(
36
+ { success: false, error: 'Invalid JSON body' },
37
+ { status: 400 }
38
+ )
39
+ }
40
+
41
+ const parseResult = checkActionSchema.safeParse(body)
42
+ if (!parseResult.success) {
43
+ return Response.json(
44
+ {
45
+ success: false,
46
+ error: 'Validation failed',
47
+ details: parseResult.error.issues
48
+ },
49
+ { status: 400 }
50
+ )
51
+ }
52
+
53
+ const { action, teamId: bodyTeamId } = parseResult.data
54
+
55
+ // 3. Get team context (from body, header, or user's default)
56
+ const teamId =
57
+ bodyTeamId ||
58
+ request.headers.get('x-team-id') ||
59
+ authResult.user.defaultTeamId
60
+
61
+ if (!teamId) {
62
+ return Response.json(
63
+ {
64
+ success: false,
65
+ error: 'No team context available. Please provide teamId in body or x-team-id header.'
66
+ },
67
+ { status: 400 }
68
+ )
69
+ }
70
+
71
+ // 4. Check action permission using MembershipService
72
+ // Note: MembershipService.get() does NOT throw for non-members
73
+ // It returns TeamMembership with role: null
74
+ const membership = await MembershipService.get(authResult.user.id, teamId)
75
+ const result = membership.canPerformAction(action)
76
+
77
+ return Response.json({
78
+ success: true,
79
+ data: result
80
+ })
81
+ }
@@ -0,0 +1,124 @@
1
+ /**
2
+ * Stripe Checkout Endpoint
3
+ *
4
+ * Creates a Stripe Checkout session for subscription upgrade.
5
+ * Redirects user to Stripe-hosted checkout page.
6
+ *
7
+ * Security: Requires team owner/admin permission (team.billing.manage)
8
+ *
9
+ * P2: Stripe Integration
10
+ */
11
+
12
+ import { NextRequest } from 'next/server'
13
+ import { authenticateRequest, createAuthError } from '@nextsparkjs/core/lib/api/auth/dual-auth'
14
+ import { createCheckoutSession } from '@nextsparkjs/core/lib/billing/gateways/stripe'
15
+ import { SubscriptionService, MembershipService } from '@nextsparkjs/core/lib/services'
16
+ import { z } from 'zod'
17
+
18
+ const checkoutSchema = z.object({
19
+ planSlug: z.string().min(1, 'Plan slug is required'),
20
+ billingPeriod: z.enum(['monthly', 'yearly']).default('monthly')
21
+ })
22
+
23
+ export async function POST(request: NextRequest) {
24
+ // 1. Dual authentication
25
+ const authResult = await authenticateRequest(request)
26
+
27
+ if (!authResult.success || !authResult.user) {
28
+ return createAuthError('Unauthorized', 401)
29
+ }
30
+
31
+ // 2. Parse and validate request body
32
+ let body
33
+ try {
34
+ body = await request.json()
35
+ } catch {
36
+ return Response.json(
37
+ { success: false, error: 'Invalid JSON body' },
38
+ { status: 400 }
39
+ )
40
+ }
41
+
42
+ const parseResult = checkoutSchema.safeParse(body)
43
+ if (!parseResult.success) {
44
+ return Response.json(
45
+ {
46
+ success: false,
47
+ error: 'Validation failed',
48
+ details: parseResult.error.issues
49
+ },
50
+ { status: 400 }
51
+ )
52
+ }
53
+
54
+ const { planSlug, billingPeriod } = parseResult.data
55
+
56
+ // 3. Get team context
57
+ const teamId =
58
+ request.headers.get('x-team-id') ||
59
+ authResult.user.defaultTeamId
60
+
61
+ if (!teamId) {
62
+ return Response.json(
63
+ {
64
+ success: false,
65
+ error: 'No team context available. Please provide x-team-id header.'
66
+ },
67
+ { status: 400 }
68
+ )
69
+ }
70
+
71
+ // 4. Permission check using MembershipService
72
+ const membership = await MembershipService.get(authResult.user.id, teamId)
73
+ const actionResult = membership.canPerformAction('billing.checkout')
74
+
75
+ if (!actionResult.allowed) {
76
+ return Response.json(
77
+ {
78
+ success: false,
79
+ error: actionResult.message,
80
+ reason: actionResult.reason,
81
+ meta: actionResult.meta,
82
+ },
83
+ { status: 403 }
84
+ )
85
+ }
86
+
87
+ // 5. Check existing subscription for customer ID
88
+ try {
89
+ const subscription = await SubscriptionService.getActive(teamId)
90
+
91
+ // Build URLs
92
+ const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:5173'
93
+ const successUrl = `${appUrl}/dashboard/settings/billing?success=true`
94
+ const cancelUrl = `${appUrl}/dashboard/settings/billing?canceled=true`
95
+
96
+ // Create Stripe Checkout session
97
+ const session = await createCheckoutSession({
98
+ teamId,
99
+ planSlug,
100
+ billingPeriod,
101
+ successUrl,
102
+ cancelUrl,
103
+ customerEmail: authResult.user.email,
104
+ customerId: subscription?.externalCustomerId || undefined
105
+ })
106
+
107
+ return Response.json({
108
+ success: true,
109
+ data: {
110
+ url: session.url,
111
+ sessionId: session.id
112
+ }
113
+ })
114
+ } catch (error) {
115
+ console.error('[checkout] Error creating checkout session:', error)
116
+ return Response.json(
117
+ {
118
+ success: false,
119
+ error: error instanceof Error ? error.message : 'Failed to create checkout session'
120
+ },
121
+ { status: 500 }
122
+ )
123
+ }
124
+ }
@@ -0,0 +1,209 @@
1
+ # Billing API
2
+
3
+ Manage subscriptions, plans, and billing operations for teams.
4
+
5
+ ## Overview
6
+
7
+ The Billing API integrates with Stripe to handle subscription management, checkout sessions, plan changes, and customer portal access. All billing operations are scoped to teams.
8
+
9
+ ## Authentication
10
+
11
+ All endpoints require authentication via:
12
+ - **Session cookie** (for browser-based requests)
13
+ - **API Key** header (for server-to-server requests)
14
+
15
+ The `x-team-id` header or user's default team is used for team context.
16
+
17
+ ## Endpoints
18
+
19
+ ### List Plans
20
+ `GET /api/v1/billing/plans`
21
+
22
+ Returns available subscription plans. Public plans visible to all, hidden plans only to superadmin.
23
+
24
+ **Example Response:**
25
+ ```json
26
+ {
27
+ "data": [
28
+ {
29
+ "id": "plan_123",
30
+ "slug": "pro",
31
+ "name": "Pro Plan",
32
+ "description": "For growing teams",
33
+ "priceMonthly": 29,
34
+ "priceYearly": 290,
35
+ "currency": "USD",
36
+ "features": ["unlimited_projects", "api_access"],
37
+ "limits": {
38
+ "team_members": 10,
39
+ "storage_gb": 100
40
+ }
41
+ }
42
+ ]
43
+ }
44
+ ```
45
+
46
+ ### Create Checkout Session
47
+ `POST /api/v1/billing/checkout`
48
+
49
+ Creates a Stripe Checkout session for subscription upgrade. Returns a URL to redirect the user.
50
+
51
+ **Required Permission:** `billing.checkout` (team owner/admin)
52
+
53
+ **Request Body:**
54
+ ```json
55
+ {
56
+ "planSlug": "pro",
57
+ "billingPeriod": "monthly"
58
+ }
59
+ ```
60
+
61
+ **Parameters:**
62
+ - `planSlug` (string, required): The plan to subscribe to
63
+ - `billingPeriod` (string): "monthly" or "yearly" (default: "monthly")
64
+
65
+ **Response:**
66
+ ```json
67
+ {
68
+ "success": true,
69
+ "data": {
70
+ "url": "https://checkout.stripe.com/...",
71
+ "sessionId": "cs_live_..."
72
+ }
73
+ }
74
+ ```
75
+
76
+ ### Open Customer Portal
77
+ `GET /api/v1/billing/portal`
78
+
79
+ Creates a Stripe Customer Portal session for managing payment methods and viewing invoices.
80
+
81
+ **Required Permission:** `billing.portal` (team owner/admin)
82
+
83
+ **Response:**
84
+ ```json
85
+ {
86
+ "success": true,
87
+ "data": {
88
+ "url": "https://billing.stripe.com/..."
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### Change Plan
94
+ `POST /api/v1/billing/change-plan`
95
+
96
+ Change the current subscription to a different plan.
97
+
98
+ **Required Permission:** `billing.change_plan` (team owner/admin)
99
+
100
+ **Request Body:**
101
+ ```json
102
+ {
103
+ "planSlug": "enterprise",
104
+ "billingPeriod": "yearly"
105
+ }
106
+ ```
107
+
108
+ ### Cancel Subscription
109
+ `POST /api/v1/billing/cancel`
110
+
111
+ Cancel the current subscription. Cancellation takes effect at the end of the billing period.
112
+
113
+ **Required Permission:** `billing.cancel` (team owner/admin)
114
+
115
+ **Response:**
116
+ ```json
117
+ {
118
+ "success": true,
119
+ "data": {
120
+ "canceledAt": "2024-02-01T00:00:00Z",
121
+ "effectiveDate": "2024-02-28T23:59:59Z"
122
+ }
123
+ }
124
+ ```
125
+
126
+ ### Check Action Permission
127
+ `POST /api/v1/billing/check-action`
128
+
129
+ Verify if the current user can perform a specific action based on RBAC, plan features, and quotas.
130
+
131
+ **Request Body:**
132
+ ```json
133
+ {
134
+ "action": "team.invite_member",
135
+ "teamId": "team_123"
136
+ }
137
+ ```
138
+
139
+ **Response:**
140
+ ```json
141
+ {
142
+ "success": true,
143
+ "data": {
144
+ "allowed": true,
145
+ "reason": null
146
+ }
147
+ }
148
+ ```
149
+
150
+ Or if not allowed:
151
+ ```json
152
+ {
153
+ "success": true,
154
+ "data": {
155
+ "allowed": false,
156
+ "reason": "quota_exceeded",
157
+ "message": "Team member limit reached",
158
+ "meta": {
159
+ "limit": 5,
160
+ "current": 5
161
+ }
162
+ }
163
+ }
164
+ ```
165
+
166
+ ### Stripe Webhook
167
+ `POST /api/v1/billing/webhooks/stripe`
168
+
169
+ Handles Stripe webhook events for subscription lifecycle management.
170
+
171
+ **Note:** This endpoint is called by Stripe, not by your application. It requires Stripe signature verification.
172
+
173
+ **Handled Events:**
174
+ - `checkout.session.completed` - New subscription created
175
+ - `customer.subscription.updated` - Plan changed
176
+ - `customer.subscription.deleted` - Subscription canceled
177
+ - `invoice.paid` - Payment successful
178
+ - `invoice.payment_failed` - Payment failed
179
+
180
+ ## Error Responses
181
+
182
+ | Status | Description |
183
+ |--------|-------------|
184
+ | 400 | Bad Request - Invalid parameters or missing required fields |
185
+ | 401 | Unauthorized - Authentication required |
186
+ | 403 | Forbidden - Insufficient permissions or plan doesn't allow action |
187
+ | 404 | Not Found - Plan or subscription not found |
188
+ | 500 | Server Error - Stripe API error or internal error |
189
+
190
+ ## Billing Period Proration
191
+
192
+ When changing plans mid-cycle:
193
+ - **Upgrade:** Prorated charge for the difference
194
+ - **Downgrade:** Credit applied to next invoice
195
+ - **Same price:** No proration
196
+
197
+ ## Test Mode
198
+
199
+ In development, Stripe test mode is used. Test card numbers:
200
+ - Success: `4242 4242 4242 4242`
201
+ - Decline: `4000 0000 0000 0002`
202
+ - 3D Secure: `4000 0025 0000 3155`
203
+
204
+ ## Related APIs
205
+
206
+ - **[Teams](/api/v1/teams)** - Team subscription and usage endpoints
207
+ - **[Teams Subscription](/api/v1/teams/{teamId}/subscription)** - View team's current subscription
208
+ - **[Teams Usage](/api/v1/teams/{teamId}/usage/{limit})** - Check quota usage
209
+ - **[Teams Invoices](/api/v1/teams/{teamId}/invoices)** - View billing invoices
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Plans API - Billing system
3
+ *
4
+ * GET /api/v1/billing/plans - List all plans (public can see public plans)
5
+ * POST /api/v1/billing/plans - Create a plan (superadmin only)
6
+ */
7
+
8
+ import { NextRequest } from 'next/server'
9
+ import { validateAndAuthenticateRequest, createApiResponse, createApiError } from '@nextsparkjs/core/lib/api/helpers'
10
+ import { PlanService } from '@nextsparkjs/core/lib/services'
11
+ import { createPlanSchema } from '@nextsparkjs/core/lib/billing/schema'
12
+ import { mutateWithRLS } from '@nextsparkjs/core/lib/db'
13
+
14
+ export async function GET(request: NextRequest) {
15
+ // Plans list is partially public (public plans visible to all, hidden plans only to superadmin)
16
+ let includeHidden = false
17
+
18
+ try {
19
+ const { auth } = await validateAndAuthenticateRequest(request)
20
+ // Check if user is superadmin (for full access including hidden plans)
21
+ includeHidden = auth.scopes.includes('*') || auth.scopes.includes('superadmin:all')
22
+ } catch {
23
+ // Not authenticated, only show public plans
24
+ includeHidden = false
25
+ }
26
+
27
+ try {
28
+ const plans = await PlanService.list({ includeHidden })
29
+ return createApiResponse(plans)
30
+ } catch (error) {
31
+ console.error('[Billing API] Error fetching plans:', error)
32
+ return createApiError('Failed to fetch plans', 500)
33
+ }
34
+ }
35
+
36
+ export async function POST(request: NextRequest) {
37
+ // Authenticate request
38
+ const { auth, rateLimitResponse } = await validateAndAuthenticateRequest(request)
39
+ if (rateLimitResponse) return rateLimitResponse
40
+
41
+ // Check superadmin permission
42
+ if (!auth.scopes.includes('*') && !auth.scopes.includes('superadmin:all')) {
43
+ return createApiError('Only superadmin can create plans', 403)
44
+ }
45
+
46
+ try {
47
+ const body = await request.json()
48
+ const data = createPlanSchema.parse(body)
49
+
50
+ const { rows } = await mutateWithRLS(
51
+ `
52
+ INSERT INTO plans (
53
+ slug, name, description, type, visibility,
54
+ "priceMonthly", "priceYearly", currency, "trialDays",
55
+ features, limits, metadata, "sortOrder"
56
+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
57
+ RETURNING *
58
+ `,
59
+ [
60
+ data.slug,
61
+ data.name,
62
+ data.description,
63
+ data.type,
64
+ data.visibility,
65
+ data.priceMonthly,
66
+ data.priceYearly,
67
+ data.currency,
68
+ data.trialDays,
69
+ JSON.stringify(data.features),
70
+ JSON.stringify(data.limits),
71
+ JSON.stringify(data.metadata),
72
+ data.sortOrder,
73
+ ]
74
+ )
75
+
76
+ return createApiResponse(rows[0], { created: true }, 201)
77
+ } catch (error: unknown) {
78
+ const errorMessage = error instanceof Error ? error.message : ''
79
+ if (errorMessage.includes('duplicate') || errorMessage.includes('unique')) {
80
+ return createApiError('Plan slug already exists', 400)
81
+ }
82
+ console.error('[Billing API] Error creating plan:', error)
83
+ return createApiError('Failed to create plan', 500)
84
+ }
85
+ }
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Stripe Customer Portal Endpoint
3
+ *
4
+ * Creates a Stripe Customer Portal session for self-service billing management.
5
+ * Users can update payment methods, view invoices, and cancel subscriptions.
6
+ *
7
+ * P6: Customer Portal
8
+ */
9
+
10
+ import { NextRequest } from 'next/server'
11
+ import { authenticateRequest, createAuthError } from '@nextsparkjs/core/lib/api/auth/dual-auth'
12
+ import { createPortalSession } from '@nextsparkjs/core/lib/billing/gateways/stripe'
13
+ import { SubscriptionService, MembershipService } from '@nextsparkjs/core/lib/services'
14
+
15
+ export async function POST(request: NextRequest) {
16
+ // 1. Dual authentication
17
+ const authResult = await authenticateRequest(request)
18
+
19
+ if (!authResult.success || !authResult.user) {
20
+ return createAuthError('Unauthorized', 401)
21
+ }
22
+
23
+ // 2. Get team context
24
+ const teamId =
25
+ request.headers.get('x-team-id') ||
26
+ authResult.user.defaultTeamId
27
+
28
+ if (!teamId) {
29
+ return Response.json(
30
+ {
31
+ success: false,
32
+ error: 'No team context available. Please provide x-team-id header.'
33
+ },
34
+ { status: 400 }
35
+ )
36
+ }
37
+
38
+ // 3. Permission check using MembershipService
39
+ const membership = await MembershipService.get(authResult.user.id, teamId)
40
+ const actionResult = membership.canPerformAction('billing.portal')
41
+
42
+ if (!actionResult.allowed) {
43
+ return Response.json(
44
+ {
45
+ success: false,
46
+ error: actionResult.message,
47
+ reason: actionResult.reason,
48
+ meta: actionResult.meta,
49
+ },
50
+ { status: 403 }
51
+ )
52
+ }
53
+
54
+ // 4. Get subscription with Stripe customer ID
55
+ try {
56
+ const subscription = await SubscriptionService.getActive(teamId)
57
+
58
+ if (!subscription?.externalCustomerId) {
59
+ return Response.json(
60
+ {
61
+ success: false,
62
+ error: 'No billing account found. Please upgrade to a paid plan first.'
63
+ },
64
+ { status: 400 }
65
+ )
66
+ }
67
+
68
+ const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:5173'
69
+ const returnUrl = `${appUrl}/dashboard/settings/billing`
70
+
71
+ const session = await createPortalSession({
72
+ customerId: subscription.externalCustomerId,
73
+ returnUrl
74
+ })
75
+
76
+ return Response.json({
77
+ success: true,
78
+ data: { url: session.url }
79
+ })
80
+ } catch (error) {
81
+ console.error('[portal] Error creating portal session:', error)
82
+ return Response.json(
83
+ {
84
+ success: false,
85
+ error: error instanceof Error ? error.message : 'Failed to create portal session'
86
+ },
87
+ { status: 500 }
88
+ )
89
+ }
90
+ }