github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/document/resolver.go (about) 1 package document 2 3 import ( 4 "context" 5 6 "github.com/kyma-incubator/compass/components/director/pkg/resource" 7 "github.com/kyma-incubator/compass/components/director/pkg/str" 8 9 dataloader "github.com/kyma-incubator/compass/components/director/internal/dataloaders" 10 11 "github.com/kyma-incubator/compass/components/director/pkg/log" 12 13 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 14 15 "github.com/kyma-incubator/compass/components/director/pkg/persistence" 16 17 "github.com/pkg/errors" 18 19 "github.com/kyma-incubator/compass/components/director/internal/model" 20 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 21 ) 22 23 // DocumentService missing godoc 24 // 25 //go:generate mockery --name=DocumentService --output=automock --outpkg=automock --case=underscore --disable-version-string 26 type DocumentService interface { 27 CreateInBundle(ctx context.Context, resourceType resource.Type, resourceID string, bundleID string, in model.DocumentInput) (string, error) 28 Get(ctx context.Context, id string) (*model.Document, error) 29 Delete(ctx context.Context, id string) error 30 ListFetchRequests(ctx context.Context, documentIDs []string) ([]*model.FetchRequest, error) 31 } 32 33 // DocumentConverter missing godoc 34 // 35 //go:generate mockery --name=DocumentConverter --output=automock --outpkg=automock --case=underscore --disable-version-string 36 type DocumentConverter interface { 37 ToGraphQL(in *model.Document) *graphql.Document 38 InputFromGraphQL(in *graphql.DocumentInput) (*model.DocumentInput, error) 39 } 40 41 // FetchRequestConverter missing godoc 42 // 43 //go:generate mockery --name=FetchRequestConverter --output=automock --outpkg=automock --case=underscore --disable-version-string 44 type FetchRequestConverter interface { 45 ToGraphQL(in *model.FetchRequest) (*graphql.FetchRequest, error) 46 InputFromGraphQL(in *graphql.FetchRequestInput) (*model.FetchRequestInput, error) 47 } 48 49 // ApplicationService missing godoc 50 // 51 //go:generate mockery --name=ApplicationService --output=automock --outpkg=automock --case=underscore --disable-version-string 52 type ApplicationService interface { 53 Exist(ctx context.Context, id string) (bool, error) 54 } 55 56 // BundleService missing godoc 57 // 58 //go:generate mockery --name=BundleService --output=automock --outpkg=automock --case=underscore --disable-version-string 59 type BundleService interface { 60 Get(ctx context.Context, id string) (*model.Bundle, error) 61 } 62 63 // Resolver missing godoc 64 type Resolver struct { 65 transact persistence.Transactioner 66 svc DocumentService 67 appSvc ApplicationService 68 bndlSvc BundleService 69 converter DocumentConverter 70 frConverter FetchRequestConverter 71 } 72 73 // NewResolver missing godoc 74 func NewResolver(transact persistence.Transactioner, svc DocumentService, appSvc ApplicationService, bndlSvc BundleService, frConverter FetchRequestConverter) *Resolver { 75 return &Resolver{ 76 transact: transact, 77 svc: svc, 78 appSvc: appSvc, 79 bndlSvc: bndlSvc, 80 frConverter: frConverter, 81 converter: &converter{frConverter: frConverter}, 82 } 83 } 84 85 // AddDocumentToBundle missing godoc 86 func (r *Resolver) AddDocumentToBundle(ctx context.Context, bundleID string, in graphql.DocumentInput) (*graphql.Document, error) { 87 tx, err := r.transact.Begin() 88 if err != nil { 89 return nil, err 90 } 91 defer r.transact.RollbackUnlessCommitted(ctx, tx) 92 93 ctx = persistence.SaveToContext(ctx, tx) 94 95 convertedIn, err := r.converter.InputFromGraphQL(&in) 96 if err != nil { 97 return nil, errors.Wrap(err, "while converting DocumentInput from GraphQL") 98 } 99 100 bndl, err := r.bndlSvc.Get(ctx, bundleID) 101 if err != nil { 102 if apperrors.IsNotFoundError(err) { 103 return nil, apperrors.NewInvalidDataError("cannot add Document to not existing Bundle") 104 } 105 return nil, errors.Wrapf(err, "while getting bundle %s", bundleID) 106 } 107 108 id, err := r.svc.CreateInBundle(ctx, resource.Application, str.PtrStrToStr(bndl.ApplicationID), bundleID, *convertedIn) 109 if err != nil { 110 return nil, err 111 } 112 113 document, err := r.svc.Get(ctx, id) 114 if err != nil { 115 return nil, err 116 } 117 118 err = tx.Commit() 119 if err != nil { 120 return nil, err 121 } 122 123 gqlDocument := r.converter.ToGraphQL(document) 124 125 return gqlDocument, nil 126 } 127 128 // DeleteDocument missing godoc 129 func (r *Resolver) DeleteDocument(ctx context.Context, id string) (*graphql.Document, error) { 130 tx, err := r.transact.Begin() 131 if err != nil { 132 return nil, err 133 } 134 defer r.transact.RollbackUnlessCommitted(ctx, tx) 135 136 ctx = persistence.SaveToContext(ctx, tx) 137 138 document, err := r.svc.Get(ctx, id) 139 if err != nil { 140 return nil, err 141 } 142 143 deletedDocument := r.converter.ToGraphQL(document) 144 145 err = r.svc.Delete(ctx, id) 146 if err != nil { 147 return nil, err 148 } 149 150 err = tx.Commit() 151 if err != nil { 152 return nil, err 153 } 154 155 return deletedDocument, nil 156 } 157 158 // FetchRequest missing godoc 159 func (r *Resolver) FetchRequest(ctx context.Context, obj *graphql.Document) (*graphql.FetchRequest, error) { 160 params := dataloader.ParamFetchRequestDocument{ID: obj.ID, Ctx: ctx} 161 return dataloader.ForFetchRequestDocument(ctx).FetchRequestDocumentByID.Load(params) 162 } 163 164 // FetchRequestDocumentDataLoader missing godoc 165 func (r *Resolver) FetchRequestDocumentDataLoader(keys []dataloader.ParamFetchRequestDocument) ([]*graphql.FetchRequest, []error) { 166 if len(keys) == 0 { 167 return nil, []error{apperrors.NewInternalError("No Documents found")} 168 } 169 170 ctx := keys[0].Ctx 171 documentIDs := make([]string, 0, len(keys)) 172 for _, key := range keys { 173 if key.ID == "" { 174 return nil, []error{apperrors.NewInternalError("Cannot fetch FetchRequest. Document ID is empty")} 175 } 176 documentIDs = append(documentIDs, key.ID) 177 } 178 179 tx, err := r.transact.Begin() 180 if err != nil { 181 return nil, []error{err} 182 } 183 defer r.transact.RollbackUnlessCommitted(ctx, tx) 184 185 ctx = persistence.SaveToContext(ctx, tx) 186 187 fetchRequests, err := r.svc.ListFetchRequests(ctx, documentIDs) 188 if err != nil { 189 return nil, []error{err} 190 } 191 192 if fetchRequests == nil { 193 return nil, nil 194 } 195 196 gqlFetchRequests := make([]*graphql.FetchRequest, 0, len(fetchRequests)) 197 for _, fr := range fetchRequests { 198 fetchRequest, err := r.frConverter.ToGraphQL(fr) 199 if err != nil { 200 return nil, []error{err} 201 } 202 gqlFetchRequests = append(gqlFetchRequests, fetchRequest) 203 } 204 205 if err = tx.Commit(); err != nil { 206 return nil, []error{err} 207 } 208 209 log.C(ctx).Infof("Successfully fetched requests for Documents %v", documentIDs) 210 return gqlFetchRequests, nil 211 }