@@ -20,7 +20,9 @@ const organizationsCacheExpiration = 24 * 60 * 60 * 1000; // 1 day
20
20
export class OrganizationService implements Disposable {
21
21
private _disposable : Disposable ;
22
22
private _organizations : Organization [ ] | null | undefined ;
23
- private _organizationSettings : Map < Organization [ 'id' ] , OrganizationSettings > | undefined ;
23
+ private _organizationSettings :
24
+ | Map < Organization [ 'id' ] , { data : OrganizationSettings ; lastValidatedDate : Date } >
25
+ | undefined ;
24
26
private _organizationMembers : Map < Organization [ 'id' ] , OrganizationMember [ ] > | undefined ;
25
27
26
28
constructor (
@@ -126,11 +128,12 @@ export class OrganizationService implements Disposable {
126
128
} ) ;
127
129
}
128
130
129
- private onSubscriptionChanged ( e : SubscriptionChangeEvent ) : void {
131
+ private async onSubscriptionChanged ( e : SubscriptionChangeEvent ) : Promise < void > {
130
132
if ( e . current ?. account ?. id == null ) {
131
133
this . updateOrganizations ( undefined ) ;
132
134
}
133
- void this . updateOrganizationPermissions ( e . current ?. activeOrganization ?. id ) ;
135
+ await this . clearAllStoredOrganizationsSettings ( ) ;
136
+ await this . updateOrganizationPermissions ( e . current ?. activeOrganization ?. id ) ;
134
137
}
135
138
136
139
private updateOrganizations ( organizations : Organization [ ] | null | undefined ) : void {
@@ -217,7 +220,23 @@ export class OrganizationService implements Disposable {
217
220
const id = orgId ?? ( await this . getActiveOrganizationId ( ) ) ;
218
221
if ( id == null ) return undefined ;
219
222
223
+ if ( ! options ?. force && ! this . _organizationSettings ?. has ( id ) ) {
224
+ const cachedOrg = this . getStoredOrganizationSettings ( id ) ;
225
+ if ( cachedOrg ) {
226
+ this . _organizationSettings ??= new Map ( ) ;
227
+ this . _organizationSettings . set ( id , cachedOrg ) ;
228
+ }
229
+ }
230
+
231
+ if ( this . _organizationSettings ?. has ( id ) ) {
232
+ const org = this . _organizationSettings . get ( id ) ;
233
+ if ( org && Date . now ( ) - org . lastValidatedDate . getTime ( ) > organizationsCacheExpiration ) {
234
+ this . _organizationSettings . delete ( id ) ;
235
+ }
236
+ }
237
+
220
238
if ( ! this . _organizationSettings ?. has ( id ) || options ?. force === true ) {
239
+ await this . deleteStoredOrganizationSettings ( id ) ;
221
240
const rsp = await this . connection . fetchGkApi (
222
241
`v1/organizations/settings` ,
223
242
{ method : 'GET' } ,
@@ -245,9 +264,43 @@ export class OrganizationService implements Disposable {
245
264
if ( this . _organizationSettings == null ) {
246
265
this . _organizationSettings = new Map ( ) ;
247
266
}
248
- this . _organizationSettings . set ( id , organizationResponse . data ) ;
267
+ this . _organizationSettings . set ( id , { data : organizationResponse . data , lastValidatedDate : new Date ( ) } ) ;
268
+ await this . storeOrganizationSettings ( id , organizationResponse . data , new Date ( ) ) ;
249
269
}
250
- return this . _organizationSettings . get ( id ) ;
270
+ return this . _organizationSettings . get ( id ) ?. data ;
271
+ }
272
+
273
+ private async clearAllStoredOrganizationsSettings ( ) : Promise < void > {
274
+ return this . container . storage . deleteWithPrefix ( `plus:organization:` ) ;
275
+ }
276
+
277
+ private async deleteStoredOrganizationSettings ( id : string ) : Promise < void > {
278
+ return this . container . storage . delete ( `plus:organization:${ id } :settings` ) ;
279
+ }
280
+
281
+ private getStoredOrganizationSettings (
282
+ id : string ,
283
+ ) : { data : OrganizationSettings ; lastValidatedDate : Date } | undefined {
284
+ const result = this . container . storage . get ( `plus:organization:${ id } :settings` ) ;
285
+ if ( ! result ?. data ) return undefined ;
286
+
287
+ const { lastValidatedAt, ...organizationSettings } = result . data ;
288
+
289
+ return {
290
+ data : organizationSettings ,
291
+ lastValidatedDate : new Date ( lastValidatedAt ) ,
292
+ } ;
293
+ }
294
+
295
+ private async storeOrganizationSettings (
296
+ id : string ,
297
+ settings : OrganizationSettings ,
298
+ lastValidatedDate : Date ,
299
+ ) : Promise < void > {
300
+ return this . container . storage . store ( `plus:organization:${ id } :settings` , {
301
+ v : 1 ,
302
+ data : { ...settings , lastValidatedAt : lastValidatedDate . getTime ( ) } ,
303
+ } ) ;
251
304
}
252
305
}
253
306
0 commit comments