-
Notifications
You must be signed in to change notification settings - Fork 12
/
cache.resolvers.go
114 lines (96 loc) · 4.22 KB
/
cache.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
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"
"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"
)
// GlobalCache is the resolver for the global_Cache field.
func (r *queryResolver) GlobalCache(ctx context.Context) (*model.GlobalCache, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "GlobalCache.global_Cache", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
response := &model.GlobalCache{}
tenantName := common.GetTenantFromContext(ctx)
userId := common.GetUserIdFromContext(ctx)
userEmail := common.GetUserEmailFromContext(ctx)
user, err := r.Services.UserService.GetById(ctx, userId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "failed GlobalCache - find user by id")
return nil, nil
}
response.User = mapper.MapEntityToUser(user)
isOwner, err := r.Services.UserService.IsOwner(ctx, user.Id)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "failed GlobalCache - is owner")
return nil, nil
}
response.IsOwner = *isOwner
if userEmail != "" {
privateKey, err := r.Services.CommonServices.PostgresRepositories.GoogleServiceAccountKeyRepository.GetApiKeyByTenantService(ctx, tenantName, postgresEntity.GSUITE_SERVICE_PRIVATE_KEY)
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
serviceEmail, err := r.Services.CommonServices.PostgresRepositories.GoogleServiceAccountKeyRepository.GetApiKeyByTenantService(ctx, tenantName, postgresEntity.GSUITE_SERVICE_EMAIL_ADDRESS)
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
if privateKey != "" && serviceEmail != "" {
response.IsGoogleActive = true
response.IsGoogleTokenExpired = false
} else {
userGoogleOauthToken, err := r.Services.CommonAuthServices.CommonAuthRepositories.OAuthTokenRepository.GetForEmail(ctx, "google", tenantName, userEmail)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed GlobalCache - get gmail token needs manual refresh")
return nil, nil
}
if userGoogleOauthToken != nil {
response.IsGoogleActive = userGoogleOauthToken.GmailSyncEnabled
response.IsGoogleTokenExpired = userGoogleOauthToken.NeedsManualRefresh
}
}
}
response.GCliCache = r.Services.Cache.GetStates() //pre-populate with states
minARRForecastValue, maxARRForecastValue, err := r.Services.OrganizationService.GetMinMaxRenewalForecastArr(ctx)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed GlobalCache - get min max arr forecast")
return nil, nil
}
response.MinARRForecastValue = minARRForecastValue
response.MaxARRForecastValue = maxARRForecastValue
contractsExistForTenant, err := r.Services.ContractService.ContractsExistForTenant(ctx)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed GlobalCache - contracts exist for tenant")
return nil, nil
}
response.ContractsExist = contractsExistForTenant
tenantSettings, err := r.Services.TenantService.GetTenantSettings(ctx)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed GlobalCache - get tenant billing profiles")
return nil, nil
}
if tenantSettings != nil && tenantSettings.LogoRepositoryFileId != "" {
attachmentById, err := r.Services.AttachmentService.GetAttachmentById(ctx, tenantSettings.LogoRepositoryFileId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed GlobalCache - get tenant logo attachment by id")
return nil, nil
}
response.CdnLogoURL = attachmentById.CdnUrl
}
return response, nil
}