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 new file mode 100644 index 00000000..a9326a13 --- /dev/null +++ b/packages/backend/src/graphql/queries/get-billing-and-usage.ee.ts @@ -0,0 +1,53 @@ +import Context from '../../types/express/context'; +import Billing from '../../helpers/billing/index.ee'; + +type Subscription = { + monthlyQuota: string; + status: string; + nextBillDate: string; + nextBillAmount: string; + updateUrl: string; + cancelUrl: string; +}; + +const getBillingAndUsage = async ( + _parent: unknown, + _params: unknown, + context: Context +) => { + const persistedSubscription = await context.currentUser.$relatedQuery( + 'subscription' + ); + + let subscription: Subscription; + + if (persistedSubscription) { + const currentPlan = Billing.paddlePlans.find( + (plan) => plan.productId === persistedSubscription.paddlePlanId + ); + + subscription = { + monthlyQuota: currentPlan.limit, + status: persistedSubscription.status, + nextBillDate: persistedSubscription.nextBillDate, + nextBillAmount: '€' + persistedSubscription.nextBillAmount, + updateUrl: persistedSubscription.updateUrl, + cancelUrl: persistedSubscription.cancelUrl, + }; + } else { + subscription = { + monthlyQuota: 'Free trial', + status: null, + nextBillDate: '---', + nextBillAmount: '---', + updateUrl: null, + cancelUrl: null, + }; + } + + return { + subscription, + }; +}; + +export default getBillingAndUsage; diff --git a/packages/backend/src/graphql/query-resolvers.ts b/packages/backend/src/graphql/query-resolvers.ts index 1c16b8d0..6facb01f 100644 --- a/packages/backend/src/graphql/query-resolvers.ts +++ b/packages/backend/src/graphql/query-resolvers.ts @@ -14,6 +14,7 @@ import getCurrentUser from './queries/get-current-user'; import getUsageData from './queries/get-usage-data.ee'; import getPaymentPlans from './queries/get-payment-plans.ee'; import getPaddleInfo from './queries/get-paddle-info.ee'; +import getBillingAndUsage from './queries/get-billing-and-usage.ee'; import getAutomatischInfo from './queries/get-automatisch-info'; import healthcheck from './queries/healthcheck'; @@ -34,6 +35,7 @@ const queryResolvers = { getUsageData, getPaymentPlans, getPaddleInfo, + getBillingAndUsage, getAutomatischInfo, healthcheck, }; diff --git a/packages/backend/src/graphql/schema.graphql b/packages/backend/src/graphql/schema.graphql index 502cbdae..3a41fad1 100644 --- a/packages/backend/src/graphql/schema.graphql +++ b/packages/backend/src/graphql/schema.graphql @@ -37,6 +37,7 @@ type Query { getUsageData: GetUsageData getPaymentPlans: [PaymentPlan] getPaddleInfo: GetPaddleInfo + getBillingAndUsage: GetBillingAndUsage getAutomatischInfo: GetAutomatischInfo healthcheck: AppHealth } @@ -472,6 +473,19 @@ type GetAutomatischInfo { isCloud: Boolean } +type GetBillingAndUsage { + subscription: Subscription +} + +type Subscription { + monthlyQuota: String + updateUrl: String + cancelUrl: String + status: String + nextBillAmount: String + nextBillDate: String +} + type GetUsageData { name: String allowedTaskCount: Int diff --git a/packages/backend/src/models/subscription.ee.ts b/packages/backend/src/models/subscription.ee.ts index 8f06d802..5cadc24b 100644 --- a/packages/backend/src/models/subscription.ee.ts +++ b/packages/backend/src/models/subscription.ee.ts @@ -47,7 +47,7 @@ class Subscription extends Base { relation: Base.BelongsToOneRelation, modelClass: User, join: { - from: 'usage_data.user_id', + from: 'subscription.user_id', to: 'users.id', }, }, diff --git a/packages/backend/src/models/user.ts b/packages/backend/src/models/user.ts index bac17b40..0fd1ed2c 100644 --- a/packages/backend/src/models/user.ts +++ b/packages/backend/src/models/user.ts @@ -10,6 +10,7 @@ import bcrypt from 'bcrypt'; import crypto from 'crypto'; import PaymentPlan from './payment-plan.ee'; import UsageData from './usage-data.ee'; +import Subscription from './subscription.ee'; class User extends Base { id!: string; @@ -26,6 +27,7 @@ class User extends Base { executions?: Execution[]; paymentPlan?: PaymentPlan; usageData?: UsageData; + subscription?: Subscription; static tableName = 'users'; @@ -99,6 +101,14 @@ class User extends Base { to: 'users.id', }, }, + subscription: { + relation: Base.HasOneRelation, + modelClass: Subscription, + join: { + from: 'subscriptions.user_id', + to: 'users.id', + }, + }, }); login(password: string) {