-
Notifications
You must be signed in to change notification settings - Fork 12
/
view.resolvers.go
237 lines (216 loc) · 8.87 KB
/
view.resolvers.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package resolver
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen version v0.17.46
import (
"context"
"encoding/json"
"strconv"
"github.com/99designs/gqlgen/graphql"
"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/mapper"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/tracing"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/common"
postgresEntity "github.com/openline-ai/openline-customer-os/packages/server/customer-os-postgres-repository/entity"
)
// TableViewDefCreate is the resolver for the tableViewDef_Create field.
func (r *mutationResolver) TableViewDefCreate(ctx context.Context, input model.TableViewDefCreateInput) (*model.TableViewDef, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.TableViewDefCreate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tenant := common.GetTenantFromContext(ctx)
userId := common.GetUserIdFromContext(ctx)
var columns []postgresEntity.ColumnView
for _, column := range input.Columns {
columns = append(columns, postgresEntity.ColumnView{
ColumnType: column.ColumnType.String(),
Width: column.Width,
Visible: column.Visible,
})
}
columnsStruct := postgresEntity.Columns{
Columns: columns,
}
columnsJsonData, err := json.Marshal(columnsStruct)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to create table view definition")
return nil, nil
}
viewDefinition := postgresEntity.TableViewDefinition{
TableType: input.TableType.String(),
TableId: input.TableID.String(),
Name: input.Name,
ColumnsJson: string(columnsJsonData),
Order: input.Order,
Icon: input.Icon,
Filters: input.Filters,
Sorting: input.Sorting,
Tenant: tenant,
UserId: userId,
}
result := r.Services.Repositories.PostgresRepositories.TableViewDefinitionRepository.CreateTableViewDefinition(ctx, viewDefinition)
if result.Error != nil {
tracing.TraceErr(span, result.Error)
graphql.AddErrorf(ctx, "Failed to create table view definition")
return nil, nil
}
viewDefinition, ok := result.Result.(postgresEntity.TableViewDefinition)
if !ok {
graphql.AddErrorf(ctx, "Failed to create table view definition")
return nil, nil
}
return mapper.MapTableViewDefinitionToModel(viewDefinition), nil
}
// TableViewDefUpdate is the resolver for the tableViewDef_Update field.
func (r *mutationResolver) TableViewDefUpdate(ctx context.Context, input model.TableViewDefUpdateInput) (*model.TableViewDef, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.TableViewDefUpdate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tenant := common.GetTenantFromContext(ctx)
userId := common.GetUserIdFromContext(ctx)
id, err := strconv.ParseUint(input.ID, 10, 64)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update table view definition")
return nil, nil
}
var columns []postgresEntity.ColumnView
for _, column := range input.Columns {
columns = append(columns, postgresEntity.ColumnView{
ColumnType: column.ColumnType.String(),
Width: column.Width,
Visible: column.Visible,
})
}
columnsStruct := postgresEntity.Columns{
Columns: columns,
}
columnsJsonData, err := json.Marshal(columnsStruct)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to create table view definition")
return nil, nil
}
viewDefinition := postgresEntity.TableViewDefinition{
ID: id,
Name: input.Name,
ColumnsJson: string(columnsJsonData),
Order: input.Order,
Icon: input.Icon,
Filters: input.Filters,
Sorting: input.Sorting,
Tenant: tenant,
UserId: userId,
}
result := r.Services.Repositories.PostgresRepositories.TableViewDefinitionRepository.UpdateTableViewDefinition(ctx, viewDefinition)
if result.Error != nil {
tracing.TraceErr(span, result.Error)
graphql.AddErrorf(ctx, "Failed to update table view definition")
return nil, nil
}
viewDefinition, ok := result.Result.(postgresEntity.TableViewDefinition)
if !ok {
graphql.AddErrorf(ctx, "Failed to update table view definition")
return nil, nil
}
return mapper.MapTableViewDefinitionToModel(viewDefinition), nil
}
// TableViewDefs is the resolver for the tableViewDefs field.
func (r *queryResolver) TableViewDefs(ctx context.Context) ([]*model.TableViewDef, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.TableViewDefs", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tenant := common.GetTenantFromContext(ctx)
userId := common.GetUserIdFromContext(ctx)
result := r.Services.Repositories.PostgresRepositories.TableViewDefinitionRepository.GetTableViewDefinitions(ctx, tenant, userId)
if result.Error != nil {
tracing.TraceErr(span, result.Error)
graphql.AddErrorf(ctx, "Failed to get table view definitions")
return nil, nil
}
tableViewDefinitions, ok := result.Result.([]postgresEntity.TableViewDefinition)
if ok && len(tableViewDefinitions) == 0 {
for _, def := range DefaultTableViewDefinitions(userId, span) {
def.Tenant = tenant
def.UserId = userId
r.Services.Repositories.PostgresRepositories.TableViewDefinitionRepository.CreateTableViewDefinition(ctx, def)
}
result := r.Services.Repositories.PostgresRepositories.TableViewDefinitionRepository.GetTableViewDefinitions(ctx, tenant, userId)
if result.Error != nil {
tracing.TraceErr(span, result.Error)
graphql.AddErrorf(ctx, "Failed to get table view definitions")
return nil, nil
}
tableViewDefinitions, _ = result.Result.([]postgresEntity.TableViewDefinition)
}
// check all organization table view definitions are created
organizationFound, customersFound, myPortfolioFound, leadsFound, nurtureFound := false, false, false, false, false
for _, def := range tableViewDefinitions {
if def.TableType == model.TableViewTypeOrganizations.String() && def.TableId == model.TableIDTypeMyPortfolio.String() {
myPortfolioFound = true
}
if def.TableType == model.TableViewTypeOrganizations.String() && def.TableId == model.TableIDTypeCustomers.String() {
customersFound = true
}
if def.TableType == model.TableViewTypeOrganizations.String() && def.TableId == model.TableIDTypeOrganizations.String() {
organizationFound = true
}
if def.TableType == model.TableViewTypeOrganizations.String() && def.TableId == model.TableIDTypeLeads.String() {
leadsFound = true
}
if def.TableType == model.TableViewTypeOrganizations.String() && def.TableId == model.TableIDTypeNurture.String() {
nurtureFound = true
}
}
if !organizationFound || !customersFound || !myPortfolioFound || !leadsFound || !nurtureFound {
if !organizationFound {
tvDef, err := DefaultTableViewDefinitionOrganization(span)
if err == nil {
tvDef.Tenant = tenant
tvDef.UserId = userId
r.Services.Repositories.PostgresRepositories.TableViewDefinitionRepository.CreateTableViewDefinition(ctx, tvDef)
}
}
if !customersFound {
tvDef, err := DefaultTableViewDefinitionCustomers(span)
if err == nil {
tvDef.Tenant = tenant
tvDef.UserId = userId
r.Services.Repositories.PostgresRepositories.TableViewDefinitionRepository.CreateTableViewDefinition(ctx, tvDef)
}
}
if !myPortfolioFound {
tvDef, err := DefaultTableViewDefinitionMyPortfolio(userId, span)
if err == nil {
tvDef.Tenant = tenant
tvDef.UserId = userId
r.Services.Repositories.PostgresRepositories.TableViewDefinitionRepository.CreateTableViewDefinition(ctx, tvDef)
}
}
if !leadsFound {
tvDef, err := DefaultTableViewDefinitionLeads(span)
if err == nil {
tvDef.Tenant = tenant
tvDef.UserId = userId
r.Services.Repositories.PostgresRepositories.TableViewDefinitionRepository.CreateTableViewDefinition(ctx, tvDef)
}
}
if !nurtureFound {
tvDef, err := DefaultTableViewDefinitionNurture(span)
if err == nil {
tvDef.Tenant = tenant
tvDef.UserId = userId
r.Services.Repositories.PostgresRepositories.TableViewDefinitionRepository.CreateTableViewDefinition(ctx, tvDef)
}
}
result := r.Services.Repositories.PostgresRepositories.TableViewDefinitionRepository.GetTableViewDefinitions(ctx, tenant, userId)
if result.Error != nil {
tracing.TraceErr(span, result.Error)
graphql.AddErrorf(ctx, "Failed to get table view definitions")
return nil, nil
}
tableViewDefinitions, _ = result.Result.([]postgresEntity.TableViewDefinition)
}
return mapper.MapTableViewDefinitionsToModel(tableViewDefinitions), nil
}