feat: Implement plan and usage API endpoint
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
import { renderObject } from '../../../../helpers/renderer.js';
|
||||||
|
|
||||||
|
export default async (request, response) => {
|
||||||
|
const planAndUsage = await request.currentUser.getPlanAndUsage();
|
||||||
|
|
||||||
|
renderObject(response, planAndUsage);
|
||||||
|
};
|
@@ -0,0 +1,68 @@
|
|||||||
|
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
||||||
|
import request from 'supertest';
|
||||||
|
import app from '../../../../app.js';
|
||||||
|
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id.js';
|
||||||
|
import { createUser } from '../../../../../test/factories/user.js';
|
||||||
|
import { createSubscription } from '../../../../../test/factories/subscription.js';
|
||||||
|
import { createUsageData } from '../../../../../test/factories/usage-data.js';
|
||||||
|
import appConfig from '../../../../config/app.js';
|
||||||
|
import { DateTime } from 'luxon';
|
||||||
|
|
||||||
|
describe('GET /api/v1/users/:userId/plan-and-usage', () => {
|
||||||
|
let user, token;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const trialExpiryDate = DateTime.now().plus({ days: 30 }).toISODate();
|
||||||
|
user = await createUser({ trialExpiryDate });
|
||||||
|
token = createAuthTokenByUserId(user.id);
|
||||||
|
|
||||||
|
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return free trial plan and usage data', async () => {
|
||||||
|
const response = await request(app)
|
||||||
|
.get(`/api/v1/users/${user.id}/plan-and-usage`)
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
const expectedResponseData = {
|
||||||
|
plan: {
|
||||||
|
id: null,
|
||||||
|
limit: null,
|
||||||
|
name: 'Free Trial',
|
||||||
|
},
|
||||||
|
usage: {
|
||||||
|
task: 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(response.body.data).toEqual(expectedResponseData);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return current plan and usage data', async () => {
|
||||||
|
await createSubscription({ userId: user.id });
|
||||||
|
|
||||||
|
await createUsageData({
|
||||||
|
userId: user.id,
|
||||||
|
consumedTaskCount: 1234,
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await request(app)
|
||||||
|
.get(`/api/v1/users/${user.id}/plan-and-usage`)
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
const expectedResponseData = {
|
||||||
|
plan: {
|
||||||
|
id: '47384',
|
||||||
|
limit: '10,000',
|
||||||
|
name: '10k - monthly',
|
||||||
|
},
|
||||||
|
usage: {
|
||||||
|
task: 1234,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(response.body.data).toEqual(expectedResponseData);
|
||||||
|
});
|
||||||
|
});
|
@@ -255,6 +255,31 @@ class User extends Base {
|
|||||||
return currentUsageData.consumedTaskCount < plan.quota;
|
return currentUsageData.consumedTaskCount < plan.quota;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getPlanAndUsage() {
|
||||||
|
const usageData = await this.$relatedQuery(
|
||||||
|
'currentUsageData'
|
||||||
|
).throwIfNotFound();
|
||||||
|
|
||||||
|
const subscription = await this.$relatedQuery('currentSubscription');
|
||||||
|
|
||||||
|
const currentPlan = Billing.paddlePlans.find(
|
||||||
|
(plan) => plan.productId === subscription?.paddlePlanId
|
||||||
|
);
|
||||||
|
|
||||||
|
const planAndUsage = {
|
||||||
|
usage: {
|
||||||
|
task: usageData.consumedTaskCount,
|
||||||
|
},
|
||||||
|
plan: {
|
||||||
|
id: subscription?.paddlePlanId || null,
|
||||||
|
name: subscription ? currentPlan.name : 'Free Trial',
|
||||||
|
limit: currentPlan?.limit || null,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return planAndUsage;
|
||||||
|
}
|
||||||
|
|
||||||
async getInvoices() {
|
async getInvoices() {
|
||||||
const subscription = await this.$relatedQuery('currentSubscription');
|
const subscription = await this.$relatedQuery('currentSubscription');
|
||||||
|
|
||||||
|
@@ -6,6 +6,7 @@ import getCurrentUserAction from '../../../controllers/api/v1/users/get-current-
|
|||||||
import getUserTrialAction from '../../../controllers/api/v1/users/get-user-trial.ee.js';
|
import getUserTrialAction from '../../../controllers/api/v1/users/get-user-trial.ee.js';
|
||||||
import getInvoicesAction from '../../../controllers/api/v1/users/get-invoices.ee.js';
|
import getInvoicesAction from '../../../controllers/api/v1/users/get-invoices.ee.js';
|
||||||
import getSubscriptionAction from '../../../controllers/api/v1/users/get-subscription.ee.js';
|
import getSubscriptionAction from '../../../controllers/api/v1/users/get-subscription.ee.js';
|
||||||
|
import getPlanAndUsageAction from '../../../controllers/api/v1/users/get-plan-and-usage.ee.js';
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
@@ -31,4 +32,11 @@ router.get(
|
|||||||
asyncHandler(getSubscriptionAction)
|
asyncHandler(getSubscriptionAction)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
router.get(
|
||||||
|
'/:userId/plan-and-usage',
|
||||||
|
authenticateUser,
|
||||||
|
checkIsCloud,
|
||||||
|
asyncHandler(getPlanAndUsageAction)
|
||||||
|
);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
15
packages/backend/test/factories/usage-data.js
Normal file
15
packages/backend/test/factories/usage-data.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { DateTime } from 'luxon';
|
||||||
|
import { createUser } from './user';
|
||||||
|
import UsageData from '../../src/models/usage-data.ee.js';
|
||||||
|
|
||||||
|
export const createUsageData = async (params = {}) => {
|
||||||
|
params.userId = params?.userId || (await createUser()).id;
|
||||||
|
params.nextResetAt =
|
||||||
|
params?.nextResetAt || DateTime.now().plus({ days: 30 }).toISODate();
|
||||||
|
|
||||||
|
params.consumedTaskCount = params?.consumedTaskCount || 0;
|
||||||
|
|
||||||
|
const usageData = await UsageData.query().insertAndFetch(params);
|
||||||
|
|
||||||
|
return usageData;
|
||||||
|
};
|
Reference in New Issue
Block a user