diff --git a/packages/twenty-front/src/generated-metadata/graphql.ts b/packages/twenty-front/src/generated-metadata/graphql.ts index 248a01549a7..94d20e35e65 100644 --- a/packages/twenty-front/src/generated-metadata/graphql.ts +++ b/packages/twenty-front/src/generated-metadata/graphql.ts @@ -6241,10 +6241,14 @@ export type GetMeteredProductsUsageQueryVariables = Exact<{ [key: string]: never export type GetMeteredProductsUsageQuery = { __typename?: 'Query', getMeteredProductsUsage: Array<{ __typename?: 'BillingMeteredProductUsage', productKey: BillingProductKey, usedCredits: number, grantedCredits: number, rolloverCredits: number, totalGrantedCredits: number, unitPriceCents: number }> }; -export type GetBillingAnalyticsQueryVariables = Exact<{ +export type BillingAnalyticsInput = { periodStart?: InputMaybe; periodEnd?: InputMaybe; userWorkspaceId?: InputMaybe; +}; + +export type GetBillingAnalyticsQueryVariables = Exact<{ + input?: InputMaybe; }>; @@ -11031,8 +11035,8 @@ export type GetMeteredProductsUsageQueryHookResult = ReturnType; export type GetMeteredProductsUsageQueryResult = Apollo.QueryResult; export const GetBillingAnalyticsDocument = gql` - query GetBillingAnalytics($periodStart: DateTime, $periodEnd: DateTime, $userWorkspaceId: String) { - getBillingAnalytics(periodStart: $periodStart, periodEnd: $periodEnd, userWorkspaceId: $userWorkspaceId) { + query GetBillingAnalytics($input: BillingAnalyticsInput) { + getBillingAnalytics(input: $input) { usageByUser { key creditsUsed diff --git a/packages/twenty-front/src/modules/billing/components/SettingsBillingAnalyticsSection.tsx b/packages/twenty-front/src/modules/billing/components/SettingsBillingAnalyticsSection.tsx index 44bfea20ffd..31478e8be5e 100644 --- a/packages/twenty-front/src/modules/billing/components/SettingsBillingAnalyticsSection.tsx +++ b/packages/twenty-front/src/modules/billing/components/SettingsBillingAnalyticsSection.tsx @@ -185,11 +185,13 @@ export const SettingsBillingAnalyticsSection = () => { const { data, loading } = useGetBillingAnalyticsQuery({ variables: { - ...(periodDates.periodStart && { - periodStart: periodDates.periodStart, - }), - ...(periodDates.periodEnd && { periodEnd: periodDates.periodEnd }), - ...(selectedUserId && { userWorkspaceId: selectedUserId }), + input: { + ...(periodDates.periodStart && { + periodStart: periodDates.periodStart, + }), + ...(periodDates.periodEnd && { periodEnd: periodDates.periodEnd }), + ...(selectedUserId && { userWorkspaceId: selectedUserId }), + }, }, }); diff --git a/packages/twenty-front/src/modules/billing/graphql/queries/getBillingAnalytics.ts b/packages/twenty-front/src/modules/billing/graphql/queries/getBillingAnalytics.ts index 3d85c5c26d4..91206efef40 100644 --- a/packages/twenty-front/src/modules/billing/graphql/queries/getBillingAnalytics.ts +++ b/packages/twenty-front/src/modules/billing/graphql/queries/getBillingAnalytics.ts @@ -1,16 +1,8 @@ import { gql } from '@apollo/client'; export const GET_BILLING_ANALYTICS = gql` - query GetBillingAnalytics( - $periodStart: DateTime - $periodEnd: DateTime - $userWorkspaceId: String - ) { - getBillingAnalytics( - periodStart: $periodStart - periodEnd: $periodEnd - userWorkspaceId: $userWorkspaceId - ) { + query GetBillingAnalytics($input: BillingAnalyticsInput) { + getBillingAnalytics(input: $input) { usageByUser { key creditsUsed diff --git a/packages/twenty-server/src/engine/core-modules/billing/billing.resolver.ts b/packages/twenty-server/src/engine/core-modules/billing/billing.resolver.ts index b9b9c960c3d..d7b007fcf53 100644 --- a/packages/twenty-server/src/engine/core-modules/billing/billing.resolver.ts +++ b/packages/twenty-server/src/engine/core-modules/billing/billing.resolver.ts @@ -336,17 +336,27 @@ export class BillingResolver { @RequireFeatureFlag(FeatureFlagKey.IS_USAGE_ANALYTICS_ENABLED) async getBillingAnalytics( @AuthWorkspace() workspace: WorkspaceEntity, - @Args({ nullable: true }) input?: BillingAnalyticsInput, + @Args('input', { nullable: true }) input?: BillingAnalyticsInput, ): Promise { - const subscription = - await this.billingSubscriptionService.getCurrentBillingSubscriptionOrThrow( - { workspaceId: workspace.id }, - ); + let defaultPeriodStart: Date; + let defaultPeriodEnd: Date; - const periodStart = - input?.periodStart ?? subscription.currentPeriodStart; - const periodEnd = - input?.periodEnd ?? subscription.currentPeriodEnd; + if (this.billingService.isBillingEnabled()) { + const subscription = + await this.billingSubscriptionService.getCurrentBillingSubscriptionOrThrow( + { workspaceId: workspace.id }, + ); + + defaultPeriodStart = subscription.currentPeriodStart; + defaultPeriodEnd = subscription.currentPeriodEnd; + } else { + defaultPeriodEnd = new Date(); + defaultPeriodStart = new Date(); + defaultPeriodStart.setDate(defaultPeriodStart.getDate() - 30); + } + + const periodStart = input?.periodStart ?? defaultPeriodStart; + const periodEnd = input?.periodEnd ?? defaultPeriodEnd; const [usageByUser, usageByResource, usageByExecutionType, timeSeries] = await Promise.all([