From b6979f3e9adc11b98dcc7a6d7dc9ed69b3697a2d Mon Sep 17 00:00:00 2001 From: Rafael Ponce de Leon <36902894+rafaelponcedeleon@users.noreply.github.com> Date: Tue, 7 Jul 2020 14:18:49 -0700 Subject: [PATCH] R1.5 update (#1081) * Embcessmod 373 phone email update (#1015) * Add new checkboxes to self registration * Add custom validation for no email and phone * Update review of self reg to show no email/phone number * Add new validation for email and phone number to registration-maker * Uncommented code * Add Strathcona Regional District to communities (#1017) * Change viewEvacuee to properly include city (#1019) * Add country to viewEvacuee (#1021) * Embcessmod 373 fixes (#1022) * Add required class to Telephone/Email labels * Add required class to telephone/email in registration-maker * Change required validation on phone number * Fix typo in registration summary * Add CSS to fix layout of checkboxes * Fix issue where no email/no phone inputs did not save state when editing registration * Add no email and no phone properties to registration reducer * Fix bug where telephone number was not actually required * uncommented code * Embcessmod 373 fixes (#1024) * Fix bug with validatiors * Fixed bug with empty no email/phone checkboxes * Set ERA version 1.2 (#1025) * EMBCESSMOD-242 tool tip text * reverted index order for needs assessment * Fixed wrong revert change * EMBCESSMOD-310 Self-Registration - Needs Assessment Fields Wrong Order * EMBCESSMOD-335 reversed values for Food, Lodging, Clothing, Transportation, Incidentals * EMBCESSMOD-335 additional fix to reversed values * EMBCESSMOD-320 Unique Task# * Fixed unit tests compilation * Added data protection keys persistent folder setting * Simplified dependencies for now * Set ERA version to 1.2 Co-authored-by: Curtis Co-authored-by: Yossi T <48697347+ytqsl@users.noreply.github.com> Co-authored-by: Yossi Tamari * ERA Release Version Added FileVersion and AssemblyVersion * Embcessmod 306 self reg evac from (#1027) * Add validators for evacuated from * Add checkbox for primary addr is same as evac'd from * Add communities selector * Text change * Fixed issue with community component not displaying * Add evac from to registration summary * Remove test console.log * Change wording on email in self-reg confirmation (#1028) * Add different styles depending on env (#1030) * Add different styles depending on env * Fix formatting issue in constants file * Added new environment variable * Embcessmod 449 env banner (#1032) * Add different styles depending on env * Fix formatting issue in constants file * Added new environment variable * Add initial modal popup * Add styles for different envs to modal * Add Big Lake to Communities.json (#1034) * Add environment modal to login link in header (#1035) * R2 landing page updates (#1037) * EMBCESSMOD-242 tool tip text * reverted index order for needs assessment * Fixed wrong revert change * EMBCESSMOD-310 Self-Registration - Needs Assessment Fields Wrong Order * EMBCESSMOD-335 reversed values for Food, Lodging, Clothing, Transportation, Incidentals * EMBCESSMOD-335 additional fix to reversed values * EMBCESSMOD-320 Unique Task# * Fixed unit tests compilation * Added data protection keys persistent folder setting * Simplified dependencies for now * Set ERA version to 1.2 * R2 landing page updates Co-authored-by: Curtis Co-authored-by: Yossi T <48697347+ytqsl@users.noreply.github.com> Co-authored-by: Yossi Tamari * R2 landing page image fix (#1038) * EMBCESSMOD-242 tool tip text * reverted index order for needs assessment * Fixed wrong revert change * EMBCESSMOD-310 Self-Registration - Needs Assessment Fields Wrong Order * EMBCESSMOD-335 reversed values for Food, Lodging, Clothing, Transportation, Incidentals * EMBCESSMOD-335 additional fix to reversed values * EMBCESSMOD-320 Unique Task# * Fixed unit tests compilation * Added data protection keys persistent folder setting * Simplified dependencies for now * Set ERA version to 1.2 * R2 landing page updates * R2 landing page image fix Co-authored-by: Curtis Co-authored-by: Yossi T <48697347+ytqsl@users.noreply.github.com> Co-authored-by: Yossi Tamari * Embcessmod 306 updates (#1039) * Change environment names to be more consistent * Add function to handle mapping primary address to evacuated from * Save value of Evac'd From Primary Addr when editing * Embcessmod 306 updates (#1040) * Change environment names to be more consistent * Add function to handle mapping primary address to evacuated from * Save value of Evac'd From Primary Addr when editing * Fix issue with changing evacuated from primary address from 'no' to 'yes' * Change env modal text to match wireframes (#1041) * Display correct env modal body text (#1043) * Add migration to fix view (#1042) * Embcessmod 306 updates (#1049) * Change environment names to be more consistent * Add function to handle mapping primary address to evacuated from * Save value of Evac'd From Primary Addr when editing * Fix issue with changing evacuated from primary address from 'no' to 'yes' * Fix bug where evac from in summary is wrong * Hide env banner for prod self-reg (#1050) * Embcessmod 494 (#1047) * Display correct env modal body text * Add Bulkley-Nechako community * Add new communities to Communities.json * Add communities to correct region * Embcessmod 306 fixes (#1052) * Add valueChange event to evacFromPrimaryAddr * Change displayRegistration() to patch evacuatedFrom control outside of from.patchValue function * Add migration to update babine lake (#1053) * Embcessmod 490 add contact info (#1055) * Add contact info for all logged in roles * Add contact info for 'everyone' role * Fix alignment of contact info for signed-out roles * Remove commented code * Update language on training and material page (#1056) * EMBCESSMOD-549 URL updates to embc domain for era training and prod (#1057) * EMBCESSMOD-242 tool tip text * reverted index order for needs assessment * Fixed wrong revert change * EMBCESSMOD-310 Self-Registration - Needs Assessment Fields Wrong Order * EMBCESSMOD-335 reversed values for Food, Lodging, Clothing, Transportation, Incidentals * EMBCESSMOD-335 additional fix to reversed values * EMBCESSMOD-320 Unique Task# * Fixed unit tests compilation * Added data protection keys persistent folder setting * Simplified dependencies for now * Set ERA version to 1.2 * R2 landing page updates * R2 landing page image fix * EMBCESSMOD-549 URL updates to embc domain for era training and prod Co-authored-by: Curtis Co-authored-by: Yossi T <48697347+ytqsl@users.noreply.github.com> Co-authored-by: Yossi Tamari * Update language on training and material page (#1058) * Embcessmod 441 additional train mats (#1060) * Add new facilitator links * Change icon to PowerPoint * Moved PDF icon to correct link * Embcessmod 475 release notes (#1059) * Add era updates release section to useful info page * Change url of updates pdf * Change release number to 1.x * R1.4 update (#1062) * EMBCESSMOD-242 tool tip text * reverted index order for needs assessment * Fixed wrong revert change * EMBCESSMOD-310 Self-Registration - Needs Assessment Fields Wrong Order * EMBCESSMOD-335 reversed values for Food, Lodging, Clothing, Transportation, Incidentals * EMBCESSMOD-335 additional fix to reversed values * EMBCESSMOD-320 Unique Task# * Fixed unit tests compilation * Added data protection keys persistent folder setting * Simplified dependencies for now * Set ERA version to 1.2 * R2 landing page updates * R2 landing page image fix * EMBCESSMOD-549 URL updates to embc domain for era training and prod * fixed self registration link to use embc.gov.bc.ca domain Co-authored-by: Curtis Co-authored-by: Yossi T <48697347+ytqsl@users.noreply.github.com> Co-authored-by: Yossi Tamari * Embcessmod 436 csv export (#1064) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Embcessmod 436 csv export (#1065) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Update Referral Query and manually calculate pdt for file name * Change the date calculation in title to use TimeZoneInfo * Embcessmod 436 csv export (#1066) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Update Referral Query and manually calculate pdt for file name * Change the date calculation in title to use TimeZoneInfo * Change file naming to match supplier * Embcessmod 436 csv export (#1067) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Update Referral Query and manually calculate pdt for file name * Change the date calculation in title to use TimeZoneInfo * Change file naming to match supplier * Change OS switch statement to old style * Embcessmod 436 csv export (#1068) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Update Referral Query and manually calculate pdt for file name * Change the date calculation in title to use TimeZoneInfo * Change file naming to match supplier * Change OS switch statement to old style * Remove unused line * Embcessmod 436 csv export (#1069) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Update Referral Query and manually calculate pdt for file name * Change the date calculation in title to use TimeZoneInfo * Change file naming to match supplier * Change OS switch statement to old style * Remove unused line * Add search params to reports * Fix whitespace that was annoying me * Embcessmod 436 csv export (#1070) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Update Referral Query and manually calculate pdt for file name * Change the date calculation in title to use TimeZoneInfo * Change file naming to match supplier * Change OS switch statement to old style * Remove unused line * Add search params to reports * Fix whitespace that was annoying me * Fix validation logic for superusers exporting * Uncommented date of birth field * Change PK of EvacueeReportItem * Remove commented out code * EMBCESSMOD-592: fixed tasks list search queries and display (#1072) * Embcessmod 436 csv export (#1071) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Update Referral Query and manually calculate pdt for file name * Change the date calculation in title to use TimeZoneInfo * Change file naming to match supplier * Change OS switch statement to old style * Remove unused line * Add search params to reports * Fix whitespace that was annoying me * Fix validation logic for superusers exporting * Uncommented date of birth field * Change PK of EvacueeReportItem * Remove commented out code * Swap Evacuated_From and Evacuated_To in export * Change Evac From and To in export query * EMBCESSMOD-592: fixed tasks list search queries and display (#1073) * EMBCESSMOD-592: fixed tasks list search queries and display * Added active/expired flag to filter api call * Embcessmod 436 csv export (#1074) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Update Referral Query and manually calculate pdt for file name * Change the date calculation in title to use TimeZoneInfo * Change file naming to match supplier * Change OS switch statement to old style * Remove unused line * Add search params to reports * Fix whitespace that was annoying me * Fix validation logic for superusers exporting * Uncommented date of birth field * Change PK of EvacueeReportItem * Remove commented out code * Swap Evacuated_From and Evacuated_To in export * Change Evac From and To in export query * Reversed Evacuated To and From in the search paramters for export * Embcessmod 436 csv export (#1075) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Update Referral Query and manually calculate pdt for file name * Change the date calculation in title to use TimeZoneInfo * Change file naming to match supplier * Change OS switch statement to old style * Remove unused line * Add search params to reports * Fix whitespace that was annoying me * Fix validation logic for superusers exporting * Uncommented date of birth field * Change PK of EvacueeReportItem * Remove commented out code * Swap Evacuated_From and Evacuated_To in export * Change Evac From and To in export query * Reversed Evacuated To and From in the search paramters for export * Add code to swap Evac_To and From in search params export * Export fixes (#1076) * Fix issue when searching by dob * Remove unused search fields * Embcessmod 436 csv export (#1077) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Update Referral Query and manually calculate pdt for file name * Change the date calculation in title to use TimeZoneInfo * Change file naming to match supplier * Change OS switch statement to old style * Remove unused line * Add search params to reports * Fix whitespace that was annoying me * Fix validation logic for superusers exporting * Uncommented date of birth field * Change PK of EvacueeReportItem * Remove commented out code * Swap Evacuated_From and Evacuated_To in export * Change Evac From and To in export query * Reversed Evacuated To and From in the search paramters for export * Add code to swap Evac_To and From in search params export * Fix issue when searching by dob * Remove unused search fields * Add code to handle null radio buttons * Remove commented out code * Embcessmod 436 csv export (#1078) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Update Referral Query and manually calculate pdt for file name * Change the date calculation in title to use TimeZoneInfo * Change file naming to match supplier * Change OS switch statement to old style * Remove unused line * Add search params to reports * Fix whitespace that was annoying me * Fix validation logic for superusers exporting * Uncommented date of birth field * Change PK of EvacueeReportItem * Remove commented out code * Swap Evacuated_From and Evacuated_To in export * Change Evac From and To in export query * Reversed Evacuated To and From in the search paramters for export * Add code to swap Evac_To and From in search params export * Fix issue when searching by dob * Remove unused search fields * Add code to handle null radio buttons * Remove commented out code * Add function for friendly search param names * Change referral query to strip commas from purchaser column * Fix issue with comma in Purchaser column * Remove unecessary cast * Embcessmod 436 csv export (#1080) * Add File-Saver package * Add EvacueeReportItem and query * Add Where Clause to Evacuee Export * Removed old download function * Add export validation for super users * Change evacuee report routing * Rename evacuee export function * Add referrals export with where clauses * Add click event for referral export * Update search button styles * Update Referral Query and manually calculate pdt for file name * Change the date calculation in title to use TimeZoneInfo * Change file naming to match supplier * Change OS switch statement to old style * Remove unused line * Add search params to reports * Fix whitespace that was annoying me * Fix validation logic for superusers exporting * Uncommented date of birth field * Change PK of EvacueeReportItem * Remove commented out code * Swap Evacuated_From and Evacuated_To in export * Change Evac From and To in export query * Reversed Evacuated To and From in the search paramters for export * Add code to swap Evac_To and From in search params export * Fix issue when searching by dob * Remove unused search fields * Add code to handle null radio buttons * Remove commented out code * Add function for friendly search param names * Change referral query to strip commas from purchaser column * Fix issue with comma in Purchaser column * Remove unecessary cast * Change reg completed and refs provided search params depending on report Co-authored-by: Wes R Co-authored-by: Curtis Co-authored-by: Yossi T <48697347+ytqsl@users.noreply.github.com> Co-authored-by: Yossi Tamari --- embc-app/Controllers/EvacueesController.cs | 146 ++++---- embc-app/Utils/CsvConverter.cs | 396 +++++++++++---------- 2 files changed, 281 insertions(+), 261 deletions(-) diff --git a/embc-app/Controllers/EvacueesController.cs b/embc-app/Controllers/EvacueesController.cs index e2f21846c..5135108aa 100644 --- a/embc-app/Controllers/EvacueesController.cs +++ b/embc-app/Controllers/EvacueesController.cs @@ -1,73 +1,73 @@ -using Gov.Jag.Embc.Public.DataInterfaces; -using Gov.Jag.Embc.Public.Utils; -using Gov.Jag.Embc.Public.ViewModels.Search; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using System; -using System.Text; -using System.Threading.Tasks; - -namespace Gov.Jag.Embc.Public.Controllers -{ - [Route("api/[controller]")] - [Authorize] - public class EvacueesController : Controller - { - private readonly IDataInterface dataInterface; - - public EvacueesController(IDataInterface dataInterface) - { - this.dataInterface = dataInterface; - } - - [HttpGet] - public async Task Get([FromQuery] EvacueeSearchQueryParameters query, string format = "json") - { - if (format.Equals("csv", System.StringComparison.OrdinalIgnoreCase)) - { - var evacuees = await dataInterface.GetEvacueeReportAsync(query); - - var fileName = $"Evacuees_Export_{ DateTime.Now:yyyyMMdd_HHmmss}.csv"; - return File(Encoding.UTF8.GetBytes(evacuees.ToCSV()), "text/csv;charset=utf-8", fileName); - } - else - { - var evacuees = await dataInterface.GetEvacueesPaginatedAsync(query); - return Json(evacuees); - } - } - - [HttpGet("getevacueereport")] - public async Task EvacueeReport([FromQuery] EvacueeSearchQueryParameters query) - { - var evacuees = await dataInterface.GetEvacueeReportAsync(query); - var today = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, GetPSTTimeZone()); - var fileName = $"Evacuees_Export_{ today:yyyyMMdd_HHmmss}.csv"; - return File(Encoding.UTF8.GetBytes(evacuees.ToCSV(query)), "text/csv;charset=utf-8", fileName); - } - - [HttpGet("getevacueereferralreport")] - public async Task EvacueeReferralReport([FromQuery] EvacueeSearchQueryParameters query) - { - var evacuees = await dataInterface.GetEvacueeReferralReportAsync(query); - var today = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, GetPSTTimeZone()); - var fileName = $"Referral_Export_{ today:yyyyMMdd_HHmmss}.csv"; - return File(Encoding.UTF8.GetBytes(evacuees.ToCSV(query)), "text/csv;charset=utf-8", fileName); - } - - private string GetPSTTimeZone() - { - switch (Environment.OSVersion.Platform) - { - case PlatformID.Win32NT: - return "Pacific Standard Time"; - case PlatformID.Unix: - return "Canada/Pacific"; - default: - throw new NotSupportedException(); - - - } - } - } -} +using Gov.Jag.Embc.Public.DataInterfaces; +using Gov.Jag.Embc.Public.Utils; +using Gov.Jag.Embc.Public.ViewModels.Search; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Text; +using System.Threading.Tasks; + +namespace Gov.Jag.Embc.Public.Controllers +{ + [Route("api/[controller]")] + [Authorize] + public class EvacueesController : Controller + { + private readonly IDataInterface dataInterface; + + public EvacueesController(IDataInterface dataInterface) + { + this.dataInterface = dataInterface; + } + + [HttpGet] + public async Task Get([FromQuery] EvacueeSearchQueryParameters query, string format = "json") + { + if (format.Equals("csv", System.StringComparison.OrdinalIgnoreCase)) + { + var evacuees = await dataInterface.GetEvacueeReportAsync(query); + + var fileName = $"Evacuees_Export_{ DateTime.Now:yyyyMMdd_HHmmss}.csv"; + return File(Encoding.UTF8.GetBytes(evacuees.ToCSV()), "text/csv;charset=utf-8", fileName); + } + else + { + var evacuees = await dataInterface.GetEvacueesPaginatedAsync(query); + return Json(evacuees); + } + } + + [HttpGet("getevacueereport")] + public async Task EvacueeReport([FromQuery] EvacueeSearchQueryParameters query) + { + var evacuees = await dataInterface.GetEvacueeReportAsync(query); + var today = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, GetPSTTimeZone()); + var fileName = $"Evacuees_Export_{ today:yyyyMMdd_HHmmss}.csv"; + return File(Encoding.UTF8.GetBytes(evacuees.ToCSV(query, true)), "text/csv;charset=utf-8", fileName); + } + + [HttpGet("getevacueereferralreport")] + public async Task EvacueeReferralReport([FromQuery] EvacueeSearchQueryParameters query) + { + var evacuees = await dataInterface.GetEvacueeReferralReportAsync(query); + var today = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, GetPSTTimeZone()); + var fileName = $"Referral_Export_{ today:yyyyMMdd_HHmmss}.csv"; + return File(Encoding.UTF8.GetBytes(evacuees.ToCSV(query, false)), "text/csv;charset=utf-8", fileName); + } + + private string GetPSTTimeZone() + { + switch (Environment.OSVersion.Platform) + { + case PlatformID.Win32NT: + return "Pacific Standard Time"; + case PlatformID.Unix: + return "Canada/Pacific"; + default: + throw new NotSupportedException(); + + + } + } + } +} diff --git a/embc-app/Utils/CsvConverter.cs b/embc-app/Utils/CsvConverter.cs index 0e558631d..52a0320ee 100644 --- a/embc-app/Utils/CsvConverter.cs +++ b/embc-app/Utils/CsvConverter.cs @@ -1,188 +1,208 @@ -using Gov.Jag.Embc.Public.ViewModels.Search; -using System.Collections.Generic; -using System.IO; -using System.Reflection; - -namespace Gov.Jag.Embc.Public.Utils -{ - //inspired by https://stackoverflow.com/questions/1890093/converting-a-generic-list-to-a-csv-string - public static class CsvConverter - { - private static void CreateHeader(IEnumerable list, TextWriter sw) - { - PropertyInfo[] properties = typeof(T).GetProperties(); - for (int i = 0; i < properties.Length - 1; i++) - { - sw.Write(properties[i].Name + ","); - } - var lastProp = properties[properties.Length - 1].Name; - sw.Write(lastProp + sw.NewLine); - } - - private static void CreateRows(IEnumerable list, TextWriter sw) - { - foreach (var item in list) - { - PropertyInfo[] properties = typeof(T).GetProperties(); - for (int i = 0; i < properties.Length - 1; i++) - { - var prop = properties[i]; - sw.Write(prop.GetValue(item) + ","); - } - var lastProp = properties[properties.Length - 1]; - sw.Write(lastProp.GetValue(item) + sw.NewLine); - } - } - - public static void CreateCSV(this IEnumerable list, string filePath) - { - using (var sw = new StreamWriter(filePath)) - { - CreateHeader(list, sw); - CreateRows(list, sw); - } - } - - public static string ToCSV(this IEnumerable list) - { - using (var sw = new StringWriter()) - { - CreateHeader(list, sw); - CreateRows(list, sw); - return sw.ToString(); - } - } - - public static string ToCSV(this IEnumerable list, EvacueeSearchQueryParameters searchParams) - { - using (var sw = new StringWriter()) - { - AddSearchParams(sw, searchParams); - CreateHeader(list, sw); - CreateRows(list, sw); - return sw.ToString(); - } - } - - private static void AddSearchParams(StringWriter sw, EvacueeSearchQueryParameters searchParams) - { - PropertyInfo[] properties = typeof(EvacueeSearchQueryParameters).GetProperties(); - - // Add Search header - sw.Write("Search Parameters"); - sw.Write(sw.NewLine); - - for (int i = 0; i < properties.Length - 1; i++) - { - object prop = properties[i].GetValue(searchParams); - string propName = GetFriendlySearchParamName(properties[i].Name); - ProcessProperty(prop, propName, sw); - if (IsValidProp(prop, propName)) - { - - sw.Write(propName + ":" + ","); - - sw.Write(properties[i].GetValue(searchParams).ToString()); - sw.Write(sw.NewLine); - } - } - sw.Write(sw.NewLine); - } - - private static bool IsValidProp(object prop, string propName) - { - // Ensure property is not null - bool result = prop != null && !string.IsNullOrEmpty(prop.ToString()); - // Ensure property is not one we ignore (limit, offset, sortby, etc.) - if (result) - { - switch (propName.ToLower()) - { - case "offset": - result = false; - break; - case "limit": - result = false; - break; - case "sortby": - result = false; - break; - default: - result = true; - break; - } - } - return result; - } - - - private static void ProcessProperty(object prop, string propName, StringWriter sw) - { - // Radio button properties that are null need to display 'show all' - bool isRadioBtn = false; - switch (propName) - { - case "Referrals Provided": - isRadioBtn = true; - break; - case "Reg Completed": - isRadioBtn = true; - break; - default: - break; - } - - if (isRadioBtn && (prop == null || string.IsNullOrEmpty(prop.ToString()))) - { - sw.Write($"{propName}:, Show all"); - sw.Write(sw.NewLine); - } - - } - - // Returns a user friendly name for the search parameters - private static string GetFriendlySearchParamName(string propName) - { - string result = string.Empty; - - switch (propName.ToLower()) - { - case "lastname": - result = "Last Name"; - break; - case "firstname": - result = "First Name"; - break; - case "incidenttasknumber": - result = "Task #"; - break; - case "essfilenumber": - result = "ESS File #"; - break; - case "evacuatedfrom": - result = "Evacuated To"; - break; - case "evacuatedto": - result = "Evacuated From"; - break; - case "hasreferrals": - result = "Referrals Provided"; - break; - case "registrationcomplete": - result = "Reg Completed"; - break; - case "dateofbirth": - result = "Date of Birth"; - break; - default: - result = propName; - break; - } - - return result; - } - - } -} - - +using Gov.Jag.Embc.Public.ViewModels.Search; +using System.Collections.Generic; +using System.IO; +using System.Reflection; + +namespace Gov.Jag.Embc.Public.Utils +{ + //inspired by https://stackoverflow.com/questions/1890093/converting-a-generic-list-to-a-csv-string + public static class CsvConverter + { + private static void CreateHeader(IEnumerable list, TextWriter sw) + { + PropertyInfo[] properties = typeof(T).GetProperties(); + for (int i = 0; i < properties.Length - 1; i++) + { + sw.Write(properties[i].Name + ","); + } + var lastProp = properties[properties.Length - 1].Name; + sw.Write(lastProp + sw.NewLine); + } + + private static void CreateRows(IEnumerable list, TextWriter sw) + { + foreach (var item in list) + { + PropertyInfo[] properties = typeof(T).GetProperties(); + for (int i = 0; i < properties.Length - 1; i++) + { + var prop = properties[i]; + sw.Write(prop.GetValue(item) + ","); + } + var lastProp = properties[properties.Length - 1]; + sw.Write(lastProp.GetValue(item) + sw.NewLine); + } + } + + public static void CreateCSV(this IEnumerable list, string filePath) + { + using (var sw = new StreamWriter(filePath)) + { + CreateHeader(list, sw); + CreateRows(list, sw); + } + } + + public static string ToCSV(this IEnumerable list) + { + using (var sw = new StringWriter()) + { + CreateHeader(list, sw); + CreateRows(list, sw); + return sw.ToString(); + } + } + + /// + /// Creates a CSV with a list of search parameters + /// + /// + /// + /// The search parameters to include in the CSV + /// Flag to control which report it is + /// + public static string ToCSV(this IEnumerable list, EvacueeSearchQueryParameters searchParams, bool isEvacueeExport) + { + using (var sw = new StringWriter()) + { + AddSearchParams(sw, searchParams, isEvacueeExport); + CreateHeader(list, sw); + CreateRows(list, sw); + return sw.ToString(); + } + } + + private static void AddSearchParams(StringWriter sw, EvacueeSearchQueryParameters searchParams, bool isEvacueeExport) + { + PropertyInfo[] properties = typeof(EvacueeSearchQueryParameters).GetProperties(); + + // Add Search header + sw.Write("Search Parameters"); + sw.Write(sw.NewLine); + + for (int i = 0; i < properties.Length - 1; i++) + { + object prop = properties[i].GetValue(searchParams); + string propName = GetFriendlySearchParamName(properties[i].Name); + ProcessProperty(prop, propName, sw, isEvacueeExport); + if (IsValidProp(prop, propName)) + { + sw.Write(propName + ":" + ","); + sw.Write(properties[i].GetValue(searchParams).ToString()); + sw.Write(sw.NewLine); + } + } + sw.Write(sw.NewLine); + } + + private static bool IsValidProp(object prop, string propName) + { + // Ensure property is not null + bool result = prop != null && !string.IsNullOrEmpty(prop.ToString()); + // Ensure property is not one we ignore (limit, offset, sortby, etc.) + if (result) + { + switch (propName.ToLower()) + { + case "offset": + result = false; + break; + case "limit": + result = false; + break; + case "sortby": + result = false; + break; + case "referrals provided": + result = false; + break; + case "reg completed": + result = false; + break; + default: + result = true; + break; + } + } + return result; + } + + + private static void ProcessProperty(object prop, string propName, StringWriter sw, bool isEvacueeExport) + { + // These radio button properties have hard coded values per report + bool isRadioBtn = false; + switch (propName) + { + case "Referrals Provided": + isRadioBtn = true; + break; + case "Reg Completed": + isRadioBtn = true; + break; + default: + break; + } + + if (isRadioBtn) + { + if (isEvacueeExport) + { + sw.Write($"{propName}:, Show all"); + } + else + { + sw.Write($"{propName}:, Yes"); + } + + sw.Write(sw.NewLine); + } + + } + + // Returns a user friendly name for the search parameters + private static string GetFriendlySearchParamName(string propName) + { + string result = string.Empty; + + switch (propName.ToLower()) + { + case "lastname": + result = "Last Name"; + break; + case "firstname": + result = "First Name"; + break; + case "incidenttasknumber": + result = "Task #"; + break; + case "essfilenumber": + result = "ESS File #"; + break; + case "evacuatedfrom": + result = "Evacuated To"; + break; + case "evacuatedto": + result = "Evacuated From"; + break; + case "hasreferrals": + result = "Referrals Provided"; + break; + case "registrationcompleted": + result = "Reg Completed"; + break; + case "dateofbirth": + result = "Date of Birth"; + break; + default: + result = propName; + break; + } + + return result; + } + + } +} + +