public void Handle(PurgeActivityTag command) { if (command == null) { throw new ArgumentNullException("command"); } var activity = _entities.Get <Activity>() .EagerLoad(_entities, new Expression <Func <Activity, object> >[] { x => x.Values.Select(y => y.Tags), }) .ById(command.ActivityId, false); var values = activity.Values.Single(x => x.Mode == activity.Mode); if (values.Tags.All(x => !x.Text.Equals(command.ActivityTagText, StringComparison.OrdinalIgnoreCase))) { return; } var tags = values.Tags.Where(x => x.Text.Equals(command.ActivityTagText, StringComparison.OrdinalIgnoreCase)).ToArray(); foreach (var tag in tags) { _entities.Purge(tag); } activity.UpdatedOnUtc = DateTime.UtcNow; activity.UpdatedByPrincipal = command.Impersonator == null ? command.Principal.Identity.Name : command.Impersonator.Identity.Name; _entities.SaveChanges(); }
public void Handle(UpdateActivity command) { if (command == null) { throw new ArgumentNullException("command"); } if (command.OnGoing.HasValue && command.OnGoing.Value) { command.EndsOn = null; } var activity = _entities.Get <Activity>() .EagerLoad(_entities, new Expression <Func <Activity, object> >[] { x => x.Values, }) .ById(command.ActivityId, false); if (command.Mode.HasValue && command.Mode.Value != activity.Mode) { activity.Mode = command.Mode.Value; } activity.UpdatedByPrincipal = command.Impersonator == null ? command.Principal.Identity.Name : command.Impersonator.Identity.Name; activity.UpdatedOnUtc = DateTime.UtcNow; var values = activity.Values.SingleOrDefault(x => x.Mode == activity.Mode); if (values == null) { var copyValuesCommand = new CopyActivityValues(command.Principal) { ActivityValuesId = activity.Values.Single(x => x.Mode != activity.Mode).RevisionId, Mode = activity.Mode, CopyToActivity = activity, NoCommit = true, }; _copyActivityValues.Handle(copyValuesCommand); values = copyValuesCommand.CreatedActivityValues; } else { values.UpdatedByPrincipal = command.Principal.Identity.Name; values.UpdatedOnUtc = DateTime.UtcNow; } values.Title = command.Title; values.Content = command.Content; values.StartsOn = command.StartsOn; values.EndsOn = command.EndsOn; values.StartsFormat = command.StartsOn.HasValue ? command.StartsFormat ?? "M/d/yyyy" : null; values.EndsFormat = command.EndsOn.HasValue ? command.EndsFormat ?? "M/d/yyyy" : null; values.OnGoing = command.OnGoing; values.WasExternallyFunded = command.WasExternallyFunded; values.WasInternallyFunded = command.WasInternallyFunded; _entities.SaveChanges(); }
public void Handle(CreateActivity command) { if (command == null) { throw new ArgumentNullException("command"); } var person = _entities.Get <Person>() .EagerLoad(_entities, new Expression <Func <Person, object> >[] { x => x.User, }) .ByUserName(command.Principal.Identity.Name, false); var activity = new Activity { Person = person, PersonId = person.RevisionId, Mode = command.Mode, CreatedByPrincipal = command.Principal.Identity.Name, CreatedOnUtc = DateTime.UtcNow }; _entities.Create(activity); command.CreatedActivity = activity; if (!command.NoCommit) { _entities.SaveChanges(); } }
public void Handle(CreateAffiliation command) { if (command == null) { throw new ArgumentNullException("command"); } var personId = !command.PersonId.HasValue ? _queryProcessor.Execute(new MyPerson(command.Principal)).RevisionId : command.PersonId.Value; var entity = new Affiliation { PersonId = personId, EstablishmentId = command.EstablishmentId, FacultyRankId = command.FacultyRankId, JobTitles = command.JobTitles, }; _entities.Create(entity); if (command.NoCommit) { command.Created = entity; return; } _entities.SaveChanges(); command.Created = _queryProcessor.Execute(new AffiliationByPrimaryKey(entity.PersonId, entity.EstablishmentId)); }
public void Handle(CopyActivity command) { if (command == null) { throw new ArgumentNullException("command"); } var createActivity = new CreateActivity(command.Principal) { Mode = command.Mode, NoCommit = true, }; _createActivity.Handle(createActivity); command.CreatedActivity = createActivity.CreatedActivity; var originalActivity = _entities.Get <Activity>().ById(command.ActivityId, false); command.CreatedActivity.Original = originalActivity; originalActivity.WorkCopy = command.CreatedActivity; if (!command.NoCommit) { _entities.SaveChanges(); } }
public void Handle(UpdateAffiliation command) { if (command == null) { throw new ArgumentNullException("command"); } var entity = !command.PersonId.HasValue ? _entities.Get <Affiliation>().Single(x => x.EstablishmentId == command.EstablishmentId && x.Person.User != null && x.Person.User.Name.Equals(command.Principal.Identity.Name, StringComparison.OrdinalIgnoreCase)) : _entities.Get <Affiliation>().Single(x => x.EstablishmentId == command.EstablishmentId && x.PersonId == command.PersonId.Value); entity.JobTitles = command.JobTitles; entity.FacultyRankId = command.FacultyRankId; // can update the establishment as long as it is not the default if (!entity.IsDefault && command.NewEstablishmentId.HasValue && command.NewEstablishmentId.Value != command.EstablishmentId) { entity.EstablishmentId = command.NewEstablishmentId.Value; } _entities.SaveChanges(); }
public void Handle(CreateActivityType command) { if (command == null) { throw new ArgumentNullException("command"); } var activity = _entities.Get <Activity>() .EagerLoad(_entities, new Expression <Func <Activity, object> >[] { x => x.Values.Select(y => y.Types), }) .ById(command.ActivityId, false); var values = activity.Values.Single(x => x.Mode == activity.Mode); values.Types.Add(new ActivityType { TypeId = command.ActivityTypeId, }); activity.UpdatedOnUtc = DateTime.UtcNow; activity.UpdatedByPrincipal = command.Impersonator == null ? command.Principal.Identity.Name : command.Impersonator.Identity.Name; _entities.SaveChanges(); }
public void Handle(PurgeActivityPlace command) { if (command == null) { throw new ArgumentNullException("command"); } var activity = _entities.Get <Activity>() .EagerLoad(_entities, new Expression <Func <Activity, object> >[] { x => x.Values.Select(y => y.Locations), }) .ById(command.ActivityId, false); var values = activity.Values.Single(x => x.Mode == activity.Mode); if (values.Locations.All(x => x.PlaceId != command.PlaceId)) { return; } var locations = values.Locations.Where(x => x.PlaceId == command.PlaceId).ToArray(); foreach (var location in locations) { _entities.Purge(location); } activity.UpdatedOnUtc = DateTime.UtcNow; activity.UpdatedByPrincipal = command.Impersonator == null ? command.Principal.Identity.Name : command.Impersonator.Identity.Name; _entities.SaveChanges(); }
public void Handle(UpdateActivityDocument command) { if (command == null) { throw new ArgumentNullException("command"); } var entity = _entities.Get <ActivityDocument>() .EagerLoad(_entities, new Expression <Func <ActivityDocument, object> >[] { x => x.ActivityValues.Activity, }) .Single(x => x.RevisionId == command.DocumentId); if (entity.Title == command.Title) { return; } entity.Title = command.Title; entity.UpdatedOnUtc = DateTime.UtcNow; entity.UpdatedByPrincipal = command.Impersonator == null ? command.Principal.Identity.Name : command.Impersonator.Identity.Name; entity.ActivityValues.Activity.UpdatedOnUtc = entity.UpdatedOnUtc; entity.ActivityValues.Activity.UpdatedByPrincipal = entity.UpdatedByPrincipal; _entities.SaveChanges(); }
public void Handle(PurgeActivityDocument command) { if (command == null) { throw new ArgumentNullException("command"); } var entity = _entities.Get <ActivityDocument>() .EagerLoad(_entities, new Expression <Func <ActivityDocument, object> >[] { x => x.ActivityValues.Activity, }) .SingleOrDefault(x => x.RevisionId == command.DocumentId) ; if (entity == null) { return; // delete idempotently } entity.ActivityValues.Activity.UpdatedOnUtc = DateTime.UtcNow; entity.ActivityValues.Activity.UpdatedByPrincipal = command.Impersonator == null ? command.Principal.Identity.Name : command.Impersonator.Identity.Name; _entities.Purge(entity); _binaryData.Delete(entity.Path); if (!command.NoCommit) { _entities.SaveChanges(); } }
public void Handle(CreateActivityValues command) { if (command == null) { throw new ArgumentNullException("command"); } var activityValues = new ActivityValues { ActivityId = command.ActivityId, Title = command.Title, Content = command.Content, StartsOn = command.StartsOn, EndsOn = command.EndsOn, OnGoing = command.OnGoing, StartsFormat = command.StartsOn.HasValue ? command.StartsFormat ?? "M/d/yyyy" : null, EndsFormat = command.EndsOn.HasValue ? command.EndsFormat ?? "M/d/yyyy" : null, Mode = command.Mode, WasExternallyFunded = command.WasExternallyFunded, WasInternallyFunded = command.WasInternallyFunded, CreatedByPrincipal = command.Principal.Identity.Name, }; _entities.Create(activityValues); command.CreatedActivityValues = activityValues; if (!command.NoCommit) { _entities.SaveChanges(); } }
public void Handle(UpdatePlace command) { if (command == null) { throw new ArgumentNullException("command"); } var entity = _entities.Get <Place>().Single(x => x.RevisionId == command.PlaceId); //var audit = new CommandEvent //{ // RaisedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name, // Name = command.GetType().FullName, // Value = JsonConvert.SerializeObject(new // { // command.PlaceId, // command.IsRegion, // }), // PreviousState = place.ToJsonAudit(), //}; var lastParentId = entity.ParentId; // do not change parent when command.ParentId is zero if (command.ParentId.HasValue && command.ParentId.Value != 0) { var parent = _entities.Get <Place>().Single(x => x.RevisionId == command.ParentId.Value); entity.Parent = parent; entity.ParentId = parent.RevisionId; } else if (!command.ParentId.HasValue) { entity.Parent = null; entity.ParentId = null; } if (command.IsRegion.HasValue) { entity.IsRegion = command.IsRegion.Value; } if (command.IsWater.HasValue) { entity.IsWater = command.IsWater.Value; } //audit.NewState = place.ToJsonAudit(); //_entities.Create(audit); if (lastParentId != entity.ParentId) { _hierarchy.Handle(new EnsurePlaceHierarchy(entity)); } _entities.SaveChanges(); }
public void Perform(FixUsfEmailAddresses job) { var peopleWithoutEmailAddress = _entities.Get <Person>() .EagerLoad(_entities, new Expression <Func <Person, object> >[] { x => x.User, }) .Where(x => x.User != null && x.User.Name.EndsWith("@usf.edu") && !x.Emails.Any()) .ToArray() ; if (peopleWithoutEmailAddress.Length < 1) { return; } var fixedPeople = new List <string>(); foreach (var person in peopleWithoutEmailAddress) { var emailValue = person.User.Name; if (person.User.Name.Equals("*****@*****.**")) { emailValue = "*****@*****.**"; } if (person.User.Name.Equals("*****@*****.**")) { emailValue = "*****@*****.**"; } person.Emails.Add(new EmailAddress { IsConfirmed = true, IsDefault = true, Number = 1, Value = emailValue, }); var fixedPerson = new StringBuilder(); fixedPerson.Append(person.DisplayName); fixedPerson.Append(string.Format(" - NetID: {0}", person.User.Name)); fixedPerson.Append(string.Format(", email = {0}", emailValue)); fixedPeople.Add(fixedPerson.ToString()); } fixedPeople = fixedPeople.OrderBy(x => x).ToList(); var output = new StringBuilder(); foreach (var item in fixedPeople) { output.AppendLine(item); } var inspect = output.ToString(); _entities.SaveChanges(); }
public void Handle(DeleteExternalUrl command) { if (command == null) { throw new ArgumentNullException("command"); } var entity = _entities.Get <ExternalUrl>().Single(x => x.Id == command.UrlId); _entities.Purge(entity); _entities.SaveChanges(); }
public void Perform(DeleteRandomUsfActivities job) { var stopwatch = new Stopwatch(); var draftText = ActivityMode.Draft.AsSentenceFragment(); // delete drafts and work copies var activities = _entities.Get <Activity>().Where(x => x.ModeText == draftText || x.Original != null); foreach (var activity in activities) { _entities.Purge(activity); } _entities.SaveChanges(); activities = _entities.Get <Activity>() .Where(x => x.Person.Affiliations.Any(y => y.IsDefault && y.EstablishmentId == 3306)) ; var random = new Random(); while (activities.Count() > 250) { var total = activities.Count(); var idToDelete = random.Next(activities.Min(x => x.RevisionId), activities.Max(x => x.RevisionId)); if (!IdsToKeep.Contains(idToDelete)) { var activityToDelete = _entities.Get <Activity>().ById(idToDelete); if (activityToDelete != null) { _entities.Purge(activityToDelete); _entities.SaveChanges(); } } if (stopwatch.Elapsed.TotalMinutes > 1) { break; } } }
public void Handle(DeleteEmailAddress command) { if (command == null) { throw new ArgumentNullException("command"); } var emailAddress = _entities.Get <EmailAddress>() .Single(x => x.PersonId == command.PersonId && x.Number == command.EmailAddressNumber); _entities.Purge(emailAddress); _entities.SaveChanges(); }
public void Handle(UpdateExternalUrl command) { if (command == null) { throw new ArgumentNullException("command"); } var entity = _entities.Get <ExternalUrl>().Single(x => x.Id == command.UrlId); entity.Description = command.Description; entity.Value = command.Value; _entities.SaveChanges(); }
public void Handle(CopyActivityAndValues command) { if (command == null) { throw new ArgumentNullException("command"); } var originalActivity = _entities.Get <Activity>() .EagerLoad(_entities, new Expression <Func <Activity, object> >[] { x => x.WorkCopy, }) .ById(command.ActivityId, false); // do not do anything if the activity already has a copy if (originalActivity.WorkCopy != null) { command.CreatedActivity = originalActivity.WorkCopy; return; } // copy activity entity var copyActivity = new CopyActivity(command.Principal) { ActivityId = command.ActivityId, Mode = originalActivity.Mode, NoCommit = true, }; _copyActivity.Handle(copyActivity); var copiedActivity = copyActivity.CreatedActivity; // copy activityvalues entity var modeText = originalActivity.Mode.AsSentenceFragment(); var originalValues = originalActivity.Values.First(x => x.ModeText == modeText); var copyActivityValues = new CopyActivityValues(command.Principal) { CopyToActivity = copiedActivity, ActivityValuesId = originalValues.RevisionId, Mode = copiedActivity.Mode, NoCommit = true, }; _copyActivityValues.Handle(copyActivityValues); command.CreatedActivity = copiedActivity; _entities.SaveChanges(); // fix image paths now that there is a pk _moveActivityDocuments.Handle(new MoveActivityDocuments(command.CreatedActivity.RevisionId)); }
public void Handle(PurgeAffiliation command) { if (command == null) { throw new ArgumentNullException("command"); } var entity = !command.PersonId.HasValue ? _entities.Get <Affiliation>().Single(x => x.EstablishmentId == command.EstablishmentId && x.Person.User != null && x.Person.User.Name.Equals(command.Principal.Identity.Name, StringComparison.OrdinalIgnoreCase)) : _entities.Get <Affiliation>().Single(x => x.EstablishmentId == command.EstablishmentId && x.PersonId == command.PersonId.Value); _entities.Purge(entity); _entities.SaveChanges(); }
public void Handle(MoveActivityDocuments command) { if (command == null) { throw new ArgumentNullException("command"); } var queryable = _entities.Get <ActivityDocument>() .EagerLoad(_entities, new Expression <Func <ActivityDocument, object> >[] { x => x.ActivityValues, }) ; if (command.ActivityId.HasValue) { queryable = queryable.Where(x => x.ActivityValues.ActivityId == command.ActivityId); } var entities = queryable.ToArray(); foreach (var entity in entities) { if (entity.Path.StartsWith(string.Format(ActivityDocument.PathFormat, entity.ActivityValues.ActivityId, ""))) { continue; } var oldPath = entity.Path; var newPath = string.Format(ActivityDocument.PathFormat, entity.ActivityValues.ActivityId, Guid.NewGuid()); try { _binaryData.Move(oldPath, newPath); entity.Path = newPath; _entities.SaveChanges(); } catch { if (_binaryData.Exists(newPath)) { _binaryData.Move(newPath, oldPath); } } } }
private void ComposeRegionsFromGeoPlanet() { var regions = _entities.Get <Place>() .Where(x => x.IsRegion && !x.IsWater && !x.Components.Any()) .ToList(); if (regions.Any()) { var mutated = false; foreach (var region in regions) { if (!region.IsRegion) { continue; } var woeId = region.GeoPlanetPlace.WoeId; var components = _entities.Get <Place>() .Where(x => x.GeoPlanetPlace != null && x.GeoPlanetPlace.BelongTos.Select(y => y.BelongToWoeId).Contains(woeId)); foreach (var component in components) { if (!component.IsCountry && !component.IsWater) { continue; } if (component.IsRegion) { continue; } if (region.Components.All(x => x.RevisionId != component.RevisionId)) { region.Components.Add(component); if (!mutated) { mutated = true; } } } } if (mutated) { _entities.SaveChanges(); } } }
public void Handle(CreateExternalUrl command) { if (command == null) { throw new ArgumentNullException("command"); } var entity = new ExternalUrl { PersonId = command.PersonId, Description = command.Description, Value = command.Value, }; _entities.Create(entity); _entities.SaveChanges(); command.Created = entity; }
public void Perform(PopulateActivityContentSearchable job) { var isChanged = false; var entities = _entities.Get <ActivityValues>(); foreach (var entity in entities) { if (string.IsNullOrWhiteSpace(entity.ContentSearchable) && !string.IsNullOrWhiteSpace(entity.Content)) { entity.Content = entity.Content; isChanged = true; } } if (isChanged) { _entities.SaveChanges(); } }
public void Perform(MigrateUsfAgreementData job) { var reportBuilder = new WorkReportBuilder("Migrate USF Agreement Data"); try { // for USF, move from description into content // do not preserve any titles as names. const int usfEstablishmentId = 3306; var usfAgreements = _entities.Get <Agreement>() .Where(x => x.Participants.Any(y => y.IsOwner && ( y.EstablishmentId == usfEstablishmentId || y.Establishment.Ancestors.Any(z => z.AncestorId == usfEstablishmentId) ) )) ; foreach (var usfAgreement in usfAgreements) { var html = usfAgreement.Description.ToHtml(); usfAgreement.Name = null; usfAgreement.Content = html; } _entities.SaveChanges(); } catch (Exception ex) { reportBuilder.Report(""); reportBuilder.Report("JOB FAILED!"); reportBuilder.Report(ex.GetType().Name); reportBuilder.Report(ex.Message); reportBuilder.Report(ex.StackTrace); _exceptionLogger.Log(ex); } finally { reportBuilder.Send(_mailSender); } }
public void Handle(EnsurePlaceHierarchies command) { if (command == null) { throw new ArgumentNullException("command"); } var queryable = _entities.Get <Place>() .EagerLoad(_entities, new Expression <Func <Place, object> >[] { x => x.Parent.Ancestors.Select(y => y.Ancestor), x => x.Ancestors.Select(y => y.Ancestor), }) .Where(x => x.Parent != null) .Where(x => x.Ancestors.Count <= x.Parent.Ancestors.Count || // place should always have more ancestors than its parent !x.Ancestors.Select(y => y.AncestorId).Contains(x.ParentId.Value) // place ancestors should always contain the parent // place should have 1 more ancestor than its closest ancestor does || x.Ancestors.Count != x.Ancestors.OrderBy(y => y.Separation).FirstOrDefault().Ancestor.Ancestors.Count + 1) .OrderBy(x => x.Ancestors.Count) ; //var placesFixed = new List<Place>(); while (queryable.Any()) { var howMany = queryable.Count(); if (howMany < 1) { break; } var place = queryable.First(); if (!command.EnsuredPlaceNames.ContainsKey(place.RevisionId)) { command.EnsuredPlaceNames.Add(place.RevisionId, place.OfficialName); } _hierarchy.Handle(new EnsurePlaceHierarchy(place)); //placesFixed.Add(place); _entities.SaveChanges(); } //var names = placesFixed.Select(x => x.OfficialName).ToArray(); }
public void Handle(CreatePlace command) { if (command == null) { throw new ArgumentNullException("command"); } var parent = command.ParentId.HasValue ? _entities.Get <Place>().Single(x => x.RevisionId == command.ParentId.Value) : null; var entity = new Place { OfficialName = command.OfficialName, ParentId = command.ParentId, Parent = parent, IsWater = command.IsWater, IsRegion = command.IsRegion, }; _entities.Create(entity); _hierarchy.Handle(new EnsurePlaceHierarchy(entity)); _entities.SaveChanges(); command.Created = _entities.Query <Place>().Single(x => x.RevisionId == entity.RevisionId); }
public void Handle(CreateEmailAddress command) { if (command == null) { throw new ArgumentNullException("command"); } // load person var person = command.Person ?? _entities.Get <Person>() .EagerLoad(_entities, new Expression <Func <Person, object> >[] { x => x.Emails, }) .Single(x => x.RevisionId == command.PersonId); // override IsDefault when person has no default email var isDefault = command.IsDefault || !person.Emails.Any(x => x.IsDefault); // clear previous default email if (isDefault) { foreach (var email in person.Emails.Where(x => x.IsDefault)) { _updateEmail.Handle(new UpdateEmailAddress(email) { NoCommit = true, IsDefault = false, IsConfirmed = email.IsConfirmed, IsFromSaml = email.IsFromSaml, Value = email.Value, }); } } // create email address entity var entity = new EmailAddress { IsConfirmed = command.IsConfirmed, IsDefault = isDefault, Value = command.Value, Number = person.Emails.NextNumber(), Person = person, IsFromSaml = command.IsFromSaml, }; person.Emails.Add(entity); // log audit var audit = new CommandEvent { RaisedBy = Thread.CurrentPrincipal.Identity.Name, Name = command.GetType().FullName, Value = JsonConvert.SerializeObject(new { command.PersonId, command.Value, command.IsConfirmed, IsDefault = isDefault, command.IsFromSaml, }), NewState = entity.ToJsonAudit(), }; _entities.Create(audit); // store if (!command.NoCommit) { _entities.SaveChanges(); } command.CreatedEmailAddress = entity; command.CreatedEmailAddressNumber = entity.Number; }
public void Handle(CopyActivityValues command) { if (command == null) { throw new ArgumentNullException("command"); } var copyToActivity = command.CopyToActivity ?? _entities.Get <Activity>().ById(command.CopyToActivityId); var copiedActivityValues = _detachedEntities.Query <ActivityValues>() .EagerLoad(_entities, new Expression <Func <ActivityValues, object> >[] { x => x.Locations, x => x.Types, x => x.Tags, x => x.Documents, }) .ById(command.ActivityValuesId, false); copiedActivityValues.Activity = copyToActivity; copiedActivityValues.ActivityId = copyToActivity.RevisionId; copiedActivityValues.Mode = command.Mode; copiedActivityValues.RevisionId = 0; EnableForCopy(copiedActivityValues, command); foreach (var location in copiedActivityValues.Locations) { EnableForCopy(location, command); } foreach (var type in copiedActivityValues.Types) { EnableForCopy(type, command); } foreach (var tag in copiedActivityValues.Tags) { EnableForCopy(tag, command); } var documentsToCopy = copiedActivityValues.Documents; copiedActivityValues.Documents = new Collection <ActivityDocument>(); foreach (var document in documentsToCopy) { _createActivityDocument.Handle(new CreateActivityDocument(command.Principal, copiedActivityValues) { FileName = document.FileName, MimeType = document.MimeType, Content = _binaryData.Get(document.Path), Title = document.Title, EntityId = document.EntityId, NoCommit = true, }); } command.CreatedActivityValues = copiedActivityValues; _entities.Create(copiedActivityValues); if (!command.NoCommit) { _entities.SaveChanges(); } }
public void Perform(AddNewEstablishmentTypes job) { var establishmentType = _entities.Get <EstablishmentType>().SingleOrDefault(x => x.CategoryCode == EstablishmentCategoryCode.Inst && x.EnglishName == "Institute"); if (establishmentType != null) { return; } _entities.Create(new EstablishmentType { CategoryCode = EstablishmentCategoryCode.Inst, EnglishName = "Institute", EnglishPluralName = "Institutes", }); _entities.Create(new EstablishmentType { CategoryCode = EstablishmentCategoryCode.Inst, EnglishName = "School", EnglishPluralName = "Schools", }); _entities.Create(new EstablishmentType { CategoryCode = EstablishmentCategoryCode.Inst, EnglishName = "Academy", EnglishPluralName = "Academies", }); _entities.Create(new EstablishmentType { CategoryCode = EstablishmentCategoryCode.Inst, EnglishName = "Centre / Center", EnglishPluralName = "Centres / Centers", }); _entities.Create(new EstablishmentType { CategoryCode = EstablishmentCategoryCode.Corp, EnglishName = "Foundation", EnglishPluralName = "Foundations", }); _entities.Create(new EstablishmentType { CategoryCode = EstablishmentCategoryCode.Corp, EnglishName = "Other Non-Governmental Organization", EnglishPluralName = "Other Non-Governmental Organizations", }); _entities.Create(new EstablishmentType { CategoryCode = EstablishmentCategoryCode.Govt, EnglishName = "Ministry", EnglishPluralName = "Ministries", }); _entities.Create(new EstablishmentType { CategoryCode = EstablishmentCategoryCode.Govt, EnglishName = "Secretariat", EnglishPluralName = "Secretariats", }); _entities.Create(new EstablishmentType { CategoryCode = EstablishmentCategoryCode.Govt, EnglishName = "Department", EnglishPluralName = "Departments", }); _entities.Create(new EstablishmentType { CategoryCode = EstablishmentCategoryCode.Govt, EnglishName = "Other Governmental Organization", EnglishPluralName = "Non-Governmental Organizations", }); _entities.SaveChanges(); }
public void Handle(CreateUser command) { if (command == null) { throw new ArgumentNullException("command"); } // determine which establishment the user should be affiliated with var emailDomain = command.Name.GetEmailDomain(); var establishmentToAffiliate = _entities.Get <Establishment>() .Single(x => x.EmailDomains.Any(y => y.Value.Equals(emailDomain, StringComparison.OrdinalIgnoreCase))); // default person to the one provided by another internal command var person = command.Person; if (person == null && command.PersonId.HasValue) { // get person by id when provided by command person = _entities.Get <Person>().Single(x => x.RevisionId == command.PersonId.Value); } else if (person == null) { // otherwise, must create a new person var createPersonCommand = new CreatePerson { DisplayName = command.Name, NoCommit = true, }; _createPerson.Handle(createPersonCommand); person = createPersonCommand.CreatedPerson; } if (!person.Emails.Any(x => x.Value.Equals(command.Name, StringComparison.OrdinalIgnoreCase))) { var createEmailCommand = new CreateEmailAddress(command.Name, person) { NoCommit = true, IsConfirmed = true, IsFromSaml = false, IsDefault = true, }; _createEmailAddress.Handle(createEmailCommand); } var affiliation = new Affiliation { IsDefault = true, Person = person, EstablishmentId = establishmentToAffiliate.RevisionId }; person.Affiliations.Add(affiliation); var user = new User { Name = command.Name, IsRegistered = command.IsRegistered, TenantId = establishmentToAffiliate.RevisionId, Person = person, }; // log audit var audit = new CommandEvent { RaisedBy = command.Principal.Identity.Name, Name = command.GetType().FullName, Value = JsonConvert.SerializeObject(new { command.Name, command.PersonId, command.IsRegistered, }), NewState = user.ToJsonAudit(), }; _entities.Create(audit); _entities.Create(user); _entities.SaveChanges(); command.CreatedUserId = user.RevisionId; var userCreated = new UserCreated(command.Principal) { UserId = user.RevisionId, PersonId = person.RevisionId, TenantId = affiliation.EstablishmentId, }; _eventTrigger.Raise(userCreated); }