Skip to content

Commit 7f2d351

Browse files
authored
fix(cat-voices): Generate fresh document version before publishing (#2295)
* fix: generate fresh document version * fix: version count * fix: latest version
1 parent b809583 commit 7f2d351

File tree

6 files changed

+216
-56
lines changed

6 files changed

+216
-56
lines changed

catalyst_voices/packages/internal/catalyst_voices_blocs/lib/src/proposal_builder/proposal_builder_bloc.dart

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -557,15 +557,28 @@ final class ProposalBuilderBloc
557557
Future<void> _publishAndSubmitProposalForReview(
558558
Emitter<ProposalBuilderState> emit,
559559
) async {
560+
final currentRef = state.metadata.documentRef!;
560561
final updatedRef = await _proposalService.publishProposal(
561562
document: _buildDocumentData(),
562563
);
563564

565+
List<DocumentVersion>? updatedVersions;
566+
if (updatedRef != currentRef) {
567+
// if a new ref has been created we need to recreate
568+
// the version history to reflect it, drop the old one
569+
// because the new one overrode it
570+
updatedVersions = _recreateDocumentVersionsWithNewRef(
571+
newRef: updatedRef,
572+
removedRef: currentRef,
573+
);
574+
}
575+
564576
_updateMetadata(
565577
emit,
566578
documentRef: updatedRef,
567579
originalDocumentRef: updatedRef,
568580
publish: ProposalPublish.publishedDraft,
581+
versions: updatedVersions,
569582
);
570583

571584
await _proposalService.submitProposalForReview(
@@ -589,15 +602,28 @@ final class ProposalBuilderBloc
589602
_logger.info('Publishing proposal');
590603
emit(state.copyWith(isChanging: true));
591604

605+
final currentRef = state.metadata.documentRef!;
592606
final updatedRef = await _proposalService.publishProposal(
593607
document: _buildDocumentData(),
594608
);
595609

610+
List<DocumentVersion>? updatedVersions;
611+
if (updatedRef != currentRef) {
612+
// if a new ref has been created we need to recreate
613+
// the version history to reflect it, drop the old one
614+
// because the new one overrode it
615+
updatedVersions = _recreateDocumentVersionsWithNewRef(
616+
newRef: updatedRef,
617+
removedRef: currentRef,
618+
);
619+
}
620+
596621
_updateMetadata(
597622
emit,
598623
documentRef: updatedRef,
599624
originalDocumentRef: updatedRef,
600625
publish: ProposalPublish.publishedDraft,
626+
versions: updatedVersions,
601627
);
602628
emitSignal(const PublishedProposalBuilderSignal());
603629
} catch (error, stackTrace) {
@@ -654,24 +680,28 @@ final class ProposalBuilderBloc
654680
);
655681
}
656682

657-
List<DocumentVersion> _recreateDocumentVersionsWithNewRef(
658-
DocumentRef newRef,
659-
) {
660-
final current = state.metadata.versions;
683+
List<DocumentVersion> _recreateDocumentVersionsWithNewRef({
684+
DocumentRef? newRef,
685+
DocumentRef? removedRef,
686+
}) {
687+
final current =
688+
state.metadata.versions.whereNot((e) => e.id == removedRef?.version);
689+
final currentId = newRef?.version ?? current.last.id;
661690

662691
return [
663-
...current.map(
664-
(e) => e.copyWith(
665-
isCurrent: false,
666-
isLatest: false,
692+
for (final (index, ver) in current.indexed)
693+
ver.copyWith(
694+
number: index + 1,
695+
isCurrent: ver.id == currentId,
696+
isLatest: ver.id == currentId,
697+
),
698+
if (newRef != null)
699+
DocumentVersion(
700+
id: newRef.version!,
701+
number: current.length + 1,
702+
isCurrent: newRef.version == currentId,
703+
isLatest: newRef.version == currentId,
667704
),
668-
),
669-
DocumentVersion(
670-
id: newRef.version!,
671-
number: current.length + 1,
672-
isCurrent: true,
673-
isLatest: true,
674-
),
675705
];
676706
}
677707

@@ -688,7 +718,7 @@ final class ProposalBuilderBloc
688718
if (updatedRef != currentRef) {
689719
// if a new ref has been created we need to recreate
690720
// the version history to reflect it
691-
updatedVersions = _recreateDocumentVersionsWithNewRef(updatedRef);
721+
updatedVersions = _recreateDocumentVersionsWithNewRef(newRef: updatedRef);
692722
}
693723

694724
_updateMetadata(

catalyst_voices/packages/internal/catalyst_voices_models/lib/src/campaign/campaign_base.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ base class CampaignBase extends Equatable {
2727

2828
// TODO(damian-molinski): this should come from api
2929
SignedDocumentRef get proposalTemplateRef {
30-
return const SignedDocumentRef(id: '0194d492-1daa-75b5-b4a4-5cf331cd8d1a');
30+
return const SignedDocumentRef(
31+
id: '0194d492-1daa-75b5-b4a4-5cf331cd8d1a',
32+
version: '0194d492-1daa-75b5-b4a4-5cf331cd8d1a',
33+
);
3134
}
3235

3336
@override

catalyst_voices/packages/internal/catalyst_voices_models/lib/src/document/data/document_data.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,12 @@ final class DocumentData extends Equatable {
2828

2929
/// Syntax sugar. Should use [DocumentDataMetadata.selfRef].
3030
DocumentRef get ref => metadata.selfRef;
31+
32+
/// Update document data with a new [ref].
33+
DocumentData copyWithSelfRef({required DocumentRef selfRef}) {
34+
return DocumentData(
35+
metadata: metadata.copyWith(selfRef: selfRef),
36+
content: content,
37+
);
38+
}
3139
}

0 commit comments

Comments
 (0)