Skip to content

Commit 093e857

Browse files
pmachapmanRaymondLuong3
authored andcommitted
SF-3604 Fix download of historic drafts
1 parent 0c0df6f commit 093e857

File tree

2 files changed

+115
-13
lines changed

2 files changed

+115
-13
lines changed

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

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,25 +1580,63 @@ CancellationToken cancellationToken
15801580
cancellationToken
15811581
);
15821582

1583-
// First, see if the document exists in the realtime service, if the chapter is not 0
1583+
// First, see if the document exists in the realtime service, if we are not retrieving a custom draft configuration
15841584
IDocument<TextDocument>? textDocument = null;
1585-
if (chapterNum != 0 && draftUsfmConfig is null)
1585+
if (draftUsfmConfig is null)
15861586
{
1587-
textDocument = await connection.FetchAsync<TextDocument>(id);
1588-
if (textDocument.IsLoaded)
1587+
// Retrieve the chapters for this book from the realtime server, if the chapter is zero
1588+
if (chapterNum == 0)
15891589
{
1590-
// Retrieve the snapshot if it exists
1591-
Snapshot<TextDocument> snapshot = await connection.FetchSnapshotAsync<TextDocument>(
1592-
id,
1593-
latestTimestampForRevision
1594-
);
1595-
if (snapshot.Data is not null)
1590+
List<object> content = [];
1591+
foreach (Chapter chapter in project.Texts.SingleOrDefault(t => t.BookNum == bookNum)?.Chapters ?? [])
1592+
{
1593+
id = TextDocument.GetDocId(sfProjectId, bookNum, chapter.Number, TextDocument.Draft);
1594+
textDocument = await connection.FetchAsync<TextDocument>(id);
1595+
if (textDocument.IsLoaded)
1596+
{
1597+
// Retrieve the snapshot if it exists
1598+
Snapshot<TextDocument> snapshot = await connection.FetchSnapshotAsync<TextDocument>(
1599+
id,
1600+
latestTimestampForRevision
1601+
);
1602+
if (snapshot.Data?.Content is not null)
1603+
{
1604+
// Append the chapter to the book content
1605+
content.AddRange(snapshot.Data.Content);
1606+
}
1607+
}
1608+
}
1609+
1610+
// Return the USJ of the entire book, if present
1611+
if (content.Count > 0)
15961612
{
1597-
return snapshot.Data;
1613+
return new Usj
1614+
{
1615+
Type = Usj.UsjType,
1616+
Version = Usj.UsjVersion,
1617+
Content = content,
1618+
};
15981619
}
1620+
}
1621+
else
1622+
{
1623+
// Otherwise, retrieve the specific chapter from the realtime server
1624+
textDocument = await connection.FetchAsync<TextDocument>(id);
1625+
if (textDocument.IsLoaded)
1626+
{
1627+
// Retrieve the snapshot if it exists
1628+
Snapshot<TextDocument> snapshot = await connection.FetchSnapshotAsync<TextDocument>(
1629+
id,
1630+
latestTimestampForRevision
1631+
);
1632+
if (snapshot.Data is not null)
1633+
{
1634+
return snapshot.Data;
1635+
}
15991636

1600-
// There is no draft at the timestamp
1601-
throw new DataNotFoundException("A draft cannot be retrieved at that timestamp");
1637+
// There is no draft at the timestamp
1638+
throw new DataNotFoundException("A draft cannot be retrieved at that timestamp");
1639+
}
16021640
}
16031641
}
16041642

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,6 +2634,70 @@ await env
26342634
);
26352635
}
26362636

2637+
[Test]
2638+
public async Task GetPreTranslationUsjAsync_ChapterZeroLoadsMultipleChaptersFromMongo()
2639+
{
2640+
// Set up test environment
2641+
var env = new TestEnvironment();
2642+
Usj testUsjChapterTwo = new Usj
2643+
{
2644+
Type = Usj.UsjType,
2645+
Version = Usj.UsjVersion,
2646+
Content =
2647+
[
2648+
new UsjMarker
2649+
{
2650+
Type = "chapter",
2651+
Marker = "c",
2652+
Number = "2",
2653+
},
2654+
new UsjMarker
2655+
{
2656+
Type = "verse",
2657+
Marker = "v",
2658+
Number = "1",
2659+
},
2660+
"Verse 1",
2661+
],
2662+
};
2663+
string id1 = TextDocument.GetDocId(Project01, 40, 1, TextDocument.Draft);
2664+
env.TextDocuments.Add(new TextDocument(id1, TestUsj));
2665+
string id2 = TextDocument.GetDocId(Project01, 40, 2, TextDocument.Draft);
2666+
env.TextDocuments.Add(new TextDocument(id2, testUsjChapterTwo));
2667+
2668+
// The chapters must be configured in the project document
2669+
await env.Projects.UpdateAsync(
2670+
Project01,
2671+
u =>
2672+
u.Add(
2673+
p => p.Texts,
2674+
new TextInfo { BookNum = 40, Chapters = [new Chapter { Number = 1 }, new Chapter { Number = 2 }] }
2675+
)
2676+
);
2677+
2678+
// SUT
2679+
IUsj actual = await env.Service.GetPreTranslationUsjAsync(
2680+
User01,
2681+
Project01,
2682+
40,
2683+
0,
2684+
false,
2685+
DateTime.UtcNow,
2686+
null,
2687+
CancellationToken.None
2688+
);
2689+
2690+
// The returned Usj will be chapter 1 and 2 combined in order
2691+
List<object> expectedContent = [.. TestUsj.Content, .. testUsjChapterTwo.Content];
2692+
Usj expected = new Usj
2693+
{
2694+
Type = Usj.UsjType,
2695+
Version = Usj.UsjVersion,
2696+
Content = expectedContent,
2697+
};
2698+
Assert.That(actual, Is.EqualTo(expected).UsingPropertiesComparer());
2699+
}
2700+
26372701
[Test]
26382702
public void GetPreTranslationUsjAsync_CorpusDoesNotSupportUsfm()
26392703
{

0 commit comments

Comments
 (0)