Skip to content

Commit

Permalink
Merge pull request #20 from praekeltfoundation/feature/DELTA-1299/var…
Browse files Browse the repository at this point in the history
…iation-messages

Variation messages on browsable FAQs
  • Loading branch information
rudigiesler authored Sep 27, 2024
2 parents 07eb825 + 1a81d4a commit a7fa968
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: praekeltfoundation/flow_tester
ref: v0.3.0
ref: v0.3.1
path: flow_tester
ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Check formatting
Expand Down
82 changes: 72 additions & 10 deletions Browsable FAQs/QA/flows/Browsable FAQs.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<!-- { section: "7ac3c144-c5fa-49b9-a924-9d9e7516c428", x: 500, y: 48} -->

```stack
trigger(on: "MESSAGE RECEIVED") when has_only_phrase(event.message.text.body, "browse")
```

# Browsable FAQs

This Journey allows users to browse through the tree structure in the CMS, to view all of the content themselves.
Expand Down Expand Up @@ -95,7 +102,7 @@ These cards fetch the current selected content from the CMS, and displays it. It
* If there is an image or media file, it should display that

```stack
card FetchContent, then: DisplayContent do
card FetchContent, then: GetVariation do
selected_content_id =
filter(page_list_data.body.results, &(&1.title == selected_content_name))[0].id
Expand All @@ -109,14 +116,69 @@ card FetchContent, then: DisplayContent do
)
end
card GetVariation when count(content_data.body.body.text) > 0, then: DisplayContent do
content_body = content_data.body.body.text.value.message
variations = content_data.body.body.text.value.variation_messages
# Find and apply gender variation
gender_variations =
filter(variations, &(&1.profile_field == "gender" and &1.value == contact.gender))
content_body = if(count(gender_variations) > 0, gender_variations[0].message, content_body)
# Find and apply relationship variation
# Relationship stored on the contact is different to the one on the CMS, so translate between them
relationship_mapping = [
["single", "single"],
["in a relationship", "in_a_relationship"],
["it's complicated", "complicated"],
["empty", ""],
["", ""]
]
relationship_status = find(relationship_mapping, &(&1[0] == contact.relationship_status))[1]
relationship_variations =
filter(variations, &(&1.profile_field == "relationship" and &1.value == relationship_status))
content_body =
if(count(relationship_variations) > 0, relationship_variations[0].message, content_body)
# Find and apply age variation
# We only have year of birth, so we have to make the 1 January assumption and just difference to current year
# Age is also stored as ranges in the CMS, so map between age and age ranges
year_of_birth = contact.year_of_birth or ""
age =
if(
isnumber(year_of_birth),
year(now()) - year_of_birth,
-1
)
age_mapping = [
[[15, 18], "15-18"],
[[19, 24], "19-24"]
]
age_mapping_result = filter(age_mapping, &(age >= &1[0][0] and age <= &1[0][1]))
age_range = if(count(age_mapping_result) > 0, age_mapping_result[0][1], "")
age_variations = filter(variations, &(&1.profile_field == "age" and &1.value == age_range))
content_body = if(count(age_variations) > 0, age_variations[0].message, content_body)
end
card GetVariation, then: DisplayContent do
log("No messages, not searching for variations")
end
card DisplayContent when count(content_data.body.body.text.value.buttons) > 0 do
content_buttons = content_data.body.body.text.value.buttons
selected_button =
buttons(ProcessButton, map(content_buttons, &[&1.value.title, &1.value.title])) do
text("""
@content_data.body.title
@content_data.body.body.text.value.message
@content_body
""")
end
end
Expand All @@ -126,7 +188,7 @@ card DisplayContent when content_data.body.has_children do
parent_body =
if(
count(content_data.body.body.text) > 0,
content_data.body.body.text.value.message,
content_body,
"Select an item"
)
Expand Down Expand Up @@ -171,7 +233,7 @@ card DisplayContent when count(content_data.body.related_pages) > 0 do
list("Select related page", SelectRelatedPage, related_pages) do
text("""
@content_data.body.title
@content_data.body.body.text.value.message
@content_body
""")
end
end
Expand Down Expand Up @@ -255,12 +317,12 @@ card DisplayContent do
buttons(Exit: "Main Menu") do
text("""
@content_data.body.title
@content_data.body.body.text.value.message
@content_body
""")
end
end
card SelectRelatedPage, then: DisplayContent do
card SelectRelatedPage, then: GetVariation do
selected_content_id =
find(related_data.body.related_pages, &(&1.title == selected_content_name)).value
Expand Down Expand Up @@ -296,15 +358,15 @@ card DisplayImage, then: MainMenu do
log("Image Type: @image_data.media_type")
image("@image_data.meta.download_url")
text("@content_data.body.body.text.value.message")
text("@content_body")
end
card DisplayMedia when media_data.media_type == "audio", then: MainMenu do
log("Media ID-2: @media_data.id")
log("Media Type-2: @media_data.media_type")
audio("@media_data.meta.download_url")
text("@content_data.body.body.text.value.message")
text("@content_body")
end
card DisplayMedia when media_data.media_type == "video", then: MainMenu do
Expand All @@ -313,7 +375,7 @@ card DisplayMedia when media_data.media_type == "video", then: MainMenu do
log("@media_data.meta.download_url")
video("@media_data.meta.download_url")
text("@content_data.body.body.text.value.message")
text("@content_body")
end
card DisplayMedia do
Expand Down Expand Up @@ -365,7 +427,7 @@ card ActionButton when selected_button.type == "go_to_page" do
query: [["child_of", "@content_data.body.meta.parent.id"]]
)
then(DisplayContent)
then(GetVariation)
end
card ActionButton do
Expand Down
2 changes: 1 addition & 1 deletion Browsable FAQs/QA/flows_json/Browsable FAQs.json

Large diffs are not rendered by default.

150 changes: 148 additions & 2 deletions Browsable FAQs/QA/tests/browsable_faqs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,54 @@ defmodule BrowsableFAQsTest do
]
}

variations_page = %ContentPage{
parent: "topic-1",
slug: "variations-page",
title: "Variations test",
wa_messages: [
%WAMsg{
message: "Default message without variations",
variation_messages: [
%Variation{
profile_field: "gender",
value: "male",
message: "Male variation of message"
},
%Variation{
profile_field: "gender",
value: "female",
message: "Female variation of message"
},
%Variation{
profile_field: "relationship",
value: "single",
message: "Single variation of message"
},
%Variation{
profile_field: "relationship",
value: "in_a_relationship",
message: "In a relationship variation of message"
},
%Variation{
profile_field: "relationship",
value: "complicated",
message: "In a complicated relationship variation of message"
},
%Variation{
profile_field: "age",
value: "15-18",
message: "15-18 year old variation of message"
},
%Variation{
profile_field: "age",
value: "19-24",
message: "19-24 year old variation of message"
}
]
}
]
}

# TODO: FakeCMS support for related_pages

media_index = %Index{slug: "media-topic", title: "Media Topic"}
Expand Down Expand Up @@ -93,7 +141,8 @@ defmodule BrowsableFAQsTest do
nested_leaf_page,
multiple_messages_leaf,
media_index,
image_page
image_page,
variations_page
])

assert :ok = FakeCMS.add_images(wh_pid, [image])
Expand Down Expand Up @@ -137,7 +186,8 @@ defmodule BrowsableFAQsTest do
[
{"Leaf Page 1", "Leaf Page 1"},
{"Parent Page 1", "Parent Page 1"},
{"Multiple Messages Leaf", "Multiple Messages Leaf"}
{"Multiple Messages Leaf", "Multiple Messages Leaf"},
{"Variations test", "Variations test"}
]}
})
end
Expand Down Expand Up @@ -257,5 +307,101 @@ defmodule BrowsableFAQsTest do
%{text: "*How can I help you today?*" <> _}
])
end

test "variation no gender" do
setup_flow()
|> FlowTester.set_contact_properties(%{"gender" => ""})
|> FlowTester.start()
|> receive_message(%{})
|> FlowTester.send("Topic 1")
|> receive_message(%{})
|> FlowTester.send("Variations test")
|> receive_message(%{text: "Variations test\nDefault message without variations\n"})
end

test "variation male" do
setup_flow()
|> FlowTester.set_contact_properties(%{"gender" => "male"})
|> FlowTester.start()
|> receive_message(%{})
|> FlowTester.send("Topic 1")
|> receive_message(%{})
|> FlowTester.send("Variations test")
|> receive_message(%{text: "Variations test\nMale variation of message\n"})
end

test "variation female" do
setup_flow()
|> FlowTester.set_contact_properties(%{"gender" => "female"})
|> FlowTester.start()
|> receive_message(%{})
|> FlowTester.send("Topic 1")
|> receive_message(%{})
|> FlowTester.send("Variations test")
|> receive_message(%{text: "Variations test\nFemale variation of message\n"})
end

test "variation relationship single" do
setup_flow()
|> FlowTester.set_contact_properties(%{"relationship_status" => "single"})
|> FlowTester.start()
|> receive_message(%{})
|> FlowTester.send("Topic 1")
|> receive_message(%{})
|> FlowTester.send("Variations test")
|> receive_message(%{text: "Variations test\nSingle variation of message\n"})
end

test "variation in relationship" do
setup_flow()
|> FlowTester.set_contact_properties(%{"relationship_status" => "in a relationship"})
|> FlowTester.start()
|> receive_message(%{})
|> FlowTester.send("Topic 1")
|> receive_message(%{})
|> FlowTester.send("Variations test")
|> receive_message(%{text: "Variations test\nIn a relationship variation of message\n"})
end

test "variation relationship complicated" do
setup_flow()
|> FlowTester.set_contact_properties(%{"relationship_status" => "it's complicated"})
|> FlowTester.start()
|> receive_message(%{})
|> FlowTester.send("Topic 1")
|> receive_message(%{})
|> FlowTester.send("Variations test")
|> receive_message(%{
text: "Variations test\nIn a complicated relationship variation of message\n"
})
end

test "variation 15-18 years old" do
setup_flow()
|> FlowTester.set_contact_properties(%{"year_of_birth" => "2008"})
|> FlowTester.set_fake_time(~U[2024-01-01 00:00:00Z])
|> FlowTester.start()
|> receive_message(%{})
|> FlowTester.send("Topic 1")
|> receive_message(%{})
|> FlowTester.send("Variations test")
|> receive_message(%{
text: "Variations test\n15-18 year old variation of message\n"
})
end

test "variation 19-24 years old" do
setup_flow()
|> FlowTester.set_contact_properties(%{"year_of_birth" => "2004"})
|> FlowTester.set_fake_time(~U[2024-01-01 00:00:00Z])
|> FlowTester.start()
|> receive_message(%{})
|> FlowTester.send("Topic 1")
|> receive_message(%{})
|> FlowTester.send("Variations test")
|> receive_message(%{
text: "Variations test\n19-24 year old variation of message\n"
})
end
end
end

0 comments on commit a7fa968

Please sign in to comment.