@@ -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 ) ;
100107class 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
0 commit comments