-
Notifications
You must be signed in to change notification settings - Fork 12
/
pipedrive_data_service.go
210 lines (190 loc) · 7.07 KB
/
pipedrive_data_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package pipedrive
import (
"context"
"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/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/source"
source_entity "github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/source/entity"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/source/repository"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/tracing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"gorm.io/gorm"
)
const (
UsersTableSuffix = "users"
OrganizationsTableSuffix = "organizations"
PersonsTableSuffix = "persons"
)
var sourceTableSuffixByDataType = map[string][]string{
string(common.USERS): {UsersTableSuffix},
string(common.ORGANIZATIONS): {OrganizationsTableSuffix},
string(common.CONTACTS): {PersonsTableSuffix},
}
type pipedriveDataService struct {
airbyteStoreDb *config.RawDataStoreDB
tenant string
instance string
processingIds map[string]source.ProcessingEntity
dataFuncs map[common.SyncedEntityType]func(context.Context, int, string) []any
log logger.Logger
}
func NewPipedriveDataService(airbyteStoreDb *config.RawDataStoreDB, tenant string, log logger.Logger) source.SourceDataService {
dataService := pipedriveDataService{
airbyteStoreDb: airbyteStoreDb,
tenant: tenant,
processingIds: map[string]source.ProcessingEntity{},
log: log,
}
dataService.dataFuncs = map[common.SyncedEntityType]func(context.Context, int, string) []any{}
dataService.dataFuncs[common.USERS] = dataService.GetUsersForSync
dataService.dataFuncs[common.ORGANIZATIONS] = dataService.GetOrganizationsForSync
dataService.dataFuncs[common.CONTACTS] = dataService.GetContactsForSync
return &dataService
}
func (s *pipedriveDataService) GetDataForSync(ctx context.Context, dataType common.SyncedEntityType, batchSize int, runId string) []interface{} {
span, ctx := opentracing.StartSpanFromContext(ctx, "PipedriveDataService.GetDataForSync")
defer span.Finish()
tracing.SetDefaultSyncServiceSpanTags(ctx, span)
span.LogFields(log.String("dataType", string(dataType)), log.Int("batchSize", batchSize))
if ok := s.dataFuncs[dataType]; ok != nil {
return s.dataFuncs[dataType](ctx, batchSize, runId)
} else {
s.log.Infof("No %s data function for %s", s.SourceId(), dataType)
return nil
}
}
func (s *pipedriveDataService) Init() {
err := s.getDb().AutoMigrate(&source_entity.SyncStatusForAirbyte{})
if err != nil {
s.log.Error(err)
}
}
func (s *pipedriveDataService) getDb() *gorm.DB {
schemaName := s.SourceId()
if len(s.instance) > 0 {
schemaName = schemaName + "_" + s.instance
}
schemaName = schemaName + "_" + s.tenant
return s.airbyteStoreDb.GetDBHandler(&config.Context{
Schema: schemaName,
})
}
func (s *pipedriveDataService) SourceId() string {
return string(entity.AirbyteSourcePipedrive)
}
func (s *pipedriveDataService) Close() {
s.processingIds = make(map[string]source.ProcessingEntity)
}
func (s *pipedriveDataService) GetUsersForSync(ctx context.Context, batchSize int, runId string) []any {
s.processingIds = make(map[string]source.ProcessingEntity)
currentEntity := string(common.USERS)
var users []any
for _, sourceTableSuffix := range sourceTableSuffixByDataType[currentEntity] {
airbyteRecords, err := repository.GetAirbyteUnprocessedRawRecords(ctx, s.getDb(), batchSize, runId, currentEntity, sourceTableSuffix)
if err != nil {
s.log.Error(err)
return nil
}
for _, v := range airbyteRecords {
if len(users) >= batchSize {
break
}
outputJSON, err := MapUser(v.AirbyteData)
user, err := source.MapJsonToUser(outputJSON, v.AirbyteAbId, s.SourceId())
if err != nil {
user = entity.UserData{
BaseData: entity.BaseData{
SyncId: v.AirbyteAbId,
},
}
}
s.processingIds[v.AirbyteAbId] = source.ProcessingEntity{
ExternalId: user.ExternalId,
Entity: currentEntity,
TableSuffix: sourceTableSuffix,
}
users = append(users, user)
}
}
return users
}
func (s *pipedriveDataService) GetOrganizationsForSync(ctx context.Context, batchSize int, runId string) []any {
s.processingIds = make(map[string]source.ProcessingEntity)
currentEntity := string(common.ORGANIZATIONS)
var organizations []any
for _, sourceTableSuffix := range sourceTableSuffixByDataType[currentEntity] {
airbyteRecords, err := repository.GetAirbyteUnprocessedRawRecords(ctx, s.getDb(), batchSize, runId, currentEntity, sourceTableSuffix)
if err != nil {
s.log.Error(err)
return nil
}
for _, v := range airbyteRecords {
if len(organizations) >= batchSize {
break
}
outputJSON, err := MapOrganization(v.AirbyteData)
organization, err := source.MapJsonToOrganization(outputJSON, v.AirbyteAbId, s.SourceId())
if err != nil {
organization = entity.OrganizationData{
BaseData: entity.BaseData{
SyncId: v.AirbyteAbId,
},
}
}
s.processingIds[v.AirbyteAbId] = source.ProcessingEntity{
ExternalId: organization.ExternalId,
Entity: currentEntity,
TableSuffix: sourceTableSuffix,
}
organizations = append(organizations, organization)
}
}
return organizations
}
func (s *pipedriveDataService) GetContactsForSync(ctx context.Context, batchSize int, runId string) []any {
s.processingIds = make(map[string]source.ProcessingEntity)
currentEntity := string(common.CONTACTS)
var contacts []any
for _, sourceTableSuffix := range sourceTableSuffixByDataType[currentEntity] {
airbyteRecords, err := repository.GetAirbyteUnprocessedRawRecords(ctx, s.getDb(), batchSize, runId, currentEntity, sourceTableSuffix)
if err != nil {
s.log.Error(err)
return nil
}
for _, v := range airbyteRecords {
if len(contacts) >= batchSize {
break
}
outputJSON, err := MapContact(v.AirbyteData)
contact, err := source.MapJsonToContact(outputJSON, v.AirbyteAbId, s.SourceId())
if err != nil {
contact = entity.ContactData{
BaseData: entity.BaseData{
SyncId: v.AirbyteAbId,
},
}
}
s.processingIds[v.AirbyteAbId] = source.ProcessingEntity{
ExternalId: contact.ExternalId,
Entity: currentEntity,
TableSuffix: sourceTableSuffix,
}
contacts = append(contacts, contact)
}
}
return contacts
}
func (s *pipedriveDataService) MarkProcessed(ctx context.Context, syncId, runId string, synced, skipped bool, reason string) error {
v, ok := s.processingIds[syncId]
if ok {
err := repository.MarkAirbyteRawRecordProcessed(ctx, s.getDb(), v.Entity, v.TableSuffix, syncId, synced, skipped, runId, v.ExternalId, reason)
if err != nil {
s.log.Errorf("error while marking %s with external reference %s as synced for %s", v.Entity, v.ExternalId, s.SourceId())
}
return err
}
return nil
}