-
Notifications
You must be signed in to change notification settings - Fork 12
/
reminder_read_repository.go
180 lines (154 loc) · 6.67 KB
/
reminder_read_repository.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
package repository
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-common-module/utils"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/tracing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
)
type ReminderReadRepository interface {
GetReminderById(ctx context.Context, tenant, id string) (*dbtype.Node, error)
GetRemindersOrderByDueDateAsc(ctx context.Context, tenant, organizationId string, dismissed *bool) ([]*dbtype.Node, error)
GetRemindersOrderByDueDateDesc(ctx context.Context, tenant, organizationId string, dismissed *bool) ([]*dbtype.Node, error)
GetRemindersForOrganization(ctx context.Context, tenant, organizationId string, dismissed *bool) ([]*dbtype.Node, error)
}
type reminderReadRepository struct {
driver *neo4j.DriverWithContext
database string
}
func (r *reminderReadRepository) prepareReadSession(ctx context.Context) neo4j.SessionWithContext {
return utils.NewNeo4jReadSession(ctx, *r.driver, utils.WithDatabaseName(r.database))
}
func NewReminderReadRepository(driver *neo4j.DriverWithContext, database string) ReminderReadRepository {
return &reminderReadRepository{
driver: driver,
database: database,
}
}
func (r *reminderReadRepository) GetReminderById(ctx context.Context, tenant, id string) (*dbtype.Node, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "ReminderReadRepository.GetReminderById")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, id)
cypher := `MATCH (:Tenant {name:$tenant})<-[:REMINDER_BELONGS_TO_TENANT]-(r:Reminder {id:$id}) RETURN r`
params := map[string]any{
"tenant": tenant,
"id": id,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
session := r.prepareReadSession(ctx)
defer session.Close(ctx)
result, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
queryResult, err := tx.Run(ctx, cypher, params)
return utils.ExtractSingleRecordFirstValueAsNode(ctx, queryResult, err)
})
if err != nil {
span.LogFields(log.Bool("result.found", false))
tracing.TraceErr(span, err)
return nil, err
}
span.LogFields(log.Bool("result.found", result != nil))
return result.(*dbtype.Node), nil
}
// GetReminderOrderByDueDateAsc returns a list of reminders ordered by due date in ascending order
func (r *reminderReadRepository) GetRemindersOrderByDueDateAsc(ctx context.Context, tenant, organizationId string, dismissed *bool) ([]*dbtype.Node, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "ReminderReadRepository.GetRemindersOrderByDueDateAsc")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
cypher := `MATCH (:Tenant {name:$tenant})<-[:REMINDER_BELONGS_TO_TENANT]-(r:Reminder)-[:REMINDER_BELONGS_TO_ORGANIZATION]->(o:Organization {id:$organizationId})`
if dismissed != nil {
if *dismissed {
cypher += ` WHERE r.dismissed = true`
} else {
cypher += ` WHERE r.dismissed IS NULL OR r.dismissed = false`
}
}
cypher += ` RETURN r ORDER BY r.dueDate ASC`
params := map[string]any{
"tenant": tenant,
"organizationId": organizationId,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
session := r.prepareReadSession(ctx)
defer session.Close(ctx)
result, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
queryResult, err := tx.Run(ctx, cypher, params)
return utils.ExtractAllRecordsFirstValueAsDbNodePtrs(ctx, queryResult, err)
})
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
span.LogFields(log.Int("result.count", len(result.([]*dbtype.Node))))
return result.([]*dbtype.Node), nil
}
// GetReminderOrderByDueDateDesc returns a list of reminders ordered by due date in descending order
func (r *reminderReadRepository) GetRemindersOrderByDueDateDesc(ctx context.Context, tenant, organizationId string, dismissed *bool) ([]*dbtype.Node, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "ReminderReadRepository.GetRemindersOrderByDueDateDesc")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
cypher := `MATCH (:Tenant {name:$tenant})<-[:REMINDER_BELONGS_TO_TENANT]-(r:Reminder)-[:REMINDER_BELONGS_TO_ORGANIZATION]->(o:Organization {id:$organizationId})`
if dismissed != nil {
if *dismissed {
cypher += ` WHERE r.dismissed = true`
} else {
cypher += ` WHERE r.dismissed IS NULL OR r.dismissed = false`
}
}
cypher += ` RETURN r ORDER BY r.dueDate DESC`
params := map[string]any{
"tenant": tenant,
"organizationId": organizationId,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
session := r.prepareReadSession(ctx)
defer session.Close(ctx)
result, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
queryResult, err := tx.Run(ctx, cypher, params)
return utils.ExtractAllRecordsFirstValueAsDbNodePtrs(ctx, queryResult, err)
})
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
span.LogFields(log.Int("result.count", len(result.([]*dbtype.Node))))
return result.([]*dbtype.Node), nil
}
// GetRemindersForOrganization returns a list of reminders for an organization
func (r *reminderReadRepository) GetRemindersForOrganization(ctx context.Context, tenant, organizationId string, dismissed *bool) ([]*dbtype.Node, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "ReminderReadRepository.GetRemindersForOrganization")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
cypher := `MATCH (:Tenant {name:$tenant})<-[:REMINDER_BELONGS_TO_TENANT]-(r:Reminder)-[:REMINDER_BELONGS_TO_ORGANIZATION]->(o:Organization {id:$organizationId})`
if dismissed != nil {
if *dismissed {
cypher += ` WHERE r.dismissed = true`
} else {
cypher += ` WHERE r.dismissed IS NULL OR r.dismissed = false`
}
}
cypher += ` RETURN r`
params := map[string]any{
"tenant": tenant,
"organizationId": organizationId,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
session := r.prepareReadSession(ctx)
defer session.Close(ctx)
result, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
queryResult, err := tx.Run(ctx, cypher, params)
return utils.ExtractAllRecordsFirstValueAsDbNodePtrs(ctx, queryResult, err)
})
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
span.LogFields(log.Int("result.count", len(result.([]*dbtype.Node))))
return result.([]*dbtype.Node), nil
}