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

@@ -516,6 +516,35 @@ class User extends Base {
return invoices;
}
async hasFolderAccess(folderId) {
if (folderId && folderId !== 'null') {
await this.$relatedQuery('folders').findById(folderId).throwIfNotFound();
}
return true;
}
getFlows({ folderId, name }) {
return this.authorizedFlows
.clone()
.withGraphFetched({
steps: true,
})
.where((builder) => {
if (name) {
builder.where('flows.name', 'ilike', `%${name}%`);
}
if (folderId === 'null') {
builder.whereNull('flows.folder_id');
} else if (folderId) {
builder.where('flows.folder_id', folderId);
}
})
.orderBy('active', 'desc')
.orderBy('updated_at', 'desc');
}
async getApps(name) {
const connections = await this.authorizedConnections
.clone()

View File

@@ -1,5 +1,6 @@
import { describe, it, expect, vi } from 'vitest';
import { beforeEach, describe, it, expect, vi } from 'vitest';
import { DateTime, Duration } from 'luxon';
import Crypto from 'crypto';
import appConfig from '../config/app.js';
import * as licenseModule from '../helpers/license.ee.js';
import Base from './base.js';
@@ -32,6 +33,7 @@ import { createStep } from '../../test/factories/step.js';
import { createExecution } from '../../test/factories/execution.js';
import { createSubscription } from '../../test/factories/subscription.js';
import { createUsageData } from '../../test/factories/usage-data.js';
import { createFolder } from '../../test/factories/folder.js';
import Billing from '../helpers/billing/index.ee.js';
describe('User model', () => {
@@ -1142,7 +1144,112 @@ describe('User model', () => {
it('should return empty array without any subscriptions', async () => {
const user = await createUser();
expect(await user.getInvoices()).toStrictEqual([]);
expect(await user.getInvoices()).toEqual([]);
});
});
describe('hasFolderAccess', () => {
let currentUser, currentUserFolder;
beforeEach(async () => {
currentUser = await createUser();
currentUserFolder = await createFolder({ userId: currentUser.id });
});
it('should return true if the user has access to the folder', async () => {
const hasAccess = await currentUser.hasFolderAccess(currentUserFolder.id);
expect(hasAccess).toBe(true);
});
it('should throw an error if the user does not have access to the folder', async () => {
const anotherUser = await createUser();
const anotherUserFolder = await createFolder({ userId: anotherUser.id });
await expect(
currentUser.hasFolderAccess(anotherUserFolder.id)
).rejects.toThrow();
});
it('should throw an error if the folder does not exist', async () => {
const nonExistingFolderUUID = Crypto.randomUUID();
await expect(
currentUser.hasFolderAccess(nonExistingFolderUUID)
).rejects.toThrow();
});
});
describe('getFlows', () => {
let currentUser, currentUserRole, folder, flowOne, flowTwo;
beforeEach(async () => {
currentUser = await createUser();
currentUserRole = await currentUser.$relatedQuery('role');
folder = await createFolder({ userId: currentUser.id });
flowOne = await createFlow({
userId: currentUser.id,
folderId: folder.id,
name: 'Flow One',
});
flowTwo = await createFlow({
userId: currentUser.id,
name: 'Flow Two',
});
await createPermission({
action: 'read',
subject: 'Flow',
roleId: currentUserRole.id,
conditions: ['isCreator'],
});
currentUser = await currentUser.$query().withGraphFetched({
role: true,
permissions: true,
});
});
it('should return flows filtered by folderId', async () => {
const flows = await currentUser.getFlows({ folderId: folder.id });
expect(flows).toHaveLength(1);
expect(flows[0].id).toBe(flowOne.id);
});
it('should return flows filtered by name', async () => {
const flows = await currentUser.getFlows({ name: 'Flow Two' });
expect(flows).toHaveLength(1);
expect(flows[0].id).toBe(flowTwo.id);
});
it('should return flows filtered by folderId and name', async () => {
const flows = await currentUser.getFlows({
folderId: folder.id,
name: 'Flow One',
});
expect(flows).toHaveLength(1);
expect(flows[0].id).toBe(flowOne.id);
});
it('should return all flows if no filters are provided', async () => {
const flows = await currentUser.getFlows({});
expect(flows).toHaveLength(2);
expect(flows.map((flow) => flow.id)).toEqual(
expect.arrayContaining([flowOne.id, flowTwo.id])
);
});
it('should return uncategorized flows if the folderId is null', async () => {
const flows = await currentUser.getFlows({ folderId: 'null' });
expect(flows).toHaveLength(1);
expect(flows[0].id).toBe(flowTwo.id);
});
});