71 lines
1.9 KiB
JavaScript
71 lines
1.9 KiB
JavaScript
import { raw } from 'objection';
|
|
import { DateTime } from 'luxon';
|
|
import Execution from '../../models/execution.js';
|
|
import paginate from '../../helpers/pagination.js';
|
|
|
|
const getExecutions = async (_parent, params, context) => {
|
|
const conditions = context.currentUser.can('read', 'Execution');
|
|
|
|
const filters = params.filters;
|
|
|
|
const userExecutions = context.currentUser.$relatedQuery('executions');
|
|
const allExecutions = Execution.query();
|
|
const executionBaseQuery = conditions.isCreator
|
|
? userExecutions
|
|
: allExecutions;
|
|
|
|
const selectStatusStatement = `
|
|
case
|
|
when count(*) filter (where execution_steps.status = 'failure') > 0
|
|
then 'failure'
|
|
else 'success'
|
|
end
|
|
as status
|
|
`;
|
|
|
|
const executions = executionBaseQuery
|
|
.clone()
|
|
.joinRelated('executionSteps as execution_steps')
|
|
.select('executions.*', raw(selectStatusStatement))
|
|
.groupBy('executions.id')
|
|
.orderBy('created_at', 'desc');
|
|
|
|
const computedExecutions = Execution.query()
|
|
.with('executions', executions)
|
|
.withSoftDeleted()
|
|
.withGraphFetched({
|
|
flow: {
|
|
steps: true,
|
|
},
|
|
});
|
|
|
|
if (filters?.flowId) {
|
|
computedExecutions.where('executions.flow_id', filters.flowId);
|
|
}
|
|
|
|
if (filters?.status) {
|
|
computedExecutions.where('executions.status', filters.status);
|
|
}
|
|
|
|
if (filters?.createdAt) {
|
|
const createdAtFilter = filters.createdAt;
|
|
if (createdAtFilter.from) {
|
|
const isoFromDateTime = DateTime.fromMillis(
|
|
parseInt(createdAtFilter.from, 10)
|
|
).toISO();
|
|
computedExecutions.where('executions.created_at', '>=', isoFromDateTime);
|
|
}
|
|
|
|
if (createdAtFilter.to) {
|
|
const isoToDateTime = DateTime.fromMillis(
|
|
parseInt(createdAtFilter.to, 10)
|
|
).toISO();
|
|
computedExecutions.where('executions.created_at', '<=', isoToDateTime);
|
|
}
|
|
}
|
|
|
|
return paginate(computedExecutions, params.limit, params.offset);
|
|
};
|
|
|
|
export default getExecutions;
|