feat: Implement folder filters for get flows API endpoint

This commit is contained in:
Faruk AYDIN
2025-02-05 19:08:49 +01:00
parent 3a7c5f24b0
commit 434029ccb8
4 changed files with 390 additions and 14 deletions

View File

@@ -2,20 +2,14 @@ import { renderObject } from '../../../../helpers/renderer.js';
import paginateRest from '../../../../helpers/pagination-rest.js';
export default async (request, response) => {
const flowsQuery = request.currentUser.authorizedFlows
.clone()
.withGraphFetched({
steps: true,
})
.where((builder) => {
if (request.query.name) {
builder.where('flows.name', 'ilike', `%${request.query.name}%`);
}
})
.orderBy('active', 'desc')
.orderBy('updated_at', 'desc');
await request.currentUser.hasFolderAccess(request.body.folderId);
const flowsQuery = request.currentUser.getFlows(flowParams(request));
const flows = await paginateRest(flowsQuery, request.query.page);
renderObject(response, flows);
};
const flowParams = (request) => {
return { folderId: request.query.folderId, name: request.query.name };
};

View File

@@ -5,6 +5,7 @@ import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-us
import { createUser } from '../../../../../test/factories/user';
import { createFlow } from '../../../../../test/factories/flow';
import { createStep } from '../../../../../test/factories/step';
import { createFolder } from '../../../../../test/factories/folder';
import { createPermission } from '../../../../../test/factories/permission';
import getFlowsMock from '../../../../../test/mocks/rest/api/v1/flows/get-flows.js';
@@ -115,4 +116,249 @@ describe('GET /api/v1/flows', () => {
expect(response.body).toStrictEqual(expectedPayload);
});
it('should return the all flows data of the current user', async () => {
const folderOne = await createFolder({ userId: currentUser.id });
const currentUserFlowOne = await createFlow({
userId: currentUser.id,
folderId: folderOne.id,
});
const triggerStepFlowOne = await createStep({
flowId: currentUserFlowOne.id,
type: 'trigger',
});
const actionStepFlowOne = await createStep({
flowId: currentUserFlowOne.id,
type: 'action',
});
const folderTwo = await createFolder({ userId: currentUser.id });
const currentUserFlowTwo = await createFlow({
userId: currentUser.id,
folderId: folderTwo.id,
});
const triggerStepFlowTwo = await createStep({
flowId: currentUserFlowTwo.id,
type: 'trigger',
});
const actionStepFlowTwo = await createStep({
flowId: currentUserFlowTwo.id,
type: 'action',
});
const currentUserFlowThree = await createFlow({ userId: currentUser.id });
const triggerStepFlowThree = await createStep({
flowId: currentUserFlowThree.id,
type: 'trigger',
});
const actionStepFlowThree = await createStep({
flowId: currentUserFlowThree.id,
type: 'action',
});
await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: ['isCreator'],
});
const response = await request(app)
.get('/api/v1/flows')
.set('Authorization', token)
.expect(200);
const expectedPayload = await getFlowsMock(
[currentUserFlowThree, currentUserFlowTwo, currentUserFlowOne],
[
triggerStepFlowOne,
actionStepFlowOne,
triggerStepFlowTwo,
actionStepFlowTwo,
triggerStepFlowThree,
actionStepFlowThree,
]
);
expect(response.body).toStrictEqual(expectedPayload);
});
it('should return the uncategorized flows data of the current user', async () => {
const folderOne = await createFolder({ userId: currentUser.id });
const currentUserFlowOne = await createFlow({
userId: currentUser.id,
folderId: folderOne.id,
});
await createStep({
flowId: currentUserFlowOne.id,
type: 'trigger',
});
await createStep({
flowId: currentUserFlowOne.id,
type: 'action',
});
const folderTwo = await createFolder({ userId: currentUser.id });
const currentUserFlowTwo = await createFlow({
userId: currentUser.id,
folderId: folderTwo.id,
});
await createStep({
flowId: currentUserFlowTwo.id,
type: 'trigger',
});
await createStep({
flowId: currentUserFlowTwo.id,
type: 'action',
});
const currentUserFlowThree = await createFlow({
userId: currentUser.id,
});
const triggerStepFlowThree = await createStep({
flowId: currentUserFlowThree.id,
type: 'trigger',
});
const actionStepFlowThree = await createStep({
flowId: currentUserFlowThree.id,
type: 'action',
});
const currentUserFlowFour = await createFlow({ userId: currentUser.id });
const triggerStepFlowFour = await createStep({
flowId: currentUserFlowFour.id,
type: 'trigger',
});
const actionStepFlowFour = await createStep({
flowId: currentUserFlowFour.id,
type: 'action',
});
await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: ['isCreator'],
});
const response = await request(app)
.get(`/api/v1/flows?folderId=null`)
.set('Authorization', token)
.expect(200);
const expectedPayload = await getFlowsMock(
[currentUserFlowFour, currentUserFlowThree],
[
triggerStepFlowThree,
actionStepFlowThree,
triggerStepFlowFour,
actionStepFlowFour,
]
);
expect(response.body).toStrictEqual(expectedPayload);
});
it('should return the all flows data of the current user for specified folder', async () => {
const folderOne = await createFolder({ userId: currentUser.id });
const currentUserFlowOne = await createFlow({
userId: currentUser.id,
folderId: folderOne.id,
});
const triggerStepFlowOne = await createStep({
flowId: currentUserFlowOne.id,
type: 'trigger',
});
const actionStepFlowOne = await createStep({
flowId: currentUserFlowOne.id,
type: 'action',
});
const currentUserFlowTwo = await createFlow({
userId: currentUser.id,
folderId: folderOne.id,
});
const triggerStepFlowTwo = await createStep({
flowId: currentUserFlowTwo.id,
type: 'trigger',
});
const actionStepFlowTwo = await createStep({
flowId: currentUserFlowTwo.id,
type: 'action',
});
const folderTwo = await createFolder({ userId: currentUser.id });
const currentUserFlowThree = await createFlow({
userId: currentUser.id,
folderId: folderTwo.id,
});
await createStep({
flowId: currentUserFlowThree.id,
type: 'trigger',
});
await createStep({
flowId: currentUserFlowThree.id,
type: 'action',
});
const currentUserFlowFour = await createFlow({ userId: currentUser.id });
await createStep({
flowId: currentUserFlowFour.id,
type: 'trigger',
});
await createStep({
flowId: currentUserFlowFour.id,
type: 'action',
});
await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: ['isCreator'],
});
const response = await request(app)
.get(`/api/v1/flows?folderId=${folderOne.id}`)
.set('Authorization', token)
.expect(200);
const expectedPayload = await getFlowsMock(
[currentUserFlowTwo, currentUserFlowOne],
[
triggerStepFlowOne,
actionStepFlowOne,
triggerStepFlowTwo,
actionStepFlowTwo,
]
);
expect(response.body).toStrictEqual(expectedPayload);
});
});