@greatapps/common 1.1.751 → 1.1.752
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/layouts/AiCreditsOfferGate.mjs +105 -0
- package/dist/components/layouts/AiCreditsOfferGate.mjs.map +1 -0
- package/dist/components/modals/promo/AiCreditsOfferModal/AiCreditsOfferModal.mjs +276 -0
- package/dist/components/modals/promo/AiCreditsOfferModal/AiCreditsOfferModal.mjs.map +1 -0
- package/dist/components/modals/promo/AiCreditsOfferModal/components/LoseOfferDialog.mjs +55 -0
- package/dist/components/modals/promo/AiCreditsOfferModal/components/LoseOfferDialog.mjs.map +1 -0
- package/dist/components/modals/promo/AiCreditsOfferModal/components/OfferIllustration.mjs +78 -0
- package/dist/components/modals/promo/AiCreditsOfferModal/components/OfferIllustration.mjs.map +1 -0
- package/dist/components/modals/promo/AiCreditsOfferModal/components/OfferPaymentField.mjs +60 -0
- package/dist/components/modals/promo/AiCreditsOfferModal/components/OfferPaymentField.mjs.map +1 -0
- package/dist/components/modals/promo/AiCreditsOfferModal/index.mjs +5 -0
- package/dist/components/modals/promo/AiCreditsOfferModal/index.mjs.map +1 -0
- package/dist/components/ui/form/ComboboxField.mjs +1 -0
- package/dist/components/ui/form/ComboboxField.mjs.map +1 -1
- package/dist/handlers.mjs +5 -1
- package/dist/handlers.mjs.map +1 -1
- package/dist/i18n/messages/en-us.mjs +32 -0
- package/dist/i18n/messages/en-us.mjs.map +1 -1
- package/dist/i18n/messages/es-es.mjs +32 -0
- package/dist/i18n/messages/es-es.mjs.map +1 -1
- package/dist/i18n/messages/pt-br.mjs +32 -0
- package/dist/i18n/messages/pt-br.mjs.map +1 -1
- package/dist/index.mjs +73 -43
- package/dist/index.mjs.map +1 -1
- package/dist/modules/auth/utils/get-user-context.mjs +2 -1
- package/dist/modules/auth/utils/get-user-context.mjs.map +1 -1
- package/dist/modules/cards/hooks/cards.hook.mjs +3 -2
- package/dist/modules/cards/hooks/cards.hook.mjs.map +1 -1
- package/dist/modules/ia-credits/constants/ai-credits-offer.constants.mjs +28 -0
- package/dist/modules/ia-credits/constants/ai-credits-offer.constants.mjs.map +1 -0
- package/dist/modules/ia-credits/handlers/get-offer-eligibility.handler.mjs +19 -0
- package/dist/modules/ia-credits/handlers/get-offer-eligibility.handler.mjs.map +1 -0
- package/dist/modules/ia-credits/handlers/purchase-ia-credits.handler.mjs +33 -0
- package/dist/modules/ia-credits/handlers/purchase-ia-credits.handler.mjs.map +1 -0
- package/dist/modules/ia-credits/hooks/purchase-ia-credits.hook.mjs +29 -0
- package/dist/modules/ia-credits/hooks/purchase-ia-credits.hook.mjs.map +1 -0
- package/dist/modules/ia-credits/hooks/use-offer-eligibility.hook.mjs +18 -0
- package/dist/modules/ia-credits/hooks/use-offer-eligibility.hook.mjs.map +1 -0
- package/dist/modules/ia-credits/services/ia-credits.service.mjs +67 -0
- package/dist/modules/ia-credits/services/ia-credits.service.mjs.map +1 -1
- package/dist/modules/ia-credits/types/ai-credits-offer.type.mjs +34 -0
- package/dist/modules/ia-credits/types/ai-credits-offer.type.mjs.map +1 -0
- package/dist/modules/ia-credits/utils/offer-stage-cookie.mjs +43 -0
- package/dist/modules/ia-credits/utils/offer-stage-cookie.mjs.map +1 -0
- package/dist/modules/plans/services/plans.service.mjs +2 -2
- package/dist/modules/plans/services/plans.service.mjs.map +1 -1
- package/dist/modules/plans/types/plan.type.mjs.map +1 -1
- package/dist/modules/plans/utils/map-api-plan-to-ui.mjs +0 -1
- package/dist/modules/plans/utils/map-api-plan-to-ui.mjs.map +1 -1
- package/dist/modules/subscriptions/types/calculate-subscription.type.mjs +0 -8
- package/dist/modules/subscriptions/types/calculate-subscription.type.mjs.map +1 -1
- package/dist/store/useAiCreditsOfferModal.mjs +14 -0
- package/dist/store/useAiCreditsOfferModal.mjs.map +1 -0
- package/package.json +3 -1
- package/src/components/layouts/AiCreditsOfferGate.tsx +199 -0
- package/src/components/modals/promo/AiCreditsOfferModal/AiCreditsOfferModal.tsx +401 -0
- package/src/components/modals/promo/AiCreditsOfferModal/components/LoseOfferDialog.tsx +97 -0
- package/src/components/modals/promo/AiCreditsOfferModal/components/OfferIllustration.tsx +121 -0
- package/src/components/modals/promo/AiCreditsOfferModal/components/OfferPaymentField.tsx +91 -0
- package/src/components/modals/promo/AiCreditsOfferModal/index.ts +1 -0
- package/src/components/ui/form/ComboboxField.tsx +6 -0
- package/src/handlers.ts +2 -0
- package/src/i18n/messages/en-us.ts +33 -0
- package/src/i18n/messages/es-es.ts +33 -0
- package/src/i18n/messages/pt-br.ts +33 -0
- package/src/index.ts +26 -0
- package/src/modules/auth/utils/get-user-context.ts +3 -0
- package/src/modules/cards/hooks/cards.hook.ts +6 -1
- package/src/modules/ia-credits/constants/ai-credits-offer.constants.ts +41 -0
- package/src/modules/ia-credits/handlers/get-offer-eligibility.handler.ts +24 -0
- package/src/modules/ia-credits/handlers/purchase-ia-credits.handler.ts +42 -0
- package/src/modules/ia-credits/hooks/purchase-ia-credits.hook.ts +43 -0
- package/src/modules/ia-credits/hooks/use-offer-eligibility.hook.ts +28 -0
- package/src/modules/ia-credits/services/ia-credits.service.ts +92 -0
- package/src/modules/ia-credits/types/ai-credits-offer.type.ts +87 -0
- package/src/modules/ia-credits/utils/offer-stage-cookie.ts +70 -0
- package/src/modules/plans/services/plans.service.ts +3 -3
- package/src/modules/plans/types/plan.type.ts +0 -1
- package/src/modules/plans/utils/map-api-plan-to-ui.ts +0 -1
- package/src/modules/subscriptions/types/calculate-subscription.type.ts +0 -8
- package/src/store/useAiCreditsOfferModal.ts +24 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
4
|
+
import { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';
|
|
5
|
+
import { PurchaseIaCreditsResultSchema } from '../types/ai-credits-offer.type';
|
|
6
|
+
import type { PurchaseIaCreditsInput, PurchaseIaCreditsResult } from '../types/ai-credits-offer.type';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Compra de créditos de IA — avulsa comum OU a oferta de primeira compra (`offer` no input). POSTa
|
|
10
|
+
* na rota local `/api/subscriptions/:id/credits/purchase` (`createPurchaseIaCreditsHandler`), que
|
|
11
|
+
* repassa CRU pro gapps-r3-api. A resposta:
|
|
12
|
+
* - `{ status: 1, data: [{ credited }] }` — cartão pago, créditos concedidos na hora.
|
|
13
|
+
* - `{ status: 2, client_secret }` — cartão pediu 3DS (confirmar com `confirmPaymentIfRequired`
|
|
14
|
+
* e re-postar o MESMO input: a chave de idempotência do backend devolve a mesma cobrança já paga).
|
|
15
|
+
* - `{ status: 0, message }` — recusado ou inelegível.
|
|
16
|
+
*
|
|
17
|
+
* Um hook só pros dois fluxos (avulsa e oferta): quem recusa PIX na oferta é o backend, o front
|
|
18
|
+
* não precisa saber a regra.
|
|
19
|
+
*/
|
|
20
|
+
export function usePurchaseIaCredits() {
|
|
21
|
+
const queryClient = useQueryClient();
|
|
22
|
+
const iaCreditsKey = useAuthQueryKey(['ia-credits']);
|
|
23
|
+
|
|
24
|
+
return useMutation({
|
|
25
|
+
mutationFn: async (
|
|
26
|
+
input: PurchaseIaCreditsInput & { subscriptionId: number | string }
|
|
27
|
+
): Promise<PurchaseIaCreditsResult> => {
|
|
28
|
+
const response = await fetch(`/api/subscriptions/${input.subscriptionId}/credits/purchase`, {
|
|
29
|
+
method: 'POST',
|
|
30
|
+
headers: { 'Content-Type': 'application/json' },
|
|
31
|
+
body: JSON.stringify({
|
|
32
|
+
...(input.offer ? { offer: input.offer } : { quantity: input.quantity }),
|
|
33
|
+
payment_method: input.paymentMethod,
|
|
34
|
+
...(input.idCard ? { id_card: input.idCard } : {}),
|
|
35
|
+
}),
|
|
36
|
+
});
|
|
37
|
+
return PurchaseIaCreditsResultSchema.parse(await response.json());
|
|
38
|
+
},
|
|
39
|
+
onSettled: () => {
|
|
40
|
+
queryClient.invalidateQueries({ queryKey: iaCreditsKey });
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useQuery } from '@tanstack/react-query';
|
|
4
|
+
import { apiFetch } from '../../../infra/http/api-fetch';
|
|
5
|
+
import { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';
|
|
6
|
+
import { AI_CREDITS_OFFER_ELIGIBILITY_QUERY_KEY } from '../constants/ai-credits-offer.constants';
|
|
7
|
+
import type { AiCreditsOfferEligibilityResponse } from '../types/ai-credits-offer.type';
|
|
8
|
+
|
|
9
|
+
export interface UseAiCreditsOfferEligibilityOptions {
|
|
10
|
+
enabled?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Elegibilidade da oferta de primeira compra de créditos de IA, resolvida SEMPRE no servidor
|
|
15
|
+
* (`GET /api/ai-credits-offer/eligibility`) — o `AiCreditsOfferGate` trata erro/timeout como
|
|
16
|
+
* inelegível (fail closed): sem confirmação do backend, a oferta não abre.
|
|
17
|
+
*/
|
|
18
|
+
export function useAiCreditsOfferEligibility(options: UseAiCreditsOfferEligibilityOptions = {}) {
|
|
19
|
+
const queryKey = useAuthQueryKey([...AI_CREDITS_OFFER_ELIGIBILITY_QUERY_KEY]);
|
|
20
|
+
|
|
21
|
+
return useQuery({
|
|
22
|
+
queryKey,
|
|
23
|
+
queryFn: ({ signal }) =>
|
|
24
|
+
apiFetch<AiCreditsOfferEligibilityResponse>('/api/ai-credits-offer/eligibility', { signal }),
|
|
25
|
+
enabled: options.enabled ?? true,
|
|
26
|
+
staleTime: 30_000,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -3,12 +3,28 @@ import 'server-only';
|
|
|
3
3
|
import { api } from '../../../infra/api/client';
|
|
4
4
|
import { ApiError, ApiPaginatedActionResult, SuccessResult } from '../../../infra/api/types';
|
|
5
5
|
import { getUserContext } from '../../auth/utils/get-user-context';
|
|
6
|
+
import { subscriptionsService } from '../../subscriptions/services/subscriptions.service';
|
|
6
7
|
import {
|
|
7
8
|
IaCreditAddBatch,
|
|
8
9
|
IaCreditAddBatchSchema,
|
|
9
10
|
IaCreditOperation,
|
|
10
11
|
IaCreditsSummaryData,
|
|
11
12
|
} from '../types';
|
|
13
|
+
import { AI_CREDITS_FIRST_PURCHASE_OFFER } from '../constants/ai-credits-offer.constants';
|
|
14
|
+
import type {
|
|
15
|
+
AiCreditsOfferEligibility,
|
|
16
|
+
PurchaseIaCreditsInput,
|
|
17
|
+
PurchaseIaCreditsResult,
|
|
18
|
+
} from '../types/ai-credits-offer.type';
|
|
19
|
+
|
|
20
|
+
/** Shape da API pra elegibilidade — `price_cents`/`anchor_cents` viram camelCase ao sair do service. */
|
|
21
|
+
interface AiCreditsOfferEligibilityApiItem {
|
|
22
|
+
eligible: boolean;
|
|
23
|
+
reason: AiCreditsOfferEligibility['reason'];
|
|
24
|
+
credits: number;
|
|
25
|
+
price_cents: number | null;
|
|
26
|
+
anchor_cents: number | null;
|
|
27
|
+
}
|
|
12
28
|
|
|
13
29
|
class IaCreditsService {
|
|
14
30
|
async getSummaryData(
|
|
@@ -68,6 +84,82 @@ class IaCreditsService {
|
|
|
68
84
|
const raw = response.data ?? [];
|
|
69
85
|
return raw.map((item) => IaCreditAddBatchSchema.parse(item));
|
|
70
86
|
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Elegibilidade da oferta de primeira compra, pra assinatura ATIVA da conta — resolvida aqui (e
|
|
90
|
+
* não recebida por parâmetro) porque o consumidor (`createGetAiCreditsOfferEligibilityHandler`)
|
|
91
|
+
* não tem id de assinatura: a rota é `/api/ai-credits-offer/eligibility`, sem `:id`.
|
|
92
|
+
*
|
|
93
|
+
* Sem assinatura ativa a resposta já sai inelegível — nem vale bater no backend, que exige
|
|
94
|
+
* `id_subscription` na URL.
|
|
95
|
+
*/
|
|
96
|
+
async offerEligibility(): Promise<AiCreditsOfferEligibility> {
|
|
97
|
+
const { id_account } = await getUserContext();
|
|
98
|
+
const { data: subscriptions } = await subscriptionsService.listSubscriptions({ limit: 1 });
|
|
99
|
+
const subscription = subscriptions[0];
|
|
100
|
+
|
|
101
|
+
if (!subscription) {
|
|
102
|
+
return {
|
|
103
|
+
eligible: false,
|
|
104
|
+
reason: 'no_active_subscription',
|
|
105
|
+
credits: AI_CREDITS_FIRST_PURCHASE_OFFER.credits,
|
|
106
|
+
priceCents: null,
|
|
107
|
+
anchorCents: null,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const endpoint = `/accounts/${id_account}/subscriptions/${subscription.id}/items/balance/purchase-offer/${AI_CREDITS_FIRST_PURCHASE_OFFER.slug}`;
|
|
112
|
+
const response = await api.apps.get<ApiPaginatedActionResult<AiCreditsOfferEligibilityApiItem>>(
|
|
113
|
+
endpoint
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
if (response.status === 0) {
|
|
117
|
+
throw new ApiError(
|
|
118
|
+
response.message || 'Erro ao consultar elegibilidade da oferta de créditos de IA',
|
|
119
|
+
'GET_OFFER_ELIGIBILITY_FAILED',
|
|
120
|
+
400
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const raw = response.data[0];
|
|
125
|
+
if (!raw) {
|
|
126
|
+
throw new ApiError(
|
|
127
|
+
'Resposta vazia ao consultar elegibilidade da oferta de créditos de IA',
|
|
128
|
+
'GET_OFFER_ELIGIBILITY_EMPTY',
|
|
129
|
+
502
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
eligible: raw.eligible,
|
|
135
|
+
reason: raw.reason,
|
|
136
|
+
credits: raw.credits,
|
|
137
|
+
priceCents: raw.price_cents,
|
|
138
|
+
anchorCents: raw.anchor_cents,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Compra de créditos de IA — avulsa comum (`offer` ausente) OU a oferta de primeira compra
|
|
144
|
+
* (`offer: AI_CREDITS_FIRST_PURCHASE_OFFER.slug`). Devolve o envelope CRU do gapps-r3-api
|
|
145
|
+
* (`status` 1/2/0), sem reembrulhar em `SuccessResult`: quem consome é sempre o hook de compra,
|
|
146
|
+
* que já sabe interpretar esses três status (ver `usePurchaseIaCredits`).
|
|
147
|
+
*/
|
|
148
|
+
async purchase(
|
|
149
|
+
subscriptionId: number | string,
|
|
150
|
+
input: PurchaseIaCreditsInput
|
|
151
|
+
): Promise<PurchaseIaCreditsResult> {
|
|
152
|
+
const { id_account } = await getUserContext();
|
|
153
|
+
|
|
154
|
+
return api.apps.post<PurchaseIaCreditsResult>(
|
|
155
|
+
`/accounts/${id_account}/subscriptions/${subscriptionId}/items/balance/purchase`,
|
|
156
|
+
{
|
|
157
|
+
...(input.offer ? { offer: input.offer } : { quantity: input.quantity }),
|
|
158
|
+
payment_method: input.paymentMethod,
|
|
159
|
+
...(input.idCard ? { id_card: input.idCard } : {}),
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
}
|
|
71
163
|
}
|
|
72
164
|
|
|
73
165
|
export const iaCreditsService = new IaCreditsService();
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
import { PAYMENT_METHOD_CARD, PAYMENT_METHOD_PIX } from '../constants/ai-credits-offer.constants';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `reason` é sempre um código de máquina do backend — nunca renderizar como texto: o modal usa uma
|
|
6
|
+
* mensagem de erro genérica já existente no catálogo (ver `AiCreditsOfferModal`).
|
|
7
|
+
*/
|
|
8
|
+
export type AiCreditsOfferIneligibilityReason =
|
|
9
|
+
| 'not_default_wl'
|
|
10
|
+
| 'gateway_not_supported'
|
|
11
|
+
| 'no_active_subscription'
|
|
12
|
+
| 'already_charged'
|
|
13
|
+
| 'has_paid_plan'
|
|
14
|
+
| 'has_card'
|
|
15
|
+
| 'offer_consumed'
|
|
16
|
+
| 'offer_card_only'
|
|
17
|
+
| null;
|
|
18
|
+
|
|
19
|
+
export interface AiCreditsOfferEligibility {
|
|
20
|
+
eligible: boolean;
|
|
21
|
+
reason: AiCreditsOfferIneligibilityReason;
|
|
22
|
+
credits: number;
|
|
23
|
+
/** Preço promocional em centavos. `null` quando `eligible` é `false` sem cálculo de preço. */
|
|
24
|
+
priceCents: number | null;
|
|
25
|
+
/** Preço de tabela em centavos, derivado de `getAddonUnitPrice` no backend — nunca hardcoded. */
|
|
26
|
+
anchorCents: number | null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Resposta do route handler local — a elegibilidade do backend + o carimbo da sessão de login. */
|
|
30
|
+
export interface AiCreditsOfferEligibilityResponse extends AiCreditsOfferEligibility {
|
|
31
|
+
/** `iat` do JWT no momento da consulta — usado pelo gate para saber se é uma sessão nova. */
|
|
32
|
+
sessionIssuedAt: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface PurchaseIaCreditsInput {
|
|
36
|
+
/** Ignorado pelo backend quando `offer` é enviado (a oferta força a quantidade do pacote). */
|
|
37
|
+
quantity?: number;
|
|
38
|
+
paymentMethod: number;
|
|
39
|
+
idCard?: string;
|
|
40
|
+
/** Slug da oferta (`AI_CREDITS_FIRST_PURCHASE_OFFER.slug`). Ausente = compra avulsa comum. */
|
|
41
|
+
offer?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Corpo aceito pela rota local de compra (`createPurchaseIaCreditsHandler`) — validado no handler
|
|
46
|
+
* porque o JSON do request não é confiável: `payment_method` fora do union (1 = cartão, 3 = pix,
|
|
47
|
+
* únicos métodos que o gapps-r3-api reconhece) nem chega a virar chamada de backend.
|
|
48
|
+
*/
|
|
49
|
+
export const PurchaseIaCreditsRequestBodySchema = z.object({
|
|
50
|
+
quantity: z.number().optional(),
|
|
51
|
+
payment_method: z.union([z.literal(PAYMENT_METHOD_CARD), z.literal(PAYMENT_METHOD_PIX)]),
|
|
52
|
+
id_card: z.string().optional(),
|
|
53
|
+
offer: z.string().optional(),
|
|
54
|
+
});
|
|
55
|
+
export type PurchaseIaCreditsRequestBody = z.infer<typeof PurchaseIaCreditsRequestBodySchema>;
|
|
56
|
+
|
|
57
|
+
/** Envelope PIX dentro de `data`, presente só quando a compra foi feita com `payment_method: 3`. */
|
|
58
|
+
const PurchaseIaCreditsPixDataSchema = z.object({
|
|
59
|
+
correlation_id: z.string().optional(),
|
|
60
|
+
qr_code: z.string().optional(),
|
|
61
|
+
qr_code_image: z.string().optional(),
|
|
62
|
+
payment_link: z.string().optional(),
|
|
63
|
+
expires_at: z.string().optional(),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Envelope CRU do gapps-r3-api — status 1 (pago), 2 (3DS pendente) ou 0 (recusado). Devolvido
|
|
68
|
+
* sem reembrulho pelo `createPurchaseIaCreditsHandler` (ver o comentário no handler); o hook valida
|
|
69
|
+
* a resposta contra este schema em vez de confiar cegamente no `fetch`.
|
|
70
|
+
*/
|
|
71
|
+
export const PurchaseIaCreditsResultSchema = z.object({
|
|
72
|
+
status: z.number(),
|
|
73
|
+
message: z.string().optional(),
|
|
74
|
+
client_secret: z.string().optional(),
|
|
75
|
+
data: z
|
|
76
|
+
.array(
|
|
77
|
+
z.object({
|
|
78
|
+
credited: z.number().optional(),
|
|
79
|
+
invoice_id: z.string().optional(),
|
|
80
|
+
pending: z.number().optional(),
|
|
81
|
+
payment_method: z.number().optional(),
|
|
82
|
+
pix: PurchaseIaCreditsPixDataSchema.optional(),
|
|
83
|
+
})
|
|
84
|
+
)
|
|
85
|
+
.optional(),
|
|
86
|
+
});
|
|
87
|
+
export type PurchaseIaCreditsResult = z.infer<typeof PurchaseIaCreditsResultSchema>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { cookieDomainFromDomain } from '../../../utils/intl/cookie-domain';
|
|
2
|
+
import { AiCreditsOfferStage } from '../constants/ai-credits-offer.constants';
|
|
3
|
+
|
|
4
|
+
const COOKIE_NAME = 'ai-credits-offer-stage';
|
|
5
|
+
const ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;
|
|
6
|
+
|
|
7
|
+
export interface OfferStageRecord {
|
|
8
|
+
stage: AiCreditsOfferStage;
|
|
9
|
+
/** `iat` do JWT na sessão em que este estágio foi gravado — é o que diferencia "sessão nova". */
|
|
10
|
+
lastSessionIat: number;
|
|
11
|
+
/** Quantas vezes um estágio REPETÍVEL (campanha / sem crédito) já foi apresentado. */
|
|
12
|
+
repeatCount: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const EMPTY_RECORD: OfferStageRecord = {
|
|
16
|
+
stage: AiCreditsOfferStage.NONE,
|
|
17
|
+
lastSessionIat: 0,
|
|
18
|
+
repeatCount: 0,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Cookie `ai-credits-offer-stage`, no domínio pai da whitelabel (`cookieDomainFromDomain` — mesmo
|
|
23
|
+
* mecanismo do cookie de locale, que por sua vez espelha o cookie de auth `greatapps`): sustenta a
|
|
24
|
+
* escada de estágios entre app/accounts/editor, 1 ano de validade.
|
|
25
|
+
*
|
|
26
|
+
* Valor: `${idAccount}:${stage}:${lastSessionIat}:${repeatCount}`. Registro de OUTRA conta
|
|
27
|
+
* (id diferente do gravado) lê como estágio zerado — last-writer-wins, aceito porque o cookie é por
|
|
28
|
+
* navegador e trocar de conta paga no mesmo device durante a janela da oferta é evento raro.
|
|
29
|
+
*/
|
|
30
|
+
export function readOfferStageCookie(idAccount: number): OfferStageRecord {
|
|
31
|
+
if (typeof document === 'undefined') return EMPTY_RECORD;
|
|
32
|
+
|
|
33
|
+
const match = new RegExp(`(?:^|;\\s*)${COOKIE_NAME}=([^;]+)`).exec(document.cookie);
|
|
34
|
+
if (!match) return EMPTY_RECORD;
|
|
35
|
+
|
|
36
|
+
const raw = decodeURIComponent(match[1]);
|
|
37
|
+
const [rawAccount, rawStage, rawIat, rawRepeat] = raw.split(':');
|
|
38
|
+
|
|
39
|
+
if (Number(rawAccount) !== idAccount) return EMPTY_RECORD;
|
|
40
|
+
|
|
41
|
+
const stage = Number(rawStage);
|
|
42
|
+
if (!(stage in AiCreditsOfferStage)) return EMPTY_RECORD;
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
stage,
|
|
46
|
+
lastSessionIat: Number(rawIat) || 0,
|
|
47
|
+
repeatCount: Number(rawRepeat) || 0,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function writeOfferStageCookie(
|
|
52
|
+
idAccount: number,
|
|
53
|
+
record: OfferStageRecord,
|
|
54
|
+
whitelabelDomain?: string | null
|
|
55
|
+
): void {
|
|
56
|
+
if (typeof document === 'undefined') return;
|
|
57
|
+
|
|
58
|
+
const value = `${idAccount}:${record.stage}:${record.lastSessionIat}:${record.repeatCount}`;
|
|
59
|
+
const domain = cookieDomainFromDomain(whitelabelDomain);
|
|
60
|
+
const parts = [
|
|
61
|
+
`${COOKIE_NAME}=${encodeURIComponent(value)}`,
|
|
62
|
+
'path=/',
|
|
63
|
+
`max-age=${ONE_YEAR_SECONDS}`,
|
|
64
|
+
'samesite=lax',
|
|
65
|
+
domain ? `domain=${domain}` : '',
|
|
66
|
+
typeof window !== 'undefined' && window.location.protocol === 'https:' ? 'secure' : '',
|
|
67
|
+
].filter(Boolean);
|
|
68
|
+
|
|
69
|
+
document.cookie = parts.join('; ');
|
|
70
|
+
}
|
|
@@ -32,8 +32,8 @@ class PlansService {
|
|
|
32
32
|
const sort = params?.sort ?? "id:ASC";
|
|
33
33
|
const active = params?.active ?? "";
|
|
34
34
|
const search = params?.search ?? "";
|
|
35
|
-
//
|
|
36
|
-
return `${key}-${idWl}-${idAccount}-${sort}-${active}-${search}-
|
|
35
|
+
// v6: descarta as entradas v5 que ficaram vivas durante a janela sem cache
|
|
36
|
+
return `${key}-${idWl}-${idAccount}-${sort}-${active}-${search}-v6`;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
/**
|
|
@@ -92,7 +92,7 @@ class PlansService {
|
|
|
92
92
|
|
|
93
93
|
async findById(idPlan: number | string): Promise<SuccessResult<Plan>> {
|
|
94
94
|
const { id_wl, id_account } = await getUserContext();
|
|
95
|
-
const cacheKey = this.buildCacheKey(`plan-${idPlan}-
|
|
95
|
+
const cacheKey = this.buildCacheKey(`plan-${idPlan}-v2`, {
|
|
96
96
|
id_wl,
|
|
97
97
|
id_account: String(id_account),
|
|
98
98
|
});
|
|
@@ -172,7 +172,6 @@ function buildUiPlan(plan: Plan, currency: CurrencyCode, translate?: TranslateFn
|
|
|
172
172
|
originalPrice: priceUnavailable ? unavailableLabel : formatCurrencyNumber(monthlyFinal, 2, currency),
|
|
173
173
|
price: priceUnavailable ? unavailableLabel : formatCurrencyNumber(monthlyFinal, 2, currency),
|
|
174
174
|
pricingByPeriod,
|
|
175
|
-
pricing: plan.pricing,
|
|
176
175
|
priceUnavailable,
|
|
177
176
|
items: currencyAwareItems,
|
|
178
177
|
};
|
|
@@ -68,10 +68,6 @@ export const CalculateSubscriptionResponseSchema = z.object({
|
|
|
68
68
|
month_original: z.number(),
|
|
69
69
|
month_original_discount: z.number().optional().default(0),
|
|
70
70
|
month_original_discount_cupom: z.number().optional().default(0),
|
|
71
|
-
// Preço "de" (âncora riscada) e badge de desconto declarado da vitrine. Ausentes
|
|
72
|
-
// quando não há ancoragem (whitelabel != 1, USD, grandfathered) — ver CONTRATO DE CAMPOS.
|
|
73
|
-
month_list: z.number().optional(),
|
|
74
|
-
month_badge_percent: z.number().optional(),
|
|
75
71
|
semmester: z.number(),
|
|
76
72
|
semmester_price: z.number().optional().default(0),
|
|
77
73
|
semmester_discount: z.number(),
|
|
@@ -79,16 +75,12 @@ export const CalculateSubscriptionResponseSchema = z.object({
|
|
|
79
75
|
semmester_original: z.number(),
|
|
80
76
|
semmester_original_discount: z.number().optional().default(0),
|
|
81
77
|
semmester_original_discount_cupom: z.number().optional().default(0),
|
|
82
|
-
semmester_list: z.number().optional(),
|
|
83
|
-
semmester_badge_percent: z.number().optional(),
|
|
84
78
|
year: z.number(),
|
|
85
79
|
year_discount: z.number(),
|
|
86
80
|
year_discount_coupom: z.number().optional().default(0),
|
|
87
81
|
year_original: z.number(),
|
|
88
82
|
year_original_discount: z.number().optional().default(0),
|
|
89
83
|
year_original_discount_cupom: z.number().optional().default(0),
|
|
90
|
-
year_list: z.number().optional(),
|
|
91
|
-
year_badge_percent: z.number().optional(),
|
|
92
84
|
plan_extras: z.array(CalculateSubscriptionPlanExtraResponseSchema),
|
|
93
85
|
target: CalculateSubscriptionTargetSchema.optional().nullable(),
|
|
94
86
|
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { useModalManager } from './useModalManager';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Oferta de primeira compra de créditos de IA. Quem abre é o `AiCreditsOfferGate` (1s depois do
|
|
6
|
+
* gatilho decidir que é a vez dela) — daí não existir payload: a oferta é sempre a mesma.
|
|
7
|
+
*
|
|
8
|
+
* Os callbacks vão em `useCallback` (as ações do zustand já são estáveis): o gate arma um
|
|
9
|
+
* `setTimeout` num efeito que tem `openModal` nas dependências, e uma identidade nova a cada render
|
|
10
|
+
* limparia o timer antes de ele disparar — a oferta nunca abriria.
|
|
11
|
+
*
|
|
12
|
+
* O fechamento usa `closeModalByKey` e não `closeModal`: o modal abre o `addCardModal` por cima (a
|
|
13
|
+
* pilha do `useModalManager` empilha), e um `closeModal` disparado com o cartão em cima derrubaria
|
|
14
|
+
* o topo da pilha, não a oferta.
|
|
15
|
+
*/
|
|
16
|
+
export const useAiCreditsOfferModal = () => {
|
|
17
|
+
const { activeModal, openModal, closeModalByKey } = useModalManager();
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
open: activeModal === 'aiCreditsOfferModal',
|
|
21
|
+
openModal: useCallback(() => openModal('aiCreditsOfferModal'), [openModal]),
|
|
22
|
+
closeModal: useCallback(() => closeModalByKey('aiCreditsOfferModal'), [closeModalByKey]),
|
|
23
|
+
};
|
|
24
|
+
};
|