private SaveGroupDto CreateSaveDto(GroupEditorViewModel viewModel) { viewModel.SetCCLeadCentreUrn(); Func <GroupModel> createDomainModel = () => new GroupModel { CompaniesHouseNumber = viewModel.CompaniesHouseNumber, GroupId = viewModel.GroupId, GroupTypeId = viewModel.GroupTypeId, GroupUId = viewModel.GroupUId, LocalAuthorityId = viewModel.LocalAuthorityId, ManagerEmailAddress = viewModel.ManagerEmailAddress, Name = viewModel.GroupName, OpenDate = viewModel.OpenDate.ToDateTime(), ClosedDate = viewModel.ClosedDate.ToDateTime(), Address = UriHelper.TryDeserializeUrlToken <AddressDto>(viewModel.AddressJsonToken) }; Func <List <LinkedEstablishmentGroup> > createLinksDomainModel = () => viewModel.LinkedEstablishments.Establishments.Select(x => new LinkedEstablishmentGroup { Urn = x.Urn, Id = x.Id, JoinedDate = x.JoinedDate ?? x.JoinedDateEditable.ToDateTime(), CCIsLeadCentre = x.CCIsLeadCentre }).ToList(); SaveGroupDto dto = null; if (viewModel.SaveMode == eSaveMode.Details) { dto = new SaveGroupDto(createDomainModel()); } else if (viewModel.SaveMode == eSaveMode.DetailsAndLinks) { dto = new SaveGroupDto(createDomainModel(), createLinksDomainModel()); } else if (viewModel.SaveMode == eSaveMode.Links) { dto = new SaveGroupDto(viewModel.GroupUId.Value, createLinksDomainModel()); } else { throw new NotImplementedException($"SaveMode '{viewModel.SaveMode}' is not supported"); } if (viewModel.CanUserCloseMATAndMarkAsCreatedInError && viewModel.CloseMATAndMarkAsCreatedInError && dto.Group != null) { dto.Group.StatusId = (int)GS.CreatedInError; } return(dto); }
private SaveGroupDto CreateSaveDto(GroupEditorViewModel viewModel) { viewModel.SetCCLeadCentreUrn(); GroupModel createDomainModel() => new GroupModel { CompaniesHouseNumber = viewModel.CompaniesHouseNumber, GroupId = viewModel.GroupId, GroupTypeId = viewModel.GroupTypeId, GroupUId = viewModel.GroupUId, LocalAuthorityId = viewModel.LocalAuthorityId, ManagerEmailAddress = viewModel.ManagerEmailAddress, Name = viewModel.GroupName, OpenDate = viewModel.OpenDate.ToDateTime(), ClosedDate = viewModel.ClosedDate.ToDateTime(), Address = UriHelper.TryDeserializeUrlToken <AddressDto>(viewModel.AddressJsonToken), StatusId = viewModel.StatusId, UKPRN = viewModel.UKPRN?.ToString() }; List <LinkedEstablishmentGroup> createLinksDomainModel() { var domainList = viewModel.LinkedEstablishments.Establishments.Select(x => new LinkedEstablishmentGroup { Urn = x.Urn, Id = x.Id, JoinedDate = x.JoinedDate ?? x.JoinedDateEditable.ToDateTime(), CCIsLeadCentre = x.CCIsLeadCentre }).ToList(); if (domainList.Any() && !domainList.Any(x => x.CCIsLeadCentre)) { domainList.First().CCIsLeadCentre = true; viewModel.LinkedEstablishments.Establishments.First().CCIsLeadCentre = true; } return(domainList); } List <LinkedEstablishmentGroup> createLinkedEstablishmentFromSearch(bool isSearch = false) => new List <LinkedEstablishmentGroup> { new LinkedEstablishmentGroup { Urn = viewModel.LinkedEstablishments.LinkedEstablishmentSearch.Urn.ToInteger(), JoinedDate = viewModel.LinkedEstablishments.LinkedEstablishmentSearch.JoinedDate .ToDateTime() ?? viewModel.OpenDate.ToDateTime(), CCIsLeadCentre = isSearch || !createLinksDomainModel().Any(x => x.CCIsLeadCentre) } }; List <LinkedEstablishmentGroup> createLinkedEstablishmentFromEdit() => new List <LinkedEstablishmentGroup> { new LinkedEstablishmentGroup { Urn = viewModel.LinkedEstablishments.Establishments.First(x => x.Urn == viewModel.ActionUrn).Urn, JoinedDate = viewModel.LinkedEstablishments.Establishments.First(x => x.Urn == viewModel.ActionUrn).JoinedDate, CCIsLeadCentre = true // the validation always needs this to be set to true when in edit } }; List <LinkedEstablishmentGroup> createLinkedEstablishmentFromAdd() { var domainList = createLinksDomainModel(); if (domainList.Any(x => x.Urn == viewModel.LinkedEstablishments.LinkedEstablishmentSearch.FoundUrn)) { // edit domainList.First(x => x.Urn == viewModel.LinkedEstablishments.LinkedEstablishmentSearch.FoundUrn) .JoinedDate = viewModel.LinkedEstablishments.LinkedEstablishmentSearch.JoinedDate.ToDateTime(); viewModel.LinkedEstablishments.Establishments.First(x => viewModel.LinkedEstablishments.LinkedEstablishmentSearch.FoundUrn != null && x.Urn == viewModel.LinkedEstablishments.LinkedEstablishmentSearch.FoundUrn.Value).JoinedDate = viewModel.LinkedEstablishments.LinkedEstablishmentSearch.JoinedDate.ToDateTime(); } else { // add domainList.AddRange(createLinkedEstablishmentFromSearch(false)); if (viewModel.LinkedEstablishments.LinkedEstablishmentSearch.FoundUrn != null) { viewModel.LinkedEstablishments.Establishments.Add(new EstablishmentGroupViewModel { Name = viewModel.LinkedEstablishments.LinkedEstablishmentSearch.Name, JoinedDate = viewModel.LinkedEstablishments.LinkedEstablishmentSearch.JoinedDate .ToDateTime(), Urn = viewModel.LinkedEstablishments.LinkedEstablishmentSearch.FoundUrn.Value, CCIsLeadCentre = viewModel.LinkedEstablishments.Establishments.All(x => x.CCIsLeadCentre == false) }); } } viewModel.LinkedEstablishments.LinkedEstablishmentSearch = new LinkedEstablishmentSearchViewModel(); return(domainList); } SaveGroupDto dto = null; if (viewModel.SaveMode == eSaveMode.Details) { dto = new SaveGroupDto(createDomainModel()); } else if (viewModel.SaveMode == eSaveMode.DetailsAndLinks) { if (viewModel.Action == ActionLinkedEstablishmentSearch) { dto = new SaveGroupDto(createDomainModel(), createLinkedEstablishmentFromSearch(true)); _ = SearchForLinkedEstablishment(viewModel); } else if (viewModel.Action.StartsWith(ActionLinkedEstablishmentEdit)) { dto = new SaveGroupDto(createDomainModel(), createLinkedEstablishmentFromEdit()); _ = EditLinkedEstablishment(viewModel); } else if (viewModel.Action == ActionLinkedEstablishmentAdd) { dto = new SaveGroupDto(createDomainModel(), createLinkedEstablishmentFromAdd()); } else if (viewModel.Action.StartsWith(ActionLinkedEstablishmentRemove)) { viewModel.LinkedEstablishments.Establishments.RemoveAll(x => x.Urn == viewModel.ActionUrn); dto = new SaveGroupDto(createDomainModel(), createLinksDomainModel()); } else { dto = new SaveGroupDto(createDomainModel(), createLinksDomainModel()); } } else if (viewModel.SaveMode == eSaveMode.Links) { dto = new SaveGroupDto(viewModel.GroupUId.Value, createLinksDomainModel()); } else { throw new NotImplementedException($"SaveMode '{viewModel.SaveMode}' is not supported"); } if (viewModel.CanUserCloseAndMarkAsCreatedInError && viewModel.CloseAndMarkAsCreatedInError && dto.Group != null) { dto.Group.StatusId = (int)GS.CreatedInError; } viewModel.DeriveCCLeadCentreUrn(); return(dto); }