Skip to content

Commit 69ea53c

Browse files
committed
Added Status
1 parent 856c89a commit 69ea53c

4 files changed

Lines changed: 121 additions & 4 deletions

File tree

src/appTemplates/AppManagingAccount.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const collectorIdGenerator = new ShortUniqueId({ dictionary: 'alphanum_lower', l
1212
* Stream structure
1313
* - [baseStreamId] "Root" stream for this app
1414
* - [baseStreamId]-[collectorsId] Each "questionnary" or "request for a set of data" has it's own stream
15+
* - [baseStreamId]-[collectorsId]-internal Private stuff not to be shared
1516
* - [baseStreamId]-[collectorsId]-public Contains events with the current settings of this app (this stream will be shared in "read" with the request)
1617
* - [baseStreamId]-[collectorsId]-pending Contains events with "pending" requests
1718
* - [baseStreamId]-[collectorsId]-inbox Contains events with "inbox" requests Will be shared in createOnly
@@ -99,6 +100,7 @@ class AppManagingAccount {
99100
}
100101

101102
const COLLECTOR_STREAMID_SUFFIXES = {
103+
internal: 'internal',
102104
public: 'public',
103105
pending: 'pending',
104106
inbox: 'inbox',
@@ -108,6 +110,12 @@ const COLLECTOR_STREAMID_SUFFIXES = {
108110
Object.freeze(COLLECTOR_STREAMID_SUFFIXES);
109111
class Collector {
110112
static STREAMID_SUFFIXES = COLLECTOR_STREAMID_SUFFIXES;
113+
static STATUSES = Object.freeze({
114+
draft: 'draft',
115+
active: 'active',
116+
deactivated: 'deactivated'
117+
});
118+
111119
appManaging;
112120
streamId;
113121
name;
@@ -126,6 +134,76 @@ class Collector {
126134
this.#cache = {};
127135
}
128136

137+
/**
138+
* @property {string} one of 'draft', 'active', 'deactivated'
139+
*/
140+
get statusCode () {
141+
if (this.#cache.status == null) throw new Error('Init Collector first');
142+
return this.#cache.status.content.status;
143+
}
144+
145+
/**
146+
* Fetch online data
147+
*/
148+
async init () {
149+
await this.checkStreamStructure();
150+
await this.getStatus();
151+
}
152+
153+
/**
154+
* @type {StatusEvent} - extends PryvEvent with a specific content
155+
* @property {Object} content - content
156+
* @property {String} content.status - one of 'draft', 'active', 'deactivated'
157+
* @property {Data} content.data - app specific data
158+
*/
159+
160+
/**
161+
* Get Collector status,
162+
* @param {boolean} forceRefresh - if true, forces fetching the status from the server
163+
* @returns {StatusEvent}
164+
*/
165+
async getStatus (forceRefresh = false) {
166+
if (!forceRefresh && this.#cache.status) return this.#cache.status;
167+
const params = { types: ['status/collector-v1'], limit: 1, streams: [this.streamIdFor(Collector.STREAMID_SUFFIXES.internal)] };
168+
const statusEvents = await this.appManaging.connection.apiOne('events.get', params, 'events');
169+
if (statusEvents.length === 0) { // non exsitent set "draft" status
170+
return this.setStatus(Collector.STATUSES.draft, {});
171+
}
172+
this.#cache.status = statusEvents[0];
173+
return this.#cache.status;
174+
}
175+
176+
/**
177+
* Change the status
178+
* @param {string} status one of of 'draft', 'active', 'deactivated'
179+
* @param {object} data - custom data
180+
* @returns {StatusEvent}
181+
*/
182+
async setStatus (status, data) {
183+
if (!Collector.STATUSES[status]) throw new HDSLibError('Unkown status key', { status, data });
184+
const event = {
185+
type: 'status/collector-v1',
186+
streamIds: [this.streamIdFor(Collector.STREAMID_SUFFIXES.internal)],
187+
content: {
188+
status,
189+
data
190+
}
191+
};
192+
const statusEvent = await this.appManaging.connection.apiOne('events.create', event, 'event');
193+
this.#cache.status = statusEvent;
194+
return this.#cache.status;
195+
}
196+
197+
/**
198+
* Create a "pending" invite to be sent to an app usin AppSharingAccount
199+
* @param {string} name a default display name for this request
200+
* @param {Object} [options]
201+
* @param {Object} [options.customData] any data to be used by the client app
202+
*/
203+
async createInvite (name, options) {
204+
205+
}
206+
129207
/**
130208
* Get sharing api endpoint
131209
*/

src/patchedPryv.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* While developping this lib some functionnalities should be
3+
* added to pyv js-lib in a second step
4+
*/
5+
const pryv = require('pryv');
6+
7+
/**
8+
* Make one api Api call
9+
* @param {string} method - methodId
10+
* @param {object} [params] - the method associated with this result
11+
* @param {string} [resultKey] - if given, returns the value or throws an error if not present
12+
* @throws {Error} if .error is present the response
13+
*/
14+
pryv.Connection.prototype.apiOne = async function (method, params = {}, expectedKey) {
15+
const result = await this.api([{ method, params }]);
16+
if (result[0] == null || result[0].error || (expectedKey != null && result[0][expectedKey] == null)) throw new Error(`Error for api method: "${method}" with params: ${JSON.stringify(params)} >> Result: ${JSON.stringify(result)}"`);
17+
if (expectedKey != null) return result[0][expectedKey];
18+
return result[0];
19+
};
20+
21+
module.exports = pryv;

tests/apptemplates.test.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint-env mocha */
22
const assert = require('node:assert/strict');
3-
const { createUserAndPermissions } = require('./test-utils/pryvService');
3+
const { createUserAndPermissions, pryv } = require('./test-utils/pryvService');
44
const AppManagingAccount = require('../src/appTemplates/AppManagingAccount');
5-
const pryv = require('pryv');
65

76
describe('[APTX] appTemplates', function () {
87
this.timeout(10000);
@@ -48,7 +47,7 @@ describe('[APTX] appTemplates', function () {
4847

4948
// check StreamStructure
5049
const resultCheckStructure = await newCollector.checkStreamStructure();
51-
assert.equal(resultCheckStructure.created.length, 5, 'Should create 5 streams');
50+
assert.equal(resultCheckStructure.created.length, 6, 'Should create 5 streams');
5251
for (const created of resultCheckStructure.created) {
5352
assert.equal(created.parentId, newCollector.streamId, 'Should have collector stream as parentid');
5453
}
@@ -57,6 +56,24 @@ describe('[APTX] appTemplates', function () {
5756
const resultCheckStructure2 = await newCollector.checkStreamStructure();
5857
assert.equal(resultCheckStructure2.created.length, 0, 'Should create 0 streams');
5958

59+
// Should throw error as status is not yet set
60+
try {
61+
// eslint-disable-next-line no-unused-expressions
62+
newCollector.statusCode;
63+
throw new Error('Should throw error');
64+
} catch (e) {
65+
assert.equal(e.message, 'Init Collector first');
66+
}
67+
68+
// Get status
69+
const currentStatus = await newCollector.getStatus();
70+
assert.equal(currentStatus.content.status, 'draft');
71+
assert.equal(newCollector.statusCode, 'draft');
72+
73+
// Get status 2nd should retrun the same
74+
const currentStatus2 = await newCollector.getStatus();
75+
assert.equal(currentStatus2, currentStatus);
76+
6077
// Sharing token creation
6178
const sharingApiEndpoint = await newCollector.sharingApiEndpoint();
6279
assert.ok(sharingApiEndpoint.startsWith('https://'));

tests/test-utils/pryvService.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require('./debug');
2-
const pryv = require('pryv');
2+
const pryv = require('../../src/patchedPryv');
33
const superagent = pryv.utils.superagent;
44

55
const ShortUniqueId = require('short-unique-id');
@@ -13,6 +13,7 @@ module.exports = {
1313
createUser,
1414
createUserAndPermissions,
1515
service,
16+
pryv,
1617
config
1718
};
1819

0 commit comments

Comments
 (0)