@@ -10,7 +10,8 @@ const Collector = require('./Collector');
1010 * The App can create multiple "collectors e.g. Questionnaries"
1111 *
1212 * Stream structure
13- * - [baseStreamId] "Root" stream for this app
13+ * - applications
14+ * - [baseStreamId] "Root" stream for this app
1415 * - [baseStreamId]-[collectorsId] Each "questionnary" or "request for a set of data" has it's own stream
1516 * - [baseStreamId]-[collectorsId]-internal Private stuff not to be shared
1617 * - [baseStreamId]-[collectorsId]-public Contains events with the current settings of this app (this stream will be shared in "read" with the request)
@@ -30,6 +31,7 @@ class AppManagingAccount {
3031 #cache;
3132
3233 /**
34+ * @private
3335 * use AppManagingAccount.newFromConnection() to create new AppManagingAccount
3436 * @param {string } appName
3537 * @param {string } baseStreamId
@@ -44,7 +46,7 @@ class AppManagingAccount {
4446
4547 /**
4648 * Return an initialized AppManagingAccount instance
47- * @param {Pryv.Connection } connection
49+ * @param {Pryv.Connection } connection - should be personalConnection, a connection with "manage" right on `baseStreamId` or "manage" on "*"
4850 * @returns {AppManagingAccount }
4951 */
5052 static async newFromConnection ( connection , baseStreamId ) {
@@ -72,7 +74,23 @@ class AppManagingAccount {
7274 return Object . values ( this . #cache. collectorsMap ) ;
7375 }
7476
77+ /**
78+ * Create an iniatilized Collector
79+ * @param {string } name
80+ * @returns {Collector }
81+ */
7582 async createCollector ( name ) {
83+ const collector = await this . createCollectorUnitialized ( name ) ;
84+ await collector . init ( ) ;
85+ return collector ;
86+ }
87+
88+ /**
89+ * Create an unitialized Collector (mostly used by tests)
90+ * @param {string } name
91+ * @returns {Collector }
92+ */
93+ async createCollectorUnitialized ( name ) {
7694 const streamId = this . baseStreamId + '-' + collectorIdGenerator . rnd ( ) ;
7795 const params = {
7896 id : streamId ,
@@ -90,15 +108,17 @@ module.exports = AppManagingAccount;
90108
91109async function newAppManagingAccountFromConnection ( connection , baseStreamId ) {
92110 const accessInfo = await connection . apiOne ( 'getAccessInfo' ) ;
93- if ( ! accessInfo . type === 'app' ) throw new Error ( 'Failed creating new AppManagingAccount, "app" authorization required: ' + JSON . stringify ( accessInfo ) ) ;
94- // check if baseStreamId is in pemission set
95- const found = accessInfo . permissions . find ( p => ( p . streamId === baseStreamId || p . streamId === '*' ) ) ;
96- if ( found && found . level !== 'manage' ) { // check if level 'manage'
97- throw new Error ( `Failed creating new AppManagingAccount, Not sufficient permissions on stream: ${ baseStreamId } ` ) ;
98- }
99- if ( ! found ) {
100- // here we may check if we can create or manage baseStreamId as it might be covered by permissions
101- throw new Error ( `Failed creating new AppManagingAccount, cannot find "${ baseStreamId } " in permission list` ) ;
111+ if ( ! accessInfo . type === 'personal' ) {
112+ if ( ! accessInfo . type === 'app' ) throw new Error ( 'Failed creating new AppManagingAccount, "app" authorization required: ' + JSON . stringify ( accessInfo ) ) ;
113+ // check if baseStreamId is in pemission set
114+ const found = accessInfo . permissions . find ( p => ( p . streamId === baseStreamId || p . streamId === '*' ) ) ;
115+ if ( found && found . level !== 'manage' ) { // check if level 'manage'
116+ throw new Error ( `Failed creating new AppManagingAccount, Not sufficient permissions on stream: ${ baseStreamId } ` ) ;
117+ }
118+ if ( ! found ) {
119+ // here we may check if we can create or manage baseStreamId as it might be covered by permissions
120+ throw new Error ( `Failed creating new AppManagingAccount, cannot find "${ baseStreamId } " in permission list` ) ;
121+ }
102122 }
103123 const appManagingAccount = new AppManagingAccount ( accessInfo . name , baseStreamId , connection ) ;
104124 return appManagingAccount ;
0 commit comments