@@ -5,6 +5,7 @@ defmodule Algora.Activities do
5
5
alias Algora.Accounts.User
6
6
alias Algora.Activities.Activity
7
7
alias Algora.Repo
8
+ alias Ecto.Multi
8
9
9
10
@ tables [
10
11
:identity_activities ,
@@ -43,9 +44,7 @@ defmodule Algora.Activities do
43
44
:owned_installations ,
44
45
:connected_installations ,
45
46
:client_contracts ,
46
- { :client_contracts , :transactions } ,
47
47
:client_contracts ,
48
- { :contractor_contracts , :transactions } ,
49
48
:contractor_contracts
50
49
]
51
50
@@ -67,20 +66,19 @@ defmodule Algora.Activities do
67
66
end
68
67
69
68
def base_query ( table_name ) when is_binary ( table_name ) do
70
- from ( _e in { table_name , Activity } )
69
+ assoc_name = schema_from_table ( table_name )
70
+ base = from ( e in { table_name , Activity } )
71
+
72
+ from ( u in subquery ( base ) ,
73
+ select_merge: % {
74
+ id: u . id ,
75
+ type: u . type ,
76
+ assoc_id: u . assoc_id ,
77
+ assoc_name: ^ table_name
78
+ }
79
+ )
71
80
end
72
81
73
- # def base_query({parent_table_name, table_name}) do
74
- # parent = to_string(parent_table_name)
75
- # child = to_string(table_name)
76
-
77
- # parent_table_name
78
- # |> base_query()
79
- # |> join(:inner, [p], assoc(p, ^parent), as: :parent)
80
- # |> join(:inner, [c], assoc(c, ^child), as: :child)
81
- # |> join(:inner, [a], assoc(a, :activities), as: :activities)
82
- # end
83
-
84
82
def base_query_for_user ( user_id ) do
85
83
[ head | tail ] = @ user_attributes
86
84
first_query = base_query_for_user ( user_id , head )
@@ -91,21 +89,22 @@ defmodule Algora.Activities do
91
89
end )
92
90
end
93
91
94
- def base_query_for_user ( user_id , { parent_table_name , table_name } ) do
95
- from u in User ,
96
- where: u . id == ^ user_id ,
97
- join: p in assoc ( u , ^ parent_table_name ) ,
98
- join: c in assoc ( p , ^ table_name ) ,
99
- join: a in assoc ( c , :activities ) ,
100
- select: a
101
- end
102
-
103
- def base_query_for_user ( user_id , name ) do
104
- from u in User ,
105
- where: u . id == ^ user_id ,
106
- join: c in assoc ( u , ^ name ) ,
107
- join: a in assoc ( c , :activities ) ,
108
- select: a
92
+ def base_query_for_user ( user_id , relation_name ) do
93
+ table_name = table_from_user_relation ( relation_name )
94
+ assoc_name = schema_from_table ( table_name )
95
+
96
+ base =
97
+ from u in User ,
98
+ where: u . id == ^ user_id ,
99
+ join: c in assoc ( u , ^ relation_name ) ,
100
+ join: a in assoc ( c , :activities ) ,
101
+ select: % {
102
+ id: a . id ,
103
+ type: a . type ,
104
+ assoc_id: a . assoc_id ,
105
+ assoc_name: ^ table_name ,
106
+ inserted_at: a . inserted_at
107
+ }
109
108
end
110
109
111
110
def all ( table_name ) when is_binary ( table_name ) do
@@ -126,20 +125,90 @@ defmodule Algora.Activities do
126
125
base_query ( )
127
126
|> order_by ( fragment ( "inserted_at DESC" ) )
128
127
|> limit ( 40 )
129
- |> Repo . all ( )
128
+ |> all_with_assoc ( )
130
129
end
131
130
132
131
def all_for_user ( user_id ) do
133
132
user_id
134
133
|> base_query_for_user ( )
135
134
|> order_by ( fragment ( "inserted_at DESC" ) )
136
135
|> limit ( 40 )
137
- |> Repo . all ( )
136
+ |> all_with_assoc ( )
138
137
end
139
138
140
139
def insert ( target , activity ) do
141
140
target
142
141
|> Activity . build_activity ( activity )
143
142
|> Algora.Repo . insert ( )
144
143
end
144
+
145
+ def all_with_assoc ( query ) do
146
+ multi =
147
+ Multi . new ( )
148
+ |> Multi . run ( :activities , fn repo , _changes ->
149
+ { :ok , repo . all ( query ) }
150
+ end )
151
+ |> Multi . run ( :associations , fn repo , % { activities: activities } ->
152
+ associations =
153
+ Enum . map ( activities , fn activity ->
154
+ repo . one ( from b in schema_from_table ( activity . assoc_name ) , where: b . id == ^ activity . assoc_id )
155
+ end )
156
+
157
+ { :ok , associations }
158
+ end )
159
+ |> Multi . run ( :preloaded , fn _repo , % { activities: activities , associations: assocs } ->
160
+ preloaded =
161
+ Enum . zip_with ( activities , assocs , fn act , assoc ->
162
+ Map . put ( act , :assoc , assoc )
163
+ end )
164
+
165
+ { :ok , preloaded }
166
+ end )
167
+
168
+ case Repo . transaction ( multi ) do
169
+ { :ok , % { preloaded: preloaded } = a } ->
170
+ preloaded
171
+
172
+ { :error , _step , reason , _changes } ->
173
+ reason
174
+ # Handle error
175
+ end
176
+ end
177
+
178
+ def schema_from_table ( "identity_activities" ) , do: Algora.Accounts.Identity
179
+ def schema_from_table ( "user_activities" ) , do: Algora.Accounts.User
180
+ def schema_from_table ( "attempt_activities" ) , do: Algora.Bounties.Attempt
181
+ def schema_from_table ( "bonus_activities" ) , do: Algora.Bounties.Bonus
182
+ def schema_from_table ( "bounty_activities" ) , do: Algora.Bounties.Bounty
183
+ def schema_from_table ( "claim_activities" ) , do: Algora.Bounties.Claim
184
+ def schema_from_table ( "tip_activities" ) , do: Algora.Bounties.Tip
185
+ def schema_from_table ( "message_activities" ) , do: Algora.Chat.Message
186
+ def schema_from_table ( "thread_activities" ) , do: Algora.Chat.Thread
187
+ def schema_from_table ( "contract_activities" ) , do: Algora.Contracts.Contract
188
+ def schema_from_table ( "timesheet_activities" ) , do: Algora.Contracts.Timesheet
189
+ def schema_from_table ( "application_activities" ) , do: Algora.Jobs.Application
190
+ def schema_from_table ( "job_activities" ) , do: Algora.Jobs.Job
191
+ def schema_from_table ( "account_activities" ) , do: Algora.Payments.Account
192
+ def schema_from_table ( "customer_activities" ) , do: Algora.Payments.Customer
193
+ def schema_from_table ( "payment_method_activities" ) , do: Algora.Payments.PaymentMethod
194
+ def schema_from_table ( "platform_transaction_activities" ) , do: Algora.Payments.PlatformTransaction
195
+ def schema_from_table ( "transaction_activities" ) , do: Algora.Payments.Transaction
196
+ def schema_from_table ( "project_activities" ) , do: Algora.Projects.Project
197
+ def schema_from_table ( "review_activities" ) , do: Algora.Reviews.Project
198
+ def schema_from_table ( "installation_activities" ) , do: Algora.Workplace.Installation
199
+ def schema_from_table ( "ticket_activities" ) , do: Algora.Workspace.Ticket
200
+ def schema_from_table ( "repository_activities" ) , do: Algora.Workspace.Repository
201
+
202
+ def table_from_user_relation ( :attempts ) , do: "attempt_activities"
203
+ def table_from_user_relation ( :claims ) , do: "claim_activities"
204
+ def table_from_user_relation ( :client_contracts ) , do: "contract_activities"
205
+ def table_from_user_relation ( :connected_installations ) , do: "installation_activities"
206
+ def table_from_user_relation ( :contractor_contracts ) , do: "contract_activities"
207
+ def table_from_user_relation ( :created_bounties ) , do: "bounty_activities"
208
+ def table_from_user_relation ( :owned_bounties ) , do: "bounty_activities"
209
+ def table_from_user_relation ( :identities ) , do: "identity_activities"
210
+ def table_from_user_relation ( :owned_installations ) , do: "installation_activities"
211
+ def table_from_user_relation ( :projects ) , do: "project_activities"
212
+ def table_from_user_relation ( :repositories ) , do: "repository_activities"
213
+ def table_from_user_relation ( :transactions ) , do: "transaction_activities"
145
214
end
0 commit comments