msupply-foundation / mobile

Open source mobile app for medical inventory control
http://msupply.org.nz/mobile
Other
43 stars 27 forks source link

Create migration method to generate name_note records for existing local vaccinations that don't have any #4384

Open kat-ms opened 3 years ago

kat-ms commented 3 years ago

Is your feature request related to a problem? Please describe.

For #4347

Describe the solution you'd like

As part of https://github.com/openmsupply/mobile/issues/4296 (8.2.0) vaccinations and refusals were modified to create a name_note on completion. Would like a migration method to create name_notes for vaccinations that were done before 8.2.0, so local history can just look at name_notes for vaccinations and refusals.

Describe alternatives you've considered

No migration - would need conditional logic to pull both local vaccinations from both [transact] and [name_note] tables and filter duplicates.

Additional context

Might need to be footrunner on desktop for Tonga only rather than mobile migration (since name_note don't have transaction ids it will be difficult to tell if a transaction has a name_note or not...?)

josh-griffin commented 3 years ago

Oh yeah I think this'll likely be easier to just do footrunner on tonga server... I think there might need to be some other things that need to happen on that server soon ..? @mark-prins do you know? Basically creating a name note for a vaccination event from all the vaccinations? 🤷

mark-prins commented 3 years ago

yes, to the footrunner on the Tonga server

I think there might need to be some other things that need to happen on that server soon ..?

not that I know of. There are some data issues apparently, though that would just be a minor correction if anything.

mark-prins commented 2 years ago

Hello.


C_BOOLEAN($bOk)
C_LONGINT($iCount; $iStep; $iProgressBarId; $iSkipped; $iFailed; $iProgressState)
C_OBJECT($obVaccination; $obPatientEvent; $obResult; $obNameNote; $obTransaction; $obTransactions; $obTransLine; $obLogDetails; $obVaccinator)
C_TEXT($tVaccinator)

$bOk:=True
$iCount:=0
$iStep:=0
$iSkipped:=0
$iFailed:=0
$obPatientEvent:=ds.patient_event.query("code='vaccination'").first()

If ($obPatientEvent=Null)
    $obPatientEvent:=ds.patient_event.new()
    $obPatientEvent.code:="vaccination"
    $obPatientEvent.description:="Vaccination"
    $obPatientEvent.event_type:="OB"
    $obPatientEvent.ID:=UUID_generate
    $obResult:=$obPatientEvent.save()

    $bOk:=$obResult.success
End if 

If ($bOk)
    $obVaccines:=ds.item.query("is_vaccine=True").toCollection("ID").extract("ID")
    $obTransactions:=ds.transact.query("type='ci' && status='fn' && transLines.item_ID in :1"; $obVaccines)

    $iProgressBarId:=progress_start_new("Creating vaccination name notes"; ""; $obTransactions.length)

    For ($iStep; 0; $obTransactions.length-1)
        $obTransaction:=$obTransactions[$iStep]
        $iProgressState:=Process state($iProgressBarId)
        If (($iProgressState=-1) | ($iProgressState=-100))
            $iStep:=$obTransactions.length+1
        End if 
        progress_update($iProgressBarId; $iStep; $obTransactions.length; "Creating name note...")
        $obExistingNameNotes:=ds.name_note.query("patient_event_ID=:1 && name_ID=:2 && entry_date=:3"; $obPatientEvent.ID; $obTransaction.name_ID; $obTransaction.entry_date)

        If ($obExistingNameNotes.length#0)
            $iSkipped:=$iSkipped+1

        Else 
            For each ($obTransLine; $obTransaction.transLines)
                $obNameNote:=ds.name_note.new()
                $obNameNote.ID:=UUID_generate
                $obNameNote.name_ID:=$obTransaction.name_ID
                $obNameNote.patient_event_ID:=$obPatientEvent.ID
                $obNameNote.entry_date:=$obTransaction.entry_date
                $obNameNote.store_ID:=$obTransaction.store_ID
                $obNameNote.created_by_user_ID:=<>t_current_user_ID

                $tVaccinator:=""
                $obVaccinator:=New object()
                If ($obTransLine.medicineAdministrator#Null)
                    $obVaccinator:=$obTransLine.medicineAdministrator.toObject()
                    $tVaccinator:=$obVaccinator.first_name+" "+$obVaccinator.last_name
                End if 

                $obData:=New object()
                $obData.refused:=False
                $obData.bonusDose:=False  // how to determine this?
                $obData.itemName:=$obTransLine.item.item_name
                $obData.itemCode:=$obTransLine.item.code
                $obData.batch:=$obTransLine.batch
                $obData.expiry:=$obTransLine.expiry_date
                $obData.vaccineDate:=$obTransaction.entry_date
                $obData.vaccinator:=$tVaccinator
                $obData.refusalReason:=""
                $obData.extra:=New object()
                $obData.extra.prescription:=$obTransaction.toObject()
                $obData.extra.vaccinator:=$obVaccinator
                If ($obTransaction.name#Null)  // which has happened in the Tonga datafile
                    $obData.extra.patient:=$obTransaction.name.toObject()
                Else 
                    $obData.extra.patient:=New object()
                End if 
                $obNameNote.data:=$obData

                $obResult:=$obNameNote.save()
                $obLogDetails:=$obNameNote.toObject()

                If ($obResult.success)
                    $iCount:=$iCount+1
                    jsonLogRecord($obLogDetails; "Created vaccination name note"; "Footrunner")
                Else 
                    $iFailed:=$iFailed+1
                    $obLogDetails:=New object("result"; $obResult; "name_note"; $obLogDetails)
                    jsonLogRecord($obLogDetails; "Failed to create vaccination name note"; "Footrunner")
                End if 

            End for each 
        End if 
    End for 
End if 

end_progress_bar($iProgressBarId)
ALERT(String($iCount)+" transactions migrated \n"+String($iSkipped)+" transactions skipped \n"+String($iFailed)+" failures")

$iCount:=0
$obRefusalPatientEvent:=ds.patient_event.query("code='RV'").first()
$obNameNotes:=ds.name_note.query("patient_event_ID=:1"; $obRefusalPatientEvent.ID)

For each ($obNameNote; $obNameNotes)
    $obNameNote.patient_event_ID:=$obPatientEvent.ID

    $obData:=New object()
    $obData.refused:=True
    $obData.bonusDose:=False
    $obData.itemName:=""
    $obData.itemCode:=""
    $obData.batch:=""
    $obData.expiry:=""
    $obData.vaccineDate:=$obNameNote.entry_date
    $obData.vaccinator:=""
    $obData.refusalReason:=""
    $obData.extra:=New object()
    $obData.extra.prescription:=New object()
    $obData.extra.vaccinator:=New object()
    If ($obTransaction.name#Null)  // which has happened in the Tonga datafile
        $obData.extra.patient:=$obTransaction.name.toObject()
    Else 
        $obData.extra.patient:=New object()
    End if 
    $obNameNote.data:=$obData

    $obResult:=$obNameNote.save()
    $obLogDetails:=$obNameNote.toObject()

    If ($obResult.success)
        $iCount:=$iCount+1
        jsonLogRecord($obLogDetails; "Updated village from "+$tVillage; "Footrunner")
    Else 
        $obLogDetails:=New object("result"; $obResult; "name_note"; $obLogDetails)
        jsonLogRecord($obLogDetails; "Failed to update name note"; "Footrunner")
    End if 
End for each 

ALERT(String($iCount)+" refusal name notes migrated")
ArpitaSussol commented 2 years ago