import Ember from 'ember';
import isTrackingHeaderField from 'wherehows-web/utils/validators/tracking-headers';
const { Component, computed, set, get, isBlank, setProperties, getWithDefault, String: { htmlSafe } } = Ember;
// TODO: DSS-6671 Extract to constants module
const missingTypes = 'Looks like some fields may contain privacy data but do not have a specified `Field Format`?';
const successUpdating = 'Your changes have been successfully saved!';
const failedUpdating = 'Oops! We are having trouble updating this dataset at the moment.';
const hiddenTrackingFieldsMsg = htmlSafe(
'
Hey! Just a heads up that some fields in this dataset have been hidden from the table(s) below. ' +
"These are tracking fields for which we've been able to predetermine the compliance classification.
" +
'
For example: header.memberId, requestHeader. ' +
'Hopefully, this saves you some scrolling!
'
);
const complianceListKey = 'privacyCompliancePolicy.compliancePurgeEntities';
// TODO: DSS-6671 Extract to constants module
const logicalTypes = ['ID', 'URN', 'REVERSED_URN', 'COMPOSITE_URN'];
/**
* Duplicate check using every to short-circuit iteration
* @param {Array} names = [] the list to check for dupes
* @return {Boolean} true is unique, false otherwise
*/
const fieldNamesAreUnique = (names = []) => names.every((name, index) => names.indexOf(name) === index);
/**
* Returns a computed macro based on a provided type will return a list of
* Compliance fields that are of that identifierType or have no type
* @param {String} type string to match against identifierType
*/
const complianceEntitiesMatchingType = type =>
computed('complianceDataFieldsSansHiddenTracking.[]', function() {
const fieldRegex = new RegExp(`${type}`, 'i');
return get(this, 'complianceDataFieldsSansHiddenTracking').filter(({ identifierType }) => {
return fieldRegex.test(identifierType) || isBlank(identifierType);
});
});
export default Component.extend({
sortColumnWithName: 'identifierField',
filterBy: 'identifierField',
sortDirection: 'asc',
searchTerm: '',
hiddenTrackingFields: hiddenTrackingFieldsMsg,
/**
* Map of radio Group state values
* Each initially has an indeterminate state, as the user
* progresses through the prompts
* @type {Object.}
*/
userIndicatesDatasetHas: {
member: null,
org: null,
group: null
},
didReceiveAttrs() {
this._super(...arguments);
// Perform validation step on the received component attributes
this.validateAttrs();
},
/**
* Ensure that props received from on this component
* are valid, otherwise flag
*/
validateAttrs() {
const fieldNames = getWithDefault(this, 'schemaFieldNamesMappedToDataTypes', []).mapBy('fieldName');
if (fieldNamesAreUnique(fieldNames.sort())) {
return set(this, '_hasBadData', false);
}
// Flag this component's data as problematic
set(this, '_hasBadData', true);
},
// Map logicalTypes to options consumable by ui
logicalTypes: ['', ...logicalTypes].map(value => ({
value,
label: value ? value.replace('_', ' ').toLowerCase().capitalize() : 'Please Select'
})),
/**
* @type {Boolean} cached boolean flag indicating that fields do contain a `kafka type`
* tracking header.
* Used to indicate to viewer that these fields are hidden.
*/
containsHiddenTrackingFields: computed('complianceDataFieldsSansHiddenTracking.length', function() {
// If their is a diff in complianceDataFields and complianceDataFieldsSansHiddenTracking,
// then we have hidden tracking fields
return get(this, 'complianceDataFieldsSansHiddenTracking.length') !== get(this, 'complianceDataFields.length');
}),
/**
* @type {Array.