Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix pagination issue in /executions - N8N-4236 #3834

Merged
merged 2 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse } from 'flatted';
import { In, Not, ObjectLiteral, LessThan, IsNull } from 'typeorm';
import { In, Not, Raw, ObjectLiteral, LessThan, IsNull } from 'typeorm';

import { Db, IExecutionFlattedDb, IExecutionResponseApi } from '../../../..';
import { ExecutionStatus } from '../../../types';
Expand Down Expand Up @@ -59,14 +59,23 @@ export async function getExecutions(data: {
status?: ExecutionStatus;
excludedExecutionsIds?: number[];
}): Promise<IExecutionResponseApi[]> {
const where = {
...(data.lastId && { id: LessThan(data.lastId) }),
...(data.status && { ...getStatusCondition(data.status) }),
...(data.workflowIds && { workflowId: In(data.workflowIds.map(String)) }),
...(data.excludedExecutionsIds && { id: Not(In(data.excludedExecutionsIds)) }),
};

if (data.lastId && data.excludedExecutionsIds) {
where.id = Raw((id) => `${id} < :lastId AND ${id} NOT IN (:...excludedExecutionsIds)`, {
lastId: data.lastId,
excludedExecutionsIds: data.excludedExecutionsIds,
});
}

const executions = await Db.collections.Execution.find({
select: getExecutionSelectableProperties(data.includeData),
where: {
...(data.lastId && { id: LessThan(data.lastId) }),
...(data.status && { ...getStatusCondition(data.status) }),
...(data.workflowIds && { workflowId: In(data.workflowIds.map(String)) }),
...(data.excludedExecutionsIds && { id: Not(In(data.excludedExecutionsIds)) }),
},
where,
order: { id: 'DESC' },
take: data.limit,
});
Expand Down
65 changes: 65 additions & 0 deletions packages/cli/test/integration/publicApi/executions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,71 @@ test('GET /executions should retrieve all successfull executions', async () => {
expect(waitTill).toBeNull();
});

test('GET /executions should paginate two executions', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });

const authOwnerAgent = utils.createAgent(app, {
apiPath: 'public',
auth: true,
user: owner,
version: 1,
});

const workflow = await testDb.createWorkflow({}, owner);

const fistSuccessfullExecution = await testDb.createSuccessfulExecution(workflow);

const secondSuccessfullExecution = await testDb.createSuccessfulExecution(workflow);

await testDb.createErrorExecution(workflow);

const firstExecutionResponse = await authOwnerAgent.get(`/executions`).query({
status: 'success',
limit: 1,
});

expect(firstExecutionResponse.statusCode).toBe(200);
expect(firstExecutionResponse.body.data.length).toBe(1);
expect(firstExecutionResponse.body.nextCursor).toBeDefined();

const secondExecutionResponse = await authOwnerAgent.get(`/executions`).query({
status: 'success',
limit: 1,
cursor: firstExecutionResponse.body.nextCursor,
});

expect(secondExecutionResponse.statusCode).toBe(200);
expect(secondExecutionResponse.body.data.length).toBe(1);
expect(secondExecutionResponse.body.nextCursor).toBeNull();

const successfullExecutions = [fistSuccessfullExecution, secondSuccessfullExecution];
const executions = [...firstExecutionResponse.body.data, ...secondExecutionResponse.body.data];

for (let i = 0; i < executions.length; i++) {
const {
id,
finished,
mode,
retryOf,
retrySuccessId,
startedAt,
stoppedAt,
workflowId,
waitTill,
} = executions[i]

expect(id).toBeDefined();
expect(finished).toBe(true);
expect(mode).toEqual(successfullExecutions[i].mode);
expect(retrySuccessId).toBeNull();
expect(retryOf).toBeNull();
expect(startedAt).not.toBeNull();
expect(stoppedAt).not.toBeNull();
expect(workflowId).toBe(successfullExecutions[i].workflowId);
expect(waitTill).toBeNull();
}
});

test('GET /executions should retrieve all error executions', async () => {
const owner = await testDb.createUser({ globalRole: globalOwnerRole, apiKey: randomApiKey() });

Expand Down