feat: Implement getInvoices graphQL query

This commit is contained in:
Faruk AYDIN
2023-03-26 02:02:36 +03:00
parent bd55b37d5f
commit 1cbf96dff1
6 changed files with 52 additions and 2 deletions

View File

@@ -59,7 +59,7 @@ const getBillingAndUsage = async (
.andWhere( .andWhere(
'created_at', 'created_at',
'>=', '>=',
DateTime.now().minus({ days: 30 }).toFormat('D') DateTime.now().minus({ days: 30 }).toISODate()
) )
.count() .count()
.first(); .first();

View File

@@ -0,0 +1,17 @@
import Context from '../../types/express/context';
import Billing from '../../helpers/billing/index.ee';
const getInvoices = async (
_parent: unknown,
_params: unknown,
context: Context
) => {
const subscription = await context.currentUser.$relatedQuery('subscription');
const invoices = await Billing.paddleClient.getInvoices(
Number(subscription.paddleSubscriptionId)
);
return invoices;
};
export default getInvoices;

View File

@@ -15,6 +15,7 @@ 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 getBillingAndUsage from './queries/get-billing-and-usage.ee';
import getInvoices from './queries/get-invoices.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';
@@ -36,6 +37,7 @@ const queryResolvers = {
getPaymentPlans, getPaymentPlans,
getPaddleInfo, getPaddleInfo,
getBillingAndUsage, getBillingAndUsage,
getInvoices,
getAutomatischInfo, getAutomatischInfo,
healthcheck, healthcheck,
}; };

View File

@@ -38,6 +38,7 @@ type Query {
getPaymentPlans: [PaymentPlan] getPaymentPlans: [PaymentPlan]
getPaddleInfo: GetPaddleInfo getPaddleInfo: GetPaddleInfo
getBillingAndUsage: GetBillingAndUsage getBillingAndUsage: GetBillingAndUsage
getInvoices: [Invoice]
getAutomatischInfo: GetAutomatischInfo getAutomatischInfo: GetAutomatischInfo
healthcheck: AppHealth healthcheck: AppHealth
} }
@@ -504,6 +505,14 @@ type GetPaddleInfo {
vendorId: Int vendorId: Int
} }
type Invoice {
id: Int
amount: Float
currency: String
payout_date: String
receipt_url: String
}
type PaymentPlan { type PaymentPlan {
name: String name: String
limit: String limit: String

View File

@@ -1,5 +1,6 @@
import axios from 'axios'; import axios from 'axios';
import appConfig from '../../config/app'; import appConfig from '../../config/app';
import { DateTime } from 'luxon';
const PADDLE_VENDOR_URL = appConfig.isDev const PADDLE_VENDOR_URL = appConfig.isDev
? 'https://sandbox-vendors.paddle.com' ? 'https://sandbox-vendors.paddle.com'
@@ -22,8 +23,29 @@ const getSubscription = async (subscriptionId: number) => {
return subscription; return subscription;
}; };
const getInvoices = async (subscriptionId: number) => {
const data = {
vendor_id: appConfig.paddleVendorId,
vendor_auth_code: appConfig.paddleVendorAuthCode,
subscription_id: subscriptionId,
is_paid: 1,
from: DateTime.now().minus({ years: 3 }).toISODate(),
to: DateTime.now().plus({ days: 3 }).toISODate(),
};
const response = await axiosInstance.post(
'/api/2.0/subscription/payments',
data
);
const invoices = response.data.response;
return invoices;
};
const paddleClient = { const paddleClient = {
getSubscription, getSubscription,
getInvoices,
}; };
export default paddleClient; export default paddleClient;

View File

@@ -148,7 +148,7 @@ class User extends Base {
} }
async startTrialPeriod() { async startTrialPeriod() {
this.trialExpiryDate = DateTime.now().plus({ days: 30 }).toFormat('D'); this.trialExpiryDate = DateTime.now().plus({ days: 30 }).toISODate();
} }
async $beforeInsert(queryContext: QueryContext) { async $beforeInsert(queryContext: QueryContext) {