forked from BricksandMortar/FixMissingGivingGroupJob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixCombinedGivers.cs
46 lines (42 loc) · 1.62 KB
/
FixCombinedGivers.cs
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
using System.Linq;
using Quartz;
using Rock.Data;
using Rock.Model;
using Rock.Web.Cache;
namespace com.bricksandmortarstudio.FixCombinedGivers
{
[DisallowConcurrentExecution]
public class FixCombinedGivers : IJob
{
public void Execute( IJobExecutionContext context )
{
var rockContext = new RockContext();
var familyGroupType = GroupTypeCache.Read( Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY );
var familyMembers = new GroupMemberService( rockContext )
.Queryable( "Group,Person" )
.Where(
g =>
g.Group.GroupType.Id == familyGroupType.Id && g.Person.GivingGroupId == 0 ||
g.Person.GivingGroupId == null );
while (familyMembers.Any())
{
//Chunk to prevent foreach saving thread errors
foreach ( var chunk in familyMembers.OrderBy( f => f.Id ).QueryChunksOfSize( 100 ) )
{
foreach ( var familyMember in chunk )
{
familyMember.Person.GivingGroupId = familyMember.GroupId;
}
rockContext.SaveChanges();
}
rockContext = new RockContext();
familyMembers = new GroupMemberService( rockContext )
.Queryable( "Group,Person" )
.Where(
g =>
g.Group.GroupType.Id == familyGroupType.Id && g.Person.GivingGroupId == 0 ||
g.Person.GivingGroupId == null );
}
}
}
}