|
1 | 1 | import { parseScripture, tryParseScripture } from '@seedcompany/scripture';
|
2 | 2 | import { Row } from '~/common/xlsx.util';
|
3 | 3 | import { ScriptureRange } from '../scripture/dto';
|
| 4 | +import { PnpExtractionResult } from './extraction-result'; |
| 5 | +import { addProblemMismatchScriptureAndVerseCount } from './isGoalRow'; |
4 | 6 | import { WrittenScripturePlanningSheet } from './planning-sheet';
|
5 | 7 |
|
6 |
| -export const extractScripture = (row: Row<WrittenScripturePlanningSheet>) => { |
| 8 | +export const extractScripture = ( |
| 9 | + row: Row<WrittenScripturePlanningSheet>, |
| 10 | + result: PnpExtractionResult, |
| 11 | +) => { |
7 | 12 | const sheet = row.sheet;
|
8 |
| - const totalVerses = sheet.totalVerses(row)!; |
9 |
| - const scriptureFromBookCol = parseScripture(sheet.bookName(row)); |
| 13 | + |
| 14 | + const totalVersesCell = sheet.totalVerses(row)!; |
| 15 | + const totalVerses = totalVersesCell.asNumber!; |
| 16 | + |
| 17 | + const bookCell = sheet.bookName(row); |
| 18 | + const scriptureFromBookCol = parseScripture(bookCell.asString); |
| 19 | + const book = scriptureFromBookCol[0].start.book; |
10 | 20 |
|
11 | 21 | const common = {
|
12 |
| - bookName: scriptureFromBookCol[0].start.book.name, |
| 22 | + bookName: book.name, |
13 | 23 | totalVerses,
|
14 | 24 | };
|
| 25 | + let mismatchError = false; |
15 | 26 |
|
16 | 27 | // If scripture from book column matches total count, use it.
|
17 |
| - if (ScriptureRange.totalVerses(...scriptureFromBookCol) === totalVerses) { |
| 28 | + const totalVersesInBookCol = ScriptureRange.totalVerses( |
| 29 | + ...scriptureFromBookCol, |
| 30 | + ); |
| 31 | + if (totalVersesInBookCol === totalVerses) { |
18 | 32 | return {
|
19 | 33 | ...common,
|
20 | 34 | scripture: scriptureFromBookCol.map(ScriptureRange.fromVerses),
|
21 | 35 | };
|
| 36 | + // If it is more than just the book name (aka not just the book name) then |
| 37 | + // the verse count will be less and if it doesn't match the total, there is a problem |
| 38 | + } else if (totalVersesInBookCol < book.totalVerses) { |
| 39 | + mismatchError = true; |
| 40 | + // TODO I think this is a redundant check. |
| 41 | + // I don't think we will ever get here because the row is filtered out with |
| 42 | + // the isGoalRow function. |
| 43 | + addProblemMismatchScriptureAndVerseCount( |
| 44 | + result, |
| 45 | + totalVersesInBookCol, |
| 46 | + bookCell, |
| 47 | + totalVersesCell, |
| 48 | + ); |
22 | 49 | }
|
23 | 50 |
|
24 | 51 | // Otherwise, if note column has scripture that matches the total count use it.
|
25 |
| - const scriptureFromNoteCol = tryParseScripture(sheet.myNote(row)); |
26 |
| - if ( |
27 |
| - scriptureFromNoteCol && |
28 |
| - ScriptureRange.totalVerses(...scriptureFromNoteCol) === totalVerses |
29 |
| - ) { |
30 |
| - return { |
31 |
| - ...common, |
32 |
| - scripture: scriptureFromNoteCol.map(ScriptureRange.fromVerses), |
33 |
| - }; |
| 52 | + const noteCell = sheet.myNote(row); |
| 53 | + const scriptureFromNoteCol = tryParseScripture(noteCell.asString); |
| 54 | + if (scriptureFromNoteCol) { |
| 55 | + const totalVersesFromNoteCol = ScriptureRange.totalVerses( |
| 56 | + ...scriptureFromNoteCol, |
| 57 | + ); |
| 58 | + if (totalVersesFromNoteCol === totalVerses) { |
| 59 | + return { |
| 60 | + ...common, |
| 61 | + scripture: scriptureFromNoteCol.map(ScriptureRange.fromVerses), |
| 62 | + }; |
| 63 | + } |
| 64 | + mismatchError = true; |
| 65 | + result.addProblem({ |
| 66 | + severity: 'Error', |
| 67 | + groups: |
| 68 | + 'Mismatch between the planned scripture in _My Notes_ column and the number of verses to translate', |
| 69 | + message: `"${noteCell.asString!}" \`${ |
| 70 | + noteCell.ref |
| 71 | + }\` is **${totalVersesFromNoteCol}** verses, but the goal declares **${totalVerses}** verses to translate \`${ |
| 72 | + totalVersesCell.ref |
| 73 | + }\``, |
| 74 | + source: noteCell, |
| 75 | + }); |
34 | 76 | }
|
35 | 77 |
|
36 | 78 | // Otherwise, fallback to unspecified scripture.
|
| 79 | + !mismatchError && |
| 80 | + result.addProblem({ |
| 81 | + severity: 'Warning', |
| 82 | + groups: 'Unspecified scripture reference', |
| 83 | + message: `"${book.name}" \`${bookCell.ref}\` does not a have specified scripture reference (either in the _Books_ or _My Notes_ column)`, |
| 84 | + source: totalVersesCell, |
| 85 | + }); |
37 | 86 | return {
|
38 | 87 | ...common,
|
39 | 88 | scripture: [],
|
|
0 commit comments