-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1379 from liveblog/hg/LBSD-2785-implement-feature…
…s-service [LBSD-2785] - Implement features service to check for subscription limits
- Loading branch information
Showing
27 changed files
with
2,879 additions
and
2,296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
interface ISettings { | ||
features: { [key: string]: boolean }; | ||
limits: { [key: string]: number }; | ||
isNetworkSubscription: boolean; | ||
} | ||
|
||
class FeaturesService { | ||
api: any; | ||
private settings: ISettings = null; | ||
|
||
constructor(api) { | ||
this.api = api; | ||
} | ||
|
||
async initialize(): Promise<void> { | ||
try { | ||
await this.loadSettings(); | ||
} catch (error) { | ||
console.warn('There has been an error fetching instance settings'); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the settings from cache or loads them if not already loaded. | ||
* @returns A promise that resolves to the settings object. | ||
*/ | ||
private async loadSettings(): Promise<void> { | ||
this.settings = await this.api.get('/instance_settings/current'); | ||
} | ||
|
||
isNetworkSubscription = () => this.settings.isNetworkSubscription; | ||
|
||
/** | ||
* Determines if a specific feature is enabled based on the current settings. | ||
* If the "network" subscription is active, all features are considered enabled. | ||
*/ | ||
isEnabled = (featureName: string) => { | ||
const settings = this.settings; | ||
|
||
if (settings.isNetworkSubscription) { | ||
return true; | ||
} | ||
|
||
return settings?.features[featureName] ?? false; | ||
} | ||
|
||
/** | ||
* Determines if the limit for a specific feature has been reached. | ||
* If the "network" subscription plan is active, there are no limits. | ||
*/ | ||
isLimitReached = (featureName: string, currentUsage: number) => { | ||
const settings = this.settings; | ||
|
||
if (settings.isNetworkSubscription) { | ||
return false; | ||
} | ||
|
||
const subscriptionLimit = settings?.limits[featureName] ?? 0; | ||
|
||
return currentUsage >= subscriptionLimit; | ||
} | ||
} | ||
|
||
angular.module('liveblog.features', []) | ||
.service('featuresService', ['api', (api) => { | ||
const featuresService = new FeaturesService(api); | ||
|
||
featuresService.initialize(); | ||
return featuresService; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.