mkQueueJob :: forall m. (TS m) => Job.Spec -> UUID -> m ScheduleCommand
mkQueueJob s uuid = do
  (depsSucceeded, depsFailed) <- depsBySuccess
  pure $ QueueJob s uuid depsFailed depsSucceeded
  where
    depsBySuccess :: m ([Job.Name], [Job.Name])
    depsBySuccess = pure . over both (fmap fst) <$> partition (available . snd) =<< depStatuses
      where available = (== Job.Available) . Job.anyAvailability

    depStatuses :: m [(Job.Name, Job.AnyStatus)]
    depStatuses = catMaybes <$> traverse depStatus depNames

    depStatus :: Job.Name -> m (Maybe (Job.Name, Job.AnyStatus))
    depStatus n = (fmap . fmap) ((,) n) (ES.getEntity id n)

    depNames :: [Job.Name]
    depNames = s ^. Job.dependsOn . Job.dependenciesList ^.. each . Job.name
mkQueueJob :: forall m. (TS m) => Job.Spec -> UUID -> m ScheduleCommand
mkQueueJob s uuid = do
  (depsSucceeded, depsFailed) <- depsBySuccess
  pure $ QueueJob s uuid depsFailed depsSucceeded
  where
    depsBySuccess :: m ([Job.Name], [Job.Name])
    depsBySuccess = pure . over both (fmap fst) <$> partition (available . snd) =<< depStatuses
      where available = (== Job.Available) . Job.anyAvailability

    depStatuses :: m [(Job.Name, Job.AnyStatus)]
    depStatuses = catMaybes <$> traverse depStatus depNames

    depStatus :: Job.Name -> m (Maybe (Job.Name, Job.AnyStatus))
    depStatus n = (fmap . fmap) ((,) n) (ES.getEntity id n)

    depNames :: [Job.Name]
    depNames = s ^. Job.dependsOn . Job.dependenciesList ^.. each . Job.name
mkQueueJob s uuid = do
  (depsSucceeded, depsFailed) <- depsBySuccess
  pure $ QueueJob s uuid depsFailed depsSucceeded
  where
    depsBySuccess :: m ([Job.Name], [Job.Name])
    depsBySuccess = pure . over both (fmap fst) <$> partition (available . snd) =<< depStatuses
      where available = (== Job.Available) . Job.anyAvailability

    depStatuses :: m [(Job.Name, Job.AnyStatus)]
    depStatuses = catMaybes <$> traverse depStatus depNames

    depStatus :: Job.Name -> m (Maybe (Job.Name, Job.AnyStatus))
    depStatus n = (fmap . fmap) ((,) n) (ES.getEntity id n)

    depNames :: [Job.Name]
    depNames = s ^. Job.dependsOn . Job.dependenciesList ^.. each . Job.name