functions/public/Write-ChurchHouseholds.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
function Write-ChurchHouseholds {
    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;
    $membersMovedInJSON = Get-ChurchMembersMovedIn -Session $session -UnitNumber $UnitNumber;

    $memberList = $memberListJSON | ConvertFrom-Json
    $membersMovedIn = $membersMovedInJSON | ConvertFrom-Json

    $persons = @();
    foreach ($member in $memberList) {
        $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.moveDate
        }
        $persons += $member
    }    

    $households = @();
    $householdList = $persons | Group-Object "householdUuid"
    foreach ($household in $householdList) {
        $thisHousehold = @{
            uuid       = $household.Name
            unitNumber = $household.Group[0].householdMember.household.unit.unitNumber
            name       = $household.Group[0].householdMember.household.directoryPreferredLocal
            surname    = $household.Group[0].householdMember.household.familyNameLocal
            address    = $household.Group[0].householdMember.household.address.addressLines -join (" ")
            members    = @()
        }
        if ($household.Group[0].newMoveIn) {
            $thisHousehold.Add("newMoveIn", $household.Group[0].newMoveIn)
            $thisHousehold.Add("newMoveInDate", $household.Group[0].newMoveInDate)
        }

        foreach ($member in $household.Group | Sort-Object @{Expression = { $_.isHead }; Ascending = $false }, @{Expression = { $_.age }; Ascending = $false }) {
            $thisHousehold.members += @{
                uuid          = $member.uuid
                householdUuid = $member.householdUuid
                head          = $member.isHead
                name          = $member.nameListPreferredLocal
                displayName   = "$($member.nameFormats.givenPreferredLocal) $($member.nameFormats.familyPreferredLocal)"
                age           = $member.age
            }
        }

        $households += $thisHousehold
    }    

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

<#
[{
    uuid: "00000000-0000-0000-0000-000000000000",
    unitNumber: 000000,
    name: "Name, Fake1 & Fake2",
    surname: "Name",
    address: "1000 W 1000 S Fake City, Fake 00000",
    newMoveIn: "true",
    newMoveInDate: "20190101"
    members: [
        {
            uuid: "00000000-0000-0000-0000-000000000000",
            householdUuid: "00000000-0000-0000-0000-000000000000",
            head: true,
            name: "Name, Fake1",
            displayName: "Fake1 Name",
            age: "0"
        },
        {
            uuid: "00000000-0000-0000-0000-000000000000",
            householdUuid: "00000000-0000-0000-0000-000000000000",
            unitNumber: 000000,
            Name: true,
            name: "Name, Fake2",
            displayName: "Fake2 Name",
            age: "0"
        }
    ]
}]
#>