/
field_set_service.go
149 lines (129 loc) · 5.7 KB
/
field_set_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
package service
import (
"context"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/entity"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/graph/model"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/repository"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/common"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/logger"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
neo4jentity "github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/entity"
)
type FieldSetService interface {
FindAll(ctx context.Context, obj *model.CustomFieldEntityType) (*entity.FieldSetEntities, error)
MergeFieldSetToContact(ctx context.Context, contactId string, input *entity.FieldSetEntity) (*entity.FieldSetEntity, error)
UpdateFieldSetInContact(ctx context.Context, contactId string, input *entity.FieldSetEntity) (*entity.FieldSetEntity, error)
DeleteByIdFromContact(ctx context.Context, contactId string, fieldSetId string) (bool, error)
}
type fieldSetService struct {
log logger.Logger
repository *repository.Repositories
}
func NewFieldSetService(log logger.Logger, repository *repository.Repositories) FieldSetService {
return &fieldSetService{
log: log,
repository: repository,
}
}
func (s *fieldSetService) getDriver() neo4j.DriverWithContext {
return *s.repository.Drivers.Neo4jDriver
}
func (s *fieldSetService) FindAll(ctx context.Context, obj *model.CustomFieldEntityType) (*entity.FieldSetEntities, error) {
session := utils.NewNeo4jReadSession(ctx, s.getDriver())
defer session.Close(ctx)
dbRecords, err := s.repository.FieldSetRepository.FindAll(ctx, session, common.GetContext(ctx).Tenant, obj)
if err != nil {
return nil, err
}
fieldSetEntities := entity.FieldSetEntities{}
for _, dbRecord := range dbRecords {
fieldSetEntity := s.mapDbNodeToFieldSetEntity(dbRecord.Values[0].(dbtype.Node))
fieldSetEntities = append(fieldSetEntities, *fieldSetEntity)
}
return &fieldSetEntities, nil
}
func (s *fieldSetService) MergeFieldSetToContact(ctx context.Context, contactId string, entity *entity.FieldSetEntity) (*entity.FieldSetEntity, error) {
session := utils.NewNeo4jWriteSession(ctx, s.getDriver())
defer session.Close(ctx)
var fieldSetDbNode *dbtype.Node
entityType := &model.CustomFieldEntityType{
ID: contactId,
EntityType: model.EntityTypeOrganization,
}
_, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (interface{}, error) {
var err error
fieldSetDbNode, err = s.repository.FieldSetRepository.MergeFieldSetToContactInTx(ctx, tx, common.GetContext(ctx).Tenant, contactId, *entity)
if err != nil {
return nil, err
}
var fieldSetId = utils.GetPropsFromNode(*fieldSetDbNode)["id"].(string)
if entity.TemplateId != nil {
err := s.repository.FieldSetRepository.LinkWithFieldSetTemplateInTx(ctx, tx, common.GetContext(ctx).Tenant, fieldSetId, *entity.TemplateId, entityType.EntityType)
if err != nil {
return nil, err
}
}
if entity.CustomFields != nil {
for _, customField := range *entity.CustomFields {
dbNode, err := s.repository.CustomFieldRepository.MergeCustomFieldToFieldSetInTx(ctx, tx, common.GetContext(ctx).Tenant, entityType, fieldSetId, customField)
if err != nil {
return nil, err
}
if customField.TemplateId != nil {
var fieldId = utils.GetPropsFromNode(*dbNode)["id"].(string)
err := s.repository.CustomFieldRepository.LinkWithCustomFieldTemplateForFieldSetInTx(ctx, tx, fieldId, fieldSetId, *customField.TemplateId)
if err != nil {
return nil, err
}
}
}
}
return nil, nil
})
if err != nil {
return nil, err
}
var fieldSetEntity = s.mapDbNodeToFieldSetEntity(*fieldSetDbNode)
return fieldSetEntity, nil
}
func (s *fieldSetService) UpdateFieldSetInContact(ctx context.Context, contactId string, entity *entity.FieldSetEntity) (*entity.FieldSetEntity, error) {
session := utils.NewNeo4jWriteSession(ctx, s.getDriver())
defer session.Close(ctx)
var fieldSetDbNode *dbtype.Node
_, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (interface{}, error) {
var err error
fieldSetDbNode, err = s.repository.FieldSetRepository.UpdateFieldSetForContactInTx(ctx, tx, common.GetContext(ctx).Tenant, contactId, *entity)
if err != nil {
return nil, err
}
return nil, nil
})
if err != nil {
return nil, err
}
var fieldSetEntity = s.mapDbNodeToFieldSetEntity(*fieldSetDbNode)
return fieldSetEntity, nil
}
func (s *fieldSetService) DeleteByIdFromContact(ctx context.Context, contactId string, fieldSetId string) (bool, error) {
session := utils.NewNeo4jWriteSession(ctx, s.getDriver())
defer session.Close(ctx)
err := s.repository.FieldSetRepository.DeleteByIdFromContact(ctx, session, common.GetContext(ctx).Tenant, contactId, fieldSetId)
if err != nil {
return false, err
}
return true, nil
}
func (s *fieldSetService) mapDbNodeToFieldSetEntity(node dbtype.Node) *entity.FieldSetEntity {
props := utils.GetPropsFromNode(node)
result := entity.FieldSetEntity{
Id: utils.StringPtr(utils.GetStringPropOrEmpty(props, "id")),
Name: utils.GetStringPropOrEmpty(props, "name"),
CreatedAt: utils.GetTimePropOrEpochStart(props, "createdAt"),
UpdatedAt: utils.GetTimePropOrEpochStart(props, "updatedAt"),
Source: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "source")),
SourceOfTruth: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "sourceOfTruth")),
}
return &result
}