functions/public/Write-ChurchPersons.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function Write-ChurchPersons {
    param (
        [Parameter(Mandatory = $true)]
        [string] $UserName,
        [Parameter(Mandatory = $true)]
        [string] $Password,
        [Parameter(Mandatory = $true)]
        [int] $UnitNumber
    )

    $session = New-ChurchSession -UserName $UserName -Password $Password;

    $memberListJSON = Get-ChurchMemberList -Session $session -UnitNumber $UnitNumber;
    $birthdayListJSON = Get-ChurchBirthdayList -Session $session
    $membersMovedInJSON = Get-ChurchMembersMovedIn -Session $session -UnitNumber $UnitNumber;
    $membersWithCallingsJSON = Get-ChurchMembersWithCallings -Session $session -UnitNumber $UnitNumber;
    $recommendStatusJSON = Get-ChurchRecommendStatus -Session $session -UnitNumber $UnitNumber;
    $actionInterviewListJSON = Get-ChurchActionInterviewList -Session $session -UnitNumber $UnitNumber;

    $memberList = $memberListJSON | ConvertFrom-Json
    $birthdayList = ($birthdayListJSON | ConvertFrom-Json) | Select-Object -ExpandProperty "birthdays"
    $membersMovedIn = $membersMovedInJSON | ConvertFrom-Json
    $membersWithCallings = $membersWithCallingsJSON | ConvertFrom-Json
    $recommendStatus = $recommendStatusJSON | ConvertFrom-Json
    $actionInterviewList = $actionInterviewListJSON | ConvertFrom-Json

    $actionInterviews = @();
    foreach ($type in $actionInterviewList) {
        foreach ($person in $type.list) {
            $action = @{
                id          = $person.id;
                type        = $type.type;
                title       = "$($type.title)$(if ($type.subTitle) { ' - ' + $type.subTitle })";
                description = $type.description;
                action      = $person.action;                
            };
            $actionInterviews += $action;
        }
    }

    $persons = @();

    foreach ($member in $memberList) {
        
        $index = $birthdayList.id.indexOf($member.legacyCmisId)
        if ($index -gt -1) {
            $birthday = $birthdayList[$index];
            $member | Add-Member -Type "NoteProperty" -Name "mrn" -Value $birthday.mrn
            $member | Add-Member -Type "NoteProperty" -Name "formattedMrn" -Value $birthday.formattedMrn
            $member.nameFormats | Add-Member -Type "NoteProperty" -Name "spokenName" -Value $birthday.spokenName
        }

        $index = $membersMovedIn.id.indexOf($member.legacyCmisId)
        if ($index -gt -1) {
            $memberMovedIn = $membersMovedIn[$index];
            $member | Add-Member -Type "NoteProperty" -Name "newMoveIn" -Value $true
            $member | Add-Member -Type "NoteProperty" -Name "newMoveInDate" -Value $memberMovedIn.moveDateCalc

            <#get prior unit detail#>
            $priorUnitNumber = $memberMovedIn.priorUnitNumber;
            if ($priorUnitNumber) {
                $priorUnitDetailJSON = Get-ChurchUnitDetail -Session $session -UnitNumber $priorUnitNumber;
                $priorUnitDetail = $priorUnitDetailJSON | ConvertFrom-Json
                if ($priorUnitDetail) {
                    $member | Add-Member -Type "NoteProperty" -Name "priorUnitDetail" -Value $priorUnitDetail
                }
            }

        }

        $index = $recommendStatus.id.indexOf($member.legacyCmisId)
        if ($index -gt -1) {
            $recommend = $recommendStatus[$index];
            $member | Add-Member -Type "NoteProperty" -Name "recommendExpirationDate" -Value $recommend.expirationDate
            $member | Add-Member -Type "NoteProperty" -Name "recommendStatus" -Value $recommend.status
            $member | Add-Member -Type "NoteProperty" -Name "recommendType" -Value $recommend.type
        }

        $indexes = @()
        $singleIndex = -1
        do {
            $singleIndex = [array]::IndexOf($actionInterviews.id, $member.legacyCmisId, $singleIndex + 1)
            if ($singleIndex -ge 0) { $indexes += $singleIndex }
        } while ($singleIndex -ge 0)

        if ($indexes.Length) {
            $member | Add-Member -Type "NoteProperty" -Name "actionInterviews" -Value @();
            foreach ($index in $indexes) {
                $actionInterview = $actionInterviews[$index];
                $member.actionInterviews += @{
                    type        = $actionInterview.type;
                    title       = $actionInterview.title;
                    description = $actionInterview.description;
                    action      = $actionInterview.action;                
                }    
            }
        }

        $indexes = @()
        $singleIndex = -1
        do {
            $singleIndex = [array]::IndexOf($membersWithCallings.id, $member.legacyCmisId, $singleIndex + 1)
            if ($singleIndex -ge 0) { $indexes += $singleIndex }
        } while ($singleIndex -ge 0)

        if ($indexes.Length) {
            $member | Add-Member -Type "NoteProperty" -Name "callings" -Value @();
            foreach ($index in $indexes) {
                $calling = $membersWithCallings[$index];
                $member.callings += @{
                    organization  = $calling.organization
                    position      = $calling.position
                    activeDate    = $calling.activeDate
                    sustainedDate = $calling.sustainedDate
                    setApartDate  = $calling.setApartDate
                    setApart      = $calling.setApart
                }    
            }
        }

        $persons += $member
    }    

    return $persons | ConvertTo-Json -Depth 10 -Compress;
}