-
Notifications
You must be signed in to change notification settings - Fork 11
/
trb_lead_option.go
69 lines (58 loc) · 1.69 KB
/
trb_lead_option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package resolvers
import (
"context"
"github.com/cmsgov/easi-app/pkg/appcontext"
"github.com/cmsgov/easi-app/pkg/models"
"github.com/cmsgov/easi-app/pkg/storage"
)
// GetTRBLeadOptions retrieves TRB options from the database and returns EUA user info for each lead option
func GetTRBLeadOptions(
ctx context.Context,
store *storage.Store,
fetchUserInfos func(context.Context, []string) ([]*models.UserInfo, error),
) ([]*models.UserInfo, error) {
leadOptions, err := store.GetTRBLeadOptions(ctx)
if err != nil {
return nil, err
}
leadEuas := make([]string, len(leadOptions))
for i, leadOption := range leadOptions {
leadEuas[i] = leadOption.EUAUserID
}
leadInfos, err := fetchUserInfos(ctx, leadEuas)
if err != nil {
return nil, err
}
return leadInfos, err
}
// CreateTRBLeadOption creates a TRBLeadOption in the database and returns the user info for the
// created lead option
func CreateTRBLeadOption(
ctx context.Context,
store *storage.Store,
fetchUserInfo func(context.Context, string) (*models.UserInfo, error),
euaID string,
) (*models.UserInfo, error) {
// Make sure it's a valid EUA
leadUserInfo, err := fetchUserInfo(ctx, euaID)
if err != nil {
return nil, err
}
leadOption := &models.TRBLeadOption{
EUAUserID: euaID,
}
leadOption.CreatedBy = appcontext.Principal(ctx).ID()
_, err = store.CreateTRBLeadOption(ctx, leadOption)
if err != nil {
return nil, err
}
return leadUserInfo, nil
}
// DeleteTRBLeadOption deletes a TRBLeadOption record from the database
func DeleteTRBLeadOption(ctx context.Context, store *storage.Store, euaID string) (bool, error) {
_, err := store.DeleteTRBLeadOption(ctx, euaID)
if err != nil {
return false, err
}
return true, nil
}