Skip to content

Commit

Permalink
Merge pull request #269 from destag/issue-268
Browse files Browse the repository at this point in the history
Add action info to double_opt_in event
  • Loading branch information
tttp authored Oct 10, 2024
2 parents ae5f0e9 + 319c0a8 commit 8413d74
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 29 deletions.
8 changes: 4 additions & 4 deletions archive/sdk/queue/src/actionMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ export type ContactV2 = {
// address: {region, locality, street, streetNumber}
// but it can differ for different PII schema

type Campaign = {
export type Campaign = {
title: string, // long name
name: string, // technical name
externalId: number // can be set by owner of campaign
}

type ActionPage = {
export type ActionPage = {
locale: string, // language or full locale eg: pl, de_AT
name: string, // technical name
thankYouTemplate: string // name of thank you template
Expand All @@ -54,7 +54,7 @@ type ActionV1 = {
testing: boolean
}

type ActionV2 = {
export type ActionV2 = {
actionType: string,
customFields: {
[key: string]: string | number | boolean | string[] | number[]
Expand All @@ -63,7 +63,7 @@ type ActionV2 = {
testing: boolean // is this a test action? (to be discarded)
}

type Tracking = {
export type Tracking = {
source: string, // utm_*
medium: string,
campaign: string,
Expand Down
8 changes: 6 additions & 2 deletions archive/sdk/queue/src/eventMessage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {ContactV2, PrivacyV2} from './actionMessage'
import {ContactV2, PrivacyV2, Campaign, ActionV2, ActionPage, Tracking} from './actionMessage'

export type EmailStatusEvent = {
eventType: 'email_status';
action?: ActionV2;
actionPage?: ActionPage;
campaign?: Campaign;
supporter: {
contact: ContactV2;
privacy: PrivacyV2;
}
},
tracking?: Tracking;
}

export type EventMessageV2 = {
Expand Down
2 changes: 1 addition & 1 deletion proca/lib/proca/action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Proca.Action do
end

def get_by_id(action_id) do
one(id: action_id, preload: [:campaign, [action_page: :org], [supporter: :contacts], :fields])
one(id: action_id, preload: [:campaign, [action_page: :org], [supporter: :contacts], :source])
end

def get_by_id_and_ref(action_id, ref) do
Expand Down
5 changes: 1 addition & 4 deletions proca/lib/proca/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ defmodule Proca.Repo do
end

defp notify_opts(opts) do
case Keyword.pop(opts, :auth) do
{nil, opts} -> {[], opts}
{auth, opts} -> {[auth: auth], opts}
end
Keyword.split(opts, [:auth, :id])
end
end
19 changes: 18 additions & 1 deletion proca/lib/proca/stage/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Proca.Stage.Event do
can read.
"""
alias Proca.{Confirm, Org, Supporter}
alias Proca.{Action, Confirm, Org, Supporter}
alias Proca.Pipes.Connection
import Proca.Stage.Support, only: [camel_case_keys: 2, to_iso8601: 1]

Expand Down Expand Up @@ -68,8 +68,11 @@ defmodule Proca.Stage.Event do
end

def put_data(data, :email_status, %Supporter{} = supporter, opts) do
alias Proca.Stage.MessageV1
alias Proca.Stage.MessageV2

action = Action.get_by_id(opts[:id])

# Find Supporters contact data belonging to that org_id
org_id = opts[:org_id]

Expand All @@ -88,8 +91,22 @@ defmodule Proca.Stage.Event do
personal_info: MessageV2.personal_info_data(contact)
}

action_data = %{
action_type: action.action_type,
custom_fields: action.fields,
created_at: action.inserted_at |> to_iso8601(),
testing: action.testing
}

data
|> Map.put(:supporter, supporter_data)
|> Map.put(:campaign, MessageV2.campaign_data(action.campaign))
|> Map.put(:campaign_id, action.campaign.id)
|> Map.put(:action_page, MessageV2.action_page_data(action.action_page))
|> Map.put(:action_page_id, action.action_page.id)
|> Map.put(:action_id, action.id)
|> Map.put(:action, action_data)
|> Map.put(:tracking, MessageV1.tracking_data(action))
end

def put_data(data, :campaign_updated, campaign, _opts) do
Expand Down
51 changes: 35 additions & 16 deletions proca/lib/proca/stage/message_v2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Proca.Stage.MessageV2 do
alias Proca.Stage.MessageV1
alias Proca.Stage.Support
alias Proca.Repo
alias Proca.{Contact, Supporter, PublicKey, Action}
alias Proca.{Campaign, Contact, Supporter, PublicKey, Action, ActionPage}

def action_data(action, stage \\ :deliver, org_id) do
action =
Expand All @@ -22,22 +22,9 @@ defmodule Proca.Stage.MessageV2 do
%{
"actionId" => action.id,
"actionPageId" => action.action_page_id,
"actionPage" => %{
"locale" => action.action_page.locale,
"name" => action.action_page.name,
"thankYouTemplate" => action.action_page.thank_you_template,
"thankYouTemplateRef" => MessageV1.action_page_template_ref(action.action_page),
"supporterConfirmTemplate" =>
action.action_page.supporter_confirm_template ||
action.action_page.org.supporter_confirm_template
},
"actionPage" => action_page_data(action.action_page),
"campaignId" => action.campaign_id,
"campaign" => %{
"name" => action.campaign.name,
"title" => action.campaign.title,
"externalId" => action.campaign.external_id,
"contactSchema" => Atom.to_string(action.campaign.contact_schema)
},
"campaign" => campaign_data(action.campaign),
"org" => %{
"name" => action.action_page.org.name,
"title" => action.action_page.org.title
Expand All @@ -59,6 +46,38 @@ defmodule Proca.Stage.MessageV2 do
|> put_action_meta(stage)
end

def action_page_data(
%ActionPage{
locale: locale,
name: name,
thank_you_template: thank_you_template,
supporter_confirm_template: supporter_confirm_template,
org: org
} = action_page
) do
%{
"locale" => locale,
"name" => name,
"thankYouTemplate" => thank_you_template,
"thankYouTemplateRef" => MessageV1.action_page_template_ref(action_page),
"supporterConfirmTemplate" => supporter_confirm_template || org.supporter_confirm_template
}
end

def campaign_data(%Campaign{
name: name,
title: title,
external_id: external_id,
contact_schema: contact_schema
}) do
%{
"name" => name,
"title" => title,
"externalId" => external_id,
"contactSchema" => Atom.to_string(contact_schema)
}
end

def contact_data(
%Supporter{
first_name: first_name,
Expand Down
4 changes: 3 additions & 1 deletion proca/lib/proca_web/controllers/confirm_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ defmodule ProcaWeb.ConfirmController do
# Change the supporter email_status
defp handle_double_opt_in(action = %Action{supporter: sup}, doi)
when doi in ["1", "yes", "true"] do
case update_and_notify(Supporter.changeset(sup, %{email_status: :double_opt_in})) do
case update_and_notify(Supporter.changeset(sup, %{email_status: :double_opt_in}),
id: action.id
) do
{:ok, sup2} -> {:ok, %{action | supporter: sup2}}
{:error, _ch} -> {:error, 400, "cannot double opt in"}
end
Expand Down

0 comments on commit 8413d74

Please sign in to comment.