package console_v1 // 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.94 import ( "context" "encoding/json" "errors" "github.com/vikstrous/dataloadgen" "go.gearno.de/kit/log" "go.probo.inc/probo/pkg/coredata" "go.probo.inc/probo/pkg/page" "go.probo.inc/probo/pkg/probo" "go.probo.inc/probo/pkg/server/api/authn" "go.probo.inc/probo/pkg/server/api/console/v1/dataloader" "go.probo.inc/probo/pkg/server/api/console/v1/schema" "go.probo.inc/probo/pkg/server/api/console/v1/types" "go.probo.inc/probo/pkg/server/gqlutils" "go.probo.inc/probo/pkg/validator" ) // Organization is the resolver for the organization field. func (r *frameworkResolver) Organization(ctx context.Context, obj *types.Framework) (*types.Organization, error) { if _, err := r.authorize(ctx, obj.ID, probo.ActionOrganizationGet); err != nil { return nil, err } loaders := dataloader.FromContext(ctx) organization, err := loaders.Organization.Load(ctx, obj.Organization.ID) if err != nil { if errors.Is(err, coredata.ErrResourceNotFound) || errors.Is(err, dataloadgen.ErrNotFound) { return nil, gqlutils.NotFound(ctx, err) } r.logger.ErrorCtx(ctx, "cannot load organization", log.Error(err)) return nil, gqlutils.Internal(ctx) } return types.NewOrganization(organization), nil } // Controls is the resolver for the controls field. func (r *frameworkResolver) Controls(ctx context.Context, obj *types.Framework, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.ControlOrderBy, filter *types.ControlFilter) (*types.ControlConnection, error) { scope, err := r.authorize(ctx, obj.ID, probo.ActionControlList) if err != nil { return nil, err } pageOrderBy := page.OrderBy[coredata.ControlOrderField]{ Field: coredata.ControlOrderFieldCreatedAt, Direction: page.OrderDirectionDesc, } if orderBy != nil { pageOrderBy = page.OrderBy[coredata.ControlOrderField]{ Field: orderBy.Field, Direction: orderBy.Direction, } } cursor := types.NewCursor(first, after, last, before, pageOrderBy) var controlFilter = coredata.NewControlFilter(nil) if filter != nil { controlFilter = coredata.NewControlFilter(filter.Query) } page, err := r.probo.Controls.ListForFrameworkID(ctx, scope, obj.ID, cursor, controlFilter) if err != nil { r.logger.ErrorCtx(ctx, "cannot list controls", log.Error(err)) return nil, gqlutils.Internal(ctx) } return types.NewControlConnection(page, r, obj.ID, controlFilter), nil } // LightLogo is the resolver for the lightLogo field. func (r *frameworkResolver) LightLogo(ctx context.Context, obj *types.Framework) (*types.File, error) { if _, err := r.authorize(ctx, obj.ID, probo.ActionFrameworkGet); err != nil { return nil, err } if obj.LightLogo == nil { return nil, nil } return r.loadFile(ctx, obj.LightLogo.ID) } // DarkLogo is the resolver for the darkLogo field. func (r *frameworkResolver) DarkLogo(ctx context.Context, obj *types.Framework) (*types.File, error) { if _, err := r.authorize(ctx, obj.ID, probo.ActionFrameworkGet); err != nil { return nil, err } if obj.DarkLogo == nil { return nil, nil } return r.loadFile(ctx, obj.DarkLogo.ID) } // Permission is the resolver for the permission field. func (r *frameworkResolver) Permission(ctx context.Context, obj *types.Framework, action string) (bool, error) { return r.Resolver.Permission(ctx, obj, action) } // TotalCount is the resolver for the totalCount field. func (r *frameworkConnectionResolver) TotalCount(ctx context.Context, obj *types.FrameworkConnection) (int, error) { scope, err := r.authorize(ctx, obj.ParentID, probo.ActionFrameworkList) if err != nil { return 0, err } switch obj.Resolver.(type) { case *organizationResolver: count, err := r.probo.Frameworks.CountForOrganizationID(ctx, scope, obj.ParentID) if err != nil { r.logger.ErrorCtx(ctx, "cannot count frameworks", log.Error(err)) return 0, gqlutils.Internal(ctx) } return count, nil } r.logger.ErrorCtx(ctx, "unsupported resolver") return 0, gqlutils.Internal(ctx) } // CreateFramework is the resolver for the createFramework field. func (r *mutationResolver) CreateFramework(ctx context.Context, input types.CreateFrameworkInput) (*types.CreateFrameworkPayload, error) { scope, err := r.authorize(ctx, input.OrganizationID, probo.ActionFrameworkCreate) if err != nil { return nil, err } framework, err := r.probo.Frameworks.Create( ctx, scope, probo.CreateFrameworkRequest{ OrganizationID: input.OrganizationID, Name: input.Name, }, ) if err != nil { if validationErrors, ok := errors.AsType[validator.ValidationErrors](err); ok { return nil, gqlutils.InvalidValidationErrors(ctx, validationErrors) } r.logger.ErrorCtx(ctx, "cannot create framework", log.Error(err)) return nil, gqlutils.Internal(ctx) } return &types.CreateFrameworkPayload{ FrameworkEdge: types.NewFrameworkEdge(framework, coredata.FrameworkOrderFieldCreatedAt), }, nil } // UpdateFramework is the resolver for the updateFramework field. func (r *mutationResolver) UpdateFramework(ctx context.Context, input types.UpdateFrameworkInput) (*types.UpdateFrameworkPayload, error) { scope, err := r.authorize(ctx, input.ID, probo.ActionFrameworkUpdate) if err != nil { return nil, err } framework, err := r.probo.Frameworks.Update( ctx, scope, probo.UpdateFrameworkRequest{ ID: input.ID, Name: input.Name, Description: gqlutils.UnwrapOmittable(input.Description), }, ) if err != nil { if validationErrors, ok := errors.AsType[validator.ValidationErrors](err); ok { return nil, gqlutils.InvalidValidationErrors(ctx, validationErrors) } r.logger.ErrorCtx(ctx, "cannot update framework", log.Error(err)) return nil, gqlutils.Internal(ctx) } return &types.UpdateFrameworkPayload{ Framework: types.NewFramework(framework), }, nil } // ImportFramework is the resolver for the importFramework field. func (r *mutationResolver) ImportFramework(ctx context.Context, input types.ImportFrameworkInput) (*types.ImportFrameworkPayload, error) { scope, err := r.authorize(ctx, input.OrganizationID, probo.ActionFrameworkImport) if err != nil { return nil, err } req := probo.ImportFrameworkRequest{} if err := json.NewDecoder(input.File.File).Decode(&req.Framework); err != nil { r.logger.ErrorCtx(ctx, "cannot decode framework", log.Error(err)) return nil, gqlutils.Internal(ctx) } framework, err := r.probo.Frameworks.Import(ctx, scope, input.OrganizationID, req) if err != nil { if errors.Is(err, coredata.ErrResourceAlreadyExists) { return nil, gqlutils.Conflict(ctx, err) } r.logger.ErrorCtx(ctx, "cannot import framework", log.Error(err)) return nil, gqlutils.Internal(ctx) } return &types.ImportFrameworkPayload{ FrameworkEdge: types.NewFrameworkEdge(framework, coredata.FrameworkOrderFieldCreatedAt), }, nil } // DeleteFramework is the resolver for the deleteFramework field. func (r *mutationResolver) DeleteFramework(ctx context.Context, input types.DeleteFrameworkInput) (*types.DeleteFrameworkPayload, error) { scope, err := r.authorize(ctx, input.FrameworkID, probo.ActionFrameworkDelete) if err != nil { return nil, err } if err := r.probo.Frameworks.Delete(ctx, scope, input.FrameworkID); err != nil { r.logger.ErrorCtx(ctx, "cannot delete framework", log.Error(err)) return nil, gqlutils.Internal(ctx) } return &types.DeleteFrameworkPayload{ DeletedFrameworkID: input.FrameworkID, }, nil } // ExportFramework is the resolver for the exportFramework field. func (r *mutationResolver) ExportFramework(ctx context.Context, input types.ExportFrameworkInput) (*types.ExportFrameworkPayload, error) { scope, err := r.authorize(ctx, input.FrameworkID, probo.ActionFrameworkExport) if err != nil { return nil, err } identity := authn.IdentityFromContext(ctx) exportJob, exportErr := r.probo.Frameworks.RequestExport( ctx, scope, input.FrameworkID, identity.EmailAddress, identity.FullName, ) if exportErr != nil { r.logger.ErrorCtx(ctx, "cannot export framework", log.Error(exportErr)) return nil, gqlutils.Internal(ctx) } return &types.ExportFrameworkPayload{ ExportJobID: exportJob.ID, }, nil } // Framework returns schema.FrameworkResolver implementation. func (r *Resolver) Framework() schema.FrameworkResolver { return &frameworkResolver{r} } // FrameworkConnection returns schema.FrameworkConnectionResolver implementation. func (r *Resolver) FrameworkConnection() schema.FrameworkConnectionResolver { return &frameworkConnectionResolver{r} } type ( frameworkResolver struct{ *Resolver } frameworkConnectionResolver struct{ *Resolver } )