@greatapps/common 1.1.716 → 1.1.717
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.
|
@@ -5,24 +5,18 @@ import { apiFetch } from "../../../infra/http/api-fetch";
|
|
|
5
5
|
import { useActiveSubscription } from "../../subscriptions/hooks/find-active-subscription.hook";
|
|
6
6
|
import { useAuthQueryKey } from "../../../hooks/useAuthQueryKey";
|
|
7
7
|
const MS_PER_DAY = 1e3 * 60 * 60 * 24;
|
|
8
|
-
const EMPTY_SUMMARY = {
|
|
9
|
-
totalCredits: 0,
|
|
10
|
-
usedCredits: 0,
|
|
11
|
-
availableCredits: 0,
|
|
12
|
-
nextExpiringCredits: null,
|
|
13
|
-
nextExpirationInDays: null
|
|
14
|
-
};
|
|
15
8
|
function startOfDay(date) {
|
|
16
9
|
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
|
|
17
10
|
}
|
|
18
11
|
function calculateSummary(data) {
|
|
19
|
-
|
|
12
|
+
const addBatches = Array.isArray(data?.addBatches) ? data.addBatches : [];
|
|
13
|
+
const availableCredits = Number(data?.availableCredits) || 0;
|
|
20
14
|
const today = startOfDay(/* @__PURE__ */ new Date());
|
|
21
15
|
let totalCredits = 0;
|
|
22
16
|
let usedCredits = 0;
|
|
23
17
|
let nextExpirationInDays = null;
|
|
24
18
|
let nextExpiringCredits = null;
|
|
25
|
-
for (const batch of
|
|
19
|
+
for (const batch of addBatches) {
|
|
26
20
|
if (batch.has_expired) continue;
|
|
27
21
|
totalCredits += batch.added_credits;
|
|
28
22
|
usedCredits += batch.consumed_credits;
|
|
@@ -37,7 +31,7 @@ function calculateSummary(data) {
|
|
|
37
31
|
return {
|
|
38
32
|
totalCredits,
|
|
39
33
|
usedCredits,
|
|
40
|
-
availableCredits: Math.max(0,
|
|
34
|
+
availableCredits: Math.max(0, availableCredits),
|
|
41
35
|
nextExpiringCredits,
|
|
42
36
|
nextExpirationInDays
|
|
43
37
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/modules/ia-credits/hooks/ia-credits.hook.ts"],"sourcesContent":["'use client';\n\nimport { useMemo } from 'react';\nimport { useQuery } from '@tanstack/react-query';\nimport { apiFetch } from '../../../infra/http/api-fetch';\nimport type { SuccessResult } from '../../../infra/api/types';\nimport type { IaCreditsSummary, IaCreditsSummaryData } from '../types';\nimport { useActiveSubscription } from '../../subscriptions/hooks/find-active-subscription.hook';\nimport { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';\n\nconst MS_PER_DAY = 1000 * 60 * 60 * 24;\n\
|
|
1
|
+
{"version":3,"sources":["../../../../src/modules/ia-credits/hooks/ia-credits.hook.ts"],"sourcesContent":["'use client';\n\nimport { useMemo } from 'react';\nimport { useQuery } from '@tanstack/react-query';\nimport { apiFetch } from '../../../infra/http/api-fetch';\nimport type { SuccessResult } from '../../../infra/api/types';\nimport type { IaCreditsSummary, IaCreditsSummaryData } from '../types';\nimport { useActiveSubscription } from '../../subscriptions/hooks/find-active-subscription.hook';\nimport { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';\n\nconst MS_PER_DAY = 1000 * 60 * 60 * 24;\n\nfunction startOfDay(date: Date): Date {\n return new Date(date.getFullYear(), date.getMonth(), date.getDate());\n}\n\nfunction calculateSummary(data: IaCreditsSummaryData | null): IaCreditsSummary {\n // Blindagem: se vier um shape inesperado (ex.: resposta/cache antigo do\n // endpoint paginado), degrada para o sumário vazio em vez de crashar.\n const addBatches = Array.isArray(data?.addBatches) ? data.addBatches : [];\n const availableCredits = Number(data?.availableCredits) || 0;\n\n const today = startOfDay(new Date());\n\n let totalCredits = 0;\n let usedCredits = 0;\n let nextExpirationInDays: number | null = null;\n let nextExpiringCredits: number | null = null;\n\n for (const batch of addBatches) {\n if (batch.has_expired) continue;\n\n totalCredits += batch.added_credits;\n usedCredits += batch.consumed_credits;\n\n if (batch.remaining_credits <= 0 || !batch.expiration_time) continue;\n\n const expirationDate = startOfDay(new Date(batch.expiration_time));\n const days = Math.max(0, Math.ceil((expirationDate.getTime() - today.getTime()) / MS_PER_DAY));\n\n if (nextExpirationInDays === null || days < nextExpirationInDays) {\n nextExpirationInDays = days;\n nextExpiringCredits = batch.remaining_credits;\n }\n }\n\n return {\n totalCredits,\n usedCredits,\n availableCredits: Math.max(0, availableCredits),\n nextExpiringCredits,\n nextExpirationInDays,\n };\n}\n\nexport interface UseIaCreditsOptions {\n refetchInterval?: number | false;\n}\n\nexport function useIaCredits(options: UseIaCreditsOptions = {}) {\n const { data: subscriptionData, isPending: isSubscriptionLoading } = useActiveSubscription();\n const subscription = subscriptionData?.data?.[0] ?? null;\n const subscriptionId = subscription?.id;\n const queryKey = useAuthQueryKey(['ia-credits', subscriptionId]);\n\n const query = useQuery({\n queryKey,\n queryFn: ({ signal }) =>\n apiFetch<SuccessResult<IaCreditsSummaryData>>(\n `/api/subscriptions/${subscriptionId}/ia-credits`,\n { signal }\n ),\n enabled: subscriptionId != null,\n refetchInterval: options.refetchInterval ?? false,\n });\n\n const summaryData = query.data?.data ?? null;\n\n const summary = useMemo(() => calculateSummary(summaryData), [summaryData]);\n\n return {\n subscription,\n summary,\n isLoading: isSubscriptionLoading || query.isPending,\n isSubscriptionLoading,\n isCreditsLoading: query.isPending,\n isError: query.isError,\n error: query.error,\n };\n}\n"],"mappings":";AAEA,SAAS,eAAe;AACxB,SAAS,gBAAgB;AACzB,SAAS,gBAAgB;AAGzB,SAAS,6BAA6B;AACtC,SAAS,uBAAuB;AAEhC,MAAM,aAAa,MAAO,KAAK,KAAK;AAEpC,SAAS,WAAW,MAAkB;AACpC,SAAO,IAAI,KAAK,KAAK,YAAY,GAAG,KAAK,SAAS,GAAG,KAAK,QAAQ,CAAC;AACrE;AAEA,SAAS,iBAAiB,MAAqD;AAG7E,QAAM,aAAa,MAAM,QAAQ,MAAM,UAAU,IAAI,KAAK,aAAa,CAAC;AACxE,QAAM,mBAAmB,OAAO,MAAM,gBAAgB,KAAK;AAE3D,QA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,WAAW,yBAAyB,MAAM;AAAA,IAC1C;AAAA,IACA,kBAAkB,MAAM;AAAA,IACxB,SAAS,MAAM;AAAA,IACf,OAAO,MAAM;AAAA,EACf;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -10,20 +10,15 @@ import { useAuthQueryKey } from '../../../hooks/useAuthQueryKey';
|
|
|
10
10
|
|
|
11
11
|
const MS_PER_DAY = 1000 * 60 * 60 * 24;
|
|
12
12
|
|
|
13
|
-
const EMPTY_SUMMARY: IaCreditsSummary = {
|
|
14
|
-
totalCredits: 0,
|
|
15
|
-
usedCredits: 0,
|
|
16
|
-
availableCredits: 0,
|
|
17
|
-
nextExpiringCredits: null,
|
|
18
|
-
nextExpirationInDays: null,
|
|
19
|
-
};
|
|
20
|
-
|
|
21
13
|
function startOfDay(date: Date): Date {
|
|
22
14
|
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
|
|
23
15
|
}
|
|
24
16
|
|
|
25
17
|
function calculateSummary(data: IaCreditsSummaryData | null): IaCreditsSummary {
|
|
26
|
-
|
|
18
|
+
// Blindagem: se vier um shape inesperado (ex.: resposta/cache antigo do
|
|
19
|
+
// endpoint paginado), degrada para o sumário vazio em vez de crashar.
|
|
20
|
+
const addBatches = Array.isArray(data?.addBatches) ? data.addBatches : [];
|
|
21
|
+
const availableCredits = Number(data?.availableCredits) || 0;
|
|
27
22
|
|
|
28
23
|
const today = startOfDay(new Date());
|
|
29
24
|
|
|
@@ -32,7 +27,7 @@ function calculateSummary(data: IaCreditsSummaryData | null): IaCreditsSummary {
|
|
|
32
27
|
let nextExpirationInDays: number | null = null;
|
|
33
28
|
let nextExpiringCredits: number | null = null;
|
|
34
29
|
|
|
35
|
-
for (const batch of
|
|
30
|
+
for (const batch of addBatches) {
|
|
36
31
|
if (batch.has_expired) continue;
|
|
37
32
|
|
|
38
33
|
totalCredits += batch.added_credits;
|
|
@@ -52,7 +47,7 @@ function calculateSummary(data: IaCreditsSummaryData | null): IaCreditsSummary {
|
|
|
52
47
|
return {
|
|
53
48
|
totalCredits,
|
|
54
49
|
usedCredits,
|
|
55
|
-
availableCredits: Math.max(0,
|
|
50
|
+
availableCredits: Math.max(0, availableCredits),
|
|
56
51
|
nextExpiringCredits,
|
|
57
52
|
nextExpirationInDays,
|
|
58
53
|
};
|