From 452c1b5381bea880c4a7dc32b9aa856e7728b185 Mon Sep 17 00:00:00 2001 From: Bryan Frimin Date: Thu, 10 Jul 2025 12:34:37 +0200 Subject: [PATCH] Cleanup publish version Signed-off-by: Bryan Frimin --- pkg/probo/document_service.go | 44 +++++++++++++++--------- pkg/server/api/console/v1/v1_resolver.go | 9 ++++- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/pkg/probo/document_service.go b/pkg/probo/document_service.go index 435d916fd..937f47893 100644 --- a/pkg/probo/document_service.go +++ b/pkg/probo/document_service.go @@ -58,6 +58,12 @@ type ( OrganizationID gid.GID `json:"organization_id"` PeopleID gid.GID `json:"people_id"` } + + BulkPublishVersionsRequest struct { + DocumentIDs []gid.GID + PublishedBy gid.GID + Changelog string + } ) const ( @@ -147,9 +153,7 @@ func (s DocumentService) GenerateChangelog( func (s *DocumentService) BulkPublishVersions( ctx context.Context, - documentIds []gid.GID, - publishedBy gid.GID, - changelog string, + req BulkPublishVersionsRequest, ) ([]*coredata.DocumentVersion, []*coredata.Document, error) { var publishedVersions []*coredata.DocumentVersion var updatedDocuments []*coredata.Document @@ -157,8 +161,13 @@ func (s *DocumentService) BulkPublishVersions( err := s.svc.pg.WithTx( ctx, func(tx pg.Conn) error { - for _, documentID := range documentIds { - document, version, err := s.publishVersionInTx(ctx, tx, documentID, publishedBy, &changelog) + people := &coredata.People{} + if err := people.LoadByID(ctx, tx, s.svc.scope, req.PublishedBy); err != nil { + return fmt.Errorf("cannot load people: %w", err) + } + + for _, documentID := range req.DocumentIDs { + document, version, err := s.publishVersionInTx(ctx, tx, documentID, people, &req.Changelog) if err != nil { return fmt.Errorf("cannot publish document %q: %w", documentID, err) } @@ -191,8 +200,18 @@ func (s *DocumentService) PublishVersion( ctx, func(tx pg.Conn) error { var err error - document, documentVersion, err = s.publishVersionInTx(ctx, tx, documentID, publishedBy, changelog) - return err + + people := &coredata.People{} + if err := people.LoadByID(ctx, tx, s.svc.scope, publishedBy); err != nil { + return fmt.Errorf("cannot load people: %w", err) + } + + document, documentVersion, err = s.publishVersionInTx(ctx, tx, documentID, people, changelog) + if err != nil { + return fmt.Errorf("cannot publish version: %w", err) + } + + return nil }, ) @@ -207,19 +226,14 @@ func (s *DocumentService) publishVersionInTx( ctx context.Context, tx pg.Conn, documentID gid.GID, - publishedBy gid.GID, + publishedBy *coredata.People, changelog *string, ) (*coredata.Document, *coredata.DocumentVersion, error) { document := &coredata.Document{} documentVersion := &coredata.DocumentVersion{} publishedVersion := &coredata.DocumentVersion{} - people := &coredata.People{} now := time.Now() - if err := people.LoadByID(ctx, tx, s.svc.scope, publishedBy); err != nil { - return nil, nil, fmt.Errorf("cannot load people: %w", err) - } - if err := document.LoadByID(ctx, tx, s.svc.scope, documentID); err != nil { return nil, nil, fmt.Errorf("cannot load document %q: %w", documentID, err) } @@ -252,9 +266,7 @@ func (s *DocumentService) publishVersionInTx( documentVersion.Status = coredata.DocumentStatusPublished documentVersion.PublishedAt = &now - if publishedBy != gid.Nil { - documentVersion.PublishedBy = &people.ID - } + documentVersion.PublishedBy = &publishedBy.ID documentVersion.UpdatedAt = now if err := document.Update(ctx, tx, s.svc.scope); err != nil { diff --git a/pkg/server/api/console/v1/v1_resolver.go b/pkg/server/api/console/v1/v1_resolver.go index 5d48102cf..d21750d67 100644 --- a/pkg/server/api/console/v1/v1_resolver.go +++ b/pkg/server/api/console/v1/v1_resolver.go @@ -1854,7 +1854,14 @@ func (r *mutationResolver) BulkPublishDocumentVersions(ctx context.Context, inpu panic(fmt.Errorf("cannot get people: %w", err)) } - documentVersions, documents, err := prb.Documents.BulkPublishVersions(ctx, input.DocumentIds, people.ID, input.Changelog) + documentVersions, documents, err := prb.Documents.BulkPublishVersions( + ctx, + probo.BulkPublishVersionsRequest{ + DocumentIDs: input.DocumentIds, + PublishedBy: people.ID, + Changelog: input.Changelog, + }, + ) if err != nil { panic(fmt.Errorf("cannot bulk publish document versions: %w", err)) }