Skip to content

Commit b7e6266

Browse files
committed
Code review fixes
1 parent 32e2400 commit b7e6266

File tree

10 files changed

+66
-103
lines changed

10 files changed

+66
-103
lines changed

src/RealtimeServer/scriptureforge/models/text-info.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ export interface Chapter {
44
isValid: boolean;
55
permissions: { [userRef: string]: string };
66
hasAudio?: boolean;
7+
/**
8+
* @deprecated Use SFProjectService.hasDraft() instead
9+
*/
710
hasDraft?: boolean;
811
draftApplied?: boolean;
912
}

src/RealtimeServer/scriptureforge/services/sf-project-migrations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ class SFProjectMigration28 extends DocMigration {
638638
const ops: Op[] = [];
639639
if (doc.data?.texts != null && doc.data?.translateConfig?.draftConfig?.currentScriptureRange == null) {
640640
const currentScriptureRange = doc.data.texts
641+
// eslint-disable-next-line @typescript-eslint/no-deprecated
641642
.filter((t: TextInfo) => t.chapters.some(c => c.hasDraft))
642643
.map((t: TextInfo) => Canon.bookNumberToId(t.bookNum, ''))
643644
.filter((id: string) => id !== '')

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/draft-generation.component.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { MatDialogRef, MatDialogState } from '@angular/material/dialog';
44
import { provideRouter } from '@angular/router';
55
import { SystemRole } from 'realtime-server/lib/esm/common/models/system-role';
66
import { createTestUser } from 'realtime-server/lib/esm/common/models/user-test-data';
7+
import { SFProjectProfile } from 'realtime-server/lib/esm/scriptureforge/models/sf-project';
78
import { SFProjectRole } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-role';
89
import { createTestProjectProfile } from 'realtime-server/lib/esm/scriptureforge/models/sf-project-test-data';
910
import { TextInfoPermission } from 'realtime-server/lib/esm/scriptureforge/models/text-info-permission';
@@ -157,8 +158,6 @@ describe('DraftGenerationComponent', () => {
157158
newDraftHistory: createTestFeatureFlag(false),
158159
usfmFormat: createTestFeatureFlag(false)
159160
});
160-
mockSFProjectService = jasmine.createSpyObj<SFProjectService>(['hasDraft']);
161-
mockSFProjectService.hasDraft.and.returnValue(true);
162161
}
163162

164163
static initProject(currentUserId: string, preTranslate: boolean = true): void {
@@ -210,6 +209,13 @@ describe('DraftGenerationComponent', () => {
210209
projectDoc$: of(projectDoc),
211210
changes$: of(projectDoc)
212211
});
212+
const matchThisProject = {
213+
asymmetricMatch: (proj: SFProjectProfile | undefined) =>
214+
proj != null && proj.paratextId === projectDoc.data?.paratextId
215+
};
216+
mockSFProjectService = jasmine.createSpyObj<SFProjectService>(['hasDraft']);
217+
mockSFProjectService.hasDraft.withArgs(matchThisProject).and.returnValue(preTranslate);
218+
mockSFProjectService.hasDraft.withArgs(matchThisProject, jasmine.anything()).and.returnValue(preTranslate);
213219
}
214220

215221
get configureDraftButton(): HTMLElement | null {

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/draft-generation.service.spec.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -906,16 +906,18 @@ describe('DraftGenerationService', () => {
906906
const projectDoc: SFProjectProfileDoc = {
907907
id: projectId,
908908
data: createTestProjectProfile({
909+
translateConfig: {
910+
draftConfig: {
911+
currentScriptureRange: '1JN;2JN;3JN'
912+
}
913+
},
909914
texts: [
910915
{
911916
bookNum: 62,
912-
chapters: [
913-
{ number: 1, hasDraft: true },
914-
{ number: 2, hasDraft: true }
915-
]
917+
chapters: [{ number: 1 }, { number: 2 }]
916918
},
917-
{ bookNum: 63, chapters: [{ number: 1, hasDraft: true }] },
918-
{ bookNum: 64, chapters: [{ number: 1, hasDraft: true }] }
919+
{ bookNum: 63, chapters: [{ number: 1 }] },
920+
{ bookNum: 64, chapters: [{ number: 1 }] }
919921
]
920922
})
921923
} as SFProjectProfileDoc;
@@ -951,7 +953,12 @@ describe('DraftGenerationService', () => {
951953
const projectDoc: SFProjectProfileDoc = {
952954
id: projectId,
953955
data: createTestProjectProfile({
954-
texts: [{ bookNum: 62, chapters: [{ number: 1, hasDraft: true }] }]
956+
translateConfig: {
957+
draftConfig: {
958+
currentScriptureRange: '1JN'
959+
}
960+
},
961+
texts: [{ bookNum: 62, chapters: [{ number: 1 }] }]
955962
})
956963
} as SFProjectProfileDoc;
957964
const lastCompletedBuild: BuildDto = {

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/draft-generation.service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,7 @@ export class DraftGenerationService {
389389
if (books.size === 0) {
390390
books = new Set<number>(
391391
// Legacy calculation for very old drafts
392-
// eslint-disable-next-line @typescript-eslint/no-deprecated
393-
projectDoc.data.texts.filter(text => text.chapters.some(c => c.hasDraft)).map(text => text.bookNum)
392+
booksFromScriptureRange(projectDoc.data.translateConfig.draftConfig.currentScriptureRange)
394393
);
395394
}
396395

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/draft-preview-books/draft-preview-books.component.spec.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,6 @@ describe('DraftPreviewBooks', () => {
142142
verify(mockedDraftHandlingService.getAndApplyDraftAsync(anything(), anything(), anything(), anything())).times(3);
143143
}));
144144

145-
it('can apply chapters with drafts and skips chapters without drafts', fakeAsync(() => {
146-
env = new TestEnvironment();
147-
const bookWithDraft: BookWithDraft = env.booksWithDrafts[1];
148-
setupDialog('project01');
149-
when(mockedDraftHandlingService.getAndApplyDraftAsync(anything(), anything(), anything(), anything())).thenResolve(
150-
undefined
151-
);
152-
expect(env.getBookButtonAtIndex(1).querySelector('.book-more')).toBeTruthy();
153-
env.component.chooseProjectToAddDraft(bookWithDraft);
154-
tick();
155-
env.fixture.detectChanges();
156-
verify(mockedDialogService.openMatDialog(DraftApplyDialogComponent, anything())).once();
157-
verify(mockedDraftHandlingService.getAndApplyDraftAsync(anything(), anything(), anything(), anything())).times(1);
158-
}));
159-
160145
it('can apply a historic draft', fakeAsync(() => {
161146
env = new TestEnvironment({
162147
additionalInfo: {
@@ -174,7 +159,7 @@ describe('DraftPreviewBooks', () => {
174159
tick();
175160
env.fixture.detectChanges();
176161
verify(mockedDialogService.openMatDialog(DraftApplyDialogComponent, anything())).once();
177-
verify(mockedDraftHandlingService.getAndApplyDraftAsync(anything(), anything(), anything(), anything())).times(1);
162+
verify(mockedDraftHandlingService.getAndApplyDraftAsync(anything(), anything(), anything(), anything())).times(2);
178163
}));
179164

180165
it('can open dialog with the current project', fakeAsync(() => {
@@ -436,7 +421,7 @@ class TestEnvironment {
436421

437422
booksWithDrafts: BookWithDraft[] = [
438423
{ bookNumber: 1, bookId: 'GEN', canEdit: true, chaptersWithDrafts: [1, 2, 3], draftApplied: false },
439-
{ bookNumber: 2, bookId: 'EXO', canEdit: true, chaptersWithDrafts: [1], draftApplied: false },
424+
{ bookNumber: 2, bookId: 'EXO', canEdit: true, chaptersWithDrafts: [1, 2], draftApplied: false },
440425
{ bookNumber: 3, bookId: 'LEV', canEdit: false, chaptersWithDrafts: [1, 2], draftApplied: false }
441426
];
442427

src/SIL.XForge.Scripture/Models/DraftConfig.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ public class DraftConfig
2323
/// <summary>
2424
/// A scripture range containing the books that have been drafted and are available in Scripture Forge.
2525
/// </summary>
26+
/// <remarks>
27+
/// This is a combination of the scripture ranges of previous drafts.
28+
/// </remarks>
2629
public string? DraftedScriptureRange { get; set; }
2730
}

src/SIL.XForge.Scripture/Services/MachineApiService.cs

Lines changed: 32 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,73 +1072,9 @@ await translationEnginesClient.GetAllBuildsAsync(translationEngineId, cancellati
10721072
.MaxBy(b => b.DateFinished);
10731073
if (translationBuild is not null)
10741074
{
1075-
// Verify that each book/chapter from the translationBuild is marked HasDraft = true
1076-
// If the projects texts chapters are not all marked as having a draft, then the webhook likely failed
1077-
// and we want to retrieve the pre-translation status to update the chapters as having a draft
1078-
Dictionary<string, List<int>> scriptureRangesWithDrafts = [];
1079-
1080-
IList<PretranslateCorpus> pretranslateCorpus = translationBuild.Pretranslate ?? [];
1081-
1082-
// Retrieve the user secret
1083-
Attempt<UserSecret> attempt = await userSecrets.TryGetAsync(curUserId, cancellationToken);
1084-
if (!attempt.TryResult(out UserSecret userSecret))
1085-
{
1086-
throw new DataNotFoundException("The user does not exist.");
1087-
}
1088-
1089-
ScrVers versification =
1090-
paratextService.GetParatextSettings(userSecret, project.ParatextId)?.Versification
1091-
?? VerseRef.defaultVersification;
1092-
1093-
ScriptureRangeParser scriptureRangeParser = new ScriptureRangeParser(versification);
1094-
foreach (PretranslateCorpus ptc in pretranslateCorpus)
1095-
{
1096-
// We are using the TranslationBuild.Pretranslate.SourceFilters.ScriptureRange to find the
1097-
// books selected for drafting. Some projects may have used the now obsolete field
1098-
// TranslationBuild.Pretranslate.ScriptureRange and will not get checked for webhook failures.
1099-
foreach (
1100-
ParallelCorpusFilter source in ptc.SourceFilters?.Where(s => s.ScriptureRange is not null) ?? []
1101-
)
1102-
{
1103-
foreach (
1104-
(string book, List<int> bookChapters) in scriptureRangeParser.GetChapters(
1105-
source.ScriptureRange
1106-
)
1107-
)
1108-
{
1109-
int bookNum = Canon.BookIdToNumber(book);
1110-
// Ensure that if chapters is blank, it contains every chapter in the book
1111-
List<int> chapters = bookChapters;
1112-
if (chapters.Count == 0)
1113-
{
1114-
chapters = [.. Enumerable.Range(1, versification.GetLastChapter(bookNum))];
1115-
}
1116-
1117-
// Set or merge the list of chapters
1118-
if (!scriptureRangesWithDrafts.TryGetValue(book, out List<int> existingChapters))
1119-
{
1120-
scriptureRangesWithDrafts[book] = chapters;
1121-
}
1122-
else
1123-
{
1124-
// Merge new chapters into existing list, avoiding duplicates
1125-
foreach (int chapter in chapters.Where(chapter => !existingChapters.Contains(chapter)))
1126-
{
1127-
existingChapters.Add(chapter);
1128-
}
1129-
1130-
// Add existing chapters to the books chapter list
1131-
scriptureRangesWithDrafts[book].AddRange(existingChapters);
1132-
}
1133-
}
1134-
}
1135-
}
1136-
11371075
// See if the current scripture range has changed
1138-
if (
1139-
project.TranslateConfig.DraftConfig.CurrentScriptureRange
1140-
!= string.Join(';', scriptureRangesWithDrafts.Keys)
1141-
)
1076+
string currentScriptureRange = GetDraftedScriptureRange(translationBuild);
1077+
if (project.TranslateConfig.DraftConfig.CurrentScriptureRange != currentScriptureRange)
11421078
{
11431079
backgroundJobClient.Enqueue<IMachineApiService>(r =>
11441080
r.RetrievePreTranslationStatusAsync(sfProjectId, CancellationToken.None)
@@ -1890,13 +1826,7 @@ await projectSecrets.UpdateAsync(
18901826
}
18911827

18921828
// Store the CurrentScriptureRange based on the books we asked Serval to draft
1893-
string currentScriptureRange = string.Join(
1894-
';',
1895-
translationBuild
1896-
.Pretranslate?.SelectMany(p => p.SourceFilters)
1897-
.Where(f => f.ScriptureRange != null)
1898-
.Select(f => f.ScriptureRange) ?? []
1899-
);
1829+
string currentScriptureRange = GetDraftedScriptureRange(translationBuild);
19001830
if (string.IsNullOrWhiteSpace(currentScriptureRange))
19011831
{
19021832
throw new DataNotFoundException(
@@ -2392,6 +2322,35 @@ private static ServalEngineDto CreateDto(TranslationEngine translationEngine) =>
23922322
TargetLanguageTag = translationEngine.TargetLanguage,
23932323
};
23942324

2325+
/// <summary>
2326+
/// Gets the drafted scripture range for a translation build.
2327+
/// </summary>
2328+
/// <param name="translationBuild">The translation build.</param>
2329+
/// <returns>The scripture range that was drafted.</returns>
2330+
private static string GetDraftedScriptureRange(TranslationBuild translationBuild)
2331+
{
2332+
List<string> booksWithDrafts = [];
2333+
ScriptureRangeParser scriptureRangeParser = new ScriptureRangeParser();
2334+
foreach (PretranslateCorpus ptc in translationBuild.Pretranslate ?? [])
2335+
{
2336+
// We are using the TranslationBuild.Pretranslate.SourceFilters.ScriptureRange to find the
2337+
// books selected for drafting. Some projects may have used the now obsolete field
2338+
// TranslationBuild.Pretranslate.ScriptureRange and will not get checked for webhook failures.
2339+
foreach (ParallelCorpusFilter source in ptc.SourceFilters?.Where(s => s.ScriptureRange is not null) ?? [])
2340+
{
2341+
foreach ((string book, List<int> _) in scriptureRangeParser.GetChapters(source.ScriptureRange))
2342+
{
2343+
if (!booksWithDrafts.Contains(book))
2344+
{
2345+
booksWithDrafts.Add(book);
2346+
}
2347+
}
2348+
}
2349+
}
2350+
2351+
return string.Join(';', booksWithDrafts);
2352+
}
2353+
23952354
/// <summary>
23962355
/// Gets the highest ranked user id on a project.
23972356
/// </summary>

test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3670,7 +3670,7 @@ public async Task RetrievePreTranslationStatusAsync_ReportsErrorWhenProjectSecre
36703670
}
36713671

36723672
[Test]
3673-
public async Task RetrievePreTranslationStatusAsync_UpdatesPreTranslationStatusAndTextDocuments()
3673+
public async Task RetrievePreTranslationStatusAsync_UpdatesPreTranslationTextDocuments()
36743674
{
36753675
// Set up test environment with a completed build
36763676
var env = new TestEnvironment();

test/SIL.XForge.Scripture.Tests/Services/PreTranslationServiceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ public TestEnvironment(TestEnvironmentOptions? options = null)
740740
)
741741
.Returns(MatthewBookUsfm);
742742
Service = Substitute.ForPartsOf<PreTranslationService>(ProjectSecrets, TranslationEnginesClient);
743-
if (options.MockPreTranslationParameters)
743+
if (options.MockLegacyPreTranslationParameters)
744744
{
745745
Service
746746
.Configure()

0 commit comments

Comments
 (0)