const { Report } = require('./models');

/**
 * Convert a legacy report to the new report schema.
 */
exports.toNewReport = function toNewReport(legacy) {
  return new Report({
    diff: {
      geometry: legacy.geometry,
      name: legacy.properties.name,
      active: legacy.properties.active,
      access: legacy.properties.access,
      opening: legacy.properties.opening,
      type: legacy.properties.type,
      accessibleType: legacy.properties.accessibleType,
      disposal: legacy.properties.disposal,
      babyChange: legacy.properties.babyChange,
      babyChangeLocation: legacy.properties.babyChangeLocation,
      changingPlace: legacy.properties.changingPlace,
      familyToilet: legacy.properties.familyToilet,
      radar: legacy.properties.radar,
      attended: legacy.properties.attended,
      automatic: legacy.properties.automatic,
      parking: legacy.properties.parking,
      fee: legacy.properties.fee,
      streetAddress: legacy.properties.streetAddress,
      postcode: legacy.properties.postcode,
      operator: legacy.properties.operator,
      reportEmail: legacy.properties.reportEmail,
      reportPhone: legacy.properties.reportPhone,
      notes: legacy.properties.notes,
      infoUrl: legacy.properties.infoUrl,
      architecturalInterest: legacy.properties.architecturalInterest,
      historicalInterest: legacy.properties.historicalInterest,
      geocoded: legacy.properties.geocoded,
      geocoding_method: legacy.properties.geocoding_method,
      orig: legacy.properties.orig,
      removal_reason: legacy.properties.removal_reason,
      area: legacy.properties.area,
    },
  });
};

/**
 * Convert a list of legacy reports to a list of new reports. This is its own
 * function as, in the future, a new report will not always be solely created
 * from the data of one legacy report.
 */
exports.toNewReports = function toNewReports(legacies) {
  return legacies.map(exports.toNewReport);
};

@@ -34,16 +34,345 @@
 els');%0A%0A
+/**%0A * Gets any useful textual information from a string or undefined if there is%0A * none.%0A */%0Afunction ignoreEmpty(string) %7B%0A  if (string === undefined) %7B%0A    return undefined;%0A  %7D%0A%0A  if (string.trim().length === 0) %7B%0A    // only whitespace, we shouldn't populate the field%0A    return undefined;%0A  %7D%0A%0A  return string.trim();%0A%7D%0A%0A
 /**%0A * C
@@ -556,16 +556,28 @@
   name: 
+ignoreEmpty(
 legacy.p
@@ -590,16 +590,17 @@
 ies.name
+)
 ,%0A      
@@ -643,24 +643,36 @@
     access: 
+ignoreEmpty(
 legacy.prope
@@ -683,16 +683,17 @@
 s.access
+)
 ,%0A      
@@ -697,24 +697,36 @@
    opening: 
+ignoreEmpty(
 legacy.prope
@@ -738,16 +738,17 @@
 .opening
+)
 ,%0A      
@@ -749,24 +749,36 @@
       type: 
+ignoreEmpty(
 legacy.prope
@@ -787,16 +787,17 @@
 ies.type
+)
 ,%0A      
@@ -812,16 +812,28 @@
 leType: 
+ignoreEmpty(
 legacy.p
@@ -856,16 +856,17 @@
 ibleType
+)
 ,%0A      
@@ -1302,16 +1302,28 @@
    fee: 
+ignoreEmpty(
 legacy.p
@@ -1335,16 +1335,17 @@
 ties.fee
+)
 ,%0A      
@@ -1593,16 +1593,28 @@
  notes: 
+ignoreEmpty(
 legacy.p
@@ -1628,16 +1628,17 @@
 es.notes
+)
 ,%0A      
@@ -1965,24 +1965,36 @@
 val_reason: 
+ignoreEmpty(
 legacy.prope
@@ -2013,16 +2013,17 @@
 l_reason
+)
 ,%0A      

trim and strip out all empty string values