1- /**
2- * Collector is used by AppManagingAccount
3- * A "Collector" can be seen as a "Request" and set of "Responses"
4- * - Responses are authorization tokens from individuals
5- */
6-
71const { HDSLibError } = require ( '../errors' ) ;
82
93const COLLECTOR_STREAMID_SUFFIXES = {
@@ -15,6 +9,12 @@ const COLLECTOR_STREAMID_SUFFIXES = {
159 error : 'error'
1610} ;
1711Object . freeze ( COLLECTOR_STREAMID_SUFFIXES ) ;
12+
13+ /**
14+ * Collector is used by AppManagingAccount
15+ * A "Collector" can be seen as a "Request" and set of "Responses"
16+ * - Responses are authorization tokens from individuals
17+ */
1818class Collector {
1919 static STREAMID_SUFFIXES = COLLECTOR_STREAMID_SUFFIXES ;
2020 static STATUSES = Object . freeze ( {
@@ -49,12 +49,35 @@ class Collector {
4949 return this . #cache. status . content . status ;
5050 }
5151
52+ /**
53+ * @typedef {RequestContent }
54+ * @property {number } version
55+ * @property {Localizable } description
56+ * @property {Localizable } consent
57+ * @property {Array<Permission> } permissions - Like Pryv permission request
58+ * @property {Object } app
59+ * @property {String } app.id
60+ * @property {String } app.url
61+ * @property {Object } app.data - to be finalized
62+ */
63+
64+ /**
65+ * @typedef {StatusData }
66+ * @property {RequestContent } requestContent
67+ */
68+
69+ /** @type {StatusData } */
70+ get statusData ( ) {
71+ if ( this . #cache. status == null ) throw new Error ( 'Init Collector first' ) ;
72+ return this . #cache. status . content . data ;
73+ }
74+
5275 /**
5376 * Fetch online data
5477 */
5578 async init ( ) {
5679 await this . checkStreamStructure ( ) ;
57- await this . getStatus ( ) ;
80+ await this . # getStatus( ) ;
5881 }
5982
6083 /**
@@ -69,12 +92,12 @@ class Collector {
6992 * @param {boolean } forceRefresh - if true, forces fetching the status from the server
7093 * @returns {StatusEvent }
7194 */
72- async getStatus ( forceRefresh = false ) {
95+ async # getStatus ( forceRefresh = false ) {
7396 if ( ! forceRefresh && this . #cache. status ) return this . #cache. status ;
7497 const params = { types : [ 'status/collector-v1' ] , limit : 1 , streams : [ this . streamIdFor ( Collector . STREAMID_SUFFIXES . internal ) ] } ;
7598 const statusEvents = await this . appManaging . connection . apiOne ( 'events.get' , params , 'events' ) ;
7699 if ( statusEvents . length === 0 ) { // non exsitent set "draft" status
77- return this . setStatus ( Collector . STATUSES . draft , { } ) ;
100+ return this . # setStatus( Collector . STATUSES . draft ) ;
78101 }
79102 this . #cache. status = statusEvents [ 0 ] ;
80103 return this . #cache. status ;
@@ -83,11 +106,15 @@ class Collector {
83106 /**
84107 * Change the status
85108 * @param {string } status one of of 'draft', 'active', 'deactivated'
86- * @param {object } data - custom data
109+ * @param {object } [ data] - if not set reuuse current data or { requestContent: {} }
87110 * @returns {StatusEvent }
88111 */
89- async setStatus ( status , data ) {
112+ async # setStatus ( status , data ) {
90113 if ( ! Collector . STATUSES [ status ] ) throw new HDSLibError ( 'Unkown status key' , { status, data } ) ;
114+ if ( ! data ) {
115+ data = ( ! this . #cache. status ) ? { requestContent : { } } : this . statusData ;
116+ }
117+
91118 const event = {
92119 type : 'status/collector-v1' ,
93120 streamIds : [ this . streamIdFor ( Collector . STREAMID_SUFFIXES . internal ) ] ,
@@ -101,13 +128,29 @@ class Collector {
101128 return this . #cache. status ;
102129 }
103130
131+ async save ( ) {
132+ if ( this . statusCode !== Collector . STATUSES . draft ) throw new Error ( `Cannot save when status = "${ this . statusCode } ".` ) ;
133+ return await this . #setStatus( Collector . STATUSES . draft ) ;
134+ }
135+
136+ async publish ( ) {
137+ const publicEventData = {
138+ type : 'request/collector-v1' ,
139+ streamIds : [ this . streamIdFor ( Collector . STREAMID_SUFFIXES . public ) ] ,
140+ content : this . statusData . requestContent
141+ } ;
142+ await this . appManaging . connection . apiOne ( 'events.create' , publicEventData , 'event' ) ;
143+ return await this . #setStatus( Collector . STATUSES . active ) ;
144+ }
145+
104146 /**
105147 * Create a "pending" invite to be sent to an app using AppSharingAccount
106148 * @param {string } name a default display name for this request
107149 * @param {Object } [options]
108150 * @param {Object } [options.customData] any data to be used by the client app
109151 */
110152 async createInvite ( name , options = { } ) {
153+ if ( this . statusCode !== Collector . STATUSES . active ) throw new Error ( `Collector must be in "active" state error to create invite, current: ${ this . statusCode } ` ) ;
111154 const eventParams = {
112155 type : 'invite/collector-v1' ,
113156 streamIds : [ this . streamIdFor ( Collector . STREAMID_SUFFIXES . pending ) ] ,
@@ -128,6 +171,7 @@ class Collector {
128171 * Get sharing api endpoint
129172 */
130173 async sharingApiEndpoint ( ) {
174+ if ( this . statusCode !== Collector . STATUSES . active ) throw new Error ( `Collector must be in "active" state error to get sharing link, current: ${ this . statusCode } ` ) ;
131175 if ( this . #cache. sharingApiEndpoint ) return this . #cache. sharingApiEndpoint ;
132176 // check if sharing present
133177 const sharedAccessId = 'a-' + this . streamId ;
@@ -152,6 +196,7 @@ class Collector {
152196 ] ;
153197 const clientData = {
154198 hdsCollector : {
199+ version : 0 ,
155200 public : {
156201 streamId : this . streamIdFor ( Collector . STREAMID_SUFFIXES . public )
157202 } ,
0 commit comments