feat: Implement draft version of getBillingAndUsage query

This commit is contained in:
Faruk AYDIN
2023-03-25 00:39:49 +03:00
parent 310497a5bf
commit b5524b18cf
5 changed files with 80 additions and 1 deletions

View File

@@ -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;

View File

@@ -14,6 +14,7 @@ import getCurrentUser from './queries/get-current-user';
import getUsageData from './queries/get-usage-data.ee'; import getUsageData from './queries/get-usage-data.ee';
import getPaymentPlans from './queries/get-payment-plans.ee'; import getPaymentPlans from './queries/get-payment-plans.ee';
import getPaddleInfo from './queries/get-paddle-info.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 getAutomatischInfo from './queries/get-automatisch-info';
import healthcheck from './queries/healthcheck'; import healthcheck from './queries/healthcheck';
@@ -34,6 +35,7 @@ const queryResolvers = {
getUsageData, getUsageData,
getPaymentPlans, getPaymentPlans,
getPaddleInfo, getPaddleInfo,
getBillingAndUsage,
getAutomatischInfo, getAutomatischInfo,
healthcheck, healthcheck,
}; };

View File

@@ -37,6 +37,7 @@ type Query {
getUsageData: GetUsageData getUsageData: GetUsageData
getPaymentPlans: [PaymentPlan] getPaymentPlans: [PaymentPlan]
getPaddleInfo: GetPaddleInfo getPaddleInfo: GetPaddleInfo
getBillingAndUsage: GetBillingAndUsage
getAutomatischInfo: GetAutomatischInfo getAutomatischInfo: GetAutomatischInfo
healthcheck: AppHealth healthcheck: AppHealth
} }
@@ -472,6 +473,19 @@ type GetAutomatischInfo {
isCloud: Boolean isCloud: Boolean
} }
type GetBillingAndUsage {
subscription: Subscription
}
type Subscription {
monthlyQuota: String
updateUrl: String
cancelUrl: String
status: String
nextBillAmount: String
nextBillDate: String
}
type GetUsageData { type GetUsageData {
name: String name: String
allowedTaskCount: Int allowedTaskCount: Int

View File

@@ -47,7 +47,7 @@ class Subscription extends Base {
relation: Base.BelongsToOneRelation, relation: Base.BelongsToOneRelation,
modelClass: User, modelClass: User,
join: { join: {
from: 'usage_data.user_id', from: 'subscription.user_id',
to: 'users.id', to: 'users.id',
}, },
}, },

View File

@@ -10,6 +10,7 @@ import bcrypt from 'bcrypt';
import crypto from 'crypto'; import crypto from 'crypto';
import PaymentPlan from './payment-plan.ee'; import PaymentPlan from './payment-plan.ee';
import UsageData from './usage-data.ee'; import UsageData from './usage-data.ee';
import Subscription from './subscription.ee';
class User extends Base { class User extends Base {
id!: string; id!: string;
@@ -26,6 +27,7 @@ class User extends Base {
executions?: Execution[]; executions?: Execution[];
paymentPlan?: PaymentPlan; paymentPlan?: PaymentPlan;
usageData?: UsageData; usageData?: UsageData;
subscription?: Subscription;
static tableName = 'users'; static tableName = 'users';
@@ -99,6 +101,14 @@ class User extends Base {
to: 'users.id', to: 'users.id',
}, },
}, },
subscription: {
relation: Base.HasOneRelation,
modelClass: Subscription,
join: {
from: 'subscriptions.user_id',
to: 'users.id',
},
},
}); });
login(password: string) { login(password: string) {