@amanat-qa/utils-backend 5.1.0 → 5.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/API/baseAPI.js +216 -0
- package/dist/DB/baseDB.js +70 -0
- package/dist/DB/baseModel.js +32 -0
- package/{src → dist}/data/JSONLoader.js +4 -3
- package/dist/data/JSONMapper.js +261 -0
- package/dist/data/dataUtils.js +168 -0
- package/dist/data/filesParser.js +123 -0
- package/dist/data/gitlabCIGenerator.js +42 -0
- package/dist/enums/dateFormats.js +9 -0
- package/{src → dist}/index.js +3 -1
- package/dist/log/logger.js +94 -0
- package/dist/random/randomizer.js +105 -0
- package/dist/time/timeUtils.js +84 -0
- package/package.json +20 -16
- package/src/API/baseAPI.js +0 -98
- package/src/DB/baseDB.js +0 -30
- package/src/DB/baseModel.js +0 -25
- package/src/data/JSONMapper.js +0 -214
- package/src/data/dataUtils.js +0 -164
- package/src/data/filesParser.js +0 -104
- package/src/data/gitlabCIGenerator.js +0 -50
- package/src/enums/dateFormats.js +0 -8
- package/src/log/logger.js +0 -57
- package/src/random/randomizer.js +0 -106
- package/src/time/timeUtils.js +0 -71
package/package.json
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amanat-qa/utils-backend",
|
|
3
|
-
"version": "5.1.
|
|
3
|
+
"version": "5.1.2",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
7
|
"type": "commonjs",
|
|
8
8
|
"files": [
|
|
9
|
-
"
|
|
9
|
+
"dist"
|
|
10
10
|
],
|
|
11
11
|
"keywords": [],
|
|
12
12
|
"license": "MIT",
|
|
13
|
-
"main": "./
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
14
|
"exports": {
|
|
15
|
-
".": "./
|
|
16
|
-
"./baseAPI": "./
|
|
17
|
-
"./baseDB": "./
|
|
18
|
-
"./baseModel": "./
|
|
19
|
-
"./JSONLoader": "./
|
|
20
|
-
"./JSONMapper": "./
|
|
21
|
-
"./dataUtils": "./
|
|
22
|
-
"./filesParser": "./
|
|
23
|
-
"./gitlabCIGenerator": "./
|
|
24
|
-
"./dateFormats": "./
|
|
25
|
-
"./logger": "./
|
|
26
|
-
"./randomizer": "./
|
|
27
|
-
"./timeUtils": "./
|
|
15
|
+
".": "./dist/index.js",
|
|
16
|
+
"./baseAPI": "./dist/API/baseAPI.js",
|
|
17
|
+
"./baseDB": "./dist/DB/baseDB.js",
|
|
18
|
+
"./baseModel": "./dist/DB/baseModel.js",
|
|
19
|
+
"./JSONLoader": "./dist/data/JSONLoader.js",
|
|
20
|
+
"./JSONMapper": "./dist/data/JSONMapper.js",
|
|
21
|
+
"./dataUtils": "./dist/data/dataUtils.js",
|
|
22
|
+
"./filesParser": "./dist/data/filesParser.js",
|
|
23
|
+
"./gitlabCIGenerator": "./dist/data/gitlabCIGenerator.js",
|
|
24
|
+
"./dateFormats": "./dist/enums/dateFormats.js",
|
|
25
|
+
"./logger": "./dist/log/logger.js",
|
|
26
|
+
"./randomizer": "./dist/random/randomizer.js",
|
|
27
|
+
"./timeUtils": "./dist/time/timeUtils.js"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "babel src --out-dir dist --extensions .js",
|
|
31
|
+
"prepare": "npm run build"
|
|
28
32
|
},
|
|
29
33
|
"dependencies": {
|
|
30
34
|
"axios": "*",
|
package/src/API/baseAPI.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
const axios = require('axios');
|
|
2
|
-
const Logger = require('../log/logger');
|
|
3
|
-
|
|
4
|
-
class BaseAPI {
|
|
5
|
-
#baseURL;
|
|
6
|
-
|
|
7
|
-
#logString;
|
|
8
|
-
|
|
9
|
-
#timeout;
|
|
10
|
-
|
|
11
|
-
#headers;
|
|
12
|
-
|
|
13
|
-
#axiosInstance;
|
|
14
|
-
|
|
15
|
-
constructor(options) {
|
|
16
|
-
this.#baseURL = options.baseURL;
|
|
17
|
-
this.#logString = options.logString;
|
|
18
|
-
this.#timeout = options.timeout;
|
|
19
|
-
this.#headers = options.headers;
|
|
20
|
-
this.#axiosInstance = this.createInstance();
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
createInstance() {
|
|
24
|
-
if (this.#logString) Logger.log(`${this.#logString} ${this.#baseURL}`);
|
|
25
|
-
return axios.create({
|
|
26
|
-
baseURL: this.#baseURL,
|
|
27
|
-
timeout: this.#timeout,
|
|
28
|
-
headers: this.#headers,
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
async get(endpoint, params) {
|
|
33
|
-
Logger.log(`[req] ▶ get ${JSON.stringify(params || {})} from ${endpoint}:`);
|
|
34
|
-
try {
|
|
35
|
-
const response = await this.#axiosInstance.get(`/${endpoint}`, { params });
|
|
36
|
-
Logger.log(`[res] status code: ${response.status}`);
|
|
37
|
-
return response;
|
|
38
|
-
} catch (error) {
|
|
39
|
-
Logger.log(`[res] status code: ${error.response.status}`);
|
|
40
|
-
Logger.log(`[res] body: ${JSON.stringify(error.response.data)}`);
|
|
41
|
-
return error.response;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async post(endpoint, params) {
|
|
46
|
-
Logger.log(`[req] ▶ post ${JSON.stringify(params || {})} to ${endpoint}:`);
|
|
47
|
-
try {
|
|
48
|
-
const response = await this.#axiosInstance.post(`/${endpoint}`, params);
|
|
49
|
-
Logger.log(`[res] status code: ${response.status}`);
|
|
50
|
-
return response;
|
|
51
|
-
} catch (error) {
|
|
52
|
-
Logger.log(`[res] status code: ${error.response.status}`);
|
|
53
|
-
Logger.log(`[res] body: ${JSON.stringify(error.response.data)}`);
|
|
54
|
-
return error.response;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async patch(endpoint, params) {
|
|
59
|
-
Logger.log(`[req] ▶ patch ${JSON.stringify(params || {})} to ${endpoint}:`);
|
|
60
|
-
try {
|
|
61
|
-
const response = await this.#axiosInstance.patch(`/${endpoint}`, params);
|
|
62
|
-
Logger.log(`[res] status code: ${response.status}`);
|
|
63
|
-
return response;
|
|
64
|
-
} catch (error) {
|
|
65
|
-
Logger.log(`[res] status code: ${error.response.status}`);
|
|
66
|
-
Logger.log(`[res] body: ${JSON.stringify(error.response.data)}`);
|
|
67
|
-
return error.response;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async put(endpoint, params) {
|
|
72
|
-
Logger.log(`[req] ▶ put ${JSON.stringify(params || {})} to ${endpoint}:`);
|
|
73
|
-
try {
|
|
74
|
-
const response = await this.#axiosInstance.put(`/${endpoint}`, params);
|
|
75
|
-
Logger.log(`[res] status code: ${response.status}`);
|
|
76
|
-
return response;
|
|
77
|
-
} catch (error) {
|
|
78
|
-
Logger.log(`[res] status code: ${error.response.status}`);
|
|
79
|
-
Logger.log(`[res] body: ${JSON.stringify(error.response.data)}`);
|
|
80
|
-
return error.response;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async delete(endpoint, params) {
|
|
85
|
-
Logger.log(`[req] ▶ delete ${JSON.stringify(params || {})} to ${endpoint}:`);
|
|
86
|
-
try {
|
|
87
|
-
const response = await this.#axiosInstance.delete(`/${endpoint}`, params);
|
|
88
|
-
Logger.log(`[res] status code: ${response.status}`);
|
|
89
|
-
return response;
|
|
90
|
-
} catch (error) {
|
|
91
|
-
Logger.log(`[res] status code: ${error.response.status}`);
|
|
92
|
-
Logger.log(`[res] body: ${JSON.stringify(error.response.data)}`);
|
|
93
|
-
return error.response;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
module.exports = BaseAPI; // test commentary... don't mind me
|
package/src/DB/baseDB.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
const { Sequelize } = require('sequelize');
|
|
2
|
-
const Logger = require('../log/logger');
|
|
3
|
-
|
|
4
|
-
class BaseDB {
|
|
5
|
-
constructor(database) {
|
|
6
|
-
this.sequelize = new Sequelize(
|
|
7
|
-
database,
|
|
8
|
-
process.env.DB_USERNAME,
|
|
9
|
-
process.env.DB_PASSWORD,
|
|
10
|
-
{
|
|
11
|
-
host: process.env.DB_HOST,
|
|
12
|
-
port: process.env.DB_PORT,
|
|
13
|
-
dialect: 'mysql',
|
|
14
|
-
logging: (msg) => Logger.log(`[sql] ▶ ${msg}`),
|
|
15
|
-
},
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async createConnection() {
|
|
20
|
-
Logger.log(`[inf] ▶ connect to ${this.sequelize.getDatabaseName()} database`);
|
|
21
|
-
await this.sequelize.authenticate();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async closeConnection() {
|
|
25
|
-
Logger.log(`[inf] ▶ close connection to ${this.sequelize.getDatabaseName()} database`);
|
|
26
|
-
await this.sequelize.close();
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
module.exports = BaseDB;
|
package/src/DB/baseModel.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const { DataTypes } = require('sequelize');
|
|
2
|
-
|
|
3
|
-
const BaseModel = {
|
|
4
|
-
attributes: {
|
|
5
|
-
id: { type: DataTypes.BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true },
|
|
6
|
-
},
|
|
7
|
-
getOptions: ({ tableName, withSoftDelete = false } = {}) => {
|
|
8
|
-
const options = {
|
|
9
|
-
timestamps: true,
|
|
10
|
-
underscored: true,
|
|
11
|
-
createdAt: 'created_at',
|
|
12
|
-
updatedAt: 'updated_at',
|
|
13
|
-
tableName,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
if (withSoftDelete) {
|
|
17
|
-
options.paranoid = true;
|
|
18
|
-
options.deletedAt = 'deleted_at';
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return options;
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
module.exports = BaseModel;
|
package/src/data/JSONMapper.js
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
/* eslint no-restricted-syntax: ['off', 'ForInStatement'] */
|
|
2
|
-
const Logger = require('../log/logger');
|
|
3
|
-
|
|
4
|
-
class JSONMapper {
|
|
5
|
-
static getNestedProperty(flattenedObj, path) {
|
|
6
|
-
const keys = [];
|
|
7
|
-
const values = [];
|
|
8
|
-
for (const key in flattenedObj) {
|
|
9
|
-
if (Object.hasOwn(flattenedObj, key)) {
|
|
10
|
-
if (key.endsWith(`.${path}`) || key === path) {
|
|
11
|
-
keys.push(key);
|
|
12
|
-
values.push(flattenedObj[key]);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return { keys, values };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
static deleteNotSimilarProperty(flattenedObj, mappingSchema) {
|
|
21
|
-
const keysToDelete = [];
|
|
22
|
-
const outputObj = { ...flattenedObj };
|
|
23
|
-
for (const key in outputObj) {
|
|
24
|
-
if (Object.hasOwn(outputObj, key)) {
|
|
25
|
-
let shouldDelete = true;
|
|
26
|
-
for (const path in mappingSchema) {
|
|
27
|
-
if (Object.hasOwn(mappingSchema, path)) {
|
|
28
|
-
if (key.endsWith(`.${path}`) || key === path) {
|
|
29
|
-
shouldDelete = false;
|
|
30
|
-
break;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (shouldDelete) keysToDelete.push(key);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
keysToDelete.forEach((key) => delete outputObj[key]);
|
|
40
|
-
return outputObj;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
static flattenJSON(obj) {
|
|
44
|
-
const result = {};
|
|
45
|
-
const recursive = (currentObj, prefix = '') => {
|
|
46
|
-
for (const objKey in currentObj) {
|
|
47
|
-
if (Object.hasOwn(currentObj, objKey)) {
|
|
48
|
-
const fullKey = prefix ? `${prefix}.${objKey}` : objKey;
|
|
49
|
-
if (typeof currentObj[objKey] === 'object') {
|
|
50
|
-
recursive(currentObj[objKey], fullKey);
|
|
51
|
-
} else {
|
|
52
|
-
result[fullKey] = currentObj[objKey];
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
recursive(obj);
|
|
59
|
-
return result;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
static mapValues(firstData, secondData, mappingSchema) {
|
|
63
|
-
const [firstName] = Object.keys(firstData);
|
|
64
|
-
const firstObj = firstData[firstName];
|
|
65
|
-
const [secondName] = Object.keys(secondData);
|
|
66
|
-
const secondObj = secondData[secondName];
|
|
67
|
-
const firstFlattenedObj = this.flattenJSON(firstObj);
|
|
68
|
-
const secondFlattenedObj = this.flattenJSON(secondObj);
|
|
69
|
-
const firstFlattenedObjKeys = Object.keys(firstFlattenedObj);
|
|
70
|
-
const secondFlattenedObjKeys = Object.keys(secondFlattenedObj);
|
|
71
|
-
for (const path in mappingSchema) {
|
|
72
|
-
if (Object.hasOwn(mappingSchema, path)) {
|
|
73
|
-
if (firstFlattenedObjKeys.some((key) => key.endsWith(path))) {
|
|
74
|
-
if (secondFlattenedObjKeys.some((key) => key.endsWith(mappingSchema[path]))) {
|
|
75
|
-
let index = 0;
|
|
76
|
-
for (const key in firstFlattenedObj) {
|
|
77
|
-
if (Object.hasOwn(firstFlattenedObj, key)) {
|
|
78
|
-
if (this.getNestedProperty(firstFlattenedObj, path).keys.includes(key)) {
|
|
79
|
-
const valuesArr = this.getNestedProperty(secondFlattenedObj, mappingSchema[path])
|
|
80
|
-
.values;
|
|
81
|
-
firstFlattenedObj[key] = index >= valuesArr.length
|
|
82
|
-
? valuesArr[valuesArr.length - 1]
|
|
83
|
-
: valuesArr[index];
|
|
84
|
-
index += 1;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
} else {
|
|
89
|
-
throw new Error(Logger.log(`[err] mapping key "${mappingSchema[path]}" wasn't found in ${secondName}`));
|
|
90
|
-
}
|
|
91
|
-
} else {
|
|
92
|
-
throw new Error(Logger.log(`[err] mapping key "${path}" wasn't found in ${firstName}`));
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return this.deleteNotSimilarProperty(firstFlattenedObj, mappingSchema);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
static rewriteValues(mappedObj, firstDict, secondDict) {
|
|
101
|
-
const firstDictKeys = Object.keys(firstDict);
|
|
102
|
-
const secondDictKeys = Object.keys(secondDict);
|
|
103
|
-
const outputObj = { ...mappedObj };
|
|
104
|
-
for (const key in outputObj) {
|
|
105
|
-
if (Object.hasOwn(outputObj, key)) {
|
|
106
|
-
if (firstDictKeys.some((path) => this.getNestedProperty(outputObj, path)
|
|
107
|
-
.keys.includes(key))
|
|
108
|
-
&& secondDictKeys.some((path) => this.getNestedProperty(outputObj, path)
|
|
109
|
-
.keys.includes(key))) {
|
|
110
|
-
secondDictKeys.forEach((dictKey) => {
|
|
111
|
-
if (this.getNestedProperty(outputObj, dictKey).keys.includes(key)) {
|
|
112
|
-
for (const dictSubKey in secondDict[dictKey]) {
|
|
113
|
-
if (Object.hasOwn(secondDict[dictKey], dictSubKey)) {
|
|
114
|
-
if (secondDict[dictKey][dictSubKey] === outputObj[key] || dictSubKey === 'constant') {
|
|
115
|
-
outputObj[key] = firstDict[dictKey][dictSubKey];
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return outputObj;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
static unflattenJSON(flattenedObj) {
|
|
129
|
-
const result = {};
|
|
130
|
-
for (const key in flattenedObj) {
|
|
131
|
-
if (Object.hasOwn(flattenedObj, key)) {
|
|
132
|
-
const keys = key.split('.');
|
|
133
|
-
let currentLevel = result;
|
|
134
|
-
for (let i = 0; i < keys.length - 1; i += 1) {
|
|
135
|
-
const currentKey = keys[i];
|
|
136
|
-
const nextKeyIsArrayIndex = /^\d+$/.test(keys[i + 1]);
|
|
137
|
-
if (!currentLevel[currentKey] || typeof currentLevel[currentKey] !== 'object') {
|
|
138
|
-
currentLevel[currentKey] = nextKeyIsArrayIndex ? [] : {};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
currentLevel = currentLevel[currentKey];
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
currentLevel[keys[keys.length - 1]] = flattenedObj[key];
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return result;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
static safeMergeObjects(...mappedObjects) {
|
|
152
|
-
const result = {};
|
|
153
|
-
const locations = {};
|
|
154
|
-
mappedObjects.forEach((mappedObj) => {
|
|
155
|
-
const [currentLocation] = Object.keys(mappedObj);
|
|
156
|
-
const data = mappedObj[currentLocation];
|
|
157
|
-
locations[currentLocation] = {};
|
|
158
|
-
for (const [key, value] of Object.entries(data)) {
|
|
159
|
-
if (key in result) {
|
|
160
|
-
if (result[key] !== value) {
|
|
161
|
-
for (const location in locations) {
|
|
162
|
-
if (Object.hasOwn(locations, location)) {
|
|
163
|
-
if (key in locations[location]) {
|
|
164
|
-
if (location[key] !== value) {
|
|
165
|
-
throw new Error(`[err] conflicting values for duplicated keys "${key}" found in the ${currentLocation} and in the ${location} during mapped objects merge`);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
result[key] = value;
|
|
174
|
-
locations[currentLocation][key] = value;
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
return result;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
static getObjectWithSpecifiedKeyFromArray(array, keyName) {
|
|
182
|
-
let result = null;
|
|
183
|
-
for (let i = 0; i < array.length; i += 1) {
|
|
184
|
-
if (this.hasKey(array[i], keyName)) result = array[i];
|
|
185
|
-
}
|
|
186
|
-
return result;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
static hasKey(obj, keyToFind) {
|
|
190
|
-
if (typeof obj !== 'object' || obj === null) return false;
|
|
191
|
-
|
|
192
|
-
for (const key in obj) {
|
|
193
|
-
if (Object.prototype.hasOwnProperty.call(obj, key)) { // Safeguard added
|
|
194
|
-
if (key === keyToFind) {
|
|
195
|
-
return true;
|
|
196
|
-
}
|
|
197
|
-
if (typeof obj[key] === 'object' && this.hasKey(obj[key], keyToFind)) {
|
|
198
|
-
return true;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
return false;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
static appendPrefixToKeys(obj, prefix) {
|
|
206
|
-
const newObj = {};
|
|
207
|
-
Object.keys(obj).forEach((key) => {
|
|
208
|
-
newObj[prefix + key] = obj[key];
|
|
209
|
-
});
|
|
210
|
-
return newObj;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
module.exports = JSONMapper;
|
package/src/data/dataUtils.js
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const moment = require('moment');
|
|
3
|
-
const JSONLoader = require('./JSONLoader');
|
|
4
|
-
const TimeUtils = require('../time/timeUtils');
|
|
5
|
-
const Randomizer = require('../random/randomizer');
|
|
6
|
-
|
|
7
|
-
class DataUtils {
|
|
8
|
-
static saveToJSON(obj) {
|
|
9
|
-
const [name] = Object.keys(obj);
|
|
10
|
-
const data = obj[name];
|
|
11
|
-
const replacer = (key, value) => (typeof value === 'undefined' ? null : value);
|
|
12
|
-
fs.writeFileSync(`./test/artifacts/${name}.json`, JSON.stringify(data, replacer, 4));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
static filterClients(clients, options = {}) {
|
|
16
|
-
const { isResident } = options;
|
|
17
|
-
const { hasPassport } = options;
|
|
18
|
-
const { hasDriverLicence } = options;
|
|
19
|
-
const { isUnderSixtyYearsOld } = options;
|
|
20
|
-
const { isJuridical } = options;
|
|
21
|
-
let filteredClients = [...clients];
|
|
22
|
-
|
|
23
|
-
filteredClients = filteredClients.filter((client) => {
|
|
24
|
-
if (isResident !== undefined) {
|
|
25
|
-
return isResident ? client.resident_bool : !client.resident_bool;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return true;
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
filteredClients = filteredClients.filter((client) => {
|
|
32
|
-
if (hasDriverLicence !== undefined) {
|
|
33
|
-
return hasDriverLicence ? client.driving_license : !client.driving_license;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return true;
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
filteredClients = filteredClients.filter((client) => {
|
|
40
|
-
if (hasPassport !== undefined) {
|
|
41
|
-
const hasLetter = /[a-zA-Z]/.test(client.document_number);
|
|
42
|
-
return hasPassport
|
|
43
|
-
? client.document_type_id === 11 && hasLetter
|
|
44
|
-
: client.document_type_id !== 11;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return true;
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
filteredClients = filteredClients.filter((client) => {
|
|
51
|
-
if (isUnderSixtyYearsOld !== undefined) {
|
|
52
|
-
return isUnderSixtyYearsOld
|
|
53
|
-
? moment(client.born) > moment().subtract(60, 'years')
|
|
54
|
-
: moment(client.born) <= moment().subtract(60, 'years');
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return true;
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
filteredClients = filteredClients.filter((client) => {
|
|
61
|
-
if (isJuridical !== undefined) {
|
|
62
|
-
return isJuridical ? !client.natural_person_bool : client.natural_person_bool;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return true;
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
return filteredClients;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/* eslint-disable camelcase */
|
|
72
|
-
static createRandomHolderAndInsuredStructures(clientsArr) {
|
|
73
|
-
const randomHolderIndex = Randomizer.getRandomInteger(clientsArr.length - 1);
|
|
74
|
-
let randomInsuredIndex;
|
|
75
|
-
do {
|
|
76
|
-
randomInsuredIndex = Randomizer.getRandomInteger(clientsArr.length - 1);
|
|
77
|
-
} while (randomInsuredIndex === randomHolderIndex);
|
|
78
|
-
const tempHolder = clientsArr[randomHolderIndex];
|
|
79
|
-
const tempInsured = clientsArr[randomInsuredIndex];
|
|
80
|
-
const resultHolder = { ...tempHolder };
|
|
81
|
-
const resultInsured = { ...tempInsured };
|
|
82
|
-
|
|
83
|
-
const verify_bool = Number(JSONLoader.configData.verification);
|
|
84
|
-
const verify_type_id = Number(JSONLoader.configData.verification) || 3;
|
|
85
|
-
const pdl = Randomizer.getRandomInteger(1);
|
|
86
|
-
|
|
87
|
-
resultInsured.pdl = pdl;
|
|
88
|
-
resultInsured.verify_bool = verify_bool;
|
|
89
|
-
resultInsured.verify_type_id = verify_type_id;
|
|
90
|
-
|
|
91
|
-
resultInsured.esbd_id = tempInsured.client_id;
|
|
92
|
-
resultInsured.driver_certificate = tempInsured.driving_license;
|
|
93
|
-
|
|
94
|
-
resultInsured.born = TimeUtils.reformatDateFromYMDToDMY(tempInsured.born);
|
|
95
|
-
resultInsured.document_gived_date = TimeUtils
|
|
96
|
-
.reformatDateFromYMDToDMY(tempInsured.document_gived_date);
|
|
97
|
-
resultInsured.driver_certificate_date = TimeUtils
|
|
98
|
-
.reformatDateFromYMDToDMY(tempInsured.date_issue_license);
|
|
99
|
-
|
|
100
|
-
resultInsured.iin = tempInsured.iin.toString();
|
|
101
|
-
|
|
102
|
-
resultInsured.natural_person_bool = JSONLoader.testData.natural_person_bool;
|
|
103
|
-
resultInsured.economics_sector_id = JSONLoader.testData.economics_sector_id;
|
|
104
|
-
resultInsured.notification_type_id = JSONLoader.testData.notification_type_id;
|
|
105
|
-
resultInsured.driver_certificate_type_id = JSONLoader.testData.driver_certificate_type_id;
|
|
106
|
-
resultInsured.experience_less = JSONLoader.testData.experience_less;
|
|
107
|
-
resultInsured.pensioner_bool = JSONLoader.testData.pensioner_bool;
|
|
108
|
-
resultInsured.country_id = JSONLoader.testData.country_id;
|
|
109
|
-
resultInsured.is_new = JSONLoader.testData.is_new;
|
|
110
|
-
resultInsured.class_id = JSONLoader.testData.class_id;
|
|
111
|
-
resultInsured.class = JSONLoader.testData.class;
|
|
112
|
-
resultInsured.document_gived_by = JSONLoader.testData.insured_document_gived_by;
|
|
113
|
-
resultInsured.email = JSONLoader.testData.insured_email;
|
|
114
|
-
resultInsured.mobile_phone = JSONLoader.testData.insured_mobile_phone;
|
|
115
|
-
resultInsured.address = JSONLoader.testData.insured_address;
|
|
116
|
-
resultInsured.is_insured = 1;
|
|
117
|
-
|
|
118
|
-
resultHolder.pdl = pdl;
|
|
119
|
-
resultHolder.verify_bool = verify_bool;
|
|
120
|
-
resultHolder.verify_type_id = verify_type_id;
|
|
121
|
-
|
|
122
|
-
resultHolder.esbd_id = tempHolder.client_id;
|
|
123
|
-
resultHolder.driver_certificate = tempHolder.driving_license;
|
|
124
|
-
|
|
125
|
-
resultHolder.born = TimeUtils.reformatDateFromYMDToDMY(tempHolder.born);
|
|
126
|
-
resultHolder.document_gived_date = TimeUtils
|
|
127
|
-
.reformatDateFromYMDToDMY(tempHolder.document_gived_date);
|
|
128
|
-
resultHolder.driver_certificate_date = TimeUtils
|
|
129
|
-
.reformatDateFromYMDToDMY(tempHolder.date_issue_license);
|
|
130
|
-
|
|
131
|
-
resultHolder.iin = tempHolder.iin.toString();
|
|
132
|
-
|
|
133
|
-
resultHolder.natural_person_bool = JSONLoader.testData.natural_person_bool;
|
|
134
|
-
resultHolder.economics_sector_id = JSONLoader.testData.economics_sector_id;
|
|
135
|
-
resultHolder.notification_type_id = JSONLoader.testData.notification_type_id;
|
|
136
|
-
resultHolder.driver_certificate_type_id = JSONLoader.testData.driver_certificate_type_id;
|
|
137
|
-
resultHolder.experience_less = JSONLoader.testData.experience_less;
|
|
138
|
-
resultHolder.pensioner_bool = JSONLoader.testData.pensioner_bool;
|
|
139
|
-
resultHolder.country_id = JSONLoader.testData.country_id;
|
|
140
|
-
resultHolder.is_new = JSONLoader.testData.is_new;
|
|
141
|
-
resultHolder.activity_kind_id = JSONLoader.testData.activity_kind_id;
|
|
142
|
-
resultHolder.document_gived_by = JSONLoader.testData.holder_document_gived_by;
|
|
143
|
-
resultHolder.email = JSONLoader.testData.holder_email;
|
|
144
|
-
resultHolder.mobile_phone = JSONLoader.testData.holder_mobile_phone;
|
|
145
|
-
resultHolder.address = JSONLoader.testData.holder_address;
|
|
146
|
-
resultHolder.is_insured = 0;
|
|
147
|
-
|
|
148
|
-
return { holder: resultHolder, insured: resultInsured };
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
static createRandomCarStructure(carsArr) {
|
|
152
|
-
const randomCarIndex = Randomizer.getRandomInteger(carsArr.length - 1);
|
|
153
|
-
const tempCar = carsArr[randomCarIndex];
|
|
154
|
-
const resultCar = { ...tempCar };
|
|
155
|
-
|
|
156
|
-
resultCar.esbd_id = tempCar.tf_id;
|
|
157
|
-
resultCar.dt_reg_cert = TimeUtils.reformatDateFromYMDToDMY(tempCar.dt_reg_cert);
|
|
158
|
-
resultCar.verified_bool = 1;
|
|
159
|
-
|
|
160
|
-
return resultCar;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
module.exports = DataUtils;
|
package/src/data/filesParser.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
require('dotenv').config({ path: path.join(__dirname, '../../../../../', '.env.test'), override: true });
|
|
4
|
-
|
|
5
|
-
const envDirectory = path.join(__dirname, '../../../../../');
|
|
6
|
-
const loaderFileLocation = path.join(__dirname, '../../../../../test/main/utils/data/', 'JSONLoader.js');
|
|
7
|
-
const testClientsFileLocation = path.join(envDirectory, './test/resources/data/testClients.json');
|
|
8
|
-
const testCarsFileLocation = path.join(envDirectory, './test/resources/data/testCars.json');
|
|
9
|
-
const averageRBNSFileLocation = path.join(envDirectory, './test/resources/data/averageRBNS.json');
|
|
10
|
-
const JSONDirectory = path.join(envDirectory, './test/resources');
|
|
11
|
-
|
|
12
|
-
const getFiles = (directory, extension) => {
|
|
13
|
-
const allFiles = fs.readdirSync(directory);
|
|
14
|
-
const selectedFiles = allFiles.filter((file) => file.endsWith(extension));
|
|
15
|
-
allFiles.forEach((file) => {
|
|
16
|
-
const fullPath = path.join(directory, file);
|
|
17
|
-
if (fs.statSync(fullPath).isDirectory()) {
|
|
18
|
-
const nestedFiles = getFiles(fullPath, extension);
|
|
19
|
-
selectedFiles.push(...nestedFiles.map((nestedFile) => path.join(file, nestedFile)));
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
return selectedFiles;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const generateClassInit = (selectedFiles, directory) => `class JSONLoader {\n${selectedFiles.map((file) => {
|
|
27
|
-
const variableName = path.parse(file).name;
|
|
28
|
-
return `\tstatic get ${variableName}() {\n\t\tconst ${variableName} = require('${path.join(directory, file)}');\n\t\treturn JSON.parse(JSON.stringify(${variableName}));\n\t}\n\n`;
|
|
29
|
-
}).join('')}`;
|
|
30
|
-
|
|
31
|
-
const generateJSONLoader = (filePath, directory, extension) => {
|
|
32
|
-
const files = getFiles(directory, extension);
|
|
33
|
-
const classInit = generateClassInit(files, directory);
|
|
34
|
-
const classExport = '}\n\nmodule.exports = JSONLoader;';
|
|
35
|
-
fs.writeFileSync(filePath, classInit + classExport);
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const setConfigData = (directory, extension) => {
|
|
39
|
-
const files = getFiles(directory, extension);
|
|
40
|
-
const configFile = files.filter((file) => file.includes('config')).pop();
|
|
41
|
-
if (configFile) {
|
|
42
|
-
const filePath = `${directory}/${configFile}`;
|
|
43
|
-
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
44
|
-
data.parallel = process.argv.includes('--parallel');
|
|
45
|
-
data.setPolicyWaitingTWB = process.argv.includes('--setPolicyWaitingTWB');
|
|
46
|
-
data.testOGPOClaim = process.argv.includes('--testOGPOClaim');
|
|
47
|
-
|
|
48
|
-
try {
|
|
49
|
-
data.verification = Boolean(JSON.parse(process.env.VERIFICATION ?? data.verification));
|
|
50
|
-
} catch (error) { // eslint-disable-next-line no-console
|
|
51
|
-
console.log(' [err] incorrect value of "VERIFICATION" .env variable!');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
try {
|
|
55
|
-
const value = JSON.parse(process.env.SET_POLICY_WAITING_TWB ?? data.setPolicyWaitingTWB);
|
|
56
|
-
data.setPolicyWaitingTWB = Boolean(value);
|
|
57
|
-
} catch (error) { // eslint-disable-next-line no-console
|
|
58
|
-
console.log(' [err] incorrect value of "SET_POLICY_WAITING_TWB" .env variable!');
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
try {
|
|
62
|
-
data.getPolicyTWB = Boolean(JSON.parse(process.env.GET_POLICY_TWB ?? data.getPolicyTWB));
|
|
63
|
-
} catch (error) { // eslint-disable-next-line no-console
|
|
64
|
-
console.log(' [err] incorrect value of "GET_POLICY_TWB" .env variable!');
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (process.env.GATEWAY_URL) {
|
|
68
|
-
const value = process.env.GATEWAY_URL.match(/\b(?:localhost|dev|staging|qa\d+)\b/g);
|
|
69
|
-
if (value) {
|
|
70
|
-
data.environment = value.pop();
|
|
71
|
-
} else { // eslint-disable-next-line no-console
|
|
72
|
-
console.log(' [err] incorrect value of "GATEWAY_URL" .env variable!');
|
|
73
|
-
}
|
|
74
|
-
} else { // eslint-disable-next-line no-console
|
|
75
|
-
console.log(' [err] "GATEWAY_URL" .env variable not exists!');
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
const value = JSON.parse(process.env.TEST_OGPO_CLAIM ?? data.testOGPOClaim);
|
|
80
|
-
data.testOGPOClaim = Boolean(value);
|
|
81
|
-
} catch (error) { // eslint-disable-next-line no-console
|
|
82
|
-
console.log(' [err] incorrect value of "TEST_OGPO_CLAIM" .env variable!');
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const checkEnvExists = (directory, extension) => {
|
|
90
|
-
const files = getFiles(directory, extension);
|
|
91
|
-
if (!files.length) throw new Error('[err] .env.test file not exists in root directory!');
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
const generateTestDataFile = (filePath) => {
|
|
95
|
-
const emptyObj = {};
|
|
96
|
-
if (!fs.existsSync(filePath)) fs.writeFileSync(filePath, JSON.stringify(emptyObj, null, 2), 'utf8');
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
checkEnvExists(envDirectory, '.test');
|
|
100
|
-
setConfigData(JSONDirectory, '.json');
|
|
101
|
-
generateTestDataFile(testCarsFileLocation);
|
|
102
|
-
generateTestDataFile(testClientsFileLocation);
|
|
103
|
-
generateTestDataFile(averageRBNSFileLocation);
|
|
104
|
-
generateJSONLoader(loaderFileLocation, JSONDirectory, '.json');
|