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