More conditions to find staled workflows

This commit is contained in:
Julien Fastré 2024-09-09 10:40:05 +02:00
parent b97eabf0d2
commit ee9530d03f
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB

View File

@ -198,13 +198,28 @@ class EntityWorkflowRepository implements ObjectRepository
return $this->repository->findOneBy($criteria); return $this->repository->findOneBy($criteria);
} }
public function findWorkflowsWithoutFinalStepAndOlderThan(\DateTimeImmutable $olderThanDate) /**
* Finds workflows that are not finalized and are older than the specified date.
*
* @param \DateTimeImmutable $olderThanDate the date to compare against
*
* @return list<int> the list of workflow IDs that meet the criteria
*/
public function findWorkflowsWithoutFinalStepAndOlderThan(\DateTimeImmutable $olderThanDate): array
{ {
$qb = $this->repository->createQueryBuilder('sw'); $qb = $this->repository->createQueryBuilder('sw');
$qb->select('sw.id') $qb->select('sw.id')
// only the workflow which are not finalized
->where('NOT EXISTS (SELECT 1 FROM chill_main_entity_workflow_step ews WHERE ews.isFinal = TRUE AND ews.entityWorkflow = sw.id)') ->where('NOT EXISTS (SELECT 1 FROM chill_main_entity_workflow_step ews WHERE ews.isFinal = TRUE AND ews.entityWorkflow = sw.id)')
->andWhere(':olderThanDate > ALL (SELECT ews.transitionAt FROM chill_main_entity_workflow_step ews WHERE ews.transitionAt IS NOT NULL AND ews.entityWorkflow = sw.id)') ->andWhere(
$qb->expr()->orX(
// only the workflow where all the last transition is older than transitionAt
':olderThanDate > ALL (SELECT ews.transitionAt FROM chill_main_entity_workflow_step ews WHERE ews.transitionAt IS NOT NULL AND ews.entityWorkflow = sw.id)',
// or the workflow which have only the initial step, with no transition
'1 = (SELECT COUNT(ews.id) FROM chill_main_entity_workflow_step ews WHERE ews.step = :initial AND ews.transitionAt IS NULL AND ews.createdAt < :olderThanDate AND ews.entityWorkflow = sw.id)',
)
)
->andWhere('sw.createdAt < :olderThanDate') ->andWhere('sw.createdAt < :olderThanDate')
->setParameter('olderThanDate', $olderThanDate); ->setParameter('olderThanDate', $olderThanDate);