feat: Implement an API endpoint to get flow folder
This commit is contained in:
12
packages/backend/src/controllers/api/v1/flows/get-folder.js
Normal file
12
packages/backend/src/controllers/api/v1/flows/get-folder.js
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { renderObject } from '../../../../helpers/renderer.js';
|
||||||
|
|
||||||
|
export default async (request, response) => {
|
||||||
|
const flow = await request.currentUser
|
||||||
|
.$relatedQuery('flows')
|
||||||
|
.findOne({ id: request.params.flowId })
|
||||||
|
.throwIfNotFound();
|
||||||
|
|
||||||
|
const folder = await flow.$relatedQuery('folder').throwIfNotFound();
|
||||||
|
|
||||||
|
renderObject(response, folder);
|
||||||
|
};
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
import { describe, it, expect, beforeEach } from 'vitest';
|
||||||
|
import request from 'supertest';
|
||||||
|
import Crypto from 'crypto';
|
||||||
|
import app from '../../../../app.js';
|
||||||
|
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id';
|
||||||
|
import { createUser } from '../../../../../test/factories/user';
|
||||||
|
import { createFlow } from '../../../../../test/factories/flow';
|
||||||
|
import { createStep } from '../../../../../test/factories/step';
|
||||||
|
import { createPermission } from '../../../../../test/factories/permission';
|
||||||
|
import { createFolder } from '../../../../../test/factories/folder';
|
||||||
|
import getFolderMock from '../../../../../test/mocks/rest/api/v1/flows/get-folder';
|
||||||
|
|
||||||
|
describe('GET /api/v1/flows/:flowId/folder', () => {
|
||||||
|
let currentUser, currentUserRole, token;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
currentUser = await createUser();
|
||||||
|
currentUserRole = await currentUser.$relatedQuery('role');
|
||||||
|
|
||||||
|
token = await createAuthTokenByUserId(currentUser.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return the folder data of current user', async () => {
|
||||||
|
const folder = await createFolder({ userId: currentUser.id });
|
||||||
|
|
||||||
|
const currentUserFlow = await createFlow({
|
||||||
|
userId: currentUser.id,
|
||||||
|
folderId: folder.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
await createStep({ flowId: currentUserFlow.id });
|
||||||
|
await createStep({ flowId: currentUserFlow.id });
|
||||||
|
|
||||||
|
await createPermission({
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Flow',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await request(app)
|
||||||
|
.get(`/api/v1/flows/${currentUserFlow.id}/folder`)
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
const expectedPayload = await getFolderMock(folder);
|
||||||
|
|
||||||
|
expect(response.body).toStrictEqual(expectedPayload);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return not found response for not existing flow UUID', async () => {
|
||||||
|
await createPermission({
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Flow',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
const notExistingFlowUUID = Crypto.randomUUID();
|
||||||
|
|
||||||
|
await request(app)
|
||||||
|
.get(`/api/v1/flows/${notExistingFlowUUID}/folder`)
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(404);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return bad request response for invalid UUID', async () => {
|
||||||
|
await createPermission({
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Flow',
|
||||||
|
roleId: currentUserRole.id,
|
||||||
|
conditions: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
await request(app)
|
||||||
|
.get('/api/v1/flows/invalidFlowUUID/folder')
|
||||||
|
.set('Authorization', token)
|
||||||
|
.expect(400);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -153,6 +153,10 @@ const authorizationList = {
|
|||||||
action: 'update',
|
action: 'update',
|
||||||
subject: 'Flow',
|
subject: 'Flow',
|
||||||
},
|
},
|
||||||
|
'GET /api/v1/flows/:flowId/folder': {
|
||||||
|
action: 'read',
|
||||||
|
subject: 'Flow',
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const authorizeUser = async (request, response, next) => {
|
export const authorizeUser = async (request, response, next) => {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { authenticateUser } from '../../../helpers/authentication.js';
|
|||||||
import { authorizeUser } from '../../../helpers/authorization.js';
|
import { authorizeUser } from '../../../helpers/authorization.js';
|
||||||
import getFlowsAction from '../../../controllers/api/v1/flows/get-flows.js';
|
import getFlowsAction from '../../../controllers/api/v1/flows/get-flows.js';
|
||||||
import getFlowAction from '../../../controllers/api/v1/flows/get-flow.js';
|
import getFlowAction from '../../../controllers/api/v1/flows/get-flow.js';
|
||||||
|
import getFolderAction from '../../../controllers/api/v1/flows/get-folder.js';
|
||||||
import updateFlowAction from '../../../controllers/api/v1/flows/update-flow.js';
|
import updateFlowAction from '../../../controllers/api/v1/flows/update-flow.js';
|
||||||
import updateFlowStatusAction from '../../../controllers/api/v1/flows/update-flow-status.js';
|
import updateFlowStatusAction from '../../../controllers/api/v1/flows/update-flow-status.js';
|
||||||
import updateFlowFolderAction from '../../../controllers/api/v1/flows/update-flow-folder.js';
|
import updateFlowFolderAction from '../../../controllers/api/v1/flows/update-flow-folder.js';
|
||||||
@@ -17,6 +18,7 @@ const router = Router();
|
|||||||
|
|
||||||
router.get('/', authenticateUser, authorizeUser, getFlowsAction);
|
router.get('/', authenticateUser, authorizeUser, getFlowsAction);
|
||||||
router.get('/:flowId', authenticateUser, authorizeUser, getFlowAction);
|
router.get('/:flowId', authenticateUser, authorizeUser, getFlowAction);
|
||||||
|
router.get('/:flowId/folder', authenticateUser, authorizeUser, getFolderAction);
|
||||||
router.post('/', authenticateUser, authorizeUser, createFlowAction);
|
router.post('/', authenticateUser, authorizeUser, createFlowAction);
|
||||||
router.patch('/:flowId', authenticateUser, authorizeUser, updateFlowAction);
|
router.patch('/:flowId', authenticateUser, authorizeUser, updateFlowAction);
|
||||||
|
|
||||||
|
|||||||
21
packages/backend/test/mocks/rest/api/v1/flows/get-folder.js
Normal file
21
packages/backend/test/mocks/rest/api/v1/flows/get-folder.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
const getFlowMock = async (folder) => {
|
||||||
|
const data = {
|
||||||
|
id: folder.id,
|
||||||
|
name: folder.name,
|
||||||
|
createdAt: folder.createdAt.getTime(),
|
||||||
|
updatedAt: folder.updatedAt.getTime(),
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: data,
|
||||||
|
meta: {
|
||||||
|
count: 1,
|
||||||
|
currentPage: null,
|
||||||
|
isArray: false,
|
||||||
|
totalPages: null,
|
||||||
|
type: 'Folder',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default getFlowMock;
|
||||||
Reference in New Issue
Block a user