-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
158 lines (138 loc) · 5.99 KB
/
index.js
File metadata and controls
158 lines (138 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const fetch = require('node-fetch');
const axios = require('axios').default;
const cron = require('node-cron');
const postgres_api = require('./postgres.js');
const TB_get_api = require('./TB/get.js');
const funcs = require('./functions');
const TB_push_api = require('./TB/push.js');
async function createConnection(options) {
process.env.TB_HOST = options.TB_HOST;
process.env.TB_PORT = options.TB_PORT
process.env.TB_USERNAME = options.TB_USERNAME;
process.env.TB_PASSWORD = options.TB_PASSWORD;
process.env.PG_HOST = options.PG_HOST;
process.env.PG_DATABASE= options.PG_DATABASE;
process.env.PG_PORT = options.PG_PORT
process.env.PG_USERNAME = options.PG_USERNAME;
process.env.PG_PASSWORD = options.PG_PASSWORD;
TB_HOST = process.env.TB_HOST;
TB_PORT = process.env.TB_PORT
TB_USERNAME = process.env.TB_USERNAME;
TB_PASSWORD = process.env.TB_PASSWORD;
await token()
await postgres_api.createPostgresConnection();
}
async function token() {
var url = 'http://' + process.env.TB_HOST + ':' + process.env.TB_PORT + '/api/auth/login';
var options = {
method: 'post',
url: url,
data: {
"username": process.env.TB_USERNAME,
"password": process.env.TB_PASSWORD
},
headers: {
"Content-type": "application/json",
"Accept": "application/json"
}
};
var token = await getAndSetToken(options);
cron.schedule("*/15 * * * *", async () => {
await getAndSetToken(options);
})
return token
}
async function getAndSetToken(options) {
try {
const response = await axios(options);
if (response.status === 200) {
process.env.TB_TOKEN = response.data.token;
return process.env.TB_TOKEN
}
} catch (error) {
console.error(error);
}
}
async function extendChildAttrs(options) {
const parentId = postgres_api.toPostgresID(options.parent_id);
const childId = postgres_api.toPostgresID(options.child_id);
const childType = options.child_type;
const parentAttrs = await postgres_api.get.getAttrsAndValuesById(parentId, options.attributeKeys);
const childAttrs = await postgres_api.get.getAttrsAndValuesById(childId, options.attributeKeys);
const parentAttrsValues = funcs.makeAttrsValuesObj(parentAttrs);
const childAttrsValues = funcs.makeAttrsValuesObj(childAttrs);
const parentKeys = Object.keys(parentAttrsValues);
for (let parentKey of parentKeys) {
// get attribute_types which are not find in child
// the write new attribute_keys
if (!(childAttrsValues.hasOwnProperty(parentKey))) {
console.log('child does not have attribute type: ', parentKey);
let dataToWrite = parentAttrsValues[parentKey];
// set child entity_id, entity_type for extending attributes of child
// set null to attributes which not existed before extending of attributes
dataToWrite = funcs.updateChildAttrsKeysValue(dataToWrite, childId, childType);
const result = await postgres_api.insertIntoAttrsKeysVals(dataToWrite);
if (result.count === dataToWrite.length) {
console.log('successfully write to db!')
}
// child and parent have common attributes type SERVER_SCOPE and etc.
// we need to find attribute_keys which not presented in child
} else {
console.log('parent and child have common attribute type ', parentKey);
let dataToWrite = [];
const parentData = parentAttrsValues[parentKey];
const childData = childAttrsValues[parentKey];
// update attributes or assign new attributes to child
// depends on options.updateAttrs
switch (options.updateAttrs) {
case true:
if (parentData.length !== childData.length) {
console.error("Different length of parent and child attribute array!");
return;
}
let updatedAttrs = funcs.updateChildAttrsKeysValue(parentData, childId, childType);
for (let attr of updatedAttrs) {
const resp = await postgres_api.updateAttrsKeysAndVals(attr);
console.log('resp ', resp);
}
break;
case false:
default:
for (let i = 0; i < parentData.length; i++) {
let match = false;
for (let j = 0; j < childData.length; j++) {
if (parentData[i].attribute_key.toString() === childData[j].attribute_key.toString()) {
match = true;
break;
}
}
if (!match) {
// Change parent properties to child
parentData[i].entity_id = childId;
parentData[i].entity_type = childType;
parentData[i].last_update_ts = Date.now()
dataToWrite.push(parentData[i]);
}
}
if (dataToWrite.length === 0) {
console.log('Not data to write! ');
continue;
}
console.log('Find attributes to assign to child !');
console.log('data to write ', dataToWrite)
const result = await postgres_api.insertIntoAttrsKeysVals(dataToWrite);
if (result.count === dataToWrite.length) {
console.log('successfully write to db!')
}
}
}
}
}
module.exports = {
get: TB_get_api,
push: TB_push_api,
postgres: postgres_api,
token: token,
createConnection: createConnection,
extendChildAttrs: extendChildAttrs,
};