Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to check published status in Flow #16674

Closed
josephdpurcell opened this issue Nov 29, 2022 · 4 comments
Closed

Unable to check published status in Flow #16674

josephdpurcell opened this issue Nov 29, 2022 · 4 comments

Comments

@josephdpurcell
Copy link

Describe the Bug

Following existing documentation I could not check the published status of an item using a manual Flow.

The documentation I found that covers Flows includes:

To Reproduce

  1. Go to Flows
  2. Create a Flow
  3. Add a Manual trigger that displays on item detail page only
  4. Add an Operation to the flow that only hits the "success" path if the item's status is "published" (you pick which one you think will work best, based on documentation provided)
  5. Test and make sure the success path is only hit when you hit the manual trigger and the item is in status "published". An easy way to do this is to have an email sent out on the success and failure paths.

Errors Shown

Depending on the Operation of choice: the condition may always pass (see recent fix for a related issue), or the condition may always fail.

What version of Directus are you using?

9.21.0

What version of Node.js are you using?

16.18.1

What database are you using?

MySQL 8.0.31

What browser are you using?

Firefox

How are you deploying Directus?

Docker

@josephdpurcell josephdpurcell changed the title Unable to check published status in workflow Unable to check published status in Flow Nov 29, 2022
@josephdpurcell
Copy link
Author

josephdpurcell commented Nov 29, 2022

My suggestion for resolution would be to improve the documentation for Flows, perhaps by adding a Guide with all the details necessary to have a working example of how to control a Flow based on published status.

I will share my Flow in case this is helpful. My use case is I want a manual trigger that when clicked will create a Campaign in my Listmonk instance, but only if the item is published.

Step 1: Create a Collection

Create a Collection called "Post" with fields:

  • uuid (string, system field added during creation; this is the ID specifier)
  • status (string, system field added during creation)
  • title (string)
  • content (Markdown)

Step 2: Create a Flow

Name: Create Campaign
`Trigger: Manual
Collections: Post
Location: Item Page Only
Asynchronous: (not enabled)

Step 3: Read Data Operation

This step is to retrieve the item so the data can be used within the Flow. This is why its the first step.

Name: Get Item
Key: get_item
Operation: Get Data
Permissions: From Trigger
Collection: Post
IDs: (empty)
Query:

{
    "filter": {
        "uuid": {
            "_eq": "{{ $trigger.body.keys[0] }}"
        }
    }
}

Emit Events: (not enabled)

Step 4: Run Script Operation to check if published

This step is to ensure the flow exits if the content is not in a published state.

Name: Is Published
Key: is_published
Operation: Run Script
Permissions: From Trigger
Code:

/* https://github.com/directus/directus/issues/14571#issuecomment-1315513057 */
module.exports = async function(data) {
    if (typeof data.$last[0].status !== 'undefined' && data.$last[0].status === 'published') {
        return {...data}
    }
    
    throw new Error('Status is not published.');
}

Step 5: Run Script Operation to prepare request body

This step is necessary to ensure the request body is formatted correctly. (You could simply do this in the request step, but when I did that the request body was malformed. I presume this is because doing it in the request step is embedding a JSON string within another JSON string as opposed to this approach which takes an object and serializes it into JSON, which is probably safer all around.)

Name: Prepare Body
Key: prepare_body
Operation: Run Script
Permissions: From Trigger
Code:

module.exports = async function(data) {
    let body = {};
    try {
        body = {
            "lists": [
                123
            ],
            "type": "regular",
            "name": `Directus: ${data.get_item[0].title}`,
            "subject": data.get_item[0].title,
            "body": data.get_item[0].content,
            "status": "draft",
            "content_type": "markdown",
            "messenger": "email"
        };
    } catch (e) {
        body = {
            "error": {
                "message": e.message,
            	"stack": e.stack
            }
        };
    }
    
	return body;
}

Step 6: Run Script Operation to check if request body is OK

This step is to ensure the flow exits if preparing the request body errored.

Name: Is Body OK
Key: is_body_ok
Operation: Run Script
Permissions: From Trigger
Code:

module.exports = async function(data) {
    if (typeof data.prepare_body.error !== 'undefined') {
        throw new Error('There was an error in body prepare');
    }
}

Step 7: Webhook / Request URL to create the campaign in Listmonk

This step is to make the HTTP request to create a campaign in Listmonk using the processed request body.

Name: Create Campaign
Key: create_campaign
Operation: Webhook / Request URL
Method: POST
URL: https://listmonk.example.com/api/campaigns
Headers:

Authorization:  Basic yourbasicauthvalue

Request Body:

{{ prepare_body }}

Bonus: Send notification on failure

At each step, you can send a notification on failure by creating an operation and connecting it to the failure on a step. I used an email notification since I couldn't figure out how to send an in-app notification to the user who triggered it. (If I figure that out I'll edit this post.)

Name: Send Notification
Key: notification_prepare_body_error
Operation: Send Email
To: myemail@example.com (don't forget to hit enter to ensure the value is saved!)
Subject: Create Campaign: Body prepare error
Type: Markdown
Body:

Body prepare error, could not create campaign.

The following should contain an error message:

\```
{{ prepare_body }}
\```

Another tip is that on the failure to send to API you can include the request information in the email (I couldn't figure out how to get the request response):

API errored, so could not create campaign because: {{ create_campaign.message }}.

Request:

\```
{{ create_campaign.config.method }} {{ create_campaign.config.url }}

{{ create_campaign.config.data }}
\```

Edit 11/29 3pm ET: Using a Send Notification operation with User value of {{ $accountability.user }} (dont forget to hit enter!) will create a notification for the user, but in my test the notification body took me to /admin/content/null/null Page Not Found.

@br41nslug
Copy link
Member

Using the great and detailed examples provided (thanks 😄) i attempted to recreate your error situation since #16483 was fixed but with no success unfortunately.

Flow:
image
Trigger on status=published:
image
Trigger on status=draft:
image
These conditions seem to be working as expected. I suspect the "always fail condition" may be due to an unrelated error in the Javascript or perhaps this issue #15291 as i see you're returning {...data} in your script without using the output. You can do return; in such situations where the intent is just to stop execution while return {...data}; adds a copy of the entire context object as property onto the updated context.

Closing the issue for now but am happy to re-open if we can get it reproducable.

@br41nslug br41nslug closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2022
@br41nslug
Copy link
Member

Tagging in @erondpowell for the docs

@josephdpurcell
Copy link
Author

josephdpurcell commented Nov 30, 2022

Thanks @br41nslug, that comment was very helpful! The return tip is very good to know, I'm going to incorporate that in my work.

A clarification... This is in a collapsible because it's not a necessary comment. But, in case this is helpful, I wanted to clarify: at the time of submitting this issue I had a functional solution. The "bug" I am presenting is that it took me a subjectively long time to get to a working solution due to (a) my own lack of knowledge of Directus (which is true, I am a novice with Directus), or (b) a lack of sufficient documentation in the area of Flows, or perhaps a combination. I chose "bug" because I didn't see an issue type I could classify this under, but in retrospect perhaps this would be better suited as a Feature Request. In my mind, I thought of a Feature Request as code-only, but I can see that applying to documentation too.

Specifically, what I hope is shown in the docs is that a Flow doesn't have the item's content with it, just the ID. You have to do a lookup using $trigger.body.keys[0]. I think that is the essential piece I missed. From there I think the Data Chain docs are sufficient to know that {{ get_item }} (where "get_item" is the operation key) will have the item's content.

To achieve this clarity, some options could be:

  • Option 1: A guide that is as-is written above, showing how to get an item's content into Listmonk.
  • Option 2: Two guides: one to show how to do conditionals on an item with the "Get Item" and "Is Published" steps shown above, and another to show how to make an HTTP request safely using an item's content using the "Prepare Request", "Is Body OK", and HTTP request steps shown above (though it wouldn't be a listmonk-specific URL).
  • Option 3: One guide that shows how to do conditional logic on an item's content using the "Get Item" and "Is Published" steps above.
  • Option 4: Build out the /reference/system/flows.html page to explain that you can't do conditional logic on an item until you read it, and give a brief example of how to filter using $trigger.body.keys[0].
  • Option 5: Build out the Read Data Operation section to explain how to read an item from the trigger.

I would be very happy to contribute to any of those options or some other option, I just need a nudge in the right direction. I see Guides are not part of the docs, so perhaps I would write it in a Google Doc and share it. But, maybe this isn't best as a guide and instead a cookbook, or just enhancing existing docs.

PS. Flows are amazing. Thanks to everyone involved in making Directus such top-notch software.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants