Skip to content

Commit

Permalink
feat: Configuração de dependência de campos
Browse files Browse the repository at this point in the history
Criação da feature de dependência de campos,
quando um campo assume um determinado valor
o outro campo deve passar por uma segunda validação de preenchimento
Escrita dos testes unitários
  • Loading branch information
LucasTonetto committed Mar 8, 2021
1 parent 10dc165 commit ab6b9e8
Show file tree
Hide file tree
Showing 24 changed files with 918 additions and 90 deletions.
8 changes: 4 additions & 4 deletions src/js/models/Adobe.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class Adobe extends AnalyticsTool_1.AnalyticsTool {
return;
}
if (
this.config.validationRules[column].length > 0 &&
!StringUtils_1.StringUtils.validateString(
this.csvLine[columnNormalized],
this.config.validationRules[column]
!this.config.validateField(
this.csvLine,
column,
this.csvLine[columnNormalized]
)
) {
this._hasValidationError = true;
Expand Down
98 changes: 78 additions & 20 deletions src/js/models/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.Config = void 0;
const JsonUtils_1 = require('../utils/JsonUtils');
const DependencyConfig_1 = require('./DependencyConfig');
const StringUtils_1 = require('../utils/StringUtils');
class Config {
constructor(jsonConfig) {
this._csvSeparator = ',';
Expand All @@ -12,6 +14,10 @@ class Config {
this._csvSeparator = jsonConfigTemp.csvSeparator;
delete jsonConfigTemp.csvSeparator;
}
this._dependenciesConfig = this._buildDependenciesConfig(
jsonConfigTemp.dependenciesConfig
);
delete jsonConfigTemp.dependenciesConfig;
this._spaceSeparator = jsonConfigTemp.spaceSeparator;
delete jsonConfigTemp.spaceSeparator;
this._insertTime = jsonConfigTemp.insertTime;
Expand Down Expand Up @@ -40,26 +46,17 @@ class Config {
!this._analyticsTool
);
}
_buildDependenciesConfig(dependenciesConfig) {
if (!dependenciesConfig) {
return [];
}
return dependenciesConfig.map(
(dependencyConfig) =>
new DependencyConfig_1.DependencyConfig(dependencyConfig)
);
}
toString() {
let jsonConfig = {};
Object.keys(this).forEach((key, index) => {
if (key === '_analyticsTool' || key === '_medias') {
jsonConfig = JsonUtils_1.JsonUtils.addParametersAt(
jsonConfig,
Object.values(this)[index] || {}
);
} else if (key === '_validationRules') {
jsonConfig = JsonUtils_1.JsonUtils.addParametersAt(jsonConfig, {
columns: this._validationRules,
});
} else if (
key !== '_analyticsToolName' &&
Object.values(this)[index]
) {
jsonConfig[key.replace('_', '')] = Object.values(this)[index];
}
});
return JSON.stringify(jsonConfig);
return JSON.stringify(this.toJson());
}
toJson() {
let jsonConfig = {};
Expand All @@ -73,6 +70,14 @@ class Config {
jsonConfig = JsonUtils_1.JsonUtils.addParametersAt(jsonConfig, {
columns: this._validationRules,
});
} else if (key === '_dependenciesConfig') {
if (this._dependenciesConfig.length > 0) {
jsonConfig[
'dependenciesConfig'
] = this._dependenciesConfig.map((dependencyConfig) => {
return dependencyConfig.toJson();
});
}
} else if (
key !== '_analyticsToolName' &&
Object.values(this)[index]
Expand All @@ -90,9 +95,62 @@ class Config {
});
return configValues.join(this._csvSeparator);
}
existsValidationRuleFor(csvColumn) {
_existsValidationRuleFor(csvColumn) {
return this.validationRules[csvColumn].length > 0;
}
_validateRulesFor(csvColumn, value) {
if (!this._existsValidationRuleFor(csvColumn)) {
return true;
}
return StringUtils_1.StringUtils.validateString(
value,
this._validationRules[csvColumn]
);
}
_getDependencyConfigFor(csvColumn) {
let dependencyColumnConfig;
this._dependenciesConfig.forEach((dependencyConfig) => {
if (dependencyConfig.columnDestiny === csvColumn) {
dependencyColumnConfig = dependencyConfig;
}
});
return dependencyColumnConfig;
}
_validateDependencyRulesFor(csvLine, csvColumn, value) {
const dependencyConfigForCsvColumn = this._getDependencyConfigFor(
csvColumn
);
if (
!dependencyConfigForCsvColumn ||
!StringUtils_1.StringUtils.validateString(
csvLine[
StringUtils_1.StringUtils.normalize(
dependencyConfigForCsvColumn.columnReference
)
],
dependencyConfigForCsvColumn.valuesReference
)
) {
return true;
}
if (dependencyConfigForCsvColumn.hasMatch) {
return StringUtils_1.StringUtils.validateString(
value,
dependencyConfigForCsvColumn.matches
);
} else {
return !StringUtils_1.StringUtils.validateString(
value,
dependencyConfigForCsvColumn.matches
);
}
}
validateField(csvLine, csvColumn, value) {
return (
this._validateRulesFor(csvColumn, value) &&
this._validateDependencyRulesFor(csvLine, csvColumn, value)
);
}
existsColumn(csvColumn) {
return !!this.validationRules[csvColumn];
}
Expand Down
44 changes: 44 additions & 0 deletions src/js/models/DependencyConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.DependencyConfig = void 0;
class DependencyConfig {
constructor(dependencyConfig) {
const {
columnReference,
hasMatch,
matches,
valuesReference,
columnDestiny,
} = dependencyConfig;
this._columnReference = columnReference;
this._hasMatch = hasMatch;
this._matches = matches;
this._valuesReference = valuesReference;
this._columnDestiny = columnDestiny;
}
toJson() {
return {
columnReference: this._columnReference,
valuesReference: this._valuesReference,
hasMatch: this._hasMatch,
columnDestiny: this._columnDestiny,
matches: this._matches,
};
}
get columnDestiny() {
return this._columnDestiny;
}
get matches() {
return this._matches;
}
get hasMatch() {
return this._hasMatch;
}
get columnReference() {
return this._columnReference;
}
get valuesReference() {
return this._valuesReference;
}
}
exports.DependencyConfig = DependencyConfig;
9 changes: 4 additions & 5 deletions src/js/models/FacebookAds.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ class FacebookAds extends Vehicle_1.Vehicle {
column
);
if (
this.config.validationRules[column].length >
0 &&
!StringUtils_1.StringUtils.validateString(
this.csvLine[normalizedColumn],
this.config.validationRules[column]
!this.config.validateField(
this.csvLine,
column,
this.csvLine[normalizedColumn]
)
) {
this._hasValidationError = true;
Expand Down
8 changes: 4 additions & 4 deletions src/js/models/GA.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ class GA extends AnalyticsTool_1.AnalyticsTool {
return;
}
if (
this.config.validationRules[column].length > 0 &&
!StringUtils_1.StringUtils.validateString(
this.csvLine[columnNormalized],
this.config.validationRules[column]
!this.config.validateField(
this.csvLine,
column,
this.csvLine[columnNormalized]
)
) {
this._hasValidationError[utm] = true;
Expand Down
8 changes: 4 additions & 4 deletions src/js/models/GeneralVehicle.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class GeneralVehicle extends Vehicle_1.Vehicle {
csvColumn
);
if (
this.config.existsValidationRuleFor(csvColumn) &&
!StringUtils_1.StringUtils.validateString(
this.csvLine[normalizedColumn],
this.config.validationRules[csvColumn]
!this.config.validateField(
this.csvLine,
csvColumn,
this.csvLine[normalizedColumn]
)
) {
this._validationErrorFounded(param, csvColumn);
Expand Down
8 changes: 4 additions & 4 deletions src/js/models/GoogleAds.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class GoogleAds extends Vehicle_1.Vehicle {
column
);
if (
this.config.validationRules[column].length > 0 &&
!StringUtils_1.StringUtils.validateString(
this.csvLine[normalizedColumn],
this.config.validationRules[column]
!this.config.validateField(
this.csvLine,
column,
this.csvLine[normalizedColumn]
)
) {
this._hasValidationError = true;
Expand Down
5 changes: 4 additions & 1 deletion src/js/models/cloud/FirestoreConnectionSingleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class FirestoreConnectionSingleton extends ObjectStore_1.ObjectStore {
super();
if (process.env.DEVELOPMENT) {
const credentials = require('../../../../gcp_key.json');
this._db = new firestore_1.Firestore({ credentials });
this._db = new firestore_1.Firestore({
projectId: 'adinfo',
credentials,
});
} else {
this._db = new firestore_1.Firestore();
}
Expand Down
5 changes: 4 additions & 1 deletion src/js/models/cloud/StorageConnectionSingleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class StorageConnectionSingleton extends FileStore_1.FileStore {
this._bucket = `adinfo-dp6-files`;
if (process.env.DEVELOPMENT) {
const credentials = require('../../../../gcp_key.json');
this._db = new storage_1.Storage({ credentials });
this._db = new storage_1.Storage({
projectId: 'adinfo',
credentials,
});
} else {
this._db = new storage_1.Storage();
}
Expand Down
9 changes: 4 additions & 5 deletions src/ts/models/Adobe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export class Adobe extends AnalyticsTool {
};
}

//TODO padronizar columns
/**
* Constroi o Cid da linha e preenche o atributo hasErrorAtCid
*/
Expand All @@ -92,10 +91,10 @@ export class Adobe extends AnalyticsTool {
return;
}
if (
this.config.validationRules[column].length > 0 &&
!StringUtils.validateString(
this.csvLine[columnNormalized],
this.config.validationRules[column]
!this.config.validateField(
this.csvLine,
column,
this.csvLine[columnNormalized]
)
) {
this._hasValidationError = true;
Expand Down
Loading

0 comments on commit ab6b9e8

Please sign in to comment.