11import { FeatureId , VendorEntitlementsDto } from '../types' ;
22import { BundlesSource , ExpirationTime , FeatureSource , NO_EXPIRE , UNBUNDLED_SRC_ID } from './types' ;
33
4+ function ensureMapInMap < K , T extends Map < any , any > > ( map : Map < K , T > , mapKey : K ) : T {
5+ if ( ! map . has ( mapKey ) ) {
6+ map . set ( mapKey , new Map ( ) as T ) ;
7+ }
8+
9+ return map . get ( mapKey ) ! ;
10+ }
11+
12+ function ensureArrayInMap < K , T > ( map : Map < K , T [ ] > , mapKey : K ) : T [ ] {
13+ if ( ! map . has ( mapKey ) ) {
14+ map . set ( mapKey , [ ] ) ;
15+ }
16+
17+ return map . get ( mapKey ) ! ;
18+ }
19+
20+ function parseExpirationTime ( time ?: string | null ) : ExpirationTime {
21+ if ( time !== undefined && time !== null ) {
22+ return new Date ( time ) . getTime ( ) ;
23+ }
24+
25+ return NO_EXPIRE ;
26+ }
27+
428export class DtoToCacheSourcesMapper {
5- map ( dto : VendorEntitlementsDto ) : BundlesSource {
29+ static map ( dto : VendorEntitlementsDto ) : BundlesSource {
630 const {
731 data : { features, entitlements, featureBundles } ,
832 } = dto ;
@@ -56,15 +80,15 @@ export class DtoToCacheSourcesMapper {
5680 if ( bundle ) {
5781 if ( userId ) {
5882 // that's user-targeted entitlement
59- const tenantUserEntitlements = this . ensureMapInMap ( bundle . user_entitlements , tenantId ) ;
60- const usersEntitlements = this . ensureArrayInMap ( tenantUserEntitlements , userId ) ;
83+ const tenantUserEntitlements = ensureMapInMap ( bundle . user_entitlements , tenantId ) ;
84+ const usersEntitlements = ensureArrayInMap ( tenantUserEntitlements , userId ) ;
6185
62- usersEntitlements . push ( this . parseExpirationTime ( expirationDate ) ) ;
86+ usersEntitlements . push ( parseExpirationTime ( expirationDate ) ) ;
6387 } else {
6488 // that's tenant-targeted entitlement
65- const tenantEntitlements = this . ensureArrayInMap ( bundle . tenant_entitlements , tenantId ) ;
89+ const tenantEntitlements = ensureArrayInMap ( bundle . tenant_entitlements , tenantId ) ;
6690
67- tenantEntitlements . push ( this . parseExpirationTime ( expirationDate ) ) ;
91+ tenantEntitlements . push ( parseExpirationTime ( expirationDate ) ) ;
6892 }
6993 } else {
7094 // TODO: issue warning here!
@@ -87,28 +111,4 @@ export class DtoToCacheSourcesMapper {
87111
88112 return bundlesMap ;
89113 }
90-
91- private ensureMapInMap < K , T extends Map < any , any > > ( map : Map < K , T > , mapKey : K ) : T {
92- if ( ! map . has ( mapKey ) ) {
93- map . set ( mapKey , new Map ( ) as T ) ;
94- }
95-
96- return map . get ( mapKey ) ! ;
97- }
98-
99- private ensureArrayInMap < K , T > ( map : Map < K , T [ ] > , mapKey : K ) : T [ ] {
100- if ( ! map . has ( mapKey ) ) {
101- map . set ( mapKey , [ ] ) ;
102- }
103-
104- return map . get ( mapKey ) ! ;
105- }
106-
107- private parseExpirationTime ( time ?: string | null ) : ExpirationTime {
108- if ( time !== undefined && time !== null ) {
109- return new Date ( time ) . getTime ( ) ;
110- }
111-
112- return NO_EXPIRE ;
113- }
114114}
0 commit comments