public IQueryable <GuestFamily> GetGuestsForFamily(int groupId) { Guid knownRelationshipGuid = new Guid(Rock.SystemGuid.GroupType.GROUPTYPE_KNOWN_RELATIONSHIPS); Guid knownRelationshipOwner = new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_OWNER); Guid knownRelationshipCanCheckin = new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_CAN_CHECK_IN); RockContext rockContext = new RockContext(); GroupMemberService groupMemberService = new GroupMemberService(rockContext); PersonService personService = new PersonService(rockContext); var familyMembers = groupMemberService.Queryable() .Where(f => f.GroupId == groupId) .Select(f => f.PersonId); var familyMembersKnownRelationshipGroups = new GroupMemberService(rockContext).Queryable() .Where(g => g.Group.GroupType.Guid == knownRelationshipGuid && g.GroupRole.Guid == knownRelationshipOwner && familyMembers.Contains(g.PersonId)) .Select(m => m.GroupId); rockContext.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); var guests = groupMemberService.Queryable() .Where(g => g.GroupRole.Guid == knownRelationshipCanCheckin && familyMembersKnownRelationshipGroups.Contains(g.GroupId)) .Select(g => g.PersonId) .Distinct().ToList(); var guestFamilies = new List <GuestFamily>(); rockContext.Database.Log = null; foreach (var guestPersonId in guests) { var families = personService.GetFamilies(guestPersonId); foreach (var family in families) { if (!guestFamilies.Select(f => f.Id).Contains(family.Id)) { GuestFamily guestFamily = new GuestFamily(); guestFamily.Id = family.Id; guestFamily.Guid = family.Guid; guestFamily.Name = family.Name; guestFamily.FamilyMembers = new List <GuestFamilyMember>(); foreach (var familyMember in family.Members) { GuestFamilyMember guestFamilyMember = new GuestFamilyMember(); guestFamilyMember.Id = familyMember.PersonId; guestFamilyMember.PersonAliasId = familyMember.Person.PrimaryAliasId.Value; guestFamilyMember.Guid = familyMember.Person.Guid; guestFamilyMember.FirstName = familyMember.Person.NickName; guestFamilyMember.LastName = familyMember.Person.LastName; guestFamilyMember.PhotoUrl = familyMember.Person.PhotoUrl; guestFamilyMember.CanCheckin = guests.Contains(familyMember.PersonId); guestFamilyMember.Role = familyMember.GroupRole.Name; guestFamilyMember.Age = familyMember.Person.Age; guestFamilyMember.Gender = familyMember.Person.Gender; guestFamily.FamilyMembers.Add(guestFamilyMember); } guestFamilies.Add(guestFamily); } } } return(guestFamilies.AsQueryable()); }
public IQueryable<GuestFamily> GetGuestsForFamily( int groupId ) { Guid knownRelationshipGuid = new Guid( Rock.SystemGuid.GroupType.GROUPTYPE_KNOWN_RELATIONSHIPS ); Guid knownRelationshipOwner = new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_OWNER ); Guid knownRelationshipCanCheckin = new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_CAN_CHECK_IN ); RockContext rockContext = new RockContext(); GroupMemberService groupMemberService = new GroupMemberService( rockContext ); PersonService personService = new PersonService( rockContext ); var familyMembers = groupMemberService.Queryable() .Where( f => f.GroupId == groupId ) .Select( f => f.PersonId ); var familyMembersKnownRelationshipGroups = new GroupMemberService( rockContext ).Queryable() .Where( g => g.Group.GroupType.Guid == knownRelationshipGuid && g.GroupRole.Guid == knownRelationshipOwner && familyMembers.Contains( g.PersonId ) ) .Select( m => m.GroupId ); rockContext.Database.Log = s => System.Diagnostics.Debug.WriteLine( s ); var guests = groupMemberService.Queryable() .Where( g => g.GroupRole.Guid == knownRelationshipCanCheckin && familyMembersKnownRelationshipGroups.Contains( g.GroupId ) ) .Select( g => g.PersonId ) .Distinct().ToList(); var guestFamilies = new List<GuestFamily>(); rockContext.Database.Log = null; foreach ( var guestPersonId in guests ) { var families = personService.GetFamilies( guestPersonId ); foreach ( var family in families ) { if ( !guestFamilies.Select( f => f.Id ).Contains( family.Id ) ) { GuestFamily guestFamily = new GuestFamily(); guestFamily.Id = family.Id; guestFamily.Guid = family.Guid; guestFamily.Name = family.Name; guestFamily.FamilyMembers = new List<GuestFamilyMember>(); foreach ( var familyMember in family.Members ) { GuestFamilyMember guestFamilyMember = new GuestFamilyMember(); guestFamilyMember.Id = familyMember.PersonId; guestFamilyMember.PersonAliasId = familyMember.Person.PrimaryAliasId.Value; guestFamilyMember.Guid = familyMember.Person.Guid; guestFamilyMember.FirstName = familyMember.Person.NickName; guestFamilyMember.LastName = familyMember.Person.LastName; guestFamilyMember.PhotoUrl = familyMember.Person.PhotoUrl; guestFamilyMember.CanCheckin = guests.Contains( familyMember.PersonId ); guestFamilyMember.Role = familyMember.GroupRole.Name; guestFamilyMember.Age = familyMember.Person.Age; guestFamilyMember.Gender = familyMember.Person.Gender; guestFamily.FamilyMembers.Add( guestFamilyMember ); } guestFamilies.Add( guestFamily ); } } } return guestFamilies.AsQueryable(); }