-
Notifications
You must be signed in to change notification settings - Fork 12
/
attachment_service.go
155 lines (132 loc) · 6.81 KB
/
attachment_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
package service
import (
"context"
"fmt"
"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/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 AttachmentService interface {
GetAttachmentById(ctx context.Context, id string) (*entity.AttachmentEntity, error)
Create(ctx context.Context, newAnalysis *entity.AttachmentEntity, source, sourceOfTruth neo4jentity.DataSource) (*entity.AttachmentEntity, error)
GetAttachmentsForNode(ctx context.Context, linkedWith repository.LinkedWith, linkedNature *repository.LinkedNature, ids []string) (*entity.AttachmentEntities, error)
LinkNodeWithAttachment(ctx context.Context, linkedWith repository.LinkedWith, linkedNature *repository.LinkedNature, attachmentId, includedById string) (*dbtype.Node, error)
UnlinkNodeWithAttachment(ctx context.Context, linkedWith repository.LinkedWith, linkedNature *repository.LinkedNature, attachmentId, includedById string) (*dbtype.Node, error)
MapDbNodeToAttachmentEntity(node dbtype.Node) *entity.AttachmentEntity
}
type attachmentService struct {
log logger.Logger
repositories *repository.Repositories
}
func NewAttachmentService(log logger.Logger, repositories *repository.Repositories) AttachmentService {
return &attachmentService{
log: log,
repositories: repositories,
}
}
func (s *attachmentService) LinkNodeWithAttachment(ctx context.Context, linkedWith repository.LinkedWith, linkedNature *repository.LinkedNature, attachmentId, includedById string) (*dbtype.Node, error) {
session := utils.NewNeo4jWriteSession(ctx, s.getNeo4jDriver())
defer session.Close(ctx)
node, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
tenant := common.GetContext(ctx).Tenant
return s.repositories.AttachmentRepository.LinkWithXXIncludesAttachmentInTx(ctx, tx, tenant, linkedWith, linkedNature, attachmentId, includedById)
})
if err != nil {
return nil, err
}
return node.(*dbtype.Node), err
}
func (s *attachmentService) UnlinkNodeWithAttachment(ctx context.Context, linkedWith repository.LinkedWith, linkedNature *repository.LinkedNature, attachmentId, includedById string) (*dbtype.Node, error) {
session := utils.NewNeo4jWriteSession(ctx, s.getNeo4jDriver())
defer session.Close(ctx)
node, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
tenant := common.GetContext(ctx).Tenant
return s.repositories.AttachmentRepository.UnlinkWithXXIncludesAttachmentInTx(ctx, tx, tenant, linkedWith, linkedNature, attachmentId, includedById)
})
if err != nil {
return nil, err
}
return node.(*dbtype.Node), err
}
func (s *attachmentService) GetAttachmentsForNode(ctx context.Context, linkedWith repository.LinkedWith, linkedNature *repository.LinkedNature, ids []string) (*entity.AttachmentEntities, error) {
records, err := s.repositories.AttachmentRepository.GetAttachmentsForXX(ctx, common.GetTenantFromContext(ctx), linkedWith, linkedNature, ids)
if err != nil {
return nil, err
}
analysisDescribes := s.convertDbNodesToAttachments(records)
return &analysisDescribes, nil
}
func (s *attachmentService) Create(ctx context.Context, newAnalysis *entity.AttachmentEntity, source, sourceOfTruth neo4jentity.DataSource) (*entity.AttachmentEntity, error) {
session := utils.NewNeo4jWriteSession(ctx, s.getNeo4jDriver())
defer session.Close(ctx)
interactionEventDbNode, err := session.ExecuteWrite(ctx, s.createAttachmentInDBTxWork(ctx, newAnalysis, source, sourceOfTruth))
if err != nil {
return nil, err
}
return s.MapDbNodeToAttachmentEntity(*interactionEventDbNode.(*dbtype.Node)), nil
}
func (s *attachmentService) createAttachmentInDBTxWork(ctx context.Context, newAttachment *entity.AttachmentEntity, source, sourceOfTruth neo4jentity.DataSource) func(tx neo4j.ManagedTransaction) (any, error) {
return func(tx neo4j.ManagedTransaction) (any, error) {
tenant := common.GetContext(ctx).Tenant
analysisDbNode, err := s.repositories.AttachmentRepository.Create(ctx, tx, tenant, newAttachment.Id, newAttachment.CdnUrl, newAttachment.BasePath, newAttachment.FileName, newAttachment.MimeType, newAttachment.Size, newAttachment.CreatedAt, source, sourceOfTruth, newAttachment.AppSource)
if err != nil {
return nil, err
}
return analysisDbNode, nil
}
}
func (s *attachmentService) GetAttachmentById(ctx context.Context, id string) (*entity.AttachmentEntity, error) {
session := utils.NewNeo4jReadSession(ctx, s.getNeo4jDriver())
defer session.Close(ctx)
queryResult, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (interface{}, error) {
result, err := tx.Run(ctx, fmt.Sprintf(`
MATCH (a:Attachment_%s {id:$id}) RETURN a`,
common.GetTenantFromContext(ctx)),
map[string]interface{}{
"id": id,
})
record, err := result.Single(ctx)
if err != nil {
return nil, err
}
return record.Values[0], nil
})
if err != nil {
return nil, err
}
return s.MapDbNodeToAttachmentEntity(queryResult.(dbtype.Node)), nil
}
func (s *attachmentService) MapDbNodeToAttachmentEntity(node dbtype.Node) *entity.AttachmentEntity {
props := utils.GetPropsFromNode(node)
createdAt := utils.GetTimePropOrEpochStart(props, "createdAt")
attachmentEntity := entity.AttachmentEntity{
Id: utils.GetStringPropOrEmpty(props, "id"),
CreatedAt: &createdAt,
FileName: utils.GetStringPropOrEmpty(props, "fileName"),
MimeType: utils.GetStringPropOrEmpty(props, "mimeType"),
CdnUrl: utils.GetStringPropOrEmpty(props, "cdnUrl"),
BasePath: utils.GetStringPropOrEmpty(props, "basePath"),
Size: utils.GetInt64PropOrZero(props, "size"),
AppSource: utils.GetStringPropOrEmpty(props, "appSource"),
Source: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "source")),
SourceOfTruth: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "sourceOfTruth")),
}
return &attachmentEntity
}
func (s *attachmentService) convertDbNodesToAttachments(records []*utils.DbNodeAndId) entity.AttachmentEntities {
attachments := entity.AttachmentEntities{}
for _, v := range records {
attachment := s.MapDbNodeToAttachmentEntity(*v.Node)
attachment.DataloaderKey = v.LinkedNodeId
attachments = append(attachments, *attachment)
}
return attachments
}
func (s *attachmentService) getNeo4jDriver() neo4j.DriverWithContext {
return *s.repositories.Drivers.Neo4jDriver
}