|
9 | 9 | * Copyright (C) The OpenCRVS Authors. OpenCRVS and the OpenCRVS |
10 | 10 | * graphic logo are (registered/a) trademark(s) of Plan International. |
11 | 11 | */ |
12 | | -import { getBirthEncounterCompositionCursor } from './../../utils/hearth-helper.js' |
| 12 | +import { |
| 13 | + getBirthEncounterCompositionCursor, |
| 14 | + getBirthEncounterCompositionCount |
| 15 | + // eslint-disable-next-line import/no-relative-parent-imports |
| 16 | +} from './../../utils/hearth-helper.js' |
13 | 17 |
|
14 | 18 | export const up = async (db, client) => { |
15 | 19 | const session = client.startSession() |
| 20 | + const limit = 50 |
| 21 | + let skip = 0 |
| 22 | + let processedDocCount = 0 |
16 | 23 | try { |
17 | 24 | await session.withTransaction(async () => { |
18 | | - const compositionCursor = await getBirthEncounterCompositionCursor(db) |
19 | | - while (await compositionCursor.hasNext()) { |
20 | | - const composition = await compositionCursor.next() |
21 | | - const compositionHistory = await db |
22 | | - .collection('Composition_history') |
23 | | - .find({ |
24 | | - id: composition.id |
25 | | - }) |
26 | | - .toArray() |
27 | | - compositionHistory.push(composition) |
28 | | - const correctionIndex = compositionHistory.findIndex((composition) => { |
29 | | - return composition.section.find( |
30 | | - (section) => |
31 | | - section.code.coding[0].code === 'birth-correction-encounters' |
32 | | - ) |
33 | | - }) |
34 | | - const immediatePrevComp = compositionHistory[correctionIndex - 1] |
35 | | - const hasDocumentSection = immediatePrevComp?.section.find( |
36 | | - (section) => section.code.coding[0].code === 'supporting-documents' |
| 25 | + const totalCompositionCount = await getBirthEncounterCompositionCount(db) |
| 26 | + while (totalCompositionCount > processedDocCount) { |
| 27 | + const compositionCursor = await getBirthEncounterCompositionCursor( |
| 28 | + db, |
| 29 | + limit, |
| 30 | + skip |
| 31 | + ) |
| 32 | + const count = await compositionCursor.count() |
| 33 | + // eslint-disable-next-line no-console |
| 34 | + console.log( |
| 35 | + `Migration - SupportingDocuments :: Processing ${ |
| 36 | + processedDocCount + 1 |
| 37 | + } - ${ |
| 38 | + processedDocCount + count |
| 39 | + } of ${totalCompositionCount} compositions...` |
37 | 40 | ) |
38 | | - if (hasDocumentSection) { |
39 | | - await db |
40 | | - .collection('Composition') |
41 | | - .updateOne( |
42 | | - { id: compositionHistory[correctionIndex].id }, |
43 | | - { $push: { section: hasDocumentSection } } |
44 | | - ) |
| 41 | + while (await compositionCursor.hasNext()) { |
| 42 | + const composition = await compositionCursor.next() |
| 43 | + const compositionHistory = await db |
| 44 | + .collection('Composition_history') |
| 45 | + .find({ |
| 46 | + id: composition.id |
| 47 | + }) |
| 48 | + .toArray() |
| 49 | + compositionHistory.push(composition) |
| 50 | + const correctionIndex = compositionHistory.findIndex( |
| 51 | + (composition) => { |
| 52 | + return composition.section.find( |
| 53 | + (section) => |
| 54 | + section.code.coding[0].code === 'birth-correction-encounters' |
| 55 | + ) |
| 56 | + } |
| 57 | + ) |
| 58 | + const immediatePrevComp = compositionHistory[correctionIndex - 1] |
| 59 | + const hasDocumentSection = immediatePrevComp?.section.find( |
| 60 | + (section) => section.code.coding[0].code === 'supporting-documents' |
| 61 | + ) |
| 62 | + if (hasDocumentSection) { |
| 63 | + await db |
| 64 | + .collection('Composition') |
| 65 | + .updateOne( |
| 66 | + { id: compositionHistory[correctionIndex].id }, |
| 67 | + { $push: { section: hasDocumentSection } } |
| 68 | + ) |
| 69 | + } |
45 | 70 | } |
| 71 | + skip += limit |
| 72 | + processedDocCount += count |
| 73 | + const percentage = ( |
| 74 | + (processedDocCount / totalCompositionCount) * |
| 75 | + 100 |
| 76 | + ).toFixed(2) |
| 77 | + // eslint-disable-next-line no-console |
| 78 | + console.log( |
| 79 | + `Migration - SupportingDocuments :: Processing done ${percentage}%` |
| 80 | + ) |
46 | 81 | } |
47 | 82 | }) |
48 | 83 | } finally { |
|
0 commit comments