Updated members uploader mapper logic to map all known types

This commit is contained in:
Nazar Gargol 2020-07-09 20:52:12 +12:00
parent 21a0bc7aa9
commit 4f56e77db5
2 changed files with 24 additions and 15 deletions

View File

@ -15,24 +15,13 @@ import {inject as service} from '@ember/service';
import {tracked} from '@glimmer/tracking';
class MembersFieldMapping {
_supportedImportFields = [
'email',
'name',
'note',
'subscribed_to_emails',
'stripe_customer_id',
'complimentary_plan',
'labels',
'created_at'
];
@tracked _mapping = {};
constructor(mapping) {
// NOTE: there are only 2 distinguishable fields that could be automatically matched, which is the reason why code is just simple assignments
if (mapping) {
this.set(mapping.email, 'email');
this.set(mapping.stripe_customer_id, 'stripe_customer_id');
for (const [key, value] of Object.entries(mapping)) {
this.set(value, key);
}
}
}

View File

@ -126,7 +126,7 @@ export default Service.extend({
},
/**
* Detects validated data types:
* Detects supported data types and auto-detects following two needed for validation:
* 1. email
* 2. stripe_customer_id
*
@ -135,6 +135,22 @@ export default Service.extend({
* @param {Array} data sampled data containing non empty values
*/
_detectDataTypes(data) {
const supportedTypes = [
'email',
'name',
'note',
'subscribed_to_emails',
'stripe_customer_id',
'complimentary_plan',
'labels',
'created_at'
];
const autoDetectedTypes = [
'email',
'stripe_customer_id'
];
let mapping = {};
let i = 0;
// loopping through all sampled data until needed data types are detected
@ -154,6 +170,10 @@ export default Service.extend({
mapping.stripe_customer_id = key;
continue;
}
if (!mapping[key] && supportedTypes.includes(key) && !(autoDetectedTypes.includes(key))) {
mapping[key] = key;
}
}
i += 1;