package activityhistory import ( "bytes" "context" "strings" "time" "github.com/vektah/gqlparser/v2/formatter" "edge-infra.dev/pkg/edge/api/graphqlhelpers" "edge-infra.dev/pkg/edge/api/middleware" "edge-infra.dev/pkg/lib/fog" "github.com/99designs/gqlgen/graphql" ) func (e ActivityHistory) ExtensionName() string { return "APIErrorConverter" } func (e ActivityHistory) Validate(_ graphql.ExecutableSchema) error { return nil } func (e ActivityHistory) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response { log := fog.FromContext(ctx) resp := next(ctx) rctx := graphql.GetOperationContext(ctx) if !graphqlhelpers.IsMutation(rctx) { return resp } rawquery := graphqlhelpers.GetRawQuery(rctx) schema, err := graphqlhelpers.ParseQuery(rawquery) if err != nil { return resp } graphqlhelpers.SanitizeDocument(schema) variables := graphqlhelpers.GetVariables(rctx) graphqlhelpers.UpdateQueryWithVariables(schema, variables) buf := bytes.NewBuffer(nil) formatter.NewFormatter(buf).FormatQueryDocument(schema) opts := []Option{ WithUserCtx(middleware.ForContext(ctx)), WithErrors(graphql.GetErrors(ctx)), WithStatus(getResponseStatus(resp)), WithInput(buf.String()), WithTimestamp(time.Now().UTC()), WithAction(strings.Join(graphqlhelpers.GetQueryNames(schema), ",")), } newAction := New(e.RootOrg, e.Resolver.TenantService).Map(opts...) if newAction.TenantEdgeID != "" { if err := e.Resolver.ActivityService.CreateAction(ctx, *newAction); err != nil { log.Error(err, "unable to record activity history action") } } return resp } func getResponseStatus(resp *graphql.Response) string { const nullStr = "null" switch { case resp != nil && len(resp.Errors) == 0 && string(resp.Data) != nullStr: return "Success" default: return "Failure" } }