-
Notifications
You must be signed in to change notification settings - Fork 12
/
tenant.resolvers.go
218 lines (188 loc) · 8.98 KB
/
tenant.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
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.47
import (
"context"
"errors"
"fmt"
"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"
"github.com/opentracing/opentracing-go/log"
)
// TenantMerge is the resolver for the tenant_Merge field.
func (r *mutationResolver) TenantMerge(ctx context.Context, tenant model.TenantInput) (string, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.TenantMerge", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
newTenant, err := r.Services.TenantService.Merge(ctx, mapper.MapTenantInputToEntity(tenant))
if err != nil {
return "", fmt.Errorf("TenantMerge: %w", err)
}
return newTenant.Name, nil
}
// TenantAddBillingProfile is the resolver for the tenant_AddBillingProfile field.
func (r *mutationResolver) TenantAddBillingProfile(ctx context.Context, input model.TenantBillingProfileInput) (*model.TenantBillingProfile, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.TenantAddBillingProfile", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
profileId, err := r.Services.TenantService.CreateTenantBillingProfile(ctx, input)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to create tenant billing profile")
return &model.TenantBillingProfile{ID: profileId}, err
}
createdTenantBillingProfileEntity, err := r.Services.TenantService.GetTenantBillingProfile(ctx, profileId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Tenant billing profile not yet available.")
return &model.TenantBillingProfile{ID: profileId}, nil
}
span.LogFields(log.String("response.tenantBillingProfileId", profileId))
return mapper.MapEntityToTenantBillingProfile(createdTenantBillingProfileEntity), nil
}
// TenantUpdateBillingProfile is the resolver for the tenant_UpdateBillingProfile field.
func (r *mutationResolver) TenantUpdateBillingProfile(ctx context.Context, input model.TenantBillingProfileUpdateInput) (*model.TenantBillingProfile, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.TenantUpdateBillingProfile", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
if input.ID == "" {
err := errors.New("missing tenant billing profile id")
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Missing tenant billing profile id")
return nil, nil
}
err := r.Services.TenantService.UpdateTenantBillingProfile(ctx, input)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update billing profile")
return nil, err
}
updatedTenantBillingProfileEntity, err := r.Services.TenantService.GetTenantBillingProfile(ctx, input.ID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to fetch tenant billing profile details")
return nil, nil
}
return mapper.MapEntityToTenantBillingProfile(updatedTenantBillingProfileEntity), nil
}
// TenantUpdateSettings is the resolver for the tenant_UpdateSettings field.
func (r *mutationResolver) TenantUpdateSettings(ctx context.Context, input *model.TenantSettingsInput) (*model.TenantSettings, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.TenantUpdateSettings", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
err := r.Services.TenantService.UpdateTenantSettings(ctx, input)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update tenant settings")
return nil, err
}
updatedTenantSettingsEntity, err := r.Services.TenantService.GetTenantSettings(ctx)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to fetch tenant settings")
return nil, nil
}
return mapper.MapEntityToTenantSettings(updatedTenantSettingsEntity), nil
}
// TenantHardDelete is the resolver for the tenant_hardDelete field.
func (r *mutationResolver) TenantHardDelete(ctx context.Context, tenant string, confirmTenant string) (bool, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.TenantHardDelete", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("request.tenant", tenant), log.String("request.confirmTenant", confirmTenant))
if tenant != confirmTenant {
err := errors.New("tenant name does not match the confirmation tenant name")
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Tenant name does not match the confirmation tenant name")
return false, nil
}
err := r.Services.TenantService.HardDelete(ctx, tenant)
if err != nil {
return false, err
}
return true, nil
}
// Tenant is the resolver for the tenant field.
func (r *queryResolver) Tenant(ctx context.Context) (string, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.Tenant", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
return common.GetTenantFromContext(ctx), nil
}
// TenantByWorkspace is the resolver for the tenant_ByWorkspace field.
func (r *queryResolver) TenantByWorkspace(ctx context.Context, workspace model.WorkspaceInput) (*string, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.TenantByWorkspace", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("request.workspace", workspace.Name))
tenant, err := r.Services.TenantService.GetTenantForWorkspace(ctx, mapper.MapWorkspaceInputToEntity(workspace))
if err != nil {
return nil, err
}
if tenant == nil || tenant.Name == "" {
return nil, nil
}
return &tenant.Name, nil
}
// TenantByEmail is the resolver for the tenant_ByEmail field.
func (r *queryResolver) TenantByEmail(ctx context.Context, email string) (*string, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.TenantByEmail", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("request.email", email))
tenant, err := r.Services.TenantService.GetTenantForUserEmail(ctx, email)
if err != nil {
return nil, err
}
if tenant == nil || tenant.Name == "" {
return nil, nil
}
return &tenant.Name, nil
}
// TenantBillingProfiles is the resolver for the tenantBillingProfiles field.
func (r *queryResolver) TenantBillingProfiles(ctx context.Context) ([]*model.TenantBillingProfile, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.TenantBillingProfiles", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tenantBillingProfileEntities, err := r.Services.TenantService.GetTenantBillingProfiles(ctx)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to fetch billing profiles")
return nil, nil
}
return mapper.MapEntitiesToTenantBillingProfiles(tenantBillingProfileEntities), nil
}
// TenantBillingProfile is the resolver for the tenantBillingProfile field.
func (r *queryResolver) TenantBillingProfile(ctx context.Context, id string) (*model.TenantBillingProfile, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.TenantBillingProfile", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("request.tenantBillingProfileId", id))
tenantBillingProfileEntity, err := r.Services.TenantService.GetTenantBillingProfile(ctx, id)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to fetch tenant billing profile details")
return nil, nil
}
return mapper.MapEntityToTenantBillingProfile(tenantBillingProfileEntity), nil
}
// TenantSettings is the resolver for the tenantSettings field.
func (r *queryResolver) TenantSettings(ctx context.Context) (*model.TenantSettings, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.TenantSettings", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tenantSettingsEntity, err := r.Services.TenantService.GetTenantSettings(ctx)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to fetch tenant settings")
return nil, nil
}
return mapper.MapEntityToTenantSettings(tenantSettingsEntity), nil
}