mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-21 12:06:35 +08:00
feat(server): delay subscription after invitation accepted or approved (#11992)
This commit is contained in:
@@ -2,11 +2,12 @@ import { Module } from '@nestjs/common';
|
||||
|
||||
import { PermissionModule } from '../../core/permission';
|
||||
import { QuotaModule } from '../../core/quota';
|
||||
import { WorkspaceModule } from '../../core/workspaces';
|
||||
import { LicenseResolver } from './resolver';
|
||||
import { LicenseService } from './service';
|
||||
|
||||
@Module({
|
||||
imports: [QuotaModule, PermissionModule],
|
||||
imports: [QuotaModule, PermissionModule, WorkspaceModule],
|
||||
providers: [LicenseService, LicenseResolver],
|
||||
})
|
||||
export class LicenseModule {}
|
||||
|
||||
@@ -47,7 +47,10 @@ export class LicenseService {
|
||||
memberLimit: quantity,
|
||||
}
|
||||
);
|
||||
await this.models.workspaceUser.refresh(workspaceId, quantity);
|
||||
this.event.emit('workspace.members.allocateSeats', {
|
||||
workspaceId,
|
||||
quantity,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -188,7 +191,7 @@ export class LicenseService {
|
||||
|
||||
@OnEvent('workspace.members.updated')
|
||||
async updateTeamSeats(payload: Events['workspace.members.updated']) {
|
||||
const { workspaceId, count } = payload;
|
||||
const { workspaceId } = payload;
|
||||
|
||||
const license = await this.db.installedLicense.findUnique({
|
||||
where: {
|
||||
@@ -200,6 +203,7 @@ export class LicenseService {
|
||||
return;
|
||||
}
|
||||
|
||||
const count = await this.models.workspaceUser.chargedCount(workspaceId);
|
||||
await this.fetchAffinePro(`/api/team/licenses/${license.key}/seats`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
|
||||
@@ -274,18 +274,21 @@ export class WorkspaceSubscriptionManager extends SubscriptionManager {
|
||||
}
|
||||
|
||||
@OnEvent('workspace.members.updated')
|
||||
async onMembersUpdated({
|
||||
workspaceId,
|
||||
count,
|
||||
}: Events['workspace.members.updated']) {
|
||||
async onMembersUpdated({ workspaceId }: Events['workspace.members.updated']) {
|
||||
const count = await this.models.workspaceUser.chargedCount(workspaceId);
|
||||
const subscription = await this.getSubscription({
|
||||
plan: SubscriptionPlan.Team,
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
if (!subscription || !subscription.stripeSubscriptionId) {
|
||||
if (
|
||||
!subscription ||
|
||||
!subscription.stripeSubscriptionId ||
|
||||
count === subscription.quantity
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const stripeSubscription = await this.stripe.subscriptions.retrieve(
|
||||
subscription.stripeSubscriptionId
|
||||
);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { OnEvent } from '../../base';
|
||||
import { WorkspaceService } from '../../core/workspaces/resolvers';
|
||||
import { EventBus, OnEvent } from '../../base';
|
||||
import { WorkspaceService } from '../../core/workspaces';
|
||||
import { Models } from '../../models';
|
||||
import { SubscriptionPlan } from './types';
|
||||
|
||||
@@ -9,7 +9,8 @@ import { SubscriptionPlan } from './types';
|
||||
export class QuotaOverride {
|
||||
constructor(
|
||||
private readonly workspace: WorkspaceService,
|
||||
private readonly models: Models
|
||||
private readonly models: Models,
|
||||
private readonly event: EventBus
|
||||
) {}
|
||||
|
||||
@OnEvent('workspace.subscription.activated')
|
||||
@@ -30,7 +31,10 @@ export class QuotaOverride {
|
||||
memberLimit: quantity,
|
||||
}
|
||||
);
|
||||
await this.models.workspaceUser.refresh(workspaceId, quantity);
|
||||
this.event.emit('workspace.members.allocateSeats', {
|
||||
workspaceId,
|
||||
quantity,
|
||||
});
|
||||
if (!isTeam) {
|
||||
// this event will triggered when subscription is activated or changed
|
||||
// we only send emails when the team workspace is activated
|
||||
|
||||
Reference in New Issue
Block a user