Merge pull request #1650 from automatisch/rest-get-invoices
feat: Implement get invoices API endpoint
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
import { renderObject } from '../../../../helpers/renderer.js';
|
||||||
|
|
||||||
|
export default async (request, response) => {
|
||||||
|
const invoices = await request.currentUser.getInvoices();
|
||||||
|
|
||||||
|
renderObject(response, invoices);
|
||||||
|
};
|
@@ -0,0 +1,34 @@
|
|||||||
|
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||||
|
import request from 'supertest';
|
||||||
|
import app from '../../../../app.js';
|
||||||
|
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id';
|
||||||
|
import { createUser } from '../../../../../test/factories/user';
|
||||||
|
import User from '../../../../models/user';
|
||||||
|
import getInvoicesMock from '../../../../../test/mocks/rest/api/v1/users/get-invoices.ee';
|
||||||
|
|
||||||
|
describe('GET /api/v1/user/invoices', () => {
|
||||||
|
let currentUser, token;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
currentUser = await createUser();
|
||||||
|
token = createAuthTokenByUserId(currentUser.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return current user invoices', async () => {
|
||||||
|
const invoices = [
|
||||||
|
{ id: 1, amount: 100, description: 'Invoice 1' },
|
||||||
|
{ id: 2, amount: 200, description: 'Invoice 2' },
|
||||||
|
];
|
||||||
|
|
||||||
|
vi.spyOn(User.prototype, 'getInvoices').mockResolvedValue(invoices);
|
||||||
|
|
||||||
|
const response = await request(app)
|
||||||
|
.get('/api/v1/users/invoices')
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
const expectedPayload = await getInvoicesMock(invoices);
|
||||||
|
|
||||||
|
expect(response.body).toEqual(expectedPayload);
|
||||||
|
});
|
||||||
|
});
|
@@ -13,10 +13,11 @@ const totalCount = (object) =>
|
|||||||
|
|
||||||
const renderObject = (response, object) => {
|
const renderObject = (response, object) => {
|
||||||
let data = isPaginated(object) ? object.records : object;
|
let data = isPaginated(object) ? object.records : object;
|
||||||
|
|
||||||
const type = isPaginated(object)
|
const type = isPaginated(object)
|
||||||
? object.records[0].constructor.name
|
? object.records[0].constructor.name
|
||||||
: Array.isArray(object)
|
: Array.isArray(object)
|
||||||
? object[0].constructor.name
|
? object?.[0]?.constructor?.name || 'Object'
|
||||||
: object.constructor.name;
|
: object.constructor.name;
|
||||||
|
|
||||||
const serializer = serializers[type];
|
const serializer = serializers[type];
|
||||||
|
@@ -15,6 +15,7 @@ import Role from './role.js';
|
|||||||
import Step from './step.js';
|
import Step from './step.js';
|
||||||
import Subscription from './subscription.ee.js';
|
import Subscription from './subscription.ee.js';
|
||||||
import UsageData from './usage-data.ee.js';
|
import UsageData from './usage-data.ee.js';
|
||||||
|
import Billing from '../helpers/billing/index.ee.js';
|
||||||
|
|
||||||
class User extends Base {
|
class User extends Base {
|
||||||
static tableName = 'users';
|
static tableName = 'users';
|
||||||
@@ -237,6 +238,20 @@ class User extends Base {
|
|||||||
return currentUsageData.consumedTaskCount < plan.quota;
|
return currentUsageData.consumedTaskCount < plan.quota;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getInvoices() {
|
||||||
|
const subscription = await this.$relatedQuery('currentSubscription');
|
||||||
|
|
||||||
|
if (!subscription) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const invoices = await Billing.paddleClient.getInvoices(
|
||||||
|
Number(subscription.paddleSubscriptionId)
|
||||||
|
);
|
||||||
|
|
||||||
|
return invoices;
|
||||||
|
}
|
||||||
|
|
||||||
async $beforeInsert(queryContext) {
|
async $beforeInsert(queryContext) {
|
||||||
await super.$beforeInsert(queryContext);
|
await super.$beforeInsert(queryContext);
|
||||||
|
|
||||||
|
@@ -3,10 +3,12 @@ import { authenticateUser } from '../../../helpers/authentication.js';
|
|||||||
import checkIsCloud from '../../../helpers/check-is-cloud.js';
|
import checkIsCloud from '../../../helpers/check-is-cloud.js';
|
||||||
import getCurrentUserAction from '../../../controllers/api/v1/users/get-current-user.js';
|
import getCurrentUserAction from '../../../controllers/api/v1/users/get-current-user.js';
|
||||||
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';
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.get('/me', authenticateUser, getCurrentUserAction);
|
router.get('/me', authenticateUser, getCurrentUserAction);
|
||||||
|
router.get('/invoices', authenticateUser, checkIsCloud, getInvoicesAction);
|
||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
'/:userId/trial',
|
'/:userId/trial',
|
||||||
|
@@ -0,0 +1,14 @@
|
|||||||
|
const getInvoicesMock = async (invoices) => {
|
||||||
|
return {
|
||||||
|
data: invoices,
|
||||||
|
meta: {
|
||||||
|
count: invoices.length,
|
||||||
|
currentPage: null,
|
||||||
|
isArray: true,
|
||||||
|
totalPages: null,
|
||||||
|
type: 'Object',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default getInvoicesMock;
|
Reference in New Issue
Block a user