-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathorganization_verify.go
More file actions
138 lines (120 loc) · 4.73 KB
/
organization_verify.go
File metadata and controls
138 lines (120 loc) · 4.73 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package component
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"time"
"github.com/google/uuid"
"opencsg.com/csghub-server/builder/rpc"
"opencsg.com/csghub-server/builder/store/database"
"opencsg.com/csghub-server/common/config"
"opencsg.com/csghub-server/common/types"
)
type OrganizationVerifyComponent interface {
Create(ctx context.Context, req *types.OrgVerifyReq) (*database.OrganizationVerify, error)
Update(ctx context.Context, id int64, status types.VerifyStatus, reason string) (*database.OrganizationVerify, error)
Get(ctx context.Context, path string) (*database.OrganizationVerify, error)
sendNotification(ctx context.Context, status types.VerifyStatus, userUUID string) error
}
type OrganizationVerifyComponentImpl struct {
orgVerifyStore database.OrganizationVerifyStore
orgStore database.OrgStore
notificationSvcClient rpc.NotificationSvcClient
config *config.Config
}
func NewOrganizationVerifyComponent(config *config.Config) (OrganizationVerifyComponent, error) {
c := &OrganizationVerifyComponentImpl{}
c.orgVerifyStore = database.NewOrganizationVerifyStore()
c.orgStore = database.NewOrgStore()
c.config = config
c.notificationSvcClient = rpc.NewNotificationSvcHttpClient(fmt.Sprintf("%s:%d", config.Notification.Host, config.Notification.Port),
rpc.AuthWithApiKey(config.APIToken))
return c, nil
}
func (o *OrganizationVerifyComponentImpl) Create(ctx context.Context, req *types.OrgVerifyReq) (*database.OrganizationVerify, error) {
orgVerify, err := o.orgVerifyStore.CreateOrganizationVerify(ctx, &database.OrganizationVerify{
Name: req.Name,
CompanyName: req.CompanyName,
UnifiedCreditCode: req.UnifiedCreditCode,
Username: req.Username,
ContactName: req.ContactName,
ContactEmail: req.ContactEmail,
BusinessLicenseImg: req.BusinessLicenseImg,
Status: types.VerifyStatusPending,
Reason: "",
UserUUID: req.UserUUID,
})
if err != nil {
return nil, fmt.Errorf("failed to create organization verify, error: %w", err)
}
err = o.orgStore.UpdateVerifyStatus(ctx, orgVerify.Name, types.VerifyStatusPending)
if err != nil {
return nil, fmt.Errorf("failed to update organization verify status, error: %w", err)
}
return orgVerify, nil
}
func (o *OrganizationVerifyComponentImpl) Update(ctx context.Context, id int64, status types.VerifyStatus, reason string) (*database.OrganizationVerify, error) {
orgVerify, err := o.orgVerifyStore.UpdateOrganizationVerify(ctx, id, status, reason)
if err != nil {
return nil, fmt.Errorf("failed to update organization verify, error: %w", err)
}
err = o.orgStore.UpdateVerifyStatus(ctx, orgVerify.Name, status)
if err != nil {
return nil, fmt.Errorf("failed to update organization verify status, error: %w", err)
}
go func() {
notificationCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err = o.sendNotification(notificationCtx, status, orgVerify.UserUUID)
if err != nil {
slog.ErrorContext(ctx, "failed to send organization verify notification", slog.Any("error", err))
}
}()
return orgVerify, nil
}
func (o *OrganizationVerifyComponentImpl) Get(ctx context.Context, path string) (*database.OrganizationVerify, error) {
orgVerify, err := o.orgVerifyStore.GetOrganizationVerify(ctx, path)
if err != nil {
return nil, fmt.Errorf("failed to find organization verify, error: %w", err)
}
return orgVerify, nil
}
func (o *OrganizationVerifyComponentImpl) sendNotification(ctx context.Context, status types.VerifyStatus, userUUID string) error {
if userUUID == "" {
return fmt.Errorf("userUUID is empty")
}
msg := types.NotificationMessage{
MsgUUID: uuid.New().String(),
UserUUIDs: []string{userUUID},
NotificationType: types.NotificationSystem,
CreateAt: time.Now(),
Template: string(types.MessageScenarioOrgVerify),
Payload: map[string]any{
"verify_status": status,
},
}
msgBytes, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("failed to marshal message, err: %w", err)
}
notificationMsg := types.MessageRequest{
Scenario: types.MessageScenarioOrgVerify,
Parameters: string(msgBytes),
Priority: types.MessagePriorityHigh,
}
var sendErr error
retryCount := o.config.Notification.NotificationRetryCount
for i := range retryCount {
if sendErr = o.notificationSvcClient.Send(ctx, ¬ificationMsg); sendErr == nil {
break
}
if i < retryCount-1 {
slog.WarnContext(ctx, "failed to send notification, retrying", "notification_msg", notificationMsg, "attempt", i+1, "error", sendErr.Error())
}
}
if sendErr != nil {
return fmt.Errorf("failed to send notification after %d attempts, err: %w", retryCount, sendErr)
}
return nil
}