From f07d9dd8139d8b2a63b7f016731de8dea48f63e5 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Sat, 25 Mar 2023 02:16:17 +0300 Subject: [PATCH] feat: Add usage data to getBillingAndUsage graphQL query --- .../queries/get-billing-and-usage.ee.ts | 22 +++++++++++++++++++ packages/backend/src/graphql/schema.graphql | 5 +++++ packages/backend/src/models/execution-step.ts | 1 + packages/backend/src/models/user.ts | 1 + 4 files changed, 29 insertions(+) diff --git a/packages/backend/src/graphql/queries/get-billing-and-usage.ee.ts b/packages/backend/src/graphql/queries/get-billing-and-usage.ee.ts index a9326a13..cb4f7f71 100644 --- a/packages/backend/src/graphql/queries/get-billing-and-usage.ee.ts +++ b/packages/backend/src/graphql/queries/get-billing-and-usage.ee.ts @@ -1,5 +1,8 @@ import Context from '../../types/express/context'; import Billing from '../../helpers/billing/index.ee'; +import Execution from '../../models/execution'; +import ExecutionStep from '../../models/execution-step'; +import { DateTime } from 'luxon'; type Subscription = { monthlyQuota: string; @@ -45,8 +48,27 @@ const getBillingAndUsage = async ( }; } + const executionIds = ( + await context.currentUser + .$relatedQuery('executions') + .select('executions.id') + ).map((execution: Execution) => execution.id); + + const executionStepCount = await ExecutionStep.query() + .whereIn('execution_id', executionIds) + .andWhere( + 'created_at', + '>=', + DateTime.now().minus({ days: 30 }).toFormat('D') + ) + .count() + .first(); + return { subscription, + usage: { + task: executionStepCount.count, + }, }; }; diff --git a/packages/backend/src/graphql/schema.graphql b/packages/backend/src/graphql/schema.graphql index 3a41fad1..02443725 100644 --- a/packages/backend/src/graphql/schema.graphql +++ b/packages/backend/src/graphql/schema.graphql @@ -475,6 +475,7 @@ type GetAutomatischInfo { type GetBillingAndUsage { subscription: Subscription + usage: Usage } type Subscription { @@ -486,6 +487,10 @@ type Subscription { nextBillDate: String } +type Usage { + task: Int +} + type GetUsageData { name: String allowedTaskCount: Int diff --git a/packages/backend/src/models/execution-step.ts b/packages/backend/src/models/execution-step.ts index 035c61c8..6b52d15d 100644 --- a/packages/backend/src/models/execution-step.ts +++ b/packages/backend/src/models/execution-step.ts @@ -16,6 +16,7 @@ class ExecutionStep extends Base { status: 'success' | 'failure'; step: Step; execution?: Execution; + count?: number; static tableName = 'execution_steps'; diff --git a/packages/backend/src/models/user.ts b/packages/backend/src/models/user.ts index 0fd1ed2c..caa55c48 100644 --- a/packages/backend/src/models/user.ts +++ b/packages/backend/src/models/user.ts @@ -6,6 +6,7 @@ import Connection from './connection'; import Flow from './flow'; import Step from './step'; import Execution from './execution'; +import ExecutionStep from './execution-step'; import bcrypt from 'bcrypt'; import crypto from 'crypto'; import PaymentPlan from './payment-plan.ee';