-
Notifications
You must be signed in to change notification settings - Fork 12
/
organization_sync_service.go
121 lines (104 loc) · 4.24 KB
/
organization_sync_service.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
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
package service
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/common"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/config"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/constants"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/entity"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/logger"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/repository"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/source"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/tracing"
"github.com/opentracing/opentracing-go"
"net/http"
"time"
)
type organizationSyncService struct {
repositories *repository.Repositories
services *Services
cfg *config.Config
log logger.Logger
}
func NewDefaultOrganizationSyncService(repositories *repository.Repositories, services *Services, cfg *config.Config, log logger.Logger) SyncService {
return &organizationSyncService{
repositories: repositories,
services: services,
cfg: cfg,
log: log,
}
}
func (s *organizationSyncService) Sync(ctx context.Context, dataService source.SourceDataService, syncDate time.Time, tenant, runId string, batchSize int) (int, int, int) {
completed, failed, skipped := 0, 0, 0
for {
organizations := dataService.GetDataForSync(ctx, common.ORGANIZATIONS, batchSize, runId)
if len(organizations) == 0 {
break
}
s.log.Infof("syncing %d organizations from %s for tenant %s", len(organizations), dataService.SourceId(), tenant)
var organizationsForWebhooks []entity.OrganizationData
for _, organization := range organizations {
inputOrganization := organization.(entity.OrganizationData)
if inputOrganization.ExternalSystem == "" {
_ = dataService.MarkProcessed(ctx, inputOrganization.SyncId, runId, false, false, "External system is empty. Error during reading data from source")
failed++
} else {
inputOrganization.AppSource = constants.AppSourceSyncCustomerOsData
organizationsForWebhooks = append(organizationsForWebhooks, inputOrganization)
}
}
if len(organizationsForWebhooks) > 0 {
err := s.postOrganizations(ctx, tenant, organizationsForWebhooks)
if err != nil {
s.log.Errorf("error while posting log organizations to webhooks: %v", err.Error())
for _, organizationForWebhooks := range organizationsForWebhooks {
failed++
_ = dataService.MarkProcessed(ctx, organizationForWebhooks.SyncId, runId, false, false, "")
}
} else {
s.log.Infof("successfully posted %d log organizations to webhooks", len(organizationsForWebhooks))
for _, organizationForWebhooks := range organizationsForWebhooks {
completed++
_ = dataService.MarkProcessed(ctx, organizationForWebhooks.SyncId, runId, true, false, "")
}
}
}
if len(organizations) < batchSize {
break
}
}
return completed, failed, skipped
}
func (s *organizationSyncService) postOrganizations(ctx context.Context, tenant string, organizations []entity.OrganizationData) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "OrganizationSyncService.postOrganizations")
defer span.Finish()
tracing.SetDefaultSyncServiceSpanTags(ctx, span)
// Convert the log entries slice to JSON
jsonData, err := json.Marshal(organizations)
if err != nil {
return err
}
// Create a new POST request
req, err := http.NewRequest("POST", s.cfg.Service.CustomerOsWebhooksAPI+"/sync/organizations", bytes.NewBuffer(jsonData))
if err != nil {
return err
}
// Set the headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-OPENLINE-API-KEY", s.cfg.Service.CustomerOsWebhooksAPIKey)
req.Header.Set("tenant", tenant)
// Create a new HTTP client and send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Check the status code to determine if the request was successful
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("API request failed with status code %d", resp.StatusCode)
}
return nil
}