/// <summary> /// The user may create an evaluation older then the most recent evaluation stored in PIMS. To support this, remove any evaluations that are within one year of the passed date. /// </summary> /// <param name="property"></param> /// <param name="updatedProperty"></param> /// <param name="disposedOn"></param> public static void RemoveEvaluationsWithinOneYear(this Entity.Property property, Entity.Property updatedProperty, DateTime?disposedOn = null) { foreach (Entity.EvaluationKeys key in Enum.GetValues(typeof(Entity.EvaluationKeys))) { DateTime?mostRecentDate = null; DateTime?date = null; if (property is Entity.Parcel) { mostRecentDate = ((Entity.Parcel)property).Evaluations.Where(d => d.Key == key).OrderByDescending(d => d.Date).FirstOrDefault()?.Date; date = ((Entity.Parcel)updatedProperty).Evaluations.FirstOrDefault(e => e.Key == key)?.Date; } else if (property is Entity.Building) { mostRecentDate = ((Entity.Building)property).Evaluations.Where(d => d.Key == key).OrderByDescending(d => d.Date).FirstOrDefault()?.Date; date = ((Entity.Building)updatedProperty).Evaluations.FirstOrDefault(e => e.Key == key)?.Date; } //If the date passed in is the most recent, we don't need to do any removal logic. if (mostRecentDate == null || date == null || mostRecentDate == date) { continue; } var maxDate = (disposedOn ?? date)?.AddYears(1); if (property is Entity.Parcel) { ((Entity.Parcel)property).Evaluations.RemoveAll(e => e.Date > date && e.Date < maxDate && key == e.Key); } else if (property is Entity.Building) { ((Entity.Building)property).Evaluations.RemoveAll(e => e.Date > date && e.Date < maxDate && key == e.Key); } } }
/// <summary> /// Update the property.ProjectNumbers with the specified 'projectNumber', de-duplicating based on the numeric portion. /// </summary> /// <param name="property"></param> /// <param name="projectNumber"></param> /// <returns></returns> public static Entity.Property UpdateProjectNumbers(this Entity.Property property, string projectNumber) { IEnumerable <string> projectNumbers = JsonSerializer.Deserialize <IEnumerable <string> >(property.ProjectNumbers ?? "[]"); property.ProjectNumbers = JsonSerializer.Serialize(AddProjectNumber(projectNumbers, projectNumber)); return(property); }
/// <summary> /// Remove from the property.ProjectNumbers using the specified 'projectNumber'. /// </summary> /// <param name="property"></param> /// <param name="projectNumber"></param> /// <returns></returns> public static Entity.Property RemoveProjectNumber(this Entity.Property property, string projectNumber) { IEnumerable <string> projectNumbers = JsonSerializer.Deserialize <IEnumerable <string> >(property.ProjectNumbers ?? "[]"); property.ProjectNumbers = JsonSerializer.Serialize(projectNumbers.Where(p => p != projectNumber)); return(property); }
/// <summary> /// Throw an exception if the passed property is not in the same agency or sub agency as this project. /// </summary> /// <param name="parcel"></param> /// <param name="projectAgencyIds"></param> /// <returns></returns> public static void ThrowIfPropertyNotInProjectAgency(this Entity.Property parcel, IEnumerable <int> projectAgencyIds) { // properties may be in the same agency or sub-agency of a project. A parcel in a parent agency may not be added to a sub-agency project. if (!parcel.AgencyId.HasValue || !projectAgencyIds.Contains(parcel.AgencyId.Value)) { throw new InvalidOperationException("Properties may not be added to Projects with a different agency."); } }
/// <summary> /// Get the latest project associated to this property, using the workflow sort order. /// </summary> /// <param name="property"></param> /// <returns>The workflow code of the latest workflow associated to this property</returns> public static Entity.Project GetLatestProject(this Entity.Property property) { Entity.Project latestProject = null; if (property is Entity.Parcel parcel && parcel.Projects.Select(pp => pp.Project).Any()) { latestProject = parcel.Projects.Select(pp => pp.Project). Aggregate((Entity.Project projectWithLatestWorkflow, Entity.Project current) => current.Workflow?.SortOrder > projectWithLatestWorkflow?.Workflow?.SortOrder && current?.Status?.IsTerminal == false ? current : projectWithLatestWorkflow); }
/// <summary> /// Throw an exception if the passed property is in an SPP project that is in a non-draft status. /// </summary> /// <param name="property"></param> /// <param name="user"></param> /// <returns></returns> public static void ThrowIfPropertyInSppProject(this Entity.Property property, ClaimsPrincipal user) { var isAdmin = user.HasPermission(Permissions.AdminProperties); if (!isAdmin && property?.ProjectNumbers?.Contains("SPP") == true) { throw new NotAuthorizedException("User may not remove buildings that are in a SPP Project."); } }