mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.1 KiB
100 lines
3.1 KiB
/*
|
|
* Squidex Headless CMS
|
|
*
|
|
* @license
|
|
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
|
|
*/
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { AnalyticsService, ApiUrlConfig, HTTP, mapVersioned, pretifyError, Version, Versioned } from '@app/framework';
|
|
import { Observable } from 'rxjs';
|
|
import { tap } from 'rxjs/operators';
|
|
|
|
export type PlansDto = Versioned<{
|
|
readonly currentPlanId: string,
|
|
readonly planOwner: string,
|
|
readonly hasPortal: boolean,
|
|
readonly plans: ReadonlyArray<PlanDto>
|
|
}>;
|
|
|
|
export class PlanDto {
|
|
constructor(
|
|
public readonly id: string,
|
|
public readonly name: string,
|
|
public readonly costs: string,
|
|
public readonly confirmText: string | undefined,
|
|
public readonly yearlyId: string,
|
|
public readonly yearlyCosts: string,
|
|
public readonly yearlyConfirmText: string | undefined,
|
|
public readonly maxApiBytes: number,
|
|
public readonly maxApiCalls: number,
|
|
public readonly maxAssetSize: number,
|
|
public readonly maxContributors: number
|
|
) {
|
|
}
|
|
}
|
|
|
|
export interface PlanChangedDto {
|
|
readonly redirectUri?: string;
|
|
}
|
|
|
|
export interface ChangePlanDto {
|
|
readonly planId: string;
|
|
}
|
|
|
|
@Injectable()
|
|
export class PlansService {
|
|
constructor(
|
|
private readonly http: HttpClient,
|
|
private readonly apiUrl: ApiUrlConfig,
|
|
private readonly analytics: AnalyticsService
|
|
) {
|
|
}
|
|
|
|
public getPlans(appName: string): Observable<PlansDto> {
|
|
const url = this.apiUrl.buildUrl(`api/apps/${appName}/plans`);
|
|
|
|
return HTTP.getVersioned(this.http, url).pipe(
|
|
mapVersioned(({ body }) => {
|
|
const items: any[] = body.plans;
|
|
|
|
const { hasPortal, currentPlanId, planOwner } = body;
|
|
|
|
const plans = {
|
|
currentPlanId,
|
|
planOwner,
|
|
plans: items.map(item =>
|
|
new PlanDto(
|
|
item.id,
|
|
item.name,
|
|
item.costs,
|
|
item.confirmText,
|
|
item.yearlyId,
|
|
item.yearlyCosts,
|
|
item.yearlyConfirmText,
|
|
item.maxApiBytes,
|
|
item.maxApiCalls,
|
|
item.maxAssetSize,
|
|
item.maxContributors)),
|
|
hasPortal
|
|
};
|
|
|
|
return plans;
|
|
}),
|
|
pretifyError('i18n:plans.loadFailed'));
|
|
}
|
|
|
|
public putPlan(appName: string, dto: ChangePlanDto, version: Version): Observable<Versioned<PlanChangedDto>> {
|
|
const url = this.apiUrl.buildUrl(`api/apps/${appName}/plan`);
|
|
|
|
return HTTP.putVersioned(this.http, url, dto, version).pipe(
|
|
mapVersioned(payload => {
|
|
return <PlanChangedDto>payload.body;
|
|
}),
|
|
tap(() => {
|
|
this.analytics.trackEvent('Plan', 'Changed', appName);
|
|
}),
|
|
pretifyError('i18n:plans.changeFailed'));
|
|
}
|
|
}
|