Merge pull request #1023 from automatisch/refactor-billing-and-usage
refactor: getBillingAndUsage graphQL query
This commit is contained in:
@@ -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,40 +23,55 @@ const getBillingAndUsage = async (
|
|||||||
'subscription'
|
'subscription'
|
||||||
);
|
);
|
||||||
|
|
||||||
let subscription: Subscription;
|
const subscription: ComputedSubscription = persistedSubscription
|
||||||
|
? paidSubscription(persistedSubscription)
|
||||||
|
: freeTrialSubscription();
|
||||||
|
|
||||||
if (persistedSubscription) {
|
return {
|
||||||
const currentPlan = Billing.paddlePlans.find(
|
subscription,
|
||||||
(plan) => plan.productId === persistedSubscription.paddlePlanId
|
usage: {
|
||||||
);
|
task: executionStepCount(context),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
subscription = {
|
const paidSubscription = (subscription: Subscription): ComputedSubscription => {
|
||||||
monthlyQuota: currentPlan.limit,
|
const currentPlan = Billing.paddlePlans.find(
|
||||||
status: persistedSubscription.status,
|
(plan) => plan.productId === subscription.paddlePlanId
|
||||||
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,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const executionIds = (
|
return {
|
||||||
|
monthlyQuota: currentPlan.limit,
|
||||||
|
status: subscription.status,
|
||||||
|
nextBillDate: subscription.nextBillDate,
|
||||||
|
nextBillAmount: '€' + subscription.nextBillAmount,
|
||||||
|
updateUrl: subscription.updateUrl,
|
||||||
|
cancelUrl: subscription.cancelUrl,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const freeTrialSubscription = (): ComputedSubscription => {
|
||||||
|
return {
|
||||||
|
monthlyQuota: 'Free trial',
|
||||||
|
status: null,
|
||||||
|
nextBillDate: '---',
|
||||||
|
nextBillAmount: '---',
|
||||||
|
updateUrl: null,
|
||||||
|
cancelUrl: null,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
Reference in New Issue
Block a user