Skip to content

Commit ddd9224

Browse files
committed
Now creating all manager's account
1 parent 89a8d65 commit ddd9224

4 files changed

Lines changed: 115 additions & 8 deletions

File tree

src/appTemplates/AppManagingAccount.js

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class AppManagingAccount {
7272
const collectorsMap = {};
7373
for (const stream of result.streams) {
7474
const collector = new Collector(this, stream);
75-
collectorsMap[collector.id] = collector;
75+
collectorsMap[collector.streamId] = collector;
7676
}
7777
this.#cache.collectorsMap = collectorsMap;
7878
return Object.values(this.#cache.collectorsMap);
@@ -92,23 +92,82 @@ class AppManagingAccount {
9292
if (result.error) throw new HDSLibError('Failed creating collector', result.error);
9393
if (!result.stream?.name) throw new HDSLibError('Failed creating collector, invalid result', result);
9494
const collector = new Collector(this, result.stream);
95-
this.#cache.collectorsMap[collector.id] = collector;
95+
this.#cache.collectorsMap[collector.streamId] = collector;
9696
return collector;
9797
}
9898
}
9999

100+
const COLLECTOR_STREAMID_SUFFIXES = {
101+
settings: 'settings',
102+
pending: 'pending',
103+
active: 'active',
104+
error: 'error'
105+
};
106+
Object.freeze(COLLECTOR_STREAMID_SUFFIXES);
100107
class Collector {
108+
static STREAMID_SUFFIXES = COLLECTOR_STREAMID_SUFFIXES;
101109
appManaging;
102-
id;
110+
streamId;
103111
name;
112+
#streamData;
113+
104114
/**
105115
* @param {AppManagingAccount} appManaging
106-
* @param {Pryv.Stream} stream
116+
* @param {Pryv.Stream} streamData
107117
*/
108-
constructor (appManaging, stream) {
109-
this.id = stream.id;
110-
this.name = stream.name;
118+
constructor (appManaging, streamData) {
119+
this.streamId = streamData.id;
120+
this.name = streamData.name;
111121
this.appManaging = appManaging;
122+
this.#streamData = streamData;
123+
}
124+
125+
/**
126+
* check if required streams are present, if not create them
127+
*/
128+
async checkStreamStructure () {
129+
// if streamData has correct children structure we assume all is OK
130+
const childrenData = this.#streamData.children;
131+
const toCreate = Object.values(Collector.STREAMID_SUFFIXES)
132+
.filter((suffix) => {
133+
if (!childrenData) return true;
134+
if (childrenData.find(child => child.id === this.streamIdFor(suffix))) return false;
135+
return true;
136+
});
137+
138+
if (toCreate.length === 0) return { created: [] };
139+
// create required streams
140+
const apiCalls = toCreate.map(suffix => ({
141+
method: 'streams.create',
142+
params: {
143+
id: this.streamIdFor(suffix),
144+
parentId: this.streamId,
145+
name: this.name + ' ' + suffix
146+
}
147+
}));
148+
const result = { created: [], errors: [] };
149+
const resultsApi = await this.appManaging.connection.api(apiCalls);
150+
for (const resultCreate of resultsApi) {
151+
if (resultCreate.error) {
152+
result.errors.push(resultCreate.error);
153+
continue;
154+
}
155+
if (resultCreate.stream) {
156+
result.created.push(resultCreate.stream);
157+
if (!this.#streamData.children) this.#streamData.children = [];
158+
this.#streamData.children.push(resultCreate.stream);
159+
continue;
160+
}
161+
result.errors.push({ id: 'unkown-error', message: 'Cannot find stream in result', data: resultCreate });
162+
}
163+
return result;
164+
}
165+
166+
/**
167+
* @param {string} suffix
168+
*/
169+
streamIdFor (suffix) {
170+
return this.streamId + '-' + suffix;
112171
}
113172
}
114173

tests/apptemplates.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('[APTX] appTemplates', function () {
2828
const collectorName = 'Test';
2929
// create a Collector
3030
const newCollector = await appManaging.createCollector(collectorName);
31-
assert.ok(newCollector.id.startsWith(baseStreamId), 'Collectors id should start with baseStreamId');
31+
assert.ok(newCollector.streamId.startsWith(baseStreamId), 'Collectors id should start with baseStreamId');
3232
assert.ok(newCollector.name, collectorName);
3333

3434
// Create a Collector with the same name should fail
@@ -45,5 +45,27 @@ describe('[APTX] appTemplates', function () {
4545
const found = collectors.find(c => c.name === collectorName);
4646
if (!found) throw new Error('Should find collector with name: ' + collectorName);
4747
assert.equal(found, newCollector);
48+
49+
// check StreamStructure
50+
const resultCheckStructure = await newCollector.checkStreamStructure();
51+
assert.equal(resultCheckStructure.created.length, 4, 'Should create 4 streams');
52+
for (const created of resultCheckStructure.created) {
53+
assert.equal(created.parentId, newCollector.streamId, 'Should have collector stream as parentid');
54+
}
55+
56+
// 2nd call of StreamStructure should be empty
57+
const resultCheckStructure2 = await newCollector.checkStreamStructure();
58+
assert.equal(resultCheckStructure2.created.length, 0, 'Should create 0 streams');
59+
60+
// creating a new Manager with same connection should load the structure
61+
const connection2 = new pryv.Connection(appManaging.connection.apiEndpoint);
62+
const appManaging2 = await AppManagingAccount.newFromConnection(connection2, baseStreamId);
63+
// check if collector is in the list
64+
const collectors2 = await appManaging2.getCollectors();
65+
const found2 = collectors2.find(c => c.name === collectorName);
66+
if (!found2) throw new Error('Should find collector with name: ' + collectorName);
67+
// call of StreamStructure should be empty as already created
68+
const resultCheckStructure3 = await newCollector.checkStreamStructure();
69+
assert.equal(resultCheckStructure3.created.length, 0, 'Should create 0 streams');
4870
});
4971
});

tests/test-utils/debug.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const util = require('util');
2+
3+
function log () {
4+
for (let i = 0; i < arguments.length; i++) {
5+
console.log(util.inspect(arguments[i], { depth: 12, colors: true }));
6+
}
7+
}
8+
9+
function stack (start = 0, length = 100) {
10+
const e = new Error();
11+
return e.stack.split('\n').filter(l => l.indexOf('node_modules') < 0).slice(start + 1, start + length + 1);
12+
}
13+
14+
function logstack () {
15+
log(...arguments, stack(2, 4));
16+
}
17+
18+
module.exports = {
19+
logstack,
20+
log,
21+
stack
22+
};
23+
24+
global.$$ = logstack;
25+
global.$$$ = log;

tests/test-utils/pryvService.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require('./debug');
12
const pryv = require('pryv');
23
const superagent = pryv.utils.superagent;
34

0 commit comments

Comments
 (0)