mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-20 03:26:47 +08:00
fix(core): workspace billing cannot be opened (#9091)
This commit is contained in:
@@ -14,7 +14,8 @@ import {
|
||||
import { EMPTY, map, mergeMap } from 'rxjs';
|
||||
|
||||
import { isBackendError, isNetworkError } from '../error';
|
||||
import type { InvoicesStore } from '../stores/invoices';
|
||||
import type { WorkspaceServerService } from '../services/workspace-server';
|
||||
import { InvoicesStore } from '../stores/invoices';
|
||||
|
||||
export type Invoice = NonNullable<
|
||||
InvoicesQuery['currentUser']
|
||||
@@ -22,12 +23,14 @@ export type Invoice = NonNullable<
|
||||
|
||||
export class WorkspaceInvoices extends Entity {
|
||||
constructor(
|
||||
private readonly store: InvoicesStore,
|
||||
private readonly workspaceService: WorkspaceService
|
||||
private readonly workspaceService: WorkspaceService,
|
||||
private readonly workspaceServerService: WorkspaceServerService
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
store = this.workspaceServerService.server?.scope.get(InvoicesStore);
|
||||
|
||||
pageNum$ = new LiveData(0);
|
||||
invoiceCount$ = new LiveData<number | undefined>(undefined);
|
||||
pageInvoices$ = new LiveData<Invoice[] | undefined>(undefined);
|
||||
@@ -43,6 +46,9 @@ export class WorkspaceInvoices extends Entity {
|
||||
(a, b) => a === b,
|
||||
pageNum => {
|
||||
return fromPromise(async signal => {
|
||||
if (!this.store) {
|
||||
throw new Error('No invoices store');
|
||||
}
|
||||
return this.store.fetchWorkspaceInvoices(
|
||||
pageNum * this.PAGE_SIZE,
|
||||
this.PAGE_SIZE,
|
||||
|
||||
@@ -15,8 +15,8 @@ import {
|
||||
import { EMPTY, mergeMap } from 'rxjs';
|
||||
|
||||
import { isBackendError, isNetworkError } from '../error';
|
||||
import type { ServerService } from '../services/server';
|
||||
import type { SubscriptionStore } from '../stores/subscription';
|
||||
import type { WorkspaceServerService } from '../services/workspace-server';
|
||||
import { SubscriptionStore } from '../stores/subscription';
|
||||
|
||||
export type SubscriptionType = NonNullable<
|
||||
SubscriptionQuery['currentUser']
|
||||
@@ -33,18 +33,25 @@ export class WorkspaceSubscription extends Entity {
|
||||
|
||||
constructor(
|
||||
private readonly workspaceService: WorkspaceService,
|
||||
private readonly serverService: ServerService,
|
||||
private readonly store: SubscriptionStore
|
||||
private readonly workspaceServerService: WorkspaceServerService
|
||||
) {
|
||||
super();
|
||||
}
|
||||
server = this.workspaceServerService.server;
|
||||
store = this.workspaceServerService.server?.scope.get(SubscriptionStore);
|
||||
|
||||
async resumeSubscription(idempotencyKey: string, plan?: SubscriptionPlan) {
|
||||
if (!this.store) {
|
||||
throw new Error('Subscription store not available');
|
||||
}
|
||||
await this.store.mutateResumeSubscription(idempotencyKey, plan);
|
||||
await this.waitForRevalidation();
|
||||
}
|
||||
|
||||
async cancelSubscription(idempotencyKey: string, plan?: SubscriptionPlan) {
|
||||
if (!this.store) {
|
||||
throw new Error('Subscription store not available');
|
||||
}
|
||||
await this.store.mutateCancelSubscription(idempotencyKey, plan);
|
||||
await this.waitForRevalidation();
|
||||
}
|
||||
@@ -54,6 +61,9 @@ export class WorkspaceSubscription extends Entity {
|
||||
recurring: SubscriptionRecurring,
|
||||
plan?: SubscriptionPlan
|
||||
) {
|
||||
if (!this.store) {
|
||||
throw new Error('Subscription store not available');
|
||||
}
|
||||
await this.store.setSubscriptionRecurring(idempotencyKey, recurring, plan);
|
||||
await this.waitForRevalidation();
|
||||
}
|
||||
@@ -70,11 +80,11 @@ export class WorkspaceSubscription extends Entity {
|
||||
exhaustMapWithTrailing(() => {
|
||||
return fromPromise(async signal => {
|
||||
const currentWorkspaceId = this.workspaceService.workspace.id;
|
||||
if (!currentWorkspaceId) {
|
||||
if (!currentWorkspaceId || !this.server) {
|
||||
return undefined; // no subscription if no user
|
||||
}
|
||||
const serverConfig =
|
||||
await this.serverService.server.features$.waitForNonNull(signal);
|
||||
|
||||
const serverConfig = await this.server.features$.waitForNonNull(signal);
|
||||
|
||||
if (!serverConfig.payment) {
|
||||
// No payment feature, no subscription
|
||||
@@ -83,6 +93,12 @@ export class WorkspaceSubscription extends Entity {
|
||||
subscription: null,
|
||||
};
|
||||
}
|
||||
if (!this.store) {
|
||||
return {
|
||||
workspaceId: currentWorkspaceId,
|
||||
subscription: null,
|
||||
};
|
||||
}
|
||||
const { workspaceId, subscription } =
|
||||
await this.store.fetchWorkspaceSubscriptions(
|
||||
currentWorkspaceId,
|
||||
@@ -101,7 +117,7 @@ export class WorkspaceSubscription extends Entity {
|
||||
when: isBackendError,
|
||||
}),
|
||||
mergeMap(data => {
|
||||
if (data && data.subscription && data.workspaceId) {
|
||||
if (data && data.subscription && data.workspaceId && this.store) {
|
||||
this.store.setCachedWorkspaceSubscription(
|
||||
data.workspaceId,
|
||||
data.subscription
|
||||
|
||||
@@ -149,16 +149,7 @@ export function configureCloudModule(framework: Framework) {
|
||||
.store(UserFeatureStore, [GraphQLService])
|
||||
.service(InvoicesService)
|
||||
.store(InvoicesStore, [GraphQLService])
|
||||
.entity(Invoices, [InvoicesStore])
|
||||
.scope(WorkspaceScope)
|
||||
.service(WorkspaceSubscriptionService, [SubscriptionStore])
|
||||
.entity(WorkspaceSubscription, [
|
||||
WorkspaceService,
|
||||
ServerService,
|
||||
SubscriptionStore,
|
||||
])
|
||||
.service(WorkspaceInvoicesService)
|
||||
.entity(WorkspaceInvoices, [InvoicesStore, WorkspaceService]);
|
||||
.entity(Invoices, [InvoicesStore]);
|
||||
|
||||
framework
|
||||
.scope(WorkspaceScope)
|
||||
@@ -167,4 +158,10 @@ export function configureCloudModule(framework: Framework) {
|
||||
.service(CloudDocMetaService)
|
||||
.entity(CloudDocMeta, [CloudDocMetaStore, DocService, GlobalCache])
|
||||
.store(CloudDocMetaStore, [WorkspaceServerService]);
|
||||
framework
|
||||
.scope(WorkspaceScope)
|
||||
.service(WorkspaceSubscriptionService, [WorkspaceServerService])
|
||||
.entity(WorkspaceSubscription, [WorkspaceService, WorkspaceServerService])
|
||||
.service(WorkspaceInvoicesService)
|
||||
.entity(WorkspaceInvoices, [WorkspaceService, WorkspaceServerService]);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
import { type CreateCheckoutSessionInput } from '@affine/graphql';
|
||||
import { Service } from '@toeverything/infra';
|
||||
|
||||
import { SubscriptionPrices } from '../entities/subscription-prices';
|
||||
import { WorkspaceSubscription } from '../entities/workspace-subscription';
|
||||
import type { SubscriptionStore } from '../stores/subscription';
|
||||
import { SubscriptionStore } from '../stores/subscription';
|
||||
import type { WorkspaceServerService } from './workspace-server';
|
||||
|
||||
export class WorkspaceSubscriptionService extends Service {
|
||||
subscription = this.framework.createEntity(WorkspaceSubscription);
|
||||
prices = this.framework.createEntity(SubscriptionPrices);
|
||||
|
||||
constructor(private readonly store: SubscriptionStore) {
|
||||
constructor(private readonly workspaceServerService: WorkspaceServerService) {
|
||||
super();
|
||||
}
|
||||
store = this.workspaceServerService.server?.scope.get(SubscriptionStore);
|
||||
|
||||
async createCheckoutSession(input: CreateCheckoutSessionInput) {
|
||||
if (!this.store) {
|
||||
throw new Error('No subscription store');
|
||||
}
|
||||
return await this.store.createCheckoutSession(input);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user