1
1
defmodule Algora.Admin do
2
2
@ moduledoc false
3
+ import Ecto.Changeset
3
4
import Ecto.Query
4
5
5
6
alias Algora.Accounts
@@ -10,6 +11,7 @@ defmodule Algora.Admin do
10
11
alias Algora.Bounties.Bounty
11
12
alias Algora.Bounties.Claim
12
13
alias Algora.Github
14
+ alias Algora.Jobs.JobPosting
13
15
alias Algora.Parser
14
16
alias Algora.Payments
15
17
alias Algora.Payments.Transaction
@@ -22,6 +24,71 @@ defmodule Algora.Admin do
22
24
23
25
require Logger
24
26
27
+ def seed_job ( opts \\ % { } ) do
28
+ with { :ok , user } <- Repo . fetch_by ( User , handle: opts . org . handle ) ,
29
+ { :ok , user } <- user |> change ( opts . org ) |> Repo . update ( ) ,
30
+ { :ok , job } <-
31
+ Repo . insert ( % JobPosting {
32
+ id: Nanoid . generate ( ) ,
33
+ user_id: user . id ,
34
+ company_name: user . name ,
35
+ company_url: user . website_url ,
36
+ title: opts . title ,
37
+ description: opts . description ,
38
+ tech_stack: opts . tech_stack || Enum . take ( user . tech_stack , 1 )
39
+ } ) do
40
+ dbg ( "#{ AlgoraWeb.Endpoint . url ( ) } /#{ user . handle } /jobs/#{ job . id } " )
41
+ end
42
+ end
43
+
44
+ def sync_contributions ( opts \\ [ ] ) do
45
+ query =
46
+ User
47
+ |> where ( [ u ] , not is_nil ( u . handle ) )
48
+ |> where ( [ u ] , not is_nil ( u . provider_login ) )
49
+ |> where ( [ u ] , u . type == :individual )
50
+ |> where ( [ u ] , fragment ( "not exists (select 1 from user_contributions where user_contributions.user_id = ?)" , u . id ) )
51
+
52
+ query =
53
+ if handles = opts [ :handles ] do
54
+ where ( query , [ u ] , u . handle in ^ handles )
55
+ else
56
+ query
57
+ end
58
+
59
+ query =
60
+ if limit = opts [ :limit ] do
61
+ limit ( query , ^ limit )
62
+ else
63
+ query
64
+ end
65
+
66
+ Repo . transaction (
67
+ fn ->
68
+ if opts [ :dry_run ] do
69
+ query
70
+ |> Repo . stream ( )
71
+ |> Enum . to_list ( )
72
+ |> length ( )
73
+ |> IO . puts ( )
74
+ else
75
+ query
76
+ |> Repo . stream ( )
77
+ |> Enum . each ( fn user ->
78
+ % { provider_login: user . provider_login }
79
+ |> Workspace.Jobs.FetchTopContributions . new ( )
80
+ |> Oban . insert ( )
81
+ |> case do
82
+ { :ok , _job } -> IO . puts ( "Enqueued job for #{ user . provider_login } " )
83
+ { :error , error } -> IO . puts ( "Failed to enqueue job for #{ user . provider_login } : #{ inspect ( error ) } " )
84
+ end
85
+ end )
86
+ end
87
+ end ,
88
+ timeout: :infinity
89
+ )
90
+ end
91
+
25
92
def release_payment ( tx_id ) do
26
93
Repo . transact ( fn ->
27
94
{ _ , [ tx ] } =
@@ -402,6 +469,7 @@ defmodule Algora.Admin do
402
469
Logger . error ( message )
403
470
404
471
% {
472
+ url: Algora . config ( [ :discord , :webhook_url ] ) ,
405
473
payload: % {
406
474
embeds: [
407
475
% {
@@ -422,16 +490,19 @@ defmodule Algora.Admin do
422
490
423
491
email_job =
424
492
Algora.Activities.SendEmail . changeset ( % {
425
- title: "Error: #{ message } " ,
493
+ title: "#{ message } " ,
426
494
body: message ,
427
- name: "Algora Alert " ,
495
+ name: "Action required " ,
428
496
429
497
} )
430
498
431
499
discord_job =
432
500
SendDiscord . changeset ( % {
501
+ url: Algora.Settings . get ( "discord_webhook_url" ) [ "critical" ] || Algora . config ( [ :discord , :webhook_url ] ) ,
433
502
payload: % {
434
- embeds: [ % { color: color ( severity ) , title: "Error" , description: message , timestamp: DateTime . utc_now ( ) } ]
503
+ embeds: [
504
+ % { color: color ( severity ) , title: "Action required" , description: message , timestamp: DateTime . utc_now ( ) }
505
+ ]
435
506
}
436
507
} )
437
508
@@ -442,6 +513,7 @@ defmodule Algora.Admin do
442
513
Logger . info ( message )
443
514
444
515
% {
516
+ url: Algora . config ( [ :discord , :webhook_url ] ) ,
445
517
payload: % {
446
518
embeds: [
447
519
% {
0 commit comments