6
6
"strings"
7
7
8
8
"github.com/google/uuid"
9
+ "github.com/target/goalert/gadb"
9
10
"github.com/target/goalert/permission"
10
11
"github.com/target/goalert/util/log"
11
12
"github.com/target/goalert/util/sqlutil"
@@ -39,39 +40,26 @@ func (db *DB) updateAuthSubjects(ctx context.Context) error {
39
40
}
40
41
defer sqlutil .Rollback (ctx , "engine: update auth subjects" , tx )
41
42
42
- type cm struct {
43
- ID uuid.UUID
44
- UserID uuid.UUID
45
- SlackUserID string
46
- SlackTeamID string
47
- }
48
-
49
- var cms []cm
50
- rows , err := tx .StmtContext (ctx , db .cmMissingSub ).QueryContext (ctx )
43
+ q := gadb .New (tx )
44
+ rows , err := q .CompatCMMissingSub (ctx )
51
45
if err != nil {
52
46
return fmt .Errorf ("query: %w" , err )
53
47
}
54
- for rows .Next () {
55
- var c cm
56
- err = rows .Scan (& c .ID , & c .UserID , & c .SlackUserID )
48
+ for _ , row := range rows {
49
+ u , err := db .cs .User (ctx , row .Value )
57
50
if err != nil {
58
- return fmt .Errorf ("scan: %w" , err )
59
- }
60
-
61
- u , err := db .cs .User (ctx , c .SlackUserID )
62
- if err != nil {
63
- log .Log (ctx , fmt .Errorf ("update auth subjects: lookup Slack user (%s): %w" , c .SlackUserID , err ))
51
+ log .Log (ctx , fmt .Errorf ("update auth subjects: lookup Slack user (%s): %w" , row .Value , err ))
64
52
continue
65
53
}
66
54
67
- c . SlackTeamID = u . TeamID
68
- cms = append ( cms , c )
69
- }
70
-
71
- for _ , c := range cms {
72
- _ , err = tx . StmtContext ( ctx , db . insertSub ). ExecContext ( ctx , c . UserID , c . SlackUserID , "slack:" + c . SlackTeamID , c . ID )
55
+ err = q . CompatUpsertAuthSubject ( ctx , gadb. CompatUpsertAuthSubjectParams {
56
+ UserID : row . UserID ,
57
+ ProviderID : "slack:" + u . TeamID ,
58
+ SubjectID : u . ID ,
59
+ CmID : uuid. NullUUID { UUID : row . ID , Valid : true },
60
+ } )
73
61
if err != nil {
74
- return fmt .Errorf ("insert : %w" , err )
62
+ return fmt .Errorf ("upsert auth subject : %w" , err )
75
63
}
76
64
}
77
65
@@ -83,36 +71,25 @@ func (db *DB) updateAuthSubjects(ctx context.Context) error {
83
71
return nil
84
72
}
85
73
74
+ // updateContactMethods will create contact methods for associated auth_subjects (e.g. Slack direct message).
75
+ //
76
+ // To do this, we look for auth_subjects that are missing the contact method ID
77
+ // field (`cm_id`) for slack, and create a Slack DM contact method for the user
78
+ // associated with the record.
86
79
func (db * DB ) updateContactMethods (ctx context.Context ) error {
87
80
tx , err := db .lock .BeginTx (ctx , nil )
88
81
if err != nil {
89
82
return fmt .Errorf ("begin tx: %w" , err )
90
83
}
91
84
defer sqlutil .Rollback (ctx , "engine: update contact methods" , tx )
92
85
93
- type sub struct {
94
- ID int
95
- UserID string
96
- SubjectID string
97
- ProviderID string
98
- }
99
-
100
- var subs []sub
101
- rows , err := tx .StmtContext (ctx , db .slackSubMissingCM ).QueryContext (ctx )
86
+ q := gadb .New (tx )
87
+ rows , err := q .CompatAuthSubSlackMissingCM (ctx )
102
88
if err != nil {
103
89
return fmt .Errorf ("query: %w" , err )
104
90
}
105
91
106
- for rows .Next () {
107
- var s sub
108
- err = rows .Scan (& s .ID , & s .UserID , & s .SubjectID , & s .ProviderID )
109
- if err != nil {
110
- return fmt .Errorf ("scan: %w" , err )
111
- }
112
- subs = append (subs , s )
113
- }
114
-
115
- for _ , s := range subs {
92
+ for _ , s := range rows {
116
93
// provider id contains the team id in the format "slack:team_id"
117
94
// but we need to store the contact method id in the format "team_id:subject_id"
118
95
teamID := strings .TrimPrefix (s .ProviderID , "slack:" )
@@ -123,12 +100,21 @@ func (db *DB) updateContactMethods(ctx context.Context) error {
123
100
continue
124
101
}
125
102
126
- _ , err = tx .StmtContext (ctx , db .insertCM ).ExecContext (ctx , uuid .New (), team .Name , "SLACK_DM" , value , s .UserID )
103
+ err = q .CompatInsertUserCM (ctx , gadb.CompatInsertUserCMParams {
104
+ ID : uuid .New (),
105
+ Name : team .Name ,
106
+ Type : gadb .EnumUserContactMethodTypeSLACKDM ,
107
+ Value : value ,
108
+ UserID : s .UserID ,
109
+ })
127
110
if err != nil {
128
111
return fmt .Errorf ("insert cm: %w" , err )
129
112
}
130
113
131
- _ , err = tx .StmtContext (ctx , db .updateSubCMID ).ExecContext (ctx , s .ID , value )
114
+ err = q .CompatAuthSubSetCMID (ctx , gadb.CompatAuthSubSetCMIDParams {
115
+ ID : s .ID ,
116
+ Value : value ,
117
+ })
132
118
if err != nil {
133
119
return fmt .Errorf ("update sub cm_id: %w" , err )
134
120
}
0 commit comments