@carlonicora/nextjs-jsonapi 5.4.0 → 5.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.
Files changed (237) hide show
  1. package/dist/{BlockNoteEditor-4YCAIEPT.js → BlockNoteEditor-AE3ZTEEH.js} +20 -20
  2. package/dist/{BlockNoteEditor-4YCAIEPT.js.map → BlockNoteEditor-AE3ZTEEH.js.map} +1 -1
  3. package/dist/{BlockNoteEditor-YQL2SGFR.mjs → BlockNoteEditor-NEYG5L6W.mjs} +5 -5
  4. package/dist/{BlockNoteViewer-AEJKJ27Q.mjs → BlockNoteViewer-3OUT5UFY.mjs} +3 -3
  5. package/dist/{BlockNoteViewer-YL3J7J2Y.js → BlockNoteViewer-4CFKQM7U.js} +4 -4
  6. package/dist/{BlockNoteViewer-YL3J7J2Y.js.map → BlockNoteViewer-4CFKQM7U.js.map} +1 -1
  7. package/dist/billing/index.d.mts +186 -25
  8. package/dist/billing/index.d.ts +186 -25
  9. package/dist/billing/index.js +1149 -588
  10. package/dist/billing/index.js.map +1 -1
  11. package/dist/billing/index.mjs +786 -225
  12. package/dist/billing/index.mjs.map +1 -1
  13. package/dist/{chunk-5PPG6PL7.js → chunk-3I5AAJXV.js} +3 -3
  14. package/dist/{chunk-5PPG6PL7.js.map → chunk-3I5AAJXV.js.map} +1 -1
  15. package/dist/{chunk-YW4ZCEWB.js → chunk-53BAULDP.js} +4 -4
  16. package/dist/{chunk-YW4ZCEWB.js.map → chunk-53BAULDP.js.map} +1 -1
  17. package/dist/{chunk-XCHSX3VC.mjs → chunk-BTPUYUQ3.mjs} +2 -2
  18. package/dist/{chunk-WC63PJSC.mjs → chunk-EYA2F6AN.mjs} +756 -24
  19. package/dist/chunk-EYA2F6AN.mjs.map +1 -0
  20. package/dist/{chunk-NFBOSPNJ.js → chunk-HKZAESS6.js} +954 -222
  21. package/dist/chunk-HKZAESS6.js.map +1 -0
  22. package/dist/{chunk-6DC2WVVB.mjs → chunk-KQHBONL4.mjs} +2 -2
  23. package/dist/{chunk-3NLAXVIN.js → chunk-LQ4DK4JF.js} +1988 -1089
  24. package/dist/chunk-LQ4DK4JF.js.map +1 -0
  25. package/dist/{chunk-YNCQSWQW.js → chunk-RL3PL3KV.js} +8 -8
  26. package/dist/{chunk-YNCQSWQW.js.map → chunk-RL3PL3KV.js.map} +1 -1
  27. package/dist/{chunk-EUKTBOI2.mjs → chunk-SFYPXCK4.mjs} +3382 -2483
  28. package/dist/chunk-SFYPXCK4.mjs.map +1 -0
  29. package/dist/{chunk-QMYQRPQC.mjs → chunk-Y7RR57EW.mjs} +2 -2
  30. package/dist/client/index.js +5 -5
  31. package/dist/client/index.js.map +1 -1
  32. package/dist/client/index.mjs +4 -4
  33. package/dist/components/editors/server.d.mts +15 -0
  34. package/dist/components/editors/server.d.ts +15 -0
  35. package/dist/components/editors/server.js +38 -0
  36. package/dist/components/editors/server.js.map +1 -0
  37. package/dist/components/editors/server.mjs +38 -0
  38. package/dist/components/editors/server.mjs.map +1 -0
  39. package/dist/components/index.d.mts +33 -10
  40. package/dist/components/index.d.ts +33 -10
  41. package/dist/components/index.js +7 -5
  42. package/dist/components/index.js.map +1 -1
  43. package/dist/components/index.mjs +8 -6
  44. package/dist/contexts/index.d.mts +9 -1
  45. package/dist/contexts/index.d.ts +9 -1
  46. package/dist/contexts/index.js +5 -5
  47. package/dist/contexts/index.mjs +4 -4
  48. package/dist/core/index.d.mts +242 -3
  49. package/dist/core/index.d.ts +242 -3
  50. package/dist/core/index.js +36 -4
  51. package/dist/core/index.js.map +1 -1
  52. package/dist/core/index.mjs +35 -3
  53. package/dist/email-automations/index.d.mts +3 -0
  54. package/dist/email-automations/index.d.ts +3 -0
  55. package/dist/email-automations/index.js +99 -95
  56. package/dist/email-automations/index.js.map +1 -1
  57. package/dist/email-automations/index.mjs +30 -26
  58. package/dist/email-automations/index.mjs.map +1 -1
  59. package/dist/features/analytics/index.d.mts +11 -4
  60. package/dist/features/analytics/index.d.ts +11 -4
  61. package/dist/features/analytics/index.js +127 -135
  62. package/dist/features/analytics/index.js.map +1 -1
  63. package/dist/features/analytics/index.mjs +16 -24
  64. package/dist/features/analytics/index.mjs.map +1 -1
  65. package/dist/features/analytics/server.d.mts +5 -2
  66. package/dist/features/analytics/server.d.ts +5 -2
  67. package/dist/features/analytics/server.js +7 -7
  68. package/dist/features/analytics/server.js.map +1 -1
  69. package/dist/features/analytics/server.mjs +5 -5
  70. package/dist/features/analytics/server.mjs.map +1 -1
  71. package/dist/features/blog/index.d.mts +12 -2
  72. package/dist/features/blog/index.d.ts +12 -2
  73. package/dist/features/blog/index.js +206 -195
  74. package/dist/features/blog/index.js.map +1 -1
  75. package/dist/features/blog/index.mjs +71 -60
  76. package/dist/features/blog/index.mjs.map +1 -1
  77. package/dist/features/blog/server.d.mts +4 -1
  78. package/dist/features/blog/server.d.ts +4 -1
  79. package/dist/features/blog/server.js +49 -21
  80. package/dist/features/blog/server.js.map +1 -1
  81. package/dist/features/blog/server.mjs +36 -8
  82. package/dist/features/blog/server.mjs.map +1 -1
  83. package/dist/features/help/index.js +38 -38
  84. package/dist/features/help/index.mjs +4 -4
  85. package/dist/features/tokenusage/index.d.mts +1 -3
  86. package/dist/features/tokenusage/index.d.ts +1 -3
  87. package/dist/features/tokenusage/index.js +127 -135
  88. package/dist/features/tokenusage/index.js.map +1 -1
  89. package/dist/features/tokenusage/index.mjs +10 -18
  90. package/dist/features/tokenusage/index.mjs.map +1 -1
  91. package/dist/{stripe-promotion-code.interface-C3qqh3mi.d.ts → format-options-BtX3o0Yu.d.ts} +104 -1
  92. package/dist/{stripe-promotion-code.interface-ClZ7DxS9.d.mts → format-options-D6c7FNDi.d.mts} +104 -1
  93. package/dist/index.d.mts +2 -2
  94. package/dist/index.d.ts +2 -2
  95. package/dist/index.js +37 -5
  96. package/dist/index.js.map +1 -1
  97. package/dist/index.mjs +36 -4
  98. package/dist/server/index.js +4 -4
  99. package/dist/server/index.mjs +3 -3
  100. package/messages/en.json +125 -3
  101. package/messages/it.json +125 -3
  102. package/package.json +9 -1
  103. package/scripts/validate-messages.mjs +1 -0
  104. package/src/billing/index.ts +1 -0
  105. package/src/client/index.ts +4 -0
  106. package/src/components/editors/server/BlockNoteServerContent.tsx +28 -0
  107. package/src/components/editors/server/__tests__/renderBlockNoteHTML.spec.ts +44 -0
  108. package/src/components/editors/server/renderBlockNoteHTML.ts +26 -0
  109. package/src/components/editors/server-entry.ts +7 -0
  110. package/src/components/index.ts +1 -0
  111. package/src/components/viewers/BlockNoteStaticViewer.tsx +44 -0
  112. package/src/core/index.ts +1 -0
  113. package/src/core/registry/ModuleRegistry.ts +3 -0
  114. package/src/features/administration/components/AdminIndexContainer.tsx +61 -33
  115. package/src/features/administration/components/__tests__/AdminIndexContainer.spec.tsx +61 -1
  116. package/src/features/administration/components/index.ts +2 -1
  117. package/src/features/administration/contexts/AdministrationContext.tsx +11 -3
  118. package/src/features/administration/contexts/__tests__/AdministrationContext.spec.tsx +40 -0
  119. package/src/features/ai-connection/ai-connection.module.ts +2 -0
  120. package/src/features/ai-connection/contexts/AiConnectionsContext.tsx +3 -16
  121. package/src/features/ai-connection/contexts/__tests__/AiConnectionsContext.spec.tsx +33 -0
  122. package/src/features/analytics/__tests__/server-entry.spec.tsx +2 -1
  123. package/src/features/analytics/analytics.modules.ts +22 -5
  124. package/src/features/analytics/components/AnalyticsAdminContainer.tsx +4 -3
  125. package/src/features/analytics/components/AnalyticsAdminPageContainer.tsx +6 -3
  126. package/src/features/analytics/components/AnalyticsJourneyContainer.tsx +4 -3
  127. package/src/features/analytics/components/__tests__/AnalyticsJourneyContainer.spec.tsx +11 -0
  128. package/src/features/analytics/components/__tests__/AnalyticsPageContainers.spec.tsx +12 -1
  129. package/src/features/analytics/components/server/AnalyticsAdminPage.tsx +6 -3
  130. package/src/features/analytics/contexts/AnalyticsAdminContext.tsx +8 -17
  131. package/src/features/analytics/contexts/__tests__/AnalyticsAdminContext.spec.tsx +17 -0
  132. package/src/features/billing/components/cards/InvoicesSummaryCard.tsx +3 -2
  133. package/src/features/billing/components/cards/SubscriptionSummaryCard.tsx +4 -3
  134. package/src/features/billing/components/containers/BillingDashboardContainer.tsx +22 -5
  135. package/src/features/billing/components/utils/__tests__/discount.spec.ts +59 -0
  136. package/src/features/billing/components/utils/__tests__/url.spec.ts +32 -0
  137. package/src/features/billing/components/utils/discount.ts +38 -0
  138. package/src/features/billing/components/utils/index.ts +2 -0
  139. package/src/features/billing/components/utils/url.ts +12 -0
  140. package/src/features/billing/contexts/CouponContext.tsx +211 -0
  141. package/src/features/billing/contexts/index.ts +1 -0
  142. package/src/features/billing/i18n-keys.ts +79 -0
  143. package/src/features/billing/stripe-coupon/components/containers/CouponContainer.tsx +38 -0
  144. package/src/features/billing/stripe-coupon/components/containers/index.ts +1 -0
  145. package/src/features/billing/stripe-coupon/components/details/CouponDetails.tsx +69 -0
  146. package/src/features/billing/stripe-coupon/components/details/index.ts +1 -0
  147. package/src/features/billing/stripe-coupon/components/forms/CouponCodeEditor.tsx +79 -0
  148. package/src/features/billing/stripe-coupon/components/forms/CouponCodeFields.tsx +192 -0
  149. package/src/features/billing/stripe-coupon/components/forms/CouponEditor.tsx +333 -0
  150. package/src/features/billing/stripe-coupon/components/forms/CouponRefresher.tsx +45 -0
  151. package/src/features/billing/stripe-coupon/components/forms/index.ts +6 -0
  152. package/src/features/billing/stripe-coupon/components/index.ts +5 -0
  153. package/src/features/billing/stripe-coupon/components/lists/CouponCodesList.tsx +61 -0
  154. package/src/features/billing/stripe-coupon/components/lists/CouponsList.tsx +67 -0
  155. package/src/features/billing/stripe-coupon/components/lists/index.ts +2 -0
  156. package/src/features/billing/stripe-coupon/components/widgets/CouponCodeActions.tsx +80 -0
  157. package/src/features/billing/stripe-coupon/components/widgets/index.ts +1 -0
  158. package/src/features/billing/stripe-coupon/data/__tests__/stripe-coupon-models.spec.ts +114 -0
  159. package/src/features/billing/stripe-coupon/data/index.ts +10 -0
  160. package/src/features/billing/stripe-coupon/data/stripe-coupon-code.fields.ts +8 -0
  161. package/src/features/billing/stripe-coupon/data/stripe-coupon-code.interface.ts +44 -0
  162. package/src/features/billing/stripe-coupon/data/stripe-coupon-code.service.ts +92 -0
  163. package/src/features/billing/stripe-coupon/data/stripe-coupon-code.ts +132 -0
  164. package/src/features/billing/stripe-coupon/data/stripe-coupon-offer.interface.ts +17 -0
  165. package/src/features/billing/stripe-coupon/data/stripe-coupon-offer.ts +70 -0
  166. package/src/features/billing/stripe-coupon/data/stripe-coupon.fields.ts +7 -0
  167. package/src/features/billing/stripe-coupon/data/stripe-coupon.interface.ts +51 -0
  168. package/src/features/billing/stripe-coupon/data/stripe-coupon.service.ts +93 -0
  169. package/src/features/billing/stripe-coupon/data/stripe-coupon.ts +165 -0
  170. package/src/features/billing/stripe-coupon/hooks/useStripeCouponCodeTableStructure.tsx +154 -0
  171. package/src/features/billing/stripe-coupon/hooks/useStripeCouponTableStructure.tsx +113 -0
  172. package/src/features/billing/stripe-coupon/index.ts +5 -0
  173. package/src/features/billing/stripe-coupon/stripe-coupon-code.module.ts +14 -0
  174. package/src/features/billing/stripe-coupon/stripe-coupon-offer.module.ts +10 -0
  175. package/src/features/billing/stripe-coupon/stripe-coupon.module.ts +18 -0
  176. package/src/features/billing/stripe-coupon/utils/__tests__/coupon-places.spec.ts +19 -0
  177. package/src/features/billing/stripe-coupon/utils/__tests__/to-promotion-code-result.spec.ts +41 -0
  178. package/src/features/billing/stripe-coupon/utils/coupon-places.ts +23 -0
  179. package/src/features/billing/stripe-coupon/utils/format-coupon-discount.ts +36 -0
  180. package/src/features/billing/stripe-coupon/utils/index.ts +3 -0
  181. package/src/features/billing/stripe-coupon/utils/to-promotion-code-result.ts +23 -0
  182. package/src/features/billing/stripe-customer/components/forms/PaymentMethodForm.tsx +24 -8
  183. package/src/features/billing/stripe-price/stripe-price.module.ts +2 -0
  184. package/src/features/billing/stripe-product/components/containers/ProductsListContainer.tsx +21 -6
  185. package/src/features/billing/stripe-product/stripe-product.module.ts +2 -0
  186. package/src/features/billing/stripe-promotion-code/components/PromoCodeInput.tsx +60 -19
  187. package/src/features/billing/stripe-promotion-code/data/stripe-promotion-code.interface.ts +2 -0
  188. package/src/features/billing/stripe-subscription/components/containers/SubscriptionsContainer.tsx +24 -5
  189. package/src/features/billing/stripe-subscription/components/lists/SubscriptionsList.tsx +21 -2
  190. package/src/features/billing/stripe-subscription/components/wizards/SubscriptionWizard.tsx +1 -1
  191. package/src/features/billing/stripe-subscription/components/wizards/WizardStepPaymentMethod.tsx +1 -1
  192. package/src/features/billing/stripe-subscription/components/wizards/WizardStepReview.tsx +60 -49
  193. package/src/features/billing/stripe-subscription/components/wizards/__tests__/review-card.spec.ts +34 -0
  194. package/src/features/billing/stripe-subscription/components/wizards/review-card.ts +24 -0
  195. package/src/features/billing/stripe-subscription/data/__tests__/stripe-subscription.discount.spec.ts +67 -0
  196. package/src/features/billing/stripe-subscription/data/stripe-subscription.interface.ts +14 -0
  197. package/src/features/billing/stripe-subscription/data/stripe-subscription.service.ts +39 -0
  198. package/src/features/billing/stripe-subscription/data/stripe-subscription.ts +21 -0
  199. package/src/features/billing/stripe-subscription/hooks/__tests__/wizard-payment.spec.ts +48 -0
  200. package/src/features/billing/stripe-subscription/hooks/__tests__/wizard-promotion.spec.ts +132 -0
  201. package/src/features/billing/stripe-subscription/hooks/useSubscriptionWizard.ts +138 -12
  202. package/src/features/billing/stripe-subscription/hooks/wizard-payment.ts +30 -0
  203. package/src/features/billing/stripe-subscription/hooks/wizard-promotion.ts +54 -0
  204. package/src/features/blog/components/admin/BlogAdminContainer.tsx +13 -6
  205. package/src/features/blog/components/admin/BlogCategoriesList.tsx +1 -1
  206. package/src/features/blog/components/admin/BlogCoverImageField.tsx +1 -1
  207. package/src/features/blog/components/admin/BlogPostContainer.tsx +0 -1
  208. package/src/features/blog/components/admin/BlogPostEditor.tsx +42 -33
  209. package/src/features/blog/components/admin/BlogPostTranslationEditor.tsx +12 -4
  210. package/src/features/blog/components/admin/BlogPostsList.tsx +1 -1
  211. package/src/features/blog/components/public/BlogPostCard.tsx +2 -9
  212. package/src/features/blog/components/public/BlogPostMeta.tsx +2 -2
  213. package/src/features/blog/components/public/BlogPostView.tsx +9 -3
  214. package/src/features/blog/components/public/__tests__/BlogList.spec.tsx +2 -0
  215. package/src/features/blog/components/public/__tests__/BlogPostView.spec.tsx +17 -11
  216. package/src/features/blog/server/fetchers.ts +57 -8
  217. package/src/features/email-automation/components/containers/EmailAutomationsListContainer.tsx +12 -3
  218. package/src/features/rbac/contexts/RbacContext.tsx +4 -7
  219. package/src/features/rbac/contexts/__tests__/RbacContext.spec.tsx +42 -0
  220. package/src/features/tokenusage/components/TokenUsageAdminContainer.tsx +4 -3
  221. package/src/features/tokenusage/contexts/TokenUsageAdminContext.tsx +3 -18
  222. package/src/features/tokenusage/contexts/__tests__/TokenUsageAdminContext.spec.tsx +17 -0
  223. package/src/features/tokenusage/tokenusage-admin.module.ts +4 -3
  224. package/src/features/tokenusage/tokenusage.modules.ts +12 -2
  225. package/src/features/user/contexts/CurrentUserContext.tsx +25 -0
  226. package/src/features/user/contexts/UserContext.tsx +18 -3
  227. package/src/features/user/contexts/__tests__/CurrentUserContext.spec.tsx +121 -2
  228. package/src/features/user/contexts/__tests__/UserContext.spec.tsx +95 -0
  229. package/dist/chunk-3NLAXVIN.js.map +0 -1
  230. package/dist/chunk-EUKTBOI2.mjs.map +0 -1
  231. package/dist/chunk-NFBOSPNJ.js.map +0 -1
  232. package/dist/chunk-WC63PJSC.mjs.map +0 -1
  233. /package/dist/{BlockNoteEditor-YQL2SGFR.mjs.map → BlockNoteEditor-NEYG5L6W.mjs.map} +0 -0
  234. /package/dist/{BlockNoteViewer-AEJKJ27Q.mjs.map → BlockNoteViewer-3OUT5UFY.mjs.map} +0 -0
  235. /package/dist/{chunk-XCHSX3VC.mjs.map → chunk-BTPUYUQ3.mjs.map} +0 -0
  236. /package/dist/{chunk-6DC2WVVB.mjs.map → chunk-KQHBONL4.mjs.map} +0 -0
  237. /package/dist/{chunk-QMYQRPQC.mjs.map → chunk-Y7RR57EW.mjs.map} +0 -0
@@ -0,0 +1,333 @@
1
+ "use client";
2
+
3
+ import { zodResolver } from "@hookform/resolvers/zod";
4
+ import { useTranslations } from "next-intl";
5
+ import { ReactNode, useCallback, useMemo } from "react";
6
+ import { useForm } from "react-hook-form";
7
+ import { v4 } from "uuid";
8
+ import { z } from "zod";
9
+ import {
10
+ DetailField,
11
+ EditorSheet,
12
+ EntityMultiSelector,
13
+ FormDateTime,
14
+ FormInput,
15
+ FormSelect,
16
+ SectionHeader,
17
+ } from "../../../../../components";
18
+ import { Modules } from "../../../../../core";
19
+ import { useI18nRouter } from "../../../../../i18n";
20
+ import { formatDate, useBillingFormatOptions } from "../../../components/utils";
21
+ import { useCouponContext } from "../../../contexts/CouponContext";
22
+ import { StripeProductInterface } from "../../../stripe-product/data/stripe-product.interface";
23
+ import { StripeProductService } from "../../../stripe-product/data/stripe-product.service";
24
+ import { StripeCouponInput, StripeCouponInterface } from "../../data/stripe-coupon.interface";
25
+ import { formatCouponDiscount } from "../../utils/format-coupon-discount";
26
+ import {
27
+ CouponCodeFields,
28
+ couponCodeDefaults,
29
+ couponCodeSchema,
30
+ couponCurrencyOptions,
31
+ optionalDate,
32
+ optionalDecimal,
33
+ optionalPositiveInt,
34
+ toCouponCodeInput,
35
+ } from "./CouponCodeFields";
36
+
37
+ export type CouponEditorProps = {
38
+ coupon?: StripeCouponInterface;
39
+ propagateChanges?: (coupon: StripeCouponInterface) => void;
40
+ onSuccess?: () => void | Promise<void>;
41
+ trigger?: ReactNode;
42
+ forceShow?: boolean;
43
+ onClose?: () => void;
44
+ dialogOpen?: boolean;
45
+ onDialogOpenChange?: (open: boolean) => void;
46
+ };
47
+
48
+ function CouponEditorInternal({
49
+ coupon,
50
+ propagateChanges,
51
+ onSuccess,
52
+ trigger,
53
+ forceShow,
54
+ onClose,
55
+ dialogOpen,
56
+ onDialogOpenChange,
57
+ }: CouponEditorProps) {
58
+ const t = useTranslations();
59
+ const fmt = useBillingFormatOptions();
60
+ const router = useI18nRouter();
61
+ const { createCoupon, renameCoupon } = useCouponContext();
62
+ const isEdit = !!coupon;
63
+
64
+ // Mirrors StripeCouponPostAttributesDTO. Edit mode is a rename: Stripe allows
65
+ // changing only a coupon's name after creation, so the schema is the name alone.
66
+ const formSchema = useMemo(() => {
67
+ const name = z.string().min(1, { message: t("billing.admin.coupons.errors.name") });
68
+ if (isEdit) return z.object({ id: z.string(), name });
69
+
70
+ return z
71
+ .object({
72
+ id: z.uuidv4(),
73
+ name,
74
+ stripeCouponId: z.string().optional(),
75
+ discountType: z.enum(["percent_off", "amount_off"]),
76
+ percentOff: optionalDecimal(),
77
+ amountOff: optionalDecimal(),
78
+ currency: z.string().min(1),
79
+ duration: z.enum(["once", "repeating", "forever"]),
80
+ durationInMonths: optionalPositiveInt(),
81
+ products: z.array(z.object({ id: z.string(), name: z.string().optional() })),
82
+ redeemBy: optionalDate(),
83
+ maxRedemptions: optionalPositiveInt(),
84
+ firstCode: couponCodeSchema(t),
85
+ })
86
+ .superRefine((values, ctx) => {
87
+ if (
88
+ values.discountType === "percent_off" &&
89
+ (values.percentOff === undefined || values.percentOff < 0.01 || values.percentOff > 100)
90
+ )
91
+ ctx.addIssue({
92
+ code: "custom",
93
+ path: ["percentOff"],
94
+ message: t("billing.admin.coupons.errors.percentOff"),
95
+ });
96
+ if (values.discountType === "amount_off" && (values.amountOff === undefined || values.amountOff <= 0))
97
+ ctx.addIssue({
98
+ code: "custom",
99
+ path: ["amountOff"],
100
+ message: t("billing.admin.coupons.errors.amountOff"),
101
+ });
102
+ if (values.duration === "repeating" && values.durationInMonths === undefined)
103
+ ctx.addIssue({
104
+ code: "custom",
105
+ path: ["durationInMonths"],
106
+ message: t("billing.admin.coupons.errors.durationInMonths"),
107
+ });
108
+ });
109
+ }, [t, isEdit]);
110
+
111
+ const getDefaultValues = useCallback(
112
+ (): any =>
113
+ coupon
114
+ ? { id: coupon.id, name: coupon.name }
115
+ : {
116
+ id: v4(),
117
+ name: "",
118
+ stripeCouponId: "",
119
+ discountType: "percent_off",
120
+ percentOff: "",
121
+ amountOff: "",
122
+ currency: "eur",
123
+ duration: "forever",
124
+ // "" not undefined for every FormInput: see couponCodeDefaults.
125
+ durationInMonths: "",
126
+ products: [],
127
+ redeemBy: undefined,
128
+ maxRedemptions: "",
129
+ firstCode: couponCodeDefaults(),
130
+ },
131
+ [coupon],
132
+ );
133
+
134
+ const form = useForm<any>({
135
+ resolver: zodResolver(formSchema as any) as any,
136
+ defaultValues: getDefaultValues(),
137
+ });
138
+
139
+ const watchDiscountType = form.watch("discountType");
140
+ const watchDuration = form.watch("duration");
141
+
142
+ const discountTypeOptions = [
143
+ { id: "percent_off", text: t("billing.admin.coupons.type.percent_off") },
144
+ { id: "amount_off", text: t("billing.admin.coupons.type.amount_off") },
145
+ ];
146
+
147
+ const durationOptions = [
148
+ { id: "once", text: t("billing.admin.coupons.duration.once") },
149
+ { id: "repeating", text: t("billing.admin.coupons.duration.repeating") },
150
+ { id: "forever", text: t("billing.admin.coupons.duration.forever") },
151
+ ];
152
+
153
+ return (
154
+ <EditorSheet
155
+ form={form}
156
+ entityType={t("billing.admin.coupons.entity")}
157
+ entityName={coupon?.name}
158
+ isEdit={isEdit}
159
+ module={Modules.StripeCoupon}
160
+ size="lg"
161
+ propagateChanges={propagateChanges}
162
+ onSuccess={onSuccess}
163
+ onNavigate={(url) => router.push(url)}
164
+ onSubmit={async (values: any) => {
165
+ if (isEdit) return await renameCoupon({ id: coupon.id, name: values.name });
166
+
167
+ const payload: StripeCouponInput = {
168
+ id: values.id,
169
+ name: values.name,
170
+ discountType: values.discountType,
171
+ duration: values.duration,
172
+ firstCode: toCouponCodeInput(values.firstCode),
173
+ };
174
+ if (values.stripeCouponId?.trim()) payload.stripeCouponId = values.stripeCouponId.trim();
175
+ if (values.discountType === "percent_off") payload.percentOff = values.percentOff;
176
+ else {
177
+ payload.amountOff = Math.round(values.amountOff * 100);
178
+ payload.currency = values.currency;
179
+ }
180
+ if (values.duration === "repeating") payload.durationInMonths = values.durationInMonths;
181
+ if (values.products.length > 0) payload.productIds = values.products.map((p: { id: string }) => p.id);
182
+ if (values.redeemBy) payload.redeemBy = values.redeemBy;
183
+ if (values.maxRedemptions !== undefined) payload.maxRedemptions = values.maxRedemptions;
184
+
185
+ return await createCoupon(payload);
186
+ }}
187
+ onReset={getDefaultValues}
188
+ trigger={trigger}
189
+ forceShow={forceShow}
190
+ onClose={onClose}
191
+ dialogOpen={dialogOpen}
192
+ onDialogOpenChange={onDialogOpenChange}
193
+ >
194
+ <div className="flex w-full flex-col gap-y-4">
195
+ <FormInput
196
+ form={form}
197
+ id="name"
198
+ name={t("billing.admin.coupons.fields.name")}
199
+ placeholder={t("billing.admin.coupons.placeholders.name")}
200
+ isRequired
201
+ />
202
+
203
+ {isEdit ? (
204
+ <>
205
+ <div className="grid grid-cols-1 gap-4 md:grid-cols-2">
206
+ <DetailField
207
+ label={t("billing.admin.coupons.fields.discount")}
208
+ value={formatCouponDiscount(coupon, t, fmt)}
209
+ />
210
+ <DetailField
211
+ label={t("billing.admin.coupons.fields.redeemBy")}
212
+ value={coupon.redeemBy ? formatDate(coupon.redeemBy, fmt) : "—"}
213
+ />
214
+ <DetailField
215
+ label={t("billing.admin.coupons.fields.maxRedemptions")}
216
+ value={
217
+ <span className="tabular-nums">
218
+ {coupon.maxRedemptions ?? t("billing.admin.coupons.places.no_limit")}
219
+ </span>
220
+ }
221
+ />
222
+ <DetailField
223
+ label={t("billing.admin.coupons.fields.stripeId")}
224
+ value={<span className="tabular-nums">{coupon.stripeCouponId}</span>}
225
+ />
226
+ </div>
227
+ {/* Typography role 10 (helper text). */}
228
+ <p className="text-muted-foreground text-xs/relaxed">{t("billing.admin.coupons.fixed_notice")}</p>
229
+ </>
230
+ ) : (
231
+ <>
232
+ <FormInput
233
+ form={form}
234
+ id="stripeCouponId"
235
+ name={t("billing.admin.coupons.fields.stripeCouponId")}
236
+ placeholder={t("billing.admin.coupons.placeholders.stripeCouponId")}
237
+ />
238
+
239
+ <FormSelect
240
+ form={form}
241
+ id="discountType"
242
+ name={t("billing.admin.coupons.fields.discountType")}
243
+ values={discountTypeOptions}
244
+ />
245
+
246
+ {watchDiscountType === "percent_off" ? (
247
+ <FormInput
248
+ form={form}
249
+ id="percentOff"
250
+ type="decimal"
251
+ name={t("billing.admin.coupons.fields.percentOff")}
252
+ placeholder={t("billing.admin.coupons.placeholders.percentOff")}
253
+ isRequired
254
+ />
255
+ ) : (
256
+ <div className="grid grid-cols-2 gap-x-4">
257
+ {/* type="decimal", NOT type="currency": the currency variant hardcodes
258
+ a Euro symbol (FormInput.tsx:105-106), which contradicts the
259
+ currency selector sitting next to it. */}
260
+ <FormInput
261
+ form={form}
262
+ id="amountOff"
263
+ type="decimal"
264
+ name={t("billing.admin.coupons.fields.amountOff")}
265
+ placeholder={t("billing.admin.coupons.placeholders.amountOff")}
266
+ isRequired
267
+ />
268
+ <FormSelect
269
+ form={form}
270
+ id="currency"
271
+ name={t("billing.admin.coupons.fields.currency")}
272
+ values={couponCurrencyOptions}
273
+ isRequired
274
+ />
275
+ </div>
276
+ )}
277
+
278
+ <FormSelect
279
+ form={form}
280
+ id="duration"
281
+ name={t("billing.admin.coupons.fields.duration")}
282
+ values={durationOptions}
283
+ />
284
+
285
+ {watchDuration === "repeating" && (
286
+ <FormInput
287
+ form={form}
288
+ id="durationInMonths"
289
+ type="number"
290
+ name={t("billing.admin.coupons.fields.durationInMonths")}
291
+ placeholder={t("billing.admin.coupons.placeholders.durationInMonths")}
292
+ isRequired
293
+ />
294
+ )}
295
+
296
+ <EntityMultiSelector<StripeProductInterface>
297
+ id="products"
298
+ form={form}
299
+ label={t("billing.admin.coupons.fields.products")}
300
+ description={t("billing.admin.coupons.help.products")}
301
+ retriever={(params) => StripeProductService.listProducts(params)}
302
+ module={Modules.StripeProduct}
303
+ getLabel={(product) => product.name}
304
+ />
305
+
306
+ <FormDateTime
307
+ form={form}
308
+ id="redeemBy"
309
+ name={t("billing.admin.coupons.fields.redeemBy")}
310
+ minDate={new Date()}
311
+ allowEmpty
312
+ />
313
+
314
+ <FormInput
315
+ form={form}
316
+ id="maxRedemptions"
317
+ type="number"
318
+ name={t("billing.admin.coupons.fields.maxRedemptions")}
319
+ placeholder={t("billing.admin.coupons.placeholders.maxRedemptions")}
320
+ />
321
+
322
+ <SectionHeader>{t("billing.admin.coupons.first_code")}</SectionHeader>
323
+ <CouponCodeFields form={form} prefix="firstCode." />
324
+ </>
325
+ )}
326
+ </div>
327
+ </EditorSheet>
328
+ );
329
+ }
330
+
331
+ export default function CouponEditor(props: CouponEditorProps) {
332
+ return <CouponEditorInternal {...props} />;
333
+ }
@@ -0,0 +1,45 @@
1
+ "use client";
2
+
3
+ import { RefreshCwIcon } from "lucide-react";
4
+ import { useTranslations } from "next-intl";
5
+ import { useState } from "react";
6
+ import { errorToast } from "../../../../../components/errors";
7
+ import { Button } from "../../../../../shadcnui";
8
+ import { useCouponContext } from "../../../contexts/CouponContext";
9
+ import { StripeCouponInterface } from "../../data/stripe-coupon.interface";
10
+
11
+ type CouponRefresherProps = {
12
+ coupon: StripeCouponInterface;
13
+ };
14
+
15
+ /**
16
+ * Re-reads the coupon and its codes from Stripe and writes the usage counts to
17
+ * our copy. Counts go stale only for subscriptions made outside our endpoints
18
+ * (the Stripe dashboard); this button fixes that.
19
+ */
20
+ export function CouponRefresher({ coupon }: CouponRefresherProps) {
21
+ const t = useTranslations();
22
+ const { refreshCounts } = useCouponContext();
23
+ const [pending, setPending] = useState<boolean>(false);
24
+
25
+ return (
26
+ <Button
27
+ variant="outline"
28
+ size="sm"
29
+ disabled={pending}
30
+ onClick={async () => {
31
+ setPending(true);
32
+ try {
33
+ await refreshCounts(coupon.id);
34
+ } catch (error) {
35
+ errorToast({ title: t("billing.admin.coupons.refresh.title"), error });
36
+ } finally {
37
+ setPending(false);
38
+ }
39
+ }}
40
+ >
41
+ <RefreshCwIcon />
42
+ {t("billing.admin.coupons.refresh.title")}
43
+ </Button>
44
+ );
45
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./CouponCodeFields";
2
+ export * from "./CouponRefresher";
3
+ export { default as CouponEditor } from "./CouponEditor";
4
+ export type { CouponEditorProps } from "./CouponEditor";
5
+ export { default as CouponCodeEditor } from "./CouponCodeEditor";
6
+ export type { CouponCodeEditorProps } from "./CouponCodeEditor";
@@ -0,0 +1,5 @@
1
+ export * from "./containers";
2
+ export * from "./details";
3
+ export * from "./forms";
4
+ export * from "./lists";
5
+ export * from "./widgets";
@@ -0,0 +1,61 @@
1
+ "use client";
2
+
3
+ import { TicketIcon } from "lucide-react";
4
+ import { useTranslations } from "next-intl";
5
+ import { ReactNode, useEffect } from "react";
6
+ import { ContentListTable } from "../../../../../components";
7
+ import { Modules } from "../../../../../core";
8
+ import { DataListRetriever, useDataListRetriever } from "../../../../../hooks";
9
+ import { EmptyState } from "../../../../../shadcnui";
10
+ import { useCouponContext } from "../../../contexts/CouponContext";
11
+ import { StripeCouponCodeFields } from "../../data/stripe-coupon-code.fields";
12
+ import { StripeCouponCodeInterface } from "../../data/stripe-coupon-code.interface";
13
+ import { StripeCouponCodeService } from "../../data/stripe-coupon-code.service";
14
+ import CouponCodeEditor from "../forms/CouponCodeEditor";
15
+
16
+ type CouponCodesListProps = {
17
+ couponId: string;
18
+ };
19
+
20
+ export function CouponCodesList({ couponId }: CouponCodesListProps) {
21
+ const t = useTranslations();
22
+ const { catalogueVersion } = useCouponContext();
23
+
24
+ const data: DataListRetriever<StripeCouponCodeInterface> = useDataListRetriever({
25
+ retriever: (params) => StripeCouponCodeService.listByCoupon({ ...params, couponId }),
26
+ retrieverParams: {},
27
+ module: Modules.StripeCouponCode,
28
+ });
29
+
30
+ // Switching "Apply automatically" on one code clears it on the others in the
31
+ // same server write, so every flag change re-reads the whole list.
32
+ useEffect(() => {
33
+ if (catalogueVersion > 0) void data.refresh();
34
+ }, [catalogueVersion]);
35
+
36
+ const functions: ReactNode[] = [<CouponCodeEditor key="create-code" couponId={couponId} />];
37
+
38
+ return (
39
+ <ContentListTable
40
+ data={data}
41
+ fields={[
42
+ StripeCouponCodeFields.code,
43
+ StripeCouponCodeFields.places,
44
+ StripeCouponCodeFields.expiresAt,
45
+ StripeCouponCodeFields.automatic,
46
+ StripeCouponCodeFields.status,
47
+ StripeCouponCodeFields.actions,
48
+ ]}
49
+ tableGeneratorType={Modules.StripeCouponCode}
50
+ functions={functions}
51
+ title={t("billing.admin.coupons.codes.title")}
52
+ emptyState={
53
+ <EmptyState
54
+ icon={TicketIcon}
55
+ title={t("billing.admin.coupons.codes.empty.title")}
56
+ description={t("billing.admin.coupons.codes.empty.description")}
57
+ />
58
+ }
59
+ />
60
+ );
61
+ }
@@ -0,0 +1,67 @@
1
+ "use client";
2
+
3
+ import { TicketPercentIcon } from "lucide-react";
4
+ import { useTranslations } from "next-intl";
5
+ import { ReactNode, useEffect } from "react";
6
+ import { ContentListTable } from "../../../../../components";
7
+ import { Modules } from "../../../../../core";
8
+ import { DataListRetriever, useDataListRetriever } from "../../../../../hooks";
9
+ import { EmptyState } from "../../../../../shadcnui";
10
+ import { useCouponContext } from "../../../contexts/CouponContext";
11
+ import { StripeCouponFields } from "../../data/stripe-coupon.fields";
12
+ import { StripeCouponInterface } from "../../data/stripe-coupon.interface";
13
+ import { StripeCouponService } from "../../data/stripe-coupon.service";
14
+ import CouponEditor from "../forms/CouponEditor";
15
+
16
+ type CouponsListProps = {
17
+ /**
18
+ * Pass this whenever the list is the page's own content inside a
19
+ * `RoundPageContainer fullWidth`. Without it ContentListTable wraps itself in
20
+ * `rounded-md border`, drawing a second bordered card inside the page's
21
+ * rounded shell.
22
+ */
23
+ fullWidth?: boolean;
24
+ };
25
+
26
+ export function CouponsList({ fullWidth }: CouponsListProps = {}) {
27
+ const t = useTranslations();
28
+ const { catalogueVersion } = useCouponContext();
29
+
30
+ const data: DataListRetriever<StripeCouponInterface> = useDataListRetriever({
31
+ retriever: (params) => StripeCouponService.listCoupons(params),
32
+ retrieverParams: {},
33
+ module: Modules.StripeCoupon,
34
+ });
35
+
36
+ // Renames and code changes happen on the detail page, so the list has to
37
+ // learn about them through the provider's counter rather than a direct callback.
38
+ useEffect(() => {
39
+ if (catalogueVersion > 0) void data.refresh();
40
+ }, [catalogueVersion]);
41
+
42
+ const functions: ReactNode[] = [<CouponEditor key="create-coupon" />];
43
+
44
+ return (
45
+ <ContentListTable
46
+ data={data}
47
+ fields={[
48
+ StripeCouponFields.name,
49
+ StripeCouponFields.discount,
50
+ StripeCouponFields.places,
51
+ StripeCouponFields.codes,
52
+ StripeCouponFields.status,
53
+ ]}
54
+ tableGeneratorType={Modules.StripeCoupon}
55
+ functions={functions}
56
+ fullWidth={fullWidth}
57
+ title={t("billing.admin.coupons.title")}
58
+ emptyState={
59
+ <EmptyState
60
+ icon={TicketPercentIcon}
61
+ title={t("billing.admin.coupons.empty.title")}
62
+ description={t("billing.admin.coupons.empty.description")}
63
+ />
64
+ }
65
+ />
66
+ );
67
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./CouponsList";
2
+ export * from "./CouponCodesList";
@@ -0,0 +1,80 @@
1
+ "use client";
2
+
3
+ import { PowerIcon, RotateCcwIcon } from "lucide-react";
4
+ import { useTranslations } from "next-intl";
5
+ import { useState } from "react";
6
+ import { errorToast } from "../../../../../components/errors";
7
+ import { Button, ConfirmDialog, Switch } from "../../../../../shadcnui";
8
+ import { useCouponContext } from "../../../contexts/CouponContext";
9
+ import { StripeCouponCodeInterface } from "../../data/stripe-coupon-code.interface";
10
+ import type { CouponCodeStatus } from "../../hooks/useStripeCouponCodeTableStructure";
11
+
12
+ type CouponCodeActionsProps = {
13
+ code: StripeCouponCodeInterface;
14
+ status: CouponCodeStatus;
15
+ };
16
+
17
+ /**
18
+ * Row controls of a code: the "Apply automatically" switch and the deactivate /
19
+ * reactivate button. The button sits behind ConfirmDialog, which is controlled
20
+ * and trigger-less by design — the caller owns the trigger button, so none of
21
+ * Base UI's trigger-composition pitfalls apply (no asChild, no nested <button>).
22
+ */
23
+ export function CouponCodeActions({ code, status }: CouponCodeActionsProps) {
24
+ const t = useTranslations();
25
+ const { updateCodeFlags } = useCouponContext();
26
+ const [open, setOpen] = useState<boolean>(false);
27
+ const [pending, setPending] = useState<boolean>(false);
28
+
29
+ // An expired or used-up code stays dead: Stripe never revives it.
30
+ const isDead = status === "expired" || status === "used_up";
31
+ const isInactive = !code.active;
32
+ const scope = isInactive ? "reactivate" : "deactivate";
33
+
34
+ return (
35
+ <div className="flex items-center justify-end gap-x-4">
36
+ <label className="flex items-center gap-x-2 text-xs">
37
+ <Switch
38
+ checked={code.automatic}
39
+ // Switching off is always allowed; switching on only for a live code.
40
+ disabled={pending || (!code.automatic && status !== "active")}
41
+ onCheckedChange={async (checked) => {
42
+ setPending(true);
43
+ try {
44
+ // The server clears the flag on every other code in the same
45
+ // write; the context's refreshCatalogue re-reads the list so the
46
+ // other row turns off too.
47
+ await updateCodeFlags({ id: code.id, automatic: checked });
48
+ } catch (error) {
49
+ errorToast({ title: t("billing.admin.coupons.fields.automatic"), error });
50
+ } finally {
51
+ setPending(false);
52
+ }
53
+ }}
54
+ />
55
+ {t("billing.admin.coupons.fields.automatic")}
56
+ </label>
57
+ <Button variant="outline" size="sm" disabled={isDead} onClick={() => setOpen(true)}>
58
+ {isInactive ? <RotateCcwIcon /> : <PowerIcon />}
59
+ {t(`billing.admin.coupons.codes.${scope}.confirm`)}
60
+ </Button>
61
+ <ConfirmDialog
62
+ open={open}
63
+ onOpenChange={setOpen}
64
+ title={t(`billing.admin.coupons.codes.${scope}.title`)}
65
+ description={t(`billing.admin.coupons.codes.${scope}.description`, { code: code.code })}
66
+ confirmLabel={t(`billing.admin.coupons.codes.${scope}.confirm`)}
67
+ cancelLabel={t("ui.buttons.cancel")}
68
+ destructive={!isInactive}
69
+ onConfirm={async () => {
70
+ try {
71
+ await updateCodeFlags({ id: code.id, active: isInactive });
72
+ } catch (error) {
73
+ errorToast({ title: t(`billing.admin.coupons.codes.${scope}.title`), error });
74
+ throw error; // keeps the dialog open — ConfirmDialog swallows rejections
75
+ }
76
+ }}
77
+ />
78
+ </div>
79
+ );
80
+ }
@@ -0,0 +1 @@
1
+ export * from "./CouponCodeActions";