Merge pull request #1023 from automatisch/refactor-billing-and-usage

refactor: getBillingAndUsage graphQL query
This commit is contained in:
Ali BARIN
2023-03-26 16:06:16 +02:00
committed by GitHub

View File

@@ -2,9 +2,10 @@ import Context from '../../types/express/context';
import Billing from '../../helpers/billing/index.ee'; import Billing from '../../helpers/billing/index.ee';
import Execution from '../../models/execution'; import Execution from '../../models/execution';
import ExecutionStep from '../../models/execution-step'; import ExecutionStep from '../../models/execution-step';
import Subscription from '../../models/subscription.ee';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
type Subscription = { type ComputedSubscription = {
monthlyQuota: string; monthlyQuota: string;
status: string; status: string;
nextBillDate: string; nextBillDate: string;
@@ -22,23 +23,35 @@ const getBillingAndUsage = async (
'subscription' 'subscription'
); );
let subscription: Subscription; const subscription: ComputedSubscription = persistedSubscription
? paidSubscription(persistedSubscription)
: freeTrialSubscription();
if (persistedSubscription) { return {
subscription,
usage: {
task: executionStepCount(context),
},
};
};
const paidSubscription = (subscription: Subscription): ComputedSubscription => {
const currentPlan = Billing.paddlePlans.find( const currentPlan = Billing.paddlePlans.find(
(plan) => plan.productId === persistedSubscription.paddlePlanId (plan) => plan.productId === subscription.paddlePlanId
); );
subscription = { return {
monthlyQuota: currentPlan.limit, monthlyQuota: currentPlan.limit,
status: persistedSubscription.status, status: subscription.status,
nextBillDate: persistedSubscription.nextBillDate, nextBillDate: subscription.nextBillDate,
nextBillAmount: '€' + persistedSubscription.nextBillAmount, nextBillAmount: '€' + subscription.nextBillAmount,
updateUrl: persistedSubscription.updateUrl, updateUrl: subscription.updateUrl,
cancelUrl: persistedSubscription.cancelUrl, cancelUrl: subscription.cancelUrl,
}; };
} else { };
subscription = {
const freeTrialSubscription = (): ComputedSubscription => {
return {
monthlyQuota: 'Free trial', monthlyQuota: 'Free trial',
status: null, status: null,
nextBillDate: '---', nextBillDate: '---',
@@ -46,16 +59,19 @@ const getBillingAndUsage = async (
updateUrl: null, updateUrl: null,
cancelUrl: null, cancelUrl: null,
}; };
} };
const executionIds = ( const executionIds = async (context: Context) => {
return (
await context.currentUser await context.currentUser
.$relatedQuery('executions') .$relatedQuery('executions')
.select('executions.id') .select('executions.id')
).map((execution: Execution) => execution.id); ).map((execution: Execution) => execution.id);
};
const executionStepCount = async (context: Context) => {
const executionStepCount = await ExecutionStep.query() const executionStepCount = await ExecutionStep.query()
.whereIn('execution_id', executionIds) .whereIn('execution_id', await executionIds(context))
.andWhere( .andWhere(
'created_at', 'created_at',
'>=', '>=',
@@ -64,12 +80,7 @@ const getBillingAndUsage = async (
.count() .count()
.first(); .first();
return { return executionStepCount.count;
subscription,
usage: {
task: executionStepCount.count,
},
};
}; };
export default getBillingAndUsage; export default getBillingAndUsage;