feat: Implement isOwner flag for get flows API endpoint

This commit is contained in:
Faruk AYDIN
2025-03-31 21:18:33 +02:00
parent 351576b6b3
commit 59b887079a
10 changed files with 133 additions and 8 deletions

View File

@@ -6,7 +6,7 @@ import { createUser } from '../../../../../test/factories/user.js';
import { createFlow } from '../../../../../test/factories/flow.js';
import { createStep } from '../../../../../test/factories/step.js';
import { createPermission } from '../../../../../test/factories/permission.js';
import getFlowsMock from '../../../../../test/mocks/rest/api/v1/flows/get-flows.js';
import getFlowsMock from '../../../../../test/mocks/rest/api/v1/apps/get-flows.js';
describe('GET /api/v1/apps/:appKey/flows', () => {
let currentUser, currentUserRole, token;

View File

@@ -7,7 +7,7 @@ import { createConnection } from '../../../../../test/factories/connection.js';
import { createFlow } from '../../../../../test/factories/flow.js';
import { createStep } from '../../../../../test/factories/step.js';
import { createPermission } from '../../../../../test/factories/permission.js';
import getFlowsMock from '../../../../../test/mocks/rest/api/v1/flows/get-flows.js';
import getFlowsMock from '../../../../../test/mocks/rest/api/v1/connections/get-flows.js';
describe('GET /api/v1/connections/:connectionId/flows', () => {
let currentUser, currentUserRole, token;

View File

@@ -61,7 +61,8 @@ describe('GET /api/v1/flows', () => {
actionStepFlowOne,
triggerStepFlowTwo,
actionStepFlowTwo,
]
],
currentUser.id
);
expect(response.body).toStrictEqual(expectedPayload);
@@ -111,7 +112,8 @@ describe('GET /api/v1/flows', () => {
actionStepFlowOne,
triggerStepFlowTwo,
actionStepFlowTwo,
]
],
currentUser.id
);
expect(response.body).toStrictEqual(expectedPayload);
@@ -184,7 +186,8 @@ describe('GET /api/v1/flows', () => {
actionStepFlowTwo,
triggerStepFlowThree,
actionStepFlowThree,
]
],
currentUser.id
);
expect(response.body).toStrictEqual(expectedPayload);
@@ -270,7 +273,8 @@ describe('GET /api/v1/flows', () => {
actionStepFlowThree,
triggerStepFlowFour,
actionStepFlowFour,
]
],
currentUser.id
);
expect(response.body).toStrictEqual(expectedPayload);
@@ -356,7 +360,8 @@ describe('GET /api/v1/flows', () => {
actionStepFlowOne,
triggerStepFlowTwo,
actionStepFlowTwo,
]
],
currentUser.id
);
expect(response.body).toStrictEqual(expectedPayload);

View File

@@ -537,6 +537,8 @@ class User extends Base {
.withGraphFetched({
steps: true,
})
.select('flows.*')
.select(Flow.raw('flows.user_id = ? as "isOwner"', [this.id]))
.where((builder) => {
if (name) {
builder.where('flows.name', 'ilike', `%${name}%`);

View File

@@ -1333,6 +1333,23 @@ describe('User model', () => {
);
});
it('should return isOwner false if the flow is owned by another user', async () => {
const anotherUser = await createUser();
await createFlow({
userId: anotherUser.id,
folderId: folder.id,
active: true,
name: 'Another User Flow One',
});
const flows = await currentUser.getFlows({ onlyOwnedFlows: false }, [
folder.id,
]);
expect(flows[0].isOwner).toBe(false);
});
it('should return specified flows with all filters together', async () => {
const flows = await currentUser.getFlows(
{

View File

@@ -11,6 +11,10 @@ const flowSerializer = (flow) => {
updatedAt: flow.updatedAt.getTime(),
};
if ('isOwner' in flow) {
flowData.isOwner = flow.isOwner;
}
if (flow.steps?.length > 0) {
flowData.steps = flow.steps.map((step) => stepSerializer(step));
}

View File

@@ -43,4 +43,22 @@ describe('flowSerializer', () => {
expect(flowSerializer(flow)).toMatchObject(expectedPayload);
});
describe('isOwner', () => {
it('should not be defined by default', async () => {
expect(flowSerializer(flow)).not.toHaveProperty('isOwner');
});
it('should return true if the flow is owned by the current user', async () => {
flow.isOwner = true;
expect(flowSerializer(flow)).toMatchObject({ isOwner: true });
});
it('should return false if the flow is owned by another user', async () => {
flow.isOwner = false;
expect(flowSerializer(flow)).toMatchObject({ isOwner: false });
});
});
});