-- NOTE: This file is auto generated by ./sql-generator

-- WorkflowRepository.search
select
  "workflow"."id",
  "workflow"."name",
  "workflow"."description",
  "workflow"."trigger",
  "workflow"."enabled",
  "workflow"."createdAt",
  "workflow"."updatedAt",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "plugin"."name" as "pluginName",
          "plugin_method"."name" as "methodName",
          "workflow_step"."config",
          "workflow_step"."enabled"
        from
          "workflow_step"
          inner join "plugin_method" on "plugin_method"."id" = "workflow_step"."pluginMethodId"
          inner join "plugin" on "plugin"."id" = "plugin_method"."pluginId"
        where
          "workflow"."id" = "workflow_step"."workflowId"
      ) as agg
  ) as "steps"
from
  "workflow"
order by
  "createdAt" desc

-- WorkflowRepository.get
select
  "workflow"."id",
  "workflow"."name",
  "workflow"."description",
  "workflow"."trigger",
  "workflow"."enabled",
  "workflow"."createdAt",
  "workflow"."updatedAt",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "plugin"."name" as "pluginName",
          "plugin_method"."name" as "methodName",
          "workflow_step"."config",
          "workflow_step"."enabled"
        from
          "workflow_step"
          inner join "plugin_method" on "plugin_method"."id" = "workflow_step"."pluginMethodId"
          inner join "plugin" on "plugin"."id" = "plugin_method"."pluginId"
        where
          "workflow"."id" = "workflow_step"."workflowId"
      ) as agg
  ) as "steps"
from
  "workflow"
where
  "id" = $1

-- WorkflowRepository.getForWorkflowRun
select
  "workflow"."id",
  "workflow"."name",
  "workflow"."trigger",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "workflow_step"."id",
          "workflow_step"."config",
          "plugin_method"."pluginId" as "pluginId",
          "plugin_method"."name" as "methodName",
          "plugin_method"."types" as "types",
          "plugin_method"."hostFunctions",
          "plugin_method"."allowedHosts"
        from
          "workflow_step"
          inner join "plugin_method" on "plugin_method"."id" = "workflow_step"."pluginMethodId"
        where
          "workflow_step"."workflowId" = "workflow"."id"
          and "workflow_step"."enabled" = $1
      ) as agg
  ) as "steps"
from
  "workflow"
where
  "id" = $2
  and "enabled" = $3

-- WorkflowRepository.delete
delete from "workflow"
where
  "id" = $1
