2020-06-12 08:22:36 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
|
|
|
import validator from 'validator';
|
2020-07-06 15:28:30 +03:00
|
|
|
import {isEmpty} from '@ember/utils';
|
2020-06-12 08:22:36 +03:00
|
|
|
|
|
|
|
export default Service.extend({
|
|
|
|
ajax: service(),
|
|
|
|
membersUtils: service(),
|
|
|
|
ghostPaths: service(),
|
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
check(data) {
|
2020-07-06 15:28:30 +03:00
|
|
|
let sampledData = this._sampleData(data);
|
|
|
|
let mapping = this._detectDataTypes(sampledData);
|
2020-12-09 22:32:31 +03:00
|
|
|
return mapping;
|
2020-07-06 15:28:30 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method implements foollowing sampling logic:
|
|
|
|
* Locate 10 non-empty cells from the start/middle(ish)/end of each column (30 non-empty values in total).
|
|
|
|
* If the data contains 30 rows or fewer, all rows should be validated.
|
|
|
|
*
|
|
|
|
* @param {Array} data JSON objects mapped from CSV file
|
|
|
|
*/
|
|
|
|
_sampleData(data, validationSampleSize = 30) {
|
|
|
|
let validatedSet = [{}];
|
|
|
|
|
|
|
|
if (data && data.length > validationSampleSize) {
|
|
|
|
let sampleKeys = Object.keys(data[0]);
|
|
|
|
|
|
|
|
sampleKeys.forEach(function (key) {
|
|
|
|
const nonEmptyKeyEntries = data.filter(entry => !isEmpty(entry[key]));
|
|
|
|
let sampledEntries = [];
|
|
|
|
|
|
|
|
if (nonEmptyKeyEntries.length <= validationSampleSize) {
|
|
|
|
sampledEntries = nonEmptyKeyEntries;
|
|
|
|
} else {
|
|
|
|
// take 3 equal parts from head, tail and middle of the data set
|
|
|
|
const partitionSize = validationSampleSize / 3;
|
|
|
|
|
|
|
|
const head = data.slice(0, partitionSize);
|
|
|
|
const tail = data.slice((data.length - partitionSize), data.length);
|
|
|
|
|
|
|
|
const middleIndex = Math.floor(data.length / 2);
|
|
|
|
const middleStartIndex = middleIndex - 2;
|
|
|
|
const middleEndIndex = middleIndex + 3;
|
|
|
|
const middle = data.slice(middleStartIndex, middleEndIndex);
|
|
|
|
|
|
|
|
validatedSet.push(...head);
|
|
|
|
validatedSet.push(...middle);
|
|
|
|
validatedSet.push(...tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
sampledEntries.forEach((entry, index) => {
|
|
|
|
if (!validatedSet[index]) {
|
|
|
|
validatedSet[index] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
validatedSet[index][key] = entry[key];
|
|
|
|
});
|
|
|
|
});
|
2020-06-12 08:22:36 +03:00
|
|
|
} else {
|
2020-07-06 15:28:30 +03:00
|
|
|
validatedSet = data;
|
2020-06-12 08:22:36 +03:00
|
|
|
}
|
2020-07-06 15:28:30 +03:00
|
|
|
|
|
|
|
return validatedSet;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2020-07-09 11:52:12 +03:00
|
|
|
* Detects supported data types and auto-detects following two needed for validation:
|
2020-07-06 15:28:30 +03:00
|
|
|
* 1. email
|
|
|
|
* 2. stripe_customer_id
|
|
|
|
*
|
|
|
|
* Returned "mapping" object contains mappings that could be accepted by the API
|
|
|
|
* to map validated types.
|
|
|
|
* @param {Array} data sampled data containing non empty values
|
|
|
|
*/
|
|
|
|
_detectDataTypes(data) {
|
2020-07-09 11:52:12 +03:00
|
|
|
const supportedTypes = [
|
|
|
|
'email',
|
|
|
|
'name',
|
|
|
|
'note',
|
|
|
|
'subscribed_to_emails',
|
|
|
|
'labels',
|
|
|
|
'created_at'
|
|
|
|
];
|
|
|
|
|
|
|
|
const autoDetectedTypes = [
|
2020-12-09 22:32:31 +03:00
|
|
|
'email'
|
2020-07-09 11:52:12 +03:00
|
|
|
];
|
|
|
|
|
2020-07-06 15:28:30 +03:00
|
|
|
let mapping = {};
|
|
|
|
let i = 0;
|
|
|
|
// loopping through all sampled data until needed data types are detected
|
|
|
|
while (i <= (data.length - 1)) {
|
|
|
|
if (mapping.email && mapping.stripe_customer_id) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let entry = data[i];
|
|
|
|
for (const [key, value] of Object.entries(entry)) {
|
|
|
|
if (!mapping.email && validator.isEmail(value)) {
|
|
|
|
mapping.email = key;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-20 06:04:35 +03:00
|
|
|
if (!mapping.name && /name/.test(key)) {
|
|
|
|
mapping.name = key;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-09 11:52:12 +03:00
|
|
|
if (!mapping[key] && supportedTypes.includes(key) && !(autoDetectedTypes.includes(key))) {
|
|
|
|
mapping[key] = key;
|
|
|
|
}
|
2020-07-06 15:28:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapping;
|
2020-06-12 08:22:36 +03:00
|
|
|
}
|
|
|
|
});
|