public async Task <string> SaveMember(TeamMember teamMember) { var context = essContextFactory.Create(); var essTeam = EssTeam(context, Guid.Parse(teamMember.TeamId)); if (essTeam == null || essTeam.statuscode == DynamicsInactiveStatus) { throw new ArgumentException($"team {teamMember.TeamId} not found"); } var essTeamUser = mapper.Map <era_essteamuser>(teamMember); var existingMember = essTeamUser.era_essteamuserid.HasValue ? context.era_essteamusers .Where(u => u._era_essteamid_value == Guid.Parse(teamMember.TeamId) && u.era_essteamuserid == essTeamUser.era_essteamuserid.Value) .SingleOrDefault() : null; context.DetachAll(); if (existingMember == null) { essTeamUser.era_essteamuserid = Guid.NewGuid(); context.AddToera_essteamusers(essTeamUser); } else { context.AttachTo(nameof(EssContext.era_essteamusers), essTeamUser); } if (teamMember.IsActive) { context.ActivateObject(essTeamUser, (int)TeamMemberStatus.Active); } else { context.ActivateObject(essTeamUser, (int)TeamMemberStatus.Inactive); } context.UpdateObject(essTeamUser); context.AttachTo(nameof(EssContext.era_essteams), essTeam); context.AddLink(essTeam, nameof(era_essteam.era_essteamuser_ESSTeamId), essTeamUser); await context.SaveChangesAsync(); context.DetachAll(); return(essTeamUser.era_essteamuserid.Value.ToString()); }
public async Task <IEnumerable <SupportFlag> > CheckCompliance(Support support) { if (support.Id == null) { throw new ArgumentNullException(nameof(support.Id)); } var ctx = essContextFactory.Create(); var checkedSupport = (await((DataServiceQuery <era_evacueesupport>)ctx.era_evacueesupports.Where(s => s.era_name == support.Id)).GetAllPagesAsync()).SingleOrDefault(); if (checkedSupport == null) { throw new ArgumentException($"Support {support.Id} not found", nameof(support)); } await ctx.LoadPropertyAsync(checkedSupport, nameof(era_evacueesupport.era_era_householdmember_era_evacueesupport)); var from = checkedSupport.era_validfrom.Value; var to = checkedSupport.era_validto.Value; var type = checkedSupport.era_supporttype.Value; var householdMembers = checkedSupport.era_era_householdmember_era_evacueesupport.ToArray(); var similarSupports = (await((DataServiceQuery <era_evacueesupport>)ctx.era_evacueesupports.Where(s => s.era_name != checkedSupport.era_name && s.era_supporttype == type && ((s.era_validfrom >= from && s.era_validfrom <= to) || (s.era_validto >= from && s.era_validto <= to)))) .GetAllPagesAsync()).ToArray(); foreach (var similarSupport in similarSupports) { await ctx.LoadPropertyAsync(similarSupport, nameof(era_evacueesupport.era_era_householdmember_era_evacueesupport)); } Func <era_householdmember, era_householdmember, bool> householdMemberMatcher = (m1, m2) => m1.era_firstname.Equals(m2.era_firstname, StringComparison.OrdinalIgnoreCase) && m1.era_lastname.Equals(m2.era_lastname, StringComparison.OrdinalIgnoreCase) && m1.era_dateofbirth.Equals(m2.era_dateofbirth); var duplicateSupports = similarSupports .Where(s => s.era_era_householdmember_era_evacueesupport.Any(m => householdMembers.Any(cm => householdMemberMatcher(cm, m)))) .ToArray(); var duplicates = duplicateSupports.Select(s => new DuplicateSupportFlag { DuplicatedSupportId = s.era_name }).ToArray(); ctx.DetachAll(); return(duplicates); }
public EvacuationRepository(IEssContextFactory essContextFactory, IMapper mapper) { this.essContext = essContextFactory.Create(); this.essContextFactory = essContextFactory; this.mapper = mapper; }
public DynamicsTestData(IEssContextFactory essContextFactory) { var essContext = essContextFactory.Create(); jurisdictions = essContext.era_jurisdictions.OrderBy(j => j.era_jurisdictionid).ToArray(); canada = essContext.era_countries.Where(c => c.era_countrycode == "CAN").Single(); bc = essContext.era_provinceterritorieses.Where(c => c.era_code == "BC").Single(); #if DEBUG this.testPrefix = $"autotest-dev"; #else this.testPrefix = $"autotest-{TestHelper.GenerateNewUniqueId(string.Empty)}"; #endif this.activeTaskId = testPrefix + "-active-task"; this.inactiveTaskId = testPrefix + "-inactive-task"; var existingTeam = essContext.era_essteams.Where(t => t.era_name == testPrefix + "-team").SingleOrDefault(); if (existingTeam != null) { essContext.LoadProperty(existingTeam, nameof(era_essteam.era_ESSTeam_ESSTeamArea_ESSTeamID)); this.testTeam = existingTeam; this.CreateTeamMember(essContext, testTeam, Guid.NewGuid(), "-second"); CreateTeamMember(essContext, testTeam, Guid.NewGuid(), "-third"); CreateTeamMember(essContext, testTeam, Guid.NewGuid(), "-fourth"); } else { this.testTeam = CreateTeam(essContext, Guid.NewGuid()); } var otherTeam = essContext.era_essteams.Where(t => t.era_name == testPrefix + "-team-other").SingleOrDefault(); if (otherTeam != null) { essContext.LoadProperty(otherTeam, nameof(era_essteam.era_ESSTeam_ESSTeamArea_ESSTeamID)); this.otherTestTeam = otherTeam; } else { this.otherTestTeam = CreateTeam(essContext, Guid.NewGuid(), "-other"); } this.activeTask = essContext.era_tasks.Where(t => t.era_name == activeTaskId).SingleOrDefault() ?? CreateTask(essContext, activeTaskId, DateTime.UtcNow); this.inactiveTask = essContext.era_tasks.Where(t => t.era_name == activeTaskId).SingleOrDefault() ?? CreateTask(essContext, inactiveTaskId, DateTime.UtcNow.AddDays(-7)); this.tier4TeamMember = essContext.era_essteamusers.Where(tu => tu.era_firstname == this.testPrefix + "-first" && tu.era_lastname == this.testPrefix + "-last").SingleOrDefault() ?? CreateTeamMember(essContext, testTeam, Guid.NewGuid()); this.otherTeamMember = essContext.era_essteamusers.Where(tu => tu.era_firstname == this.testPrefix + "-first-other" && tu.era_lastname == this.testPrefix + "-last-other").SingleOrDefault() ?? CreateTeamMember(essContext, this.otherTestTeam, Guid.NewGuid(), "-other", EMBC.ESS.Resources.Teams.TeamUserRoleOptionSet.Tier1); this.testContact = essContext.contacts.Where(c => c.firstname == this.testPrefix + "-first" && c.lastname == this.testPrefix + "-last").SingleOrDefault() ?? CreateContact(essContext); this.supplierA = essContext.era_suppliers.Where(c => c.era_name == testPrefix + "-supplier-A").SingleOrDefault() ?? CreateSupplier(essContext, "A", this.testTeam); this.supplierB = essContext.era_suppliers.Where(c => c.era_name == testPrefix + "-supplier-B").SingleOrDefault() ?? CreateSupplier(essContext, "B", this.testTeam); this.supplierC = essContext.era_suppliers.Where(c => c.era_name == testPrefix + "-supplier-C").SingleOrDefault() ?? CreateSupplier(essContext, "C", this.otherTestTeam); this.inactiveSupplier = essContext.era_suppliers.Where(c => c.era_name == testPrefix + "-supplier-inactive").SingleOrDefault() ?? CreateSupplier(essContext, "inactive", null); var evacuationfile = essContext.era_evacuationfiles .Expand(f => f.era_CurrentNeedsAssessmentid) .Expand(f => f.era_Registrant) .Where(f => f.era_name == testPrefix + "-digital").SingleOrDefault(); if (evacuationfile == null) { evacuationfile = CreateEvacuationFile(essContext, this.testContact, testPrefix + "-digital"); } else { essContext.LoadProperty(evacuationfile, nameof(era_evacuationfile.era_era_evacuationfile_era_householdmember_EvacuationFileid)); CreateEvacueeSupports(essContext, evacuationfile, this.testContact, this.tier4TeamMember, testPrefix); } var paperEvacuationfile = essContext.era_evacuationfiles .Expand(f => f.era_CurrentNeedsAssessmentid) .Expand(f => f.era_Registrant) .Where(f => f.era_name == testPrefix + "-paper").SingleOrDefault(); if (paperEvacuationfile == null) { paperEvacuationfile = CreateEvacuationFile(essContext, this.testContact, testPrefix + "-paper", testPrefix + "-paper"); CreateEvacueeSupports(essContext, paperEvacuationfile, this.testContact, this.tier4TeamMember, testPrefix); } else { essContext.LoadProperty(paperEvacuationfile, nameof(era_evacuationfile.era_era_evacuationfile_era_householdmember_EvacuationFileid)); } essContext.SaveChanges(); essContext.DeactivateObject(this.inactiveSupplier, 2); essContext.SaveChanges(); essContext.DetachAll(); this.testEvacuationfile = essContext.era_evacuationfiles .Expand(f => f.era_CurrentNeedsAssessmentid) .Expand(f => f.era_Registrant) .Where(f => f.era_evacuationfileid == evacuationfile.era_evacuationfileid).Single(); essContext.LoadProperty(this.testEvacuationfile, nameof(era_evacuationfile.era_era_evacuationfile_era_evacueesupport_ESSFileId)); essContext.LoadProperty(this.testEvacuationfile, nameof(era_evacuationfile.era_era_evacuationfile_era_householdmember_EvacuationFileid)); this.testPaperEvacuationFile = essContext.era_evacuationfiles .Expand(f => f.era_CurrentNeedsAssessmentid) .Expand(f => f.era_Registrant) .Where(f => f.era_evacuationfileid == paperEvacuationfile.era_evacuationfileid).Single(); essContext.LoadProperty(this.testPaperEvacuationFile, nameof(era_evacuationfile.era_era_evacuationfile_era_evacueesupport_ESSFileId)); essContext.LoadProperty(this.testPaperEvacuationFile, nameof(era_evacuationfile.era_era_evacuationfile_era_householdmember_EvacuationFileid)); essContext.DetachAll(); this.essContextFactory = essContextFactory; }