Last active
November 8, 2023 20:42
-
-
Save maxx-coffee/c3dce03c17576b944ad1fca0c6498883 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Events.NewLead do | |
@moduledoc """ | |
Module use to process the "new_lead" event. | |
example event: | |
%{ | |
"tags" => [], | |
"gmaid" => "USA_22", | |
"state" => "NY", | |
"action" => "new_lead", | |
"postal" => "10013", | |
"email" => "[email protected]", | |
"address1" => "NEW YORK NY 10013", | |
"last_name" => "Fake", | |
"lead_type" => "CallEvent", | |
"contact_id" => 1, | |
"first_name" => "Fake", | |
"phone_work" => "2222222222", | |
"platform_id" => 1, | |
"occurrence_time" => "2023-11-08T20:09:51Z", | |
"master_advertiser_id" => 516007, | |
"contact_interaction_id" => 1 | |
} | |
""" | |
@event_name "new_lead" | |
@actions [ | |
:email_user, | |
:email_lead | |
] | |
@rules [ | |
:for_exsiting_contact, | |
:for_new_contact, | |
:in_time_frame | |
] | |
def email_user(event, %{args: program_args}) do | |
emails = program_args |> Map.get(:emails) | |
emails |> IO.inspect() | |
#queue_user_email(event, emails) | |
end | |
def email_lead(%{"email" => email}, _) do | |
email |> IO.inspect() | |
#queue_lead_email(event, email) | |
end | |
def process_event(event) do | |
programs() | |
|> Enum.each(fn program -> | |
run_program(program, event) | |
end) | |
end | |
def run_program(%{rules: rules, action: action} = program, event) do | |
case check_rules(event, rules, program) do | |
{:ok, result} -> | |
apply(__MODULE__, action |> String.to_existing_atom(), [event, program]) | |
:error -> | |
:noop | |
end | |
end | |
def check_rules(_,[], _, return), do: return | |
def check_rules(_, _, _, :error) do | |
:error | |
end | |
def check_rules(event, [rule | rules], %{rule_args: rule_args} = program, {:ok, rule_returns} \\ {:ok, %{}}) do | |
rule = rule |> String.to_existing_atom() | |
rule_args = rule_args |> Map.get(rule) | |
case apply(__MODULE__, rule, [event, rule_args]) do | |
{:ok, return} -> | |
results = {:ok, rule_returns |> Map.merge(return)} | |
check_rules(event, rules, program, results) | |
_ -> | |
:error | |
end | |
end | |
def for_existing_contact(lead, _) do | |
{:ok, %{contact: %{id: 1}}} | |
end | |
def in_time_frame(lead, args) do | |
IO.inspect(args) | |
{:ok, %{}} | |
end | |
def programs() do | |
# this would fetch the programs for the gmaid / event from the DB | |
[ | |
%{ | |
gmaid: "USA_22", | |
event: "new_lead", | |
action: "email_lead", | |
version: 1, | |
rules: [ | |
"for_existing_contact" | |
], | |
rule_args: %{}, | |
args: %{ | |
email_on: "2023-11-10 17:00:00" | |
} | |
}, | |
%{ | |
gmaid: "USA_22", | |
event: "new_lead", | |
action: "email_user", | |
version: 1, | |
rules: [ | |
"for_existing_contact", | |
"in_time_frame" | |
], | |
rule_args: %{ | |
in_time_frame: %{ | |
start: "09:00:00", | |
ending: "17:00:00", | |
on: ["mon","tue","wed","thu","fri"] | |
} | |
}, | |
args: %{ | |
emails: ["[email protected]"] | |
} | |
} | |
] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment