mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-14 08:36:22 +08:00
103 lines
2.4 KiB
TypeScript
103 lines
2.4 KiB
TypeScript
import {
|
|
Args,
|
|
Field,
|
|
Int,
|
|
Mutation,
|
|
ObjectType,
|
|
Parent,
|
|
ResolveField,
|
|
Resolver,
|
|
} from '@nestjs/graphql';
|
|
|
|
import { UseNamedGuard } from '../../base';
|
|
import { CurrentUser } from '../../core/auth';
|
|
import { AccessController } from '../../core/permission';
|
|
import { WorkspaceType } from '../../core/workspaces';
|
|
import { SubscriptionRecurring } from '../payment/types';
|
|
import { LicenseService } from './service';
|
|
|
|
@ObjectType()
|
|
export class License {
|
|
@Field(() => Int)
|
|
quantity!: number;
|
|
|
|
@Field(() => SubscriptionRecurring)
|
|
recurring!: string;
|
|
|
|
@Field(() => Date)
|
|
installedAt!: Date;
|
|
|
|
@Field(() => Date)
|
|
validatedAt!: Date;
|
|
|
|
@Field(() => Date, { nullable: true })
|
|
expiredAt!: Date | null;
|
|
}
|
|
|
|
@UseNamedGuard('selfhost')
|
|
@Resolver(() => WorkspaceType)
|
|
export class LicenseResolver {
|
|
constructor(
|
|
private readonly service: LicenseService,
|
|
private readonly ac: AccessController
|
|
) {}
|
|
|
|
@ResolveField(() => License, {
|
|
complexity: 2,
|
|
description: 'The selfhost license of the workspace',
|
|
nullable: true,
|
|
})
|
|
async license(
|
|
@CurrentUser() user: CurrentUser,
|
|
@Parent() workspace: WorkspaceType
|
|
): Promise<License | null> {
|
|
await this.ac
|
|
.user(user.id)
|
|
.workspace(workspace.id)
|
|
.assert('Workspace.Payment.Manage');
|
|
return this.service.getLicense(workspace.id);
|
|
}
|
|
|
|
@Mutation(() => License)
|
|
async activateLicense(
|
|
@CurrentUser() user: CurrentUser,
|
|
@Args('workspaceId') workspaceId: string,
|
|
@Args('license') license: string
|
|
) {
|
|
await this.ac
|
|
.user(user.id)
|
|
.workspace(workspaceId)
|
|
.assert('Workspace.Payment.Manage');
|
|
|
|
return this.service.activateTeamLicense(workspaceId, license);
|
|
}
|
|
|
|
@Mutation(() => Boolean)
|
|
async deactivateLicense(
|
|
@CurrentUser() user: CurrentUser,
|
|
@Args('workspaceId') workspaceId: string
|
|
) {
|
|
await this.ac
|
|
.user(user.id)
|
|
.workspace(workspaceId)
|
|
.assert('Workspace.Payment.Manage');
|
|
|
|
return this.service.deactivateTeamLicense(workspaceId);
|
|
}
|
|
|
|
@Mutation(() => String)
|
|
async createSelfhostWorkspaceCustomerPortal(
|
|
@CurrentUser() user: CurrentUser,
|
|
@Args('workspaceId') workspaceId: string
|
|
) {
|
|
await this.ac
|
|
.user(user.id)
|
|
.workspace(workspaceId)
|
|
.assert('Workspace.Payment.Manage');
|
|
|
|
const { url } = await this.service.createCustomerPortal(workspaceId);
|
|
|
|
return url;
|
|
}
|
|
}
|