Merge pull request #2414 from automatisch/executions-time-range-filter
feat: Implement time based filters for executions API endpoint
This commit is contained in:
@@ -15,5 +15,7 @@ const executionParams = (request) => {
|
||||
return {
|
||||
name: request.query.name,
|
||||
status: request.query.status,
|
||||
startDateTime: request.query.startDateTime,
|
||||
endDateTime: request.query.endDateTime,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -566,7 +566,7 @@ class User extends Base {
|
||||
.orderBy('updated_at', 'desc');
|
||||
}
|
||||
|
||||
getExecutions({ name, status }) {
|
||||
getExecutions({ name, status, startDateTime, endDateTime }) {
|
||||
return this.authorizedExecutions
|
||||
.clone()
|
||||
.withSoftDeleted()
|
||||
@@ -590,6 +590,22 @@ class User extends Base {
|
||||
} else if (status === 'failure') {
|
||||
builder.where('executions.status', 'failure');
|
||||
}
|
||||
|
||||
if (startDateTime) {
|
||||
const startDate = DateTime.fromMillis(Number(startDateTime));
|
||||
|
||||
if (startDate.isValid) {
|
||||
builder.where('executions.created_at', '>=', startDate.toISO());
|
||||
}
|
||||
}
|
||||
|
||||
if (endDateTime) {
|
||||
const endDate = DateTime.fromMillis(Number(endDateTime));
|
||||
|
||||
if (endDate.isValid) {
|
||||
builder.where('executions.created_at', '<=', endDate.toISO());
|
||||
}
|
||||
}
|
||||
})
|
||||
.orderBy('created_at', 'desc');
|
||||
}
|
||||
|
||||
@@ -1380,12 +1380,18 @@ describe('User model', () => {
|
||||
status: 'success',
|
||||
});
|
||||
|
||||
// sleep for 10 milliseconds to make sure the created_at values are different
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
|
||||
executionTwo = await createExecution({
|
||||
flowId: flow.id,
|
||||
testRun: true,
|
||||
status: 'failure',
|
||||
});
|
||||
|
||||
// sleep for 10 milliseconds to make sure the created_at values are different
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
|
||||
executionThree = await createExecution({
|
||||
flowId: anotherUserFlow.id,
|
||||
testRun: false,
|
||||
@@ -1421,6 +1427,17 @@ describe('User model', () => {
|
||||
expect(executions[0].id).toBe(executionTwo.id);
|
||||
});
|
||||
|
||||
it('should return executions filtered by startDateTime and endDateTime', async () => {
|
||||
const executions = await currentUser.getExecutions({
|
||||
startDateTime: executionOne.createdAt,
|
||||
endDateTime: executionTwo.createdAt,
|
||||
});
|
||||
|
||||
expect(executions).toHaveLength(2);
|
||||
expect(executions[0].id).toBe(executionTwo.id);
|
||||
expect(executions[1].id).toBe(executionOne.id);
|
||||
});
|
||||
|
||||
it('should return all executions when no filter is applied', async () => {
|
||||
const executions = await currentUser.getExecutions({});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user