zetkin/app.zetkin.org

Import UI crashes when configuring date-field in non-Scandinavian country

Closed this issue · 0 comments

Description

The recent addition of date format parsing in the importer UI assumes that all countries have equivalents of the Swedish/Norwegian/Danish concept of "personnummer" that consist of dates. When used in an organization with a different country code than SE/NO/DK, it crashes.

Steps to reproduce

  1. On the dev server, modify org 1 (using a PATCH request to https://app.dev.zetkin.org/api/orgs/1) to have country code UK
  2. Go to https://app.dev.zetkin.org/organize/1/people
  3. Click "Create"
  4. Click "import people"
  5. Drop any CSV
  6. For any column, pick "Extra date" as the field to import into

Expected Behaviour

You should be allowed to configure the parsing.

Actual Behaviour

Crash

Screenshots (if you have any)

image

Proposed solution

The root of the issue seems to be this logic:

type PersonNumberFormat = 'se' | 'dk' | 'no';
const personNumberFormats: PersonNumberFormat[] = [];
if (organization) {
personNumberFormats.push(
organization.country.toLowerCase() as PersonNumberFormat
);
} else {
personNumberFormats.push('se', 'dk', 'no');
}

Note how the type cast on line 59 claims that any string will be 'se' | 'dk' | 'no'. Typecasts are dangerous. Removing the typecast will reveal the error, and force an if statement to make sure that the string is actually what this logic assumes it is (which it isn't when the org country is Germany or UK, for example).