@@ -3,47 +3,79 @@ defmodule CodeCorps.Analytics.SegmentTraitsBuilder do
33 Builds Segment traits from provided data
44 """
55
6- @ spec build ( struct ) :: map
7- def build ( record ) , do: traits ( record )
6+ alias CodeCorps.Repo
87
9- defp traits ( user = % CodeCorps.User { } ) do
10- % {
11- admin: user . admin ,
12- biography: user . biography ,
13- created_at: user . inserted_at ,
14- email: user . email ,
15- first_name: user . first_name ,
16- github_id: user . github_id ,
17- github_username: user . github_username ,
18- last_name: user . last_name ,
19- sign_up_context: user . sign_up_context ,
20- state: user . state ,
21- twitter: user . twitter ,
22- type: user . type ,
23- username: user . username ,
24- website: user . website
25- }
26- end
8+ @ spec build ( struct | map ) :: map
9+ def build ( record ) , do: traits ( record )
2710
28- defp traits ( comment = % CodeCorps.Comment { } ) do
29- comment = comment |> CodeCorps.Repo . preload ( :task )
11+ @ spec traits ( struct | map ) :: map
12+ defp traits ( % CodeCorps.Comment { } = comment ) do
13+ comment = comment |> Repo . preload ( :task )
3014 % {
3115 comment_id: comment . id ,
3216 task: comment . task . title ,
3317 task_id: comment . task . id ,
3418 project_id: comment . task . project_id
3519 }
3620 end
37-
38- defp traits ( record = % CodeCorps.ProjectUser { } ) do
39- record = record |> CodeCorps.Repo . preload ( :project )
21+ defp traits ( % CodeCorps.DonationGoal { } = donation_goal ) do
4022 % {
23+ amount: donation_goal . amount ,
24+ current: donation_goal . current ,
25+ project_id: donation_goal . project_id
26+ }
27+ end
28+ defp traits ( % CodeCorps.GithubAppInstallation { } = installation ) do
29+ % {
30+ access_token_expires_at: installation . access_token_expires_at ,
31+ github_account_login: installation . github_account_login ,
32+ github_account_type: installation . github_account_type ,
33+ github_id: installation . github_id ,
34+ origin: installation . origin ,
35+ state: installation . state ,
36+ project_id: installation . project_id ,
37+ user_id: installation . user_id
38+ }
39+ end
40+ defp traits ( % CodeCorps.ProjectGithubRepo { } = record ) do
41+ record = record |> Repo . preload ( [ :project , :github_repo ] )
42+ % {
43+ project: record . project . title ,
44+ project_id: record . project_id ,
45+ github_repo_id: record . github_repo_id ,
46+ github_repo_github_account_login: record . github_repo . github_account_login ,
47+ github_repo_github_account_type: record . github_repo . github_account_type ,
48+ github_repo_github_id: record . github_repo . github_id ,
49+ github_repo_name: record . github_repo . name
50+ }
51+ end
52+ defp traits ( % CodeCorps.ProjectSkill { } = record ) do
53+ record = record |> Repo . preload ( [ :project , :skill ] )
54+ % {
55+ skill: record . skill . title ,
56+ skill_id: record . skill_id ,
4157 project: record . project . title ,
4258 project_id: record . project_id
4359 }
4460 end
45-
46- defp traits ( charge = % CodeCorps.StripeConnectCharge { } ) do
61+ defp traits ( % CodeCorps.ProjectUser { } = record ) do
62+ record = record |> Repo . preload ( :project )
63+ % {
64+ project: record . project . title ,
65+ project_id: record . project_id
66+ }
67+ end
68+ defp traits ( % CodeCorps.StripeConnectAccount { } = account ) do
69+ % {
70+ id: account . id ,
71+ business_name: account . business_name ,
72+ display_name: account . display_name ,
73+ email: account . email ,
74+ id_from_stripe: account . id_from_stripe ,
75+ organization_id: account . organization_id ,
76+ }
77+ end
78+ defp traits ( % CodeCorps.StripeConnectCharge { } = charge ) do
4779 # NOTE: this only works for some currencies
4880 revenue = charge . amount / 100
4981 currency = String . capitalize ( charge . currency ) # ISO 4127 format
@@ -55,38 +87,121 @@ defmodule CodeCorps.Analytics.SegmentTraitsBuilder do
5587 user_id: charge . user_id
5688 }
5789 end
90+ defp traits ( % CodeCorps.StripeConnectPlan { } = plan ) do
91+ % {
92+ id: plan . id ,
93+ amount: plan . amount ,
94+ created: plan . created ,
95+ id_from_stripe: plan . id_from_stripe ,
96+ name: plan . name ,
97+ project_id: plan . project_id
98+ }
99+ end
100+ defp traits ( % CodeCorps.StripeConnectSubscription { } = subscription ) do
101+ subscription = subscription |> Repo . preload ( :stripe_connect_plan )
58102
59- defp traits ( task = % CodeCorps.Task { } ) do
60103 % {
104+ id: subscription . id ,
105+ created: subscription . created ,
106+ cancelled_at: subscription . cancelled_at ,
107+ current_period_start: subscription . current_period_start ,
108+ current_period_end: subscription . current_period_end ,
109+ ended_at: subscription . ended_at ,
110+ id_from_stripe: subscription . id_from_stripe ,
111+ quantity: subscription . quantity ,
112+ status: subscription . status ,
113+ start: subscription . start ,
114+ plan_id: subscription . stripe_connect_plan_id ,
115+ user_id: subscription . user_id ,
116+ project_id: subscription . stripe_connect_plan . project_id
117+ }
118+ end
119+ defp traits ( % CodeCorps.StripePlatformCard { } = card ) do
120+ % {
121+ id: card . id ,
122+ brand: card . brand ,
123+ exp_month: card . exp_month ,
124+ exp_year: card . exp_year ,
125+ id_from_stripe: card . id_from_stripe ,
126+ last4: card . last4 ,
127+ name: card . name ,
128+ user_id: card . user_id
129+ }
130+ end
131+ defp traits ( % CodeCorps.StripePlatformCustomer { } = customer ) do
132+ % {
133+ id: customer . id ,
134+ created: customer . created ,
135+ currency: customer . currency ,
136+ delinquent: customer . delinquent ,
137+ email: customer . email ,
138+ id_from_stripe: customer . id_from_stripe ,
139+ user_id: customer . user_id
140+ }
141+ end
142+ defp traits ( % CodeCorps.Task { } = task ) do
143+ % {
144+ order: task . order ,
61145 task: task . title ,
62146 task_id: task . id ,
147+ task_list_id: task . task_list_id ,
63148 project_id: task . project_id
64149 }
65150 end
66-
67- defp traits ( user_category = % CodeCorps.UserCategory { } ) do
68- user_category = user_category |> CodeCorps.Repo . preload ( :category )
151+ defp traits ( % CodeCorps.TaskSkill { } = task_skill ) do
152+ task_skill = task_skill |> Repo . preload ( [ :skill , :task ] )
153+ % {
154+ skill: task_skill . skill . title ,
155+ skill_id: task_skill . skill . id ,
156+ task: task_skill . task . title
157+ }
158+ end
159+ defp traits ( % CodeCorps.User { } = user ) do
160+ % {
161+ admin: user . admin ,
162+ biography: user . biography ,
163+ created_at: user . inserted_at ,
164+ email: user . email ,
165+ first_name: user . first_name ,
166+ github_id: user . github_id ,
167+ github_username: user . github_username ,
168+ last_name: user . last_name ,
169+ sign_up_context: user . sign_up_context ,
170+ state: user . state ,
171+ twitter: user . twitter ,
172+ type: user . type ,
173+ username: user . username ,
174+ website: user . website
175+ }
176+ end
177+ defp traits ( % CodeCorps.UserCategory { } = user_category ) do
178+ user_category = user_category |> Repo . preload ( :category )
69179 % {
70180 category: user_category . category . name ,
71181 category_id: user_category . category . id
72182 }
73183 end
74-
75- defp traits ( user_role = % CodeCorps.UserRole { } ) do
76- user_role = user_role |> CodeCorps.Repo . preload ( :role )
184+ defp traits ( % CodeCorps.UserRole { } = user_role ) do
185+ user_role = user_role |> Repo . preload ( :role )
77186 % {
78187 role: user_role . role . name ,
79188 role_id: user_role . role . id
80189 }
81190 end
82-
83- defp traits ( user_skill = % CodeCorps.UserSkill { } ) do
84- user_skill = user_skill |> CodeCorps.Repo . preload ( :skill )
191+ defp traits ( % CodeCorps.UserSkill { } = user_skill ) do
192+ user_skill = user_skill |> Repo . preload ( :skill )
85193 % {
86194 skill: user_skill . skill . title ,
87195 skill_id: user_skill . skill . id
88196 }
89197 end
198+ defp traits ( % CodeCorps.UserTask { } = user_task ) do
199+ user_task = user_task |> Repo . preload ( :task )
90200
91- defp traits ( _ ) , do: % { }
201+ % {
202+ task: user_task . task . title ,
203+ task_id: user_task . task_id
204+ }
205+ end
206+ defp traits ( % { token: _ , user_id: _ } ) , do: % { }
92207end
0 commit comments