5748 Create contacts for emails sent and received by email aliases (#5855)
Closes #5748 - Create feature flag - Add scope `https://www.googleapis.com/auth/profile.emails.read` when connecting an account - Get email aliases with google people API, store them in connectedAccount and refresh them before each message-import - Update the contact creation logic accordingly - Refactor --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -9,14 +9,14 @@ import {
|
||||
|
||||
import { Response } from 'express';
|
||||
|
||||
import { GoogleAPIsProviderEnabledGuard } from 'src/engine/core-modules/auth/guards/google-apis-provider-enabled.guard';
|
||||
import { GoogleAPIsOauthGuard } from 'src/engine/core-modules/auth/guards/google-apis-oauth.guard';
|
||||
import { GoogleAPIsRequest } from 'src/engine/core-modules/auth/strategies/google-apis.auth.strategy';
|
||||
import { GoogleAPIsOauthRequestCodeGuard } from 'src/engine/core-modules/auth/guards/google-apis-oauth-request-code.guard';
|
||||
import { GoogleAPIsService } from 'src/engine/core-modules/auth/services/google-apis.service';
|
||||
import { TokenService } from 'src/engine/core-modules/auth/services/token.service';
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
import { OnboardingService } from 'src/engine/core-modules/onboarding/onboarding.service';
|
||||
import { LoadServiceWithWorkspaceContext } from 'src/engine/twenty-orm/context/load-service-with-workspace.context';
|
||||
import { GoogleAPIsOauthExchangeCodeForTokenGuard } from 'src/engine/core-modules/auth/guards/google-apis-oauth-exchange-code-for-token.guard';
|
||||
import { GoogleAPIsRequest } from 'src/engine/core-modules/auth/types/google-api-request.type';
|
||||
|
||||
@Controller('auth/google-apis')
|
||||
export class GoogleAPIsAuthController {
|
||||
@ -29,14 +29,14 @@ export class GoogleAPIsAuthController {
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
@UseGuards(GoogleAPIsProviderEnabledGuard, GoogleAPIsOauthGuard)
|
||||
@UseGuards(GoogleAPIsOauthRequestCodeGuard)
|
||||
async googleAuth() {
|
||||
// As this method is protected by Google Auth guard, it will trigger Google SSO flow
|
||||
return;
|
||||
}
|
||||
|
||||
@Get('get-access-token')
|
||||
@UseGuards(GoogleAPIsProviderEnabledGuard, GoogleAPIsOauthGuard)
|
||||
@UseGuards(GoogleAPIsOauthExchangeCodeForTokenGuard)
|
||||
async googleAuthGetAccessToken(
|
||||
@Req() req: GoogleAPIsRequest,
|
||||
@Res() res: Response,
|
||||
@ -44,7 +44,7 @@ export class GoogleAPIsAuthController {
|
||||
const { user } = req;
|
||||
|
||||
const {
|
||||
email,
|
||||
emails,
|
||||
accessToken,
|
||||
refreshToken,
|
||||
transientToken,
|
||||
@ -68,6 +68,8 @@ export class GoogleAPIsAuthController {
|
||||
throw new Error('Workspace not found');
|
||||
}
|
||||
|
||||
const handle = emails[0].value;
|
||||
|
||||
const googleAPIsServiceInstance =
|
||||
await this.loadServiceWithWorkspaceContext.load(
|
||||
this.googleAPIsService,
|
||||
@ -75,7 +77,7 @@ export class GoogleAPIsAuthController {
|
||||
);
|
||||
|
||||
await googleAPIsServiceInstance.refreshGoogleRefreshToken({
|
||||
handle: email,
|
||||
handle,
|
||||
workspaceMemberId: workspaceMemberId,
|
||||
workspaceId: workspaceId,
|
||||
accessToken,
|
||||
|
||||
@ -0,0 +1,74 @@
|
||||
import {
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { TokenService } from 'src/engine/core-modules/auth/services/token.service';
|
||||
import {
|
||||
GoogleAPIScopeConfig,
|
||||
GoogleAPIsOauthExchangeCodeForTokenStrategy,
|
||||
} from 'src/engine/core-modules/auth/strategies/google-apis-oauth-exchange-code-for-token.auth.strategy';
|
||||
import {
|
||||
FeatureFlagEntity,
|
||||
FeatureFlagKeys,
|
||||
} from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
import { setRequestExtraParams } from 'src/engine/core-modules/auth/utils/google-apis-set-request-extra-params.util';
|
||||
|
||||
@Injectable()
|
||||
export class GoogleAPIsOauthExchangeCodeForTokenGuard extends AuthGuard(
|
||||
'google-apis',
|
||||
) {
|
||||
constructor(
|
||||
private readonly environmentService: EnvironmentService,
|
||||
private readonly tokenService: TokenService,
|
||||
@InjectRepository(FeatureFlagEntity, 'core')
|
||||
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async canActivate(context: ExecutionContext) {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const state = JSON.parse(request.query.state);
|
||||
|
||||
if (
|
||||
!this.environmentService.get('MESSAGING_PROVIDER_GMAIL_ENABLED') &&
|
||||
!this.environmentService.get('CALENDAR_PROVIDER_GOOGLE_ENABLED')
|
||||
) {
|
||||
throw new NotFoundException('Google apis auth is not enabled');
|
||||
}
|
||||
|
||||
const { workspaceId } = await this.tokenService.verifyTransientToken(
|
||||
state.transientToken,
|
||||
);
|
||||
|
||||
const scopeConfig: GoogleAPIScopeConfig = {
|
||||
isMessagingAliasFetchingEnabled:
|
||||
!!(await this.featureFlagRepository.findOneBy({
|
||||
workspaceId,
|
||||
key: FeatureFlagKeys.IsMessagingAliasFetchingEnabled,
|
||||
value: true,
|
||||
})),
|
||||
};
|
||||
|
||||
new GoogleAPIsOauthExchangeCodeForTokenStrategy(
|
||||
this.environmentService,
|
||||
scopeConfig,
|
||||
);
|
||||
|
||||
setRequestExtraParams(request, {
|
||||
transientToken: state.transientToken,
|
||||
redirectLocation: state.redirectLocation,
|
||||
calendarVisibility: state.calendarVisibility,
|
||||
messageVisibility: state.messageVisibility,
|
||||
});
|
||||
|
||||
return (await super.canActivate(context)) as boolean;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,72 @@
|
||||
import {
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { TokenService } from 'src/engine/core-modules/auth/services/token.service';
|
||||
import { GoogleAPIScopeConfig } from 'src/engine/core-modules/auth/strategies/google-apis-oauth-exchange-code-for-token.auth.strategy';
|
||||
import {
|
||||
FeatureFlagEntity,
|
||||
FeatureFlagKeys,
|
||||
} from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
import { GoogleAPIsOauthRequestCodeStrategy } from 'src/engine/core-modules/auth/strategies/google-apis-oauth-request-code.auth.strategy';
|
||||
import { setRequestExtraParams } from 'src/engine/core-modules/auth/utils/google-apis-set-request-extra-params.util';
|
||||
|
||||
@Injectable()
|
||||
export class GoogleAPIsOauthRequestCodeGuard extends AuthGuard('google-apis') {
|
||||
constructor(
|
||||
private readonly environmentService: EnvironmentService,
|
||||
private readonly tokenService: TokenService,
|
||||
@InjectRepository(FeatureFlagEntity, 'core')
|
||||
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
|
||||
) {
|
||||
super({
|
||||
prompt: 'select_account',
|
||||
});
|
||||
}
|
||||
|
||||
async canActivate(context: ExecutionContext) {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
|
||||
if (
|
||||
!this.environmentService.get('MESSAGING_PROVIDER_GMAIL_ENABLED') &&
|
||||
!this.environmentService.get('CALENDAR_PROVIDER_GOOGLE_ENABLED')
|
||||
) {
|
||||
throw new NotFoundException('Google apis auth is not enabled');
|
||||
}
|
||||
|
||||
const { workspaceId } = await this.tokenService.verifyTransientToken(
|
||||
request.query.transientToken,
|
||||
);
|
||||
|
||||
const scopeConfig: GoogleAPIScopeConfig = {
|
||||
isMessagingAliasFetchingEnabled:
|
||||
!!(await this.featureFlagRepository.findOneBy({
|
||||
workspaceId,
|
||||
key: FeatureFlagKeys.IsMessagingAliasFetchingEnabled,
|
||||
value: true,
|
||||
})),
|
||||
};
|
||||
|
||||
new GoogleAPIsOauthRequestCodeStrategy(
|
||||
this.environmentService,
|
||||
scopeConfig,
|
||||
);
|
||||
setRequestExtraParams(request, {
|
||||
transientToken: request.query.transientToken,
|
||||
redirectLocation: request.query.redirectLocation,
|
||||
calendarVisibility: request.query.calendarVisibility,
|
||||
messageVisibility: request.query.messageVisibility,
|
||||
});
|
||||
|
||||
const activate = (await super.canActivate(context)) as boolean;
|
||||
|
||||
return activate;
|
||||
}
|
||||
}
|
||||
@ -1,39 +0,0 @@
|
||||
import { ExecutionContext, Injectable } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Injectable()
|
||||
export class GoogleAPIsOauthGuard extends AuthGuard('google-apis') {
|
||||
constructor() {
|
||||
super({
|
||||
prompt: 'select_account',
|
||||
});
|
||||
}
|
||||
|
||||
async canActivate(context: ExecutionContext) {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const transientToken = request.query.transientToken;
|
||||
const redirectLocation = request.query.redirectLocation;
|
||||
const calendarVisibility = request.query.calendarVisibility;
|
||||
const messageVisibility = request.query.messageVisibility;
|
||||
|
||||
if (transientToken && typeof transientToken === 'string') {
|
||||
request.params.transientToken = transientToken;
|
||||
}
|
||||
|
||||
if (redirectLocation && typeof redirectLocation === 'string') {
|
||||
request.params.redirectLocation = redirectLocation;
|
||||
}
|
||||
|
||||
if (calendarVisibility && typeof calendarVisibility === 'string') {
|
||||
request.params.calendarVisibility = calendarVisibility;
|
||||
}
|
||||
|
||||
if (messageVisibility && typeof messageVisibility === 'string') {
|
||||
request.params.messageVisibility = messageVisibility;
|
||||
}
|
||||
|
||||
const activate = (await super.canActivate(context)) as boolean;
|
||||
|
||||
return activate;
|
||||
}
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
import { Injectable, CanActivate, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { TokenService } from 'src/engine/core-modules/auth/services/token.service';
|
||||
import {
|
||||
GoogleAPIScopeConfig,
|
||||
GoogleAPIsStrategy,
|
||||
} from 'src/engine/core-modules/auth/strategies/google-apis.auth.strategy';
|
||||
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
|
||||
@Injectable()
|
||||
export class GoogleAPIsProviderEnabledGuard implements CanActivate {
|
||||
constructor(
|
||||
private readonly environmentService: EnvironmentService,
|
||||
private readonly tokenService: TokenService,
|
||||
@InjectRepository(FeatureFlagEntity, 'core')
|
||||
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
|
||||
) {}
|
||||
|
||||
async canActivate(): Promise<boolean> {
|
||||
if (
|
||||
!this.environmentService.get('MESSAGING_PROVIDER_GMAIL_ENABLED') &&
|
||||
!this.environmentService.get('CALENDAR_PROVIDER_GOOGLE_ENABLED')
|
||||
) {
|
||||
throw new NotFoundException('Google apis auth is not enabled');
|
||||
}
|
||||
|
||||
const scopeConfig: GoogleAPIScopeConfig = {
|
||||
isCalendarEnabled: !!this.environmentService.get(
|
||||
'MESSAGING_PROVIDER_GMAIL_ENABLED',
|
||||
),
|
||||
};
|
||||
|
||||
new GoogleAPIsStrategy(this.environmentService, scopeConfig);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { Strategy } from 'passport-google-oauth20';
|
||||
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
|
||||
export type GoogleAPIScopeConfig = {
|
||||
isCalendarEnabled?: boolean;
|
||||
isMessagingAliasFetchingEnabled?: boolean;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class GoogleAPIsOauthCommonStrategy extends PassportStrategy(
|
||||
Strategy,
|
||||
'google-apis',
|
||||
) {
|
||||
constructor(
|
||||
environmentService: EnvironmentService,
|
||||
scopeConfig: GoogleAPIScopeConfig,
|
||||
) {
|
||||
const scopes = [
|
||||
'email',
|
||||
'profile',
|
||||
'https://www.googleapis.com/auth/gmail.readonly',
|
||||
'https://www.googleapis.com/auth/calendar.events',
|
||||
];
|
||||
|
||||
if (scopeConfig?.isMessagingAliasFetchingEnabled) {
|
||||
scopes.push('https://www.googleapis.com/auth/profile.emails.read');
|
||||
}
|
||||
|
||||
super({
|
||||
clientID: environmentService.get('AUTH_GOOGLE_CLIENT_ID'),
|
||||
clientSecret: environmentService.get('AUTH_GOOGLE_CLIENT_SECRET'),
|
||||
callbackURL: environmentService.get('AUTH_GOOGLE_APIS_CALLBACK_URL'),
|
||||
scope: scopes,
|
||||
passReqToCallback: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { VerifyCallback } from 'passport-google-oauth20';
|
||||
|
||||
import { GoogleAPIsOauthCommonStrategy } from 'src/engine/core-modules/auth/strategies/google-apis-oauth-common.auth.strategy';
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
import { GoogleAPIsRequest } from 'src/engine/core-modules/auth/types/google-api-request.type';
|
||||
|
||||
export type GoogleAPIScopeConfig = {
|
||||
isCalendarEnabled?: boolean;
|
||||
isMessagingAliasFetchingEnabled?: boolean;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class GoogleAPIsOauthExchangeCodeForTokenStrategy extends GoogleAPIsOauthCommonStrategy {
|
||||
constructor(
|
||||
environmentService: EnvironmentService,
|
||||
scopeConfig: GoogleAPIScopeConfig,
|
||||
) {
|
||||
super(environmentService, scopeConfig);
|
||||
}
|
||||
|
||||
async validate(
|
||||
request: GoogleAPIsRequest,
|
||||
accessToken: string,
|
||||
refreshToken: string,
|
||||
profile: any,
|
||||
done: VerifyCallback,
|
||||
): Promise<void> {
|
||||
const { name, emails, photos } = profile;
|
||||
|
||||
const state =
|
||||
typeof request.query.state === 'string'
|
||||
? JSON.parse(request.query.state)
|
||||
: undefined;
|
||||
|
||||
const user: GoogleAPIsRequest['user'] = {
|
||||
emails,
|
||||
firstName: name.givenName,
|
||||
lastName: name.familyName,
|
||||
picture: photos?.[0]?.value,
|
||||
accessToken,
|
||||
refreshToken,
|
||||
transientToken: state.transientToken,
|
||||
redirectLocation: state.redirectLocation,
|
||||
calendarVisibility: state.calendarVisibility,
|
||||
messageVisibility: state.messageVisibility,
|
||||
};
|
||||
|
||||
done(null, user);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { GoogleAPIsOauthCommonStrategy } from 'src/engine/core-modules/auth/strategies/google-apis-oauth-common.auth.strategy';
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
|
||||
export type GoogleAPIScopeConfig = {
|
||||
isCalendarEnabled?: boolean;
|
||||
isMessagingAliasFetchingEnabled?: boolean;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class GoogleAPIsOauthRequestCodeStrategy extends GoogleAPIsOauthCommonStrategy {
|
||||
constructor(
|
||||
environmentService: EnvironmentService,
|
||||
scopeConfig: GoogleAPIScopeConfig,
|
||||
) {
|
||||
super(environmentService, scopeConfig);
|
||||
}
|
||||
|
||||
authenticate(req: any, options: any) {
|
||||
options = {
|
||||
...options,
|
||||
accessType: 'offline',
|
||||
prompt: 'consent',
|
||||
state: JSON.stringify({
|
||||
transientToken: req.params.transientToken,
|
||||
redirectLocation: req.params.redirectLocation,
|
||||
calendarVisibility: req.params.calendarVisibility,
|
||||
messageVisibility: req.params.messageVisibility,
|
||||
}),
|
||||
};
|
||||
|
||||
return super.authenticate(req, options);
|
||||
}
|
||||
}
|
||||
@ -1,110 +0,0 @@
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { Strategy, VerifyCallback } from 'passport-google-oauth20';
|
||||
import { Request } from 'express';
|
||||
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
import { CalendarChannelVisibility } from 'src/modules/calendar/standard-objects/calendar-channel.workspace-entity';
|
||||
import { MessageChannelVisibility } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
||||
|
||||
export type GoogleAPIsRequest = Omit<
|
||||
Request,
|
||||
'user' | 'workspace' | 'cacheVersion'
|
||||
> & {
|
||||
user: {
|
||||
firstName?: string | null;
|
||||
lastName?: string | null;
|
||||
email: string;
|
||||
picture: string | null;
|
||||
workspaceInviteHash?: string;
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
transientToken: string;
|
||||
redirectLocation?: string;
|
||||
calendarVisibility?: CalendarChannelVisibility;
|
||||
messageVisibility?: MessageChannelVisibility;
|
||||
};
|
||||
};
|
||||
|
||||
export type GoogleAPIScopeConfig = {
|
||||
isCalendarEnabled?: boolean;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class GoogleAPIsStrategy extends PassportStrategy(
|
||||
Strategy,
|
||||
'google-apis',
|
||||
) {
|
||||
constructor(
|
||||
environmentService: EnvironmentService,
|
||||
scopeConfig: GoogleAPIScopeConfig,
|
||||
) {
|
||||
const scope = ['email', 'profile'];
|
||||
|
||||
if (environmentService.get('MESSAGING_PROVIDER_GMAIL_ENABLED')) {
|
||||
scope.push('https://www.googleapis.com/auth/gmail.readonly');
|
||||
}
|
||||
|
||||
if (
|
||||
environmentService.get('CALENDAR_PROVIDER_GOOGLE_ENABLED') &&
|
||||
scopeConfig?.isCalendarEnabled
|
||||
) {
|
||||
scope.push('https://www.googleapis.com/auth/calendar.events');
|
||||
}
|
||||
|
||||
super({
|
||||
clientID: environmentService.get('AUTH_GOOGLE_CLIENT_ID'),
|
||||
clientSecret: environmentService.get('AUTH_GOOGLE_CLIENT_SECRET'),
|
||||
callbackURL: environmentService.get('AUTH_GOOGLE_APIS_CALLBACK_URL'),
|
||||
scope,
|
||||
passReqToCallback: true,
|
||||
});
|
||||
}
|
||||
|
||||
authenticate(req: any, options: any) {
|
||||
options = {
|
||||
...options,
|
||||
accessType: 'offline',
|
||||
prompt: 'consent',
|
||||
state: JSON.stringify({
|
||||
transientToken: req.params.transientToken,
|
||||
redirectLocation: req.params.redirectLocation,
|
||||
calendarVisibility: req.params.calendarVisibility,
|
||||
messageVisibility: req.params.messageVisibility,
|
||||
}),
|
||||
};
|
||||
|
||||
return super.authenticate(req, options);
|
||||
}
|
||||
|
||||
async validate(
|
||||
request: GoogleAPIsRequest,
|
||||
accessToken: string,
|
||||
refreshToken: string,
|
||||
profile: any,
|
||||
done: VerifyCallback,
|
||||
): Promise<void> {
|
||||
const { name, emails, photos } = profile;
|
||||
|
||||
const state =
|
||||
typeof request.query.state === 'string'
|
||||
? JSON.parse(request.query.state)
|
||||
: undefined;
|
||||
|
||||
const user: GoogleAPIsRequest['user'] = {
|
||||
email: emails[0].value,
|
||||
firstName: name.givenName,
|
||||
lastName: name.familyName,
|
||||
picture: photos?.[0]?.value,
|
||||
accessToken,
|
||||
refreshToken,
|
||||
transientToken: state.transientToken,
|
||||
redirectLocation: state.redirectLocation,
|
||||
calendarVisibility: state.calendarVisibility,
|
||||
messageVisibility: state.messageVisibility,
|
||||
};
|
||||
|
||||
done(null, user);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
import { Request } from 'express';
|
||||
|
||||
import { CalendarChannelVisibility } from 'src/modules/calendar/standard-objects/calendar-channel.workspace-entity';
|
||||
import { MessageChannelVisibility } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
||||
|
||||
export type GoogleAPIsRequest = Omit<
|
||||
Request,
|
||||
'user' | 'workspace' | 'cacheVersion'
|
||||
> & {
|
||||
user: {
|
||||
firstName?: string | null;
|
||||
lastName?: string | null;
|
||||
emails: { value: string }[];
|
||||
picture: string | null;
|
||||
workspaceInviteHash?: string;
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
transientToken: string;
|
||||
redirectLocation?: string;
|
||||
calendarVisibility?: CalendarChannelVisibility;
|
||||
messageVisibility?: MessageChannelVisibility;
|
||||
};
|
||||
};
|
||||
@ -0,0 +1,40 @@
|
||||
import { GoogleAPIsRequest } from 'src/engine/core-modules/auth/types/google-api-request.type';
|
||||
import { setRequestExtraParams } from 'src/engine/core-modules/auth/utils/google-apis-set-request-extra-params.util';
|
||||
import { CalendarChannelVisibility } from 'src/modules/calendar/standard-objects/calendar-channel.workspace-entity';
|
||||
import { MessageChannelVisibility } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
||||
|
||||
describe('googleApisSetRequestExtraParams', () => {
|
||||
it('should set request extra params', () => {
|
||||
const request = {
|
||||
params: {},
|
||||
} as GoogleAPIsRequest;
|
||||
|
||||
setRequestExtraParams(request, {
|
||||
transientToken: 'abc',
|
||||
redirectLocation: '/test',
|
||||
calendarVisibility: CalendarChannelVisibility.SHARE_EVERYTHING,
|
||||
messageVisibility: MessageChannelVisibility.SHARE_EVERYTHING,
|
||||
});
|
||||
|
||||
expect(request.params).toEqual({
|
||||
transientToken: 'abc',
|
||||
redirectLocation: '/test',
|
||||
calendarVisibility: CalendarChannelVisibility.SHARE_EVERYTHING,
|
||||
messageVisibility: MessageChannelVisibility.SHARE_EVERYTHING,
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw error if transientToken is not provided', () => {
|
||||
const request = {
|
||||
params: {},
|
||||
} as GoogleAPIsRequest;
|
||||
|
||||
expect(() => {
|
||||
setRequestExtraParams(request, {
|
||||
redirectLocation: '/test',
|
||||
calendarVisibility: CalendarChannelVisibility.SHARE_EVERYTHING,
|
||||
messageVisibility: MessageChannelVisibility.SHARE_EVERYTHING,
|
||||
});
|
||||
}).toThrow('transientToken is required');
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,38 @@
|
||||
import { GoogleAPIsRequest } from 'src/engine/core-modules/auth/types/google-api-request.type';
|
||||
|
||||
type GoogleAPIsRequestExtraParams = {
|
||||
transientToken?: string;
|
||||
redirectLocation?: string;
|
||||
calendarVisibility?: string;
|
||||
messageVisibility?: string;
|
||||
};
|
||||
|
||||
export const setRequestExtraParams = (
|
||||
request: GoogleAPIsRequest,
|
||||
params: GoogleAPIsRequestExtraParams,
|
||||
): void => {
|
||||
const {
|
||||
transientToken,
|
||||
redirectLocation,
|
||||
calendarVisibility,
|
||||
messageVisibility,
|
||||
} = params;
|
||||
|
||||
if (!transientToken) {
|
||||
throw new Error('transientToken is required');
|
||||
}
|
||||
|
||||
request.params.transientToken = transientToken;
|
||||
|
||||
if (redirectLocation) {
|
||||
request.params.redirectLocation = redirectLocation;
|
||||
}
|
||||
|
||||
if (calendarVisibility) {
|
||||
request.params.calendarVisibility = calendarVisibility;
|
||||
}
|
||||
|
||||
if (messageVisibility) {
|
||||
request.params.messageVisibility = messageVisibility;
|
||||
}
|
||||
};
|
||||
@ -11,9 +11,9 @@ import {
|
||||
import { Response } from 'express';
|
||||
|
||||
import {
|
||||
BillingService,
|
||||
BillingWorkspaceService,
|
||||
WebhookEvent,
|
||||
} from 'src/engine/core-modules/billing/billing.service';
|
||||
} from 'src/engine/core-modules/billing/billing.workspace-service';
|
||||
import { StripeService } from 'src/engine/core-modules/billing/stripe/stripe.service';
|
||||
|
||||
@Controller('billing')
|
||||
@ -22,7 +22,7 @@ export class BillingController {
|
||||
|
||||
constructor(
|
||||
private readonly stripeService: StripeService,
|
||||
private readonly billingService: BillingService,
|
||||
private readonly billingWorkspaceService: BillingWorkspaceService,
|
||||
) {}
|
||||
|
||||
@Post('/webhooks')
|
||||
@ -42,7 +42,7 @@ export class BillingController {
|
||||
);
|
||||
|
||||
if (event.type === WebhookEvent.SETUP_INTENT_SUCCEEDED) {
|
||||
await this.billingService.handleUnpaidInvoices(event.data);
|
||||
await this.billingWorkspaceService.handleUnpaidInvoices(event.data);
|
||||
}
|
||||
|
||||
if (
|
||||
@ -58,7 +58,7 @@ export class BillingController {
|
||||
return;
|
||||
}
|
||||
|
||||
await this.billingService.upsertBillingSubscription(
|
||||
await this.billingWorkspaceService.upsertBillingSubscription(
|
||||
workspaceId,
|
||||
event.data,
|
||||
);
|
||||
|
||||
@ -11,6 +11,7 @@ import { BillingResolver } from 'src/engine/core-modules/billing/billing.resolve
|
||||
import { BillingWorkspaceMemberListener } from 'src/engine/core-modules/billing/listeners/billing-workspace-member.listener';
|
||||
import { UserWorkspaceModule } from 'src/engine/core-modules/user-workspace/user-workspace.module';
|
||||
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
import { BillingWorkspaceService } from 'src/engine/core-modules/billing/billing.workspace-service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -27,7 +28,12 @@ import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-
|
||||
),
|
||||
],
|
||||
controllers: [BillingController],
|
||||
providers: [BillingService, BillingResolver, BillingWorkspaceMemberListener],
|
||||
exports: [BillingService],
|
||||
providers: [
|
||||
BillingService,
|
||||
BillingWorkspaceService,
|
||||
BillingResolver,
|
||||
BillingWorkspaceMemberListener,
|
||||
],
|
||||
exports: [BillingService, BillingWorkspaceService],
|
||||
})
|
||||
export class BillingModule {}
|
||||
|
||||
@ -3,8 +3,8 @@ import { UseGuards } from '@nestjs/common';
|
||||
|
||||
import {
|
||||
AvailableProduct,
|
||||
BillingService,
|
||||
} from 'src/engine/core-modules/billing/billing.service';
|
||||
BillingWorkspaceService,
|
||||
} from 'src/engine/core-modules/billing/billing.workspace-service';
|
||||
import { ProductInput } from 'src/engine/core-modules/billing/dto/product.input';
|
||||
import { assert } from 'src/utils/assert';
|
||||
import { ProductPricesEntity } from 'src/engine/core-modules/billing/dto/product-prices.entity';
|
||||
@ -18,11 +18,14 @@ import { UpdateBillingEntity } from 'src/engine/core-modules/billing/dto/update-
|
||||
|
||||
@Resolver()
|
||||
export class BillingResolver {
|
||||
constructor(private readonly billingService: BillingService) {}
|
||||
constructor(
|
||||
private readonly billingWorkspaceService: BillingWorkspaceService,
|
||||
) {}
|
||||
|
||||
@Query(() => ProductPricesEntity)
|
||||
async getProductPrices(@Args() { product }: ProductInput) {
|
||||
const stripeProductId = this.billingService.getProductStripeId(product);
|
||||
const stripeProductId =
|
||||
this.billingWorkspaceService.getProductStripeId(product);
|
||||
|
||||
assert(
|
||||
stripeProductId,
|
||||
@ -32,7 +35,7 @@ export class BillingResolver {
|
||||
);
|
||||
|
||||
const productPrices =
|
||||
await this.billingService.getProductPrices(stripeProductId);
|
||||
await this.billingWorkspaceService.getProductPrices(stripeProductId);
|
||||
|
||||
return {
|
||||
totalNumberOfPrices: productPrices.length,
|
||||
@ -47,7 +50,7 @@ export class BillingResolver {
|
||||
@Args() { returnUrlPath }: BillingSessionInput,
|
||||
) {
|
||||
return {
|
||||
url: await this.billingService.computeBillingPortalSessionURL(
|
||||
url: await this.billingWorkspaceService.computeBillingPortalSessionURL(
|
||||
user.defaultWorkspaceId,
|
||||
returnUrlPath,
|
||||
),
|
||||
@ -60,7 +63,7 @@ export class BillingResolver {
|
||||
@AuthUser() user: User,
|
||||
@Args() { recurringInterval, successUrlPath }: CheckoutSessionInput,
|
||||
) {
|
||||
const stripeProductId = this.billingService.getProductStripeId(
|
||||
const stripeProductId = this.billingWorkspaceService.getProductStripeId(
|
||||
AvailableProduct.BasePlan,
|
||||
);
|
||||
|
||||
@ -70,7 +73,7 @@ export class BillingResolver {
|
||||
);
|
||||
|
||||
const productPrices =
|
||||
await this.billingService.getProductPrices(stripeProductId);
|
||||
await this.billingWorkspaceService.getProductPrices(stripeProductId);
|
||||
|
||||
const stripePriceId = productPrices.filter(
|
||||
(price) => price.recurringInterval === recurringInterval,
|
||||
@ -82,7 +85,7 @@ export class BillingResolver {
|
||||
);
|
||||
|
||||
return {
|
||||
url: await this.billingService.computeCheckoutSessionURL(
|
||||
url: await this.billingWorkspaceService.computeCheckoutSessionURL(
|
||||
user,
|
||||
stripePriceId,
|
||||
successUrlPath,
|
||||
@ -93,7 +96,7 @@ export class BillingResolver {
|
||||
@Mutation(() => UpdateBillingEntity)
|
||||
@UseGuards(JwtAuthGuard)
|
||||
async updateBillingSubscription(@AuthUser() user: User) {
|
||||
await this.billingService.updateBillingSubscription(user);
|
||||
await this.billingWorkspaceService.updateBillingSubscription(user);
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
@ -1,51 +1,21 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import Stripe from 'stripe';
|
||||
import { In, Not, Repository } from 'typeorm';
|
||||
import { In, Repository } from 'typeorm';
|
||||
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
import { StripeService } from 'src/engine/core-modules/billing/stripe/stripe.service';
|
||||
import {
|
||||
BillingSubscription,
|
||||
SubscriptionInterval,
|
||||
SubscriptionStatus,
|
||||
} from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
|
||||
import { BillingSubscriptionItem } from 'src/engine/core-modules/billing/entities/billing-subscription-item.entity';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { ProductPriceEntity } from 'src/engine/core-modules/billing/dto/product-price.entity';
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { assert } from 'src/utils/assert';
|
||||
import { UserWorkspaceService } from 'src/engine/core-modules/user-workspace/user-workspace.service';
|
||||
import {
|
||||
FeatureFlagEntity,
|
||||
FeatureFlagKeys,
|
||||
} from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
|
||||
export enum AvailableProduct {
|
||||
BasePlan = 'base-plan',
|
||||
}
|
||||
|
||||
export enum WebhookEvent {
|
||||
CUSTOMER_SUBSCRIPTION_CREATED = 'customer.subscription.created',
|
||||
CUSTOMER_SUBSCRIPTION_UPDATED = 'customer.subscription.updated',
|
||||
CUSTOMER_SUBSCRIPTION_DELETED = 'customer.subscription.deleted',
|
||||
SETUP_INTENT_SUCCEEDED = 'setup_intent.succeeded',
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class BillingService {
|
||||
protected readonly logger = new Logger(BillingService.name);
|
||||
constructor(
|
||||
private readonly stripeService: StripeService,
|
||||
private readonly userWorkspaceService: UserWorkspaceService,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
@InjectRepository(BillingSubscription, 'core')
|
||||
private readonly billingSubscriptionRepository: Repository<BillingSubscription>,
|
||||
@InjectRepository(FeatureFlagEntity, 'core')
|
||||
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
|
||||
@InjectRepository(BillingSubscriptionItem, 'core')
|
||||
private readonly billingSubscriptionItemRepository: Repository<BillingSubscriptionItem>,
|
||||
@InjectRepository(Workspace, 'core')
|
||||
private readonly workspaceRepository: Repository<Workspace>,
|
||||
) {}
|
||||
@ -68,283 +38,4 @@ export class BillingService {
|
||||
})
|
||||
).map((workspace) => workspace.id);
|
||||
}
|
||||
|
||||
async isBillingEnabledForWorkspace(workspaceId: string) {
|
||||
const isFreeAccessEnabled = await this.featureFlagRepository.findOneBy({
|
||||
workspaceId,
|
||||
key: FeatureFlagKeys.IsFreeAccessEnabled,
|
||||
value: true,
|
||||
});
|
||||
|
||||
return (
|
||||
!isFreeAccessEnabled && this.environmentService.get('IS_BILLING_ENABLED')
|
||||
);
|
||||
}
|
||||
|
||||
getProductStripeId(product: AvailableProduct) {
|
||||
if (product === AvailableProduct.BasePlan) {
|
||||
return this.environmentService.get('BILLING_STRIPE_BASE_PLAN_PRODUCT_ID');
|
||||
}
|
||||
}
|
||||
|
||||
async getProductPrices(stripeProductId: string) {
|
||||
const productPrices =
|
||||
await this.stripeService.getProductPrices(stripeProductId);
|
||||
|
||||
return this.formatProductPrices(productPrices.data);
|
||||
}
|
||||
|
||||
formatProductPrices(prices: Stripe.Price[]) {
|
||||
const result: Record<string, ProductPriceEntity> = {};
|
||||
|
||||
prices.forEach((item) => {
|
||||
const interval = item.recurring?.interval;
|
||||
|
||||
if (!interval || !item.unit_amount) {
|
||||
return;
|
||||
}
|
||||
if (
|
||||
!result[interval] ||
|
||||
item.created > (result[interval]?.created || 0)
|
||||
) {
|
||||
result[interval] = {
|
||||
unitAmount: item.unit_amount,
|
||||
recurringInterval: interval,
|
||||
created: item.created,
|
||||
stripePriceId: item.id,
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
return Object.values(result).sort((a, b) => a.unitAmount - b.unitAmount);
|
||||
}
|
||||
|
||||
async getCurrentBillingSubscription(criteria: {
|
||||
workspaceId?: string;
|
||||
stripeCustomerId?: string;
|
||||
}) {
|
||||
const notCanceledSubscriptions =
|
||||
await this.billingSubscriptionRepository.find({
|
||||
where: { ...criteria, status: Not(SubscriptionStatus.Canceled) },
|
||||
relations: ['billingSubscriptionItems'],
|
||||
});
|
||||
|
||||
assert(
|
||||
notCanceledSubscriptions.length <= 1,
|
||||
`More than one not canceled subscription for workspace ${criteria.workspaceId}`,
|
||||
);
|
||||
|
||||
return notCanceledSubscriptions?.[0];
|
||||
}
|
||||
|
||||
async getBillingSubscription(stripeSubscriptionId: string) {
|
||||
return this.billingSubscriptionRepository.findOneOrFail({
|
||||
where: { stripeSubscriptionId },
|
||||
});
|
||||
}
|
||||
|
||||
async getStripeCustomerId(workspaceId: string) {
|
||||
const subscriptions = await this.billingSubscriptionRepository.find({
|
||||
where: { workspaceId },
|
||||
});
|
||||
|
||||
return subscriptions?.[0]?.stripeCustomerId;
|
||||
}
|
||||
|
||||
async getBillingSubscriptionItem(
|
||||
workspaceId: string,
|
||||
stripeProductId = this.environmentService.get(
|
||||
'BILLING_STRIPE_BASE_PLAN_PRODUCT_ID',
|
||||
),
|
||||
) {
|
||||
const billingSubscription = await this.getCurrentBillingSubscription({
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
if (!billingSubscription) {
|
||||
throw new Error(
|
||||
`Cannot find billingSubscriptionItem for product ${stripeProductId} for workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
|
||||
const billingSubscriptionItem =
|
||||
billingSubscription.billingSubscriptionItems.filter(
|
||||
(billingSubscriptionItem) =>
|
||||
billingSubscriptionItem.stripeProductId === stripeProductId,
|
||||
)?.[0];
|
||||
|
||||
if (!billingSubscriptionItem) {
|
||||
throw new Error(
|
||||
`Cannot find billingSubscriptionItem for product ${stripeProductId} for workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
|
||||
return billingSubscriptionItem;
|
||||
}
|
||||
|
||||
async computeBillingPortalSessionURL(
|
||||
workspaceId: string,
|
||||
returnUrlPath?: string,
|
||||
) {
|
||||
const stripeCustomerId = await this.getStripeCustomerId(workspaceId);
|
||||
|
||||
if (!stripeCustomerId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const frontBaseUrl = this.environmentService.get('FRONT_BASE_URL');
|
||||
const returnUrl = returnUrlPath
|
||||
? frontBaseUrl + returnUrlPath
|
||||
: frontBaseUrl;
|
||||
|
||||
const session = await this.stripeService.createBillingPortalSession(
|
||||
stripeCustomerId,
|
||||
returnUrl,
|
||||
);
|
||||
|
||||
assert(session.url, 'Error: missing billingPortal.session.url');
|
||||
|
||||
return session.url;
|
||||
}
|
||||
|
||||
async updateBillingSubscription(user: User) {
|
||||
const billingSubscription = await this.getCurrentBillingSubscription({
|
||||
workspaceId: user.defaultWorkspaceId,
|
||||
});
|
||||
const newInterval =
|
||||
billingSubscription?.interval === SubscriptionInterval.Year
|
||||
? SubscriptionInterval.Month
|
||||
: SubscriptionInterval.Year;
|
||||
const billingSubscriptionItem = await this.getBillingSubscriptionItem(
|
||||
user.defaultWorkspaceId,
|
||||
);
|
||||
const stripeProductId = this.getProductStripeId(AvailableProduct.BasePlan);
|
||||
|
||||
if (!stripeProductId) {
|
||||
throw new Error('Stripe product id not found for basePlan');
|
||||
}
|
||||
const productPrices = await this.getProductPrices(stripeProductId);
|
||||
|
||||
const stripePriceId = productPrices.filter(
|
||||
(price) => price.recurringInterval === newInterval,
|
||||
)?.[0]?.stripePriceId;
|
||||
|
||||
await this.stripeService.updateBillingSubscriptionItem(
|
||||
billingSubscriptionItem,
|
||||
stripePriceId,
|
||||
);
|
||||
}
|
||||
|
||||
async computeCheckoutSessionURL(
|
||||
user: User,
|
||||
priceId: string,
|
||||
successUrlPath?: string,
|
||||
): Promise<string> {
|
||||
const frontBaseUrl = this.environmentService.get('FRONT_BASE_URL');
|
||||
const successUrl = successUrlPath
|
||||
? frontBaseUrl + successUrlPath
|
||||
: frontBaseUrl;
|
||||
|
||||
const quantity =
|
||||
(await this.userWorkspaceService.getWorkspaceMemberCount()) || 1;
|
||||
|
||||
const stripeCustomerId = (
|
||||
await this.billingSubscriptionRepository.findOneBy({
|
||||
workspaceId: user.defaultWorkspaceId,
|
||||
})
|
||||
)?.stripeCustomerId;
|
||||
|
||||
const session = await this.stripeService.createCheckoutSession(
|
||||
user,
|
||||
priceId,
|
||||
quantity,
|
||||
successUrl,
|
||||
frontBaseUrl,
|
||||
stripeCustomerId,
|
||||
);
|
||||
|
||||
assert(session.url, 'Error: missing checkout.session.url');
|
||||
|
||||
return session.url;
|
||||
}
|
||||
|
||||
async deleteSubscription(workspaceId: string) {
|
||||
const subscriptionToCancel = await this.getCurrentBillingSubscription({
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
if (subscriptionToCancel) {
|
||||
await this.stripeService.cancelSubscription(
|
||||
subscriptionToCancel.stripeSubscriptionId,
|
||||
);
|
||||
await this.billingSubscriptionRepository.delete(subscriptionToCancel.id);
|
||||
}
|
||||
}
|
||||
|
||||
async handleUnpaidInvoices(data: Stripe.SetupIntentSucceededEvent.Data) {
|
||||
const billingSubscription = await this.getCurrentBillingSubscription({
|
||||
stripeCustomerId: data.object.customer as string,
|
||||
});
|
||||
|
||||
if (billingSubscription?.status === 'unpaid') {
|
||||
await this.stripeService.collectLastInvoice(
|
||||
billingSubscription.stripeSubscriptionId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async upsertBillingSubscription(
|
||||
workspaceId: string,
|
||||
data:
|
||||
| Stripe.CustomerSubscriptionUpdatedEvent.Data
|
||||
| Stripe.CustomerSubscriptionCreatedEvent.Data
|
||||
| Stripe.CustomerSubscriptionDeletedEvent.Data,
|
||||
) {
|
||||
const workspace = this.workspaceRepository.find({
|
||||
where: { id: workspaceId },
|
||||
});
|
||||
|
||||
if (!workspace) {
|
||||
return;
|
||||
}
|
||||
|
||||
await this.billingSubscriptionRepository.upsert(
|
||||
{
|
||||
workspaceId: workspaceId,
|
||||
stripeCustomerId: data.object.customer as string,
|
||||
stripeSubscriptionId: data.object.id,
|
||||
status: data.object.status,
|
||||
interval: data.object.items.data[0].plan.interval,
|
||||
},
|
||||
{
|
||||
conflictPaths: ['stripeSubscriptionId'],
|
||||
skipUpdateIfNoValuesChanged: true,
|
||||
},
|
||||
);
|
||||
|
||||
const billingSubscription = await this.getBillingSubscription(
|
||||
data.object.id,
|
||||
);
|
||||
|
||||
await this.billingSubscriptionItemRepository.upsert(
|
||||
data.object.items.data.map((item) => {
|
||||
return {
|
||||
billingSubscriptionId: billingSubscription.id,
|
||||
stripeProductId: item.price.product as string,
|
||||
stripePriceId: item.price.id,
|
||||
stripeSubscriptionItemId: item.id,
|
||||
quantity: item.quantity,
|
||||
};
|
||||
}),
|
||||
{
|
||||
conflictPaths: ['billingSubscriptionId', 'stripeProductId'],
|
||||
skipUpdateIfNoValuesChanged: true,
|
||||
},
|
||||
);
|
||||
|
||||
await this.featureFlagRepository.delete({
|
||||
workspaceId,
|
||||
key: FeatureFlagKeys.IsFreeAccessEnabled,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,332 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import Stripe from 'stripe';
|
||||
import { Not, Repository } from 'typeorm';
|
||||
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
import { StripeService } from 'src/engine/core-modules/billing/stripe/stripe.service';
|
||||
import {
|
||||
BillingSubscription,
|
||||
SubscriptionInterval,
|
||||
SubscriptionStatus,
|
||||
} from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
|
||||
import { BillingSubscriptionItem } from 'src/engine/core-modules/billing/entities/billing-subscription-item.entity';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { ProductPriceEntity } from 'src/engine/core-modules/billing/dto/product-price.entity';
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { assert } from 'src/utils/assert';
|
||||
import { UserWorkspaceService } from 'src/engine/core-modules/user-workspace/user-workspace.service';
|
||||
import {
|
||||
FeatureFlagEntity,
|
||||
FeatureFlagKeys,
|
||||
} from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
import { BillingService } from 'src/engine/core-modules/billing/billing.service';
|
||||
|
||||
export enum AvailableProduct {
|
||||
BasePlan = 'base-plan',
|
||||
}
|
||||
|
||||
export enum WebhookEvent {
|
||||
CUSTOMER_SUBSCRIPTION_CREATED = 'customer.subscription.created',
|
||||
CUSTOMER_SUBSCRIPTION_UPDATED = 'customer.subscription.updated',
|
||||
CUSTOMER_SUBSCRIPTION_DELETED = 'customer.subscription.deleted',
|
||||
SETUP_INTENT_SUCCEEDED = 'setup_intent.succeeded',
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class BillingWorkspaceService {
|
||||
protected readonly logger = new Logger(BillingService.name);
|
||||
constructor(
|
||||
private readonly stripeService: StripeService,
|
||||
private readonly userWorkspaceService: UserWorkspaceService,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
@InjectRepository(BillingSubscription, 'core')
|
||||
private readonly billingSubscriptionRepository: Repository<BillingSubscription>,
|
||||
@InjectRepository(FeatureFlagEntity, 'core')
|
||||
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
|
||||
@InjectRepository(BillingSubscriptionItem, 'core')
|
||||
private readonly billingSubscriptionItemRepository: Repository<BillingSubscriptionItem>,
|
||||
@InjectRepository(Workspace, 'core')
|
||||
private readonly workspaceRepository: Repository<Workspace>,
|
||||
) {}
|
||||
|
||||
async isBillingEnabledForWorkspace(workspaceId: string) {
|
||||
const isFreeAccessEnabled = await this.featureFlagRepository.findOneBy({
|
||||
workspaceId,
|
||||
key: FeatureFlagKeys.IsFreeAccessEnabled,
|
||||
value: true,
|
||||
});
|
||||
|
||||
return (
|
||||
!isFreeAccessEnabled && this.environmentService.get('IS_BILLING_ENABLED')
|
||||
);
|
||||
}
|
||||
|
||||
getProductStripeId(product: AvailableProduct) {
|
||||
if (product === AvailableProduct.BasePlan) {
|
||||
return this.environmentService.get('BILLING_STRIPE_BASE_PLAN_PRODUCT_ID');
|
||||
}
|
||||
}
|
||||
|
||||
async getProductPrices(stripeProductId: string) {
|
||||
const productPrices =
|
||||
await this.stripeService.getProductPrices(stripeProductId);
|
||||
|
||||
return this.formatProductPrices(productPrices.data);
|
||||
}
|
||||
|
||||
formatProductPrices(prices: Stripe.Price[]) {
|
||||
const result: Record<string, ProductPriceEntity> = {};
|
||||
|
||||
prices.forEach((item) => {
|
||||
const interval = item.recurring?.interval;
|
||||
|
||||
if (!interval || !item.unit_amount) {
|
||||
return;
|
||||
}
|
||||
if (
|
||||
!result[interval] ||
|
||||
item.created > (result[interval]?.created || 0)
|
||||
) {
|
||||
result[interval] = {
|
||||
unitAmount: item.unit_amount,
|
||||
recurringInterval: interval,
|
||||
created: item.created,
|
||||
stripePriceId: item.id,
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
return Object.values(result).sort((a, b) => a.unitAmount - b.unitAmount);
|
||||
}
|
||||
|
||||
async getCurrentBillingSubscription(criteria: {
|
||||
workspaceId?: string;
|
||||
stripeCustomerId?: string;
|
||||
}) {
|
||||
const notCanceledSubscriptions =
|
||||
await this.billingSubscriptionRepository.find({
|
||||
where: { ...criteria, status: Not(SubscriptionStatus.Canceled) },
|
||||
relations: ['billingSubscriptionItems'],
|
||||
});
|
||||
|
||||
assert(
|
||||
notCanceledSubscriptions.length <= 1,
|
||||
`More than one not canceled subscription for workspace ${criteria.workspaceId}`,
|
||||
);
|
||||
|
||||
return notCanceledSubscriptions?.[0];
|
||||
}
|
||||
|
||||
async getBillingSubscription(stripeSubscriptionId: string) {
|
||||
return this.billingSubscriptionRepository.findOneOrFail({
|
||||
where: { stripeSubscriptionId },
|
||||
});
|
||||
}
|
||||
|
||||
async getStripeCustomerId(workspaceId: string) {
|
||||
const subscriptions = await this.billingSubscriptionRepository.find({
|
||||
where: { workspaceId },
|
||||
});
|
||||
|
||||
return subscriptions?.[0]?.stripeCustomerId;
|
||||
}
|
||||
|
||||
async getBillingSubscriptionItem(
|
||||
workspaceId: string,
|
||||
stripeProductId = this.environmentService.get(
|
||||
'BILLING_STRIPE_BASE_PLAN_PRODUCT_ID',
|
||||
),
|
||||
) {
|
||||
const billingSubscription = await this.getCurrentBillingSubscription({
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
if (!billingSubscription) {
|
||||
throw new Error(
|
||||
`Cannot find billingSubscriptionItem for product ${stripeProductId} for workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
|
||||
const billingSubscriptionItem =
|
||||
billingSubscription.billingSubscriptionItems.filter(
|
||||
(billingSubscriptionItem) =>
|
||||
billingSubscriptionItem.stripeProductId === stripeProductId,
|
||||
)?.[0];
|
||||
|
||||
if (!billingSubscriptionItem) {
|
||||
throw new Error(
|
||||
`Cannot find billingSubscriptionItem for product ${stripeProductId} for workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
|
||||
return billingSubscriptionItem;
|
||||
}
|
||||
|
||||
async computeBillingPortalSessionURL(
|
||||
workspaceId: string,
|
||||
returnUrlPath?: string,
|
||||
) {
|
||||
const stripeCustomerId = await this.getStripeCustomerId(workspaceId);
|
||||
|
||||
if (!stripeCustomerId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const frontBaseUrl = this.environmentService.get('FRONT_BASE_URL');
|
||||
const returnUrl = returnUrlPath
|
||||
? frontBaseUrl + returnUrlPath
|
||||
: frontBaseUrl;
|
||||
|
||||
const session = await this.stripeService.createBillingPortalSession(
|
||||
stripeCustomerId,
|
||||
returnUrl,
|
||||
);
|
||||
|
||||
assert(session.url, 'Error: missing billingPortal.session.url');
|
||||
|
||||
return session.url;
|
||||
}
|
||||
|
||||
async updateBillingSubscription(user: User) {
|
||||
const billingSubscription = await this.getCurrentBillingSubscription({
|
||||
workspaceId: user.defaultWorkspaceId,
|
||||
});
|
||||
const newInterval =
|
||||
billingSubscription?.interval === SubscriptionInterval.Year
|
||||
? SubscriptionInterval.Month
|
||||
: SubscriptionInterval.Year;
|
||||
const billingSubscriptionItem = await this.getBillingSubscriptionItem(
|
||||
user.defaultWorkspaceId,
|
||||
);
|
||||
const stripeProductId = this.getProductStripeId(AvailableProduct.BasePlan);
|
||||
|
||||
if (!stripeProductId) {
|
||||
throw new Error('Stripe product id not found for basePlan');
|
||||
}
|
||||
const productPrices = await this.getProductPrices(stripeProductId);
|
||||
|
||||
const stripePriceId = productPrices.filter(
|
||||
(price) => price.recurringInterval === newInterval,
|
||||
)?.[0]?.stripePriceId;
|
||||
|
||||
await this.stripeService.updateBillingSubscriptionItem(
|
||||
billingSubscriptionItem,
|
||||
stripePriceId,
|
||||
);
|
||||
}
|
||||
|
||||
async computeCheckoutSessionURL(
|
||||
user: User,
|
||||
priceId: string,
|
||||
successUrlPath?: string,
|
||||
): Promise<string> {
|
||||
const frontBaseUrl = this.environmentService.get('FRONT_BASE_URL');
|
||||
const successUrl = successUrlPath
|
||||
? frontBaseUrl + successUrlPath
|
||||
: frontBaseUrl;
|
||||
|
||||
const quantity =
|
||||
(await this.userWorkspaceService.getWorkspaceMemberCount()) || 1;
|
||||
|
||||
const stripeCustomerId = (
|
||||
await this.billingSubscriptionRepository.findOneBy({
|
||||
workspaceId: user.defaultWorkspaceId,
|
||||
})
|
||||
)?.stripeCustomerId;
|
||||
|
||||
const session = await this.stripeService.createCheckoutSession(
|
||||
user,
|
||||
priceId,
|
||||
quantity,
|
||||
successUrl,
|
||||
frontBaseUrl,
|
||||
stripeCustomerId,
|
||||
);
|
||||
|
||||
assert(session.url, 'Error: missing checkout.session.url');
|
||||
|
||||
return session.url;
|
||||
}
|
||||
|
||||
async deleteSubscription(workspaceId: string) {
|
||||
const subscriptionToCancel = await this.getCurrentBillingSubscription({
|
||||
workspaceId,
|
||||
});
|
||||
|
||||
if (subscriptionToCancel) {
|
||||
await this.stripeService.cancelSubscription(
|
||||
subscriptionToCancel.stripeSubscriptionId,
|
||||
);
|
||||
await this.billingSubscriptionRepository.delete(subscriptionToCancel.id);
|
||||
}
|
||||
}
|
||||
|
||||
async handleUnpaidInvoices(data: Stripe.SetupIntentSucceededEvent.Data) {
|
||||
const billingSubscription = await this.getCurrentBillingSubscription({
|
||||
stripeCustomerId: data.object.customer as string,
|
||||
});
|
||||
|
||||
if (billingSubscription?.status === 'unpaid') {
|
||||
await this.stripeService.collectLastInvoice(
|
||||
billingSubscription.stripeSubscriptionId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async upsertBillingSubscription(
|
||||
workspaceId: string,
|
||||
data:
|
||||
| Stripe.CustomerSubscriptionUpdatedEvent.Data
|
||||
| Stripe.CustomerSubscriptionCreatedEvent.Data
|
||||
| Stripe.CustomerSubscriptionDeletedEvent.Data,
|
||||
) {
|
||||
const workspace = this.workspaceRepository.find({
|
||||
where: { id: workspaceId },
|
||||
});
|
||||
|
||||
if (!workspace) {
|
||||
return;
|
||||
}
|
||||
|
||||
await this.billingSubscriptionRepository.upsert(
|
||||
{
|
||||
workspaceId: workspaceId,
|
||||
stripeCustomerId: data.object.customer as string,
|
||||
stripeSubscriptionId: data.object.id,
|
||||
status: data.object.status,
|
||||
interval: data.object.items.data[0].plan.interval,
|
||||
},
|
||||
{
|
||||
conflictPaths: ['stripeSubscriptionId'],
|
||||
skipUpdateIfNoValuesChanged: true,
|
||||
},
|
||||
);
|
||||
|
||||
const billingSubscription = await this.getBillingSubscription(
|
||||
data.object.id,
|
||||
);
|
||||
|
||||
await this.billingSubscriptionItemRepository.upsert(
|
||||
data.object.items.data.map((item) => {
|
||||
return {
|
||||
billingSubscriptionId: billingSubscription.id,
|
||||
stripeProductId: item.price.product as string,
|
||||
stripePriceId: item.price.id,
|
||||
stripeSubscriptionItemId: item.id,
|
||||
quantity: item.quantity,
|
||||
};
|
||||
}),
|
||||
{
|
||||
conflictPaths: ['billingSubscriptionId', 'stripeProductId'],
|
||||
skipUpdateIfNoValuesChanged: true,
|
||||
},
|
||||
);
|
||||
|
||||
await this.featureFlagRepository.delete({
|
||||
workspaceId,
|
||||
key: FeatureFlagKeys.IsFreeAccessEnabled,
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,7 @@ import { ArgsType, Field } from '@nestjs/graphql';
|
||||
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
import { AvailableProduct } from 'src/engine/core-modules/billing/billing.service';
|
||||
import { AvailableProduct } from 'src/engine/core-modules/billing/billing.workspace-service';
|
||||
|
||||
@ArgsType()
|
||||
export class ProductInput {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { Logger, Scope } from '@nestjs/common';
|
||||
|
||||
import { BillingService } from 'src/engine/core-modules/billing/billing.service';
|
||||
import { BillingWorkspaceService } from 'src/engine/core-modules/billing/billing.workspace-service';
|
||||
import { UserWorkspaceService } from 'src/engine/core-modules/user-workspace/user-workspace.service';
|
||||
import { StripeService } from 'src/engine/core-modules/billing/stripe/stripe.service';
|
||||
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
|
||||
@ -16,7 +16,7 @@ export class UpdateSubscriptionJob {
|
||||
protected readonly logger = new Logger(UpdateSubscriptionJob.name);
|
||||
|
||||
constructor(
|
||||
private readonly billingService: BillingService,
|
||||
private readonly billingWorkspaceService: BillingWorkspaceService,
|
||||
private readonly userWorkspaceService: UserWorkspaceService,
|
||||
private readonly stripeService: StripeService,
|
||||
) {}
|
||||
@ -32,7 +32,9 @@ export class UpdateSubscriptionJob {
|
||||
|
||||
try {
|
||||
const billingSubscriptionItem =
|
||||
await this.billingService.getBillingSubscriptionItem(data.workspaceId);
|
||||
await this.billingWorkspaceService.getBillingSubscriptionItem(
|
||||
data.workspaceId,
|
||||
);
|
||||
|
||||
await this.stripeService.updateSubscriptionItem(
|
||||
billingSubscriptionItem.stripeSubscriptionItemId,
|
||||
|
||||
@ -10,14 +10,14 @@ import {
|
||||
UpdateSubscriptionJobData,
|
||||
} from 'src/engine/core-modules/billing/jobs/update-subscription.job';
|
||||
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
|
||||
import { BillingService } from 'src/engine/core-modules/billing/billing.service';
|
||||
import { BillingWorkspaceService } from 'src/engine/core-modules/billing/billing.workspace-service';
|
||||
|
||||
@Injectable()
|
||||
export class BillingWorkspaceMemberListener {
|
||||
constructor(
|
||||
@InjectMessageQueue(MessageQueue.billingQueue)
|
||||
private readonly messageQueueService: MessageQueueService,
|
||||
private readonly billingService: BillingService,
|
||||
private readonly billingWorkspaceService: BillingWorkspaceService,
|
||||
) {}
|
||||
|
||||
@OnEvent('workspaceMember.created')
|
||||
@ -26,7 +26,7 @@ export class BillingWorkspaceMemberListener {
|
||||
payload: ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>,
|
||||
) {
|
||||
const isBillingEnabledForWorkspace =
|
||||
await this.billingService.isBillingEnabledForWorkspace(
|
||||
await this.billingWorkspaceService.isBillingEnabledForWorkspace(
|
||||
payload.workspaceId,
|
||||
);
|
||||
|
||||
|
||||
@ -22,6 +22,7 @@ export enum FeatureFlagKeys {
|
||||
IsPostgreSQLIntegrationEnabled = 'IS_POSTGRESQL_INTEGRATION_ENABLED',
|
||||
IsStripeIntegrationEnabled = 'IS_STRIPE_INTEGRATION_ENABLED',
|
||||
IsContactCreationForSentAndReceivedEmailsEnabled = 'IS_CONTACT_CREATION_FOR_SENT_AND_RECEIVED_EMAILS_ENABLED',
|
||||
IsMessagingAliasFetchingEnabled = 'IS_MESSAGING_ALIAS_FETCHING_ENABLED',
|
||||
IsGoogleCalendarSyncV2Enabled = 'IS_GOOGLE_CALENDAR_SYNC_V2_ENABLED',
|
||||
IsFreeAccessEnabled = 'IS_FREE_ACCESS_ENABLED',
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ import { NestjsQueryTypeOrmModule } from '@ptc-org/nestjs-query-typeorm';
|
||||
|
||||
import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
|
||||
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
import { IsFeatureEnabledService } from 'src/engine/core-modules/feature-flag/services/is-feature-enabled.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -17,7 +18,7 @@ import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-
|
||||
resolvers: [],
|
||||
}),
|
||||
],
|
||||
exports: [],
|
||||
providers: [],
|
||||
exports: [IsFeatureEnabledService],
|
||||
providers: [IsFeatureEnabledService],
|
||||
})
|
||||
export class FeatureFlagModule {}
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import {
|
||||
FeatureFlagEntity,
|
||||
FeatureFlagKeys,
|
||||
} from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
|
||||
@Injectable()
|
||||
export class IsFeatureEnabledService {
|
||||
constructor(
|
||||
@InjectRepository(FeatureFlagEntity, 'core')
|
||||
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
|
||||
) {}
|
||||
|
||||
public async isFeatureEnabled(
|
||||
key: FeatureFlagKeys,
|
||||
workspaceId: string,
|
||||
): Promise<boolean> {
|
||||
const featureFlag = await this.featureFlagRepository.findOneBy({
|
||||
workspaceId,
|
||||
key,
|
||||
value: true,
|
||||
});
|
||||
|
||||
return !!featureFlag?.value;
|
||||
}
|
||||
}
|
||||
@ -14,7 +14,7 @@ import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inje
|
||||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
||||
import { SubscriptionStatus } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
|
||||
import { isDefined } from 'src/utils/is-defined';
|
||||
import { BillingService } from 'src/engine/core-modules/billing/billing.service';
|
||||
import { BillingWorkspaceService } from 'src/engine/core-modules/billing/billing.workspace-service';
|
||||
|
||||
enum OnboardingStepValues {
|
||||
SKIPPED = 'SKIPPED',
|
||||
@ -33,7 +33,7 @@ type OnboardingKeyValueType = {
|
||||
@Injectable()
|
||||
export class OnboardingService {
|
||||
constructor(
|
||||
private readonly billingService: BillingService,
|
||||
private readonly billingWorkspaceService: BillingWorkspaceService,
|
||||
private readonly workspaceManagerService: WorkspaceManagerService,
|
||||
private readonly userWorkspaceService: UserWorkspaceService,
|
||||
private readonly keyValuePairService: KeyValuePairService<OnboardingKeyValueType>,
|
||||
@ -45,7 +45,7 @@ export class OnboardingService {
|
||||
|
||||
private async isSubscriptionIncompleteOnboardingStatus(user: User) {
|
||||
const isBillingEnabledForWorkspace =
|
||||
await this.billingService.isBillingEnabledForWorkspace(
|
||||
await this.billingWorkspaceService.isBillingEnabledForWorkspace(
|
||||
user.defaultWorkspaceId,
|
||||
);
|
||||
|
||||
@ -54,7 +54,7 @@ export class OnboardingService {
|
||||
}
|
||||
|
||||
const currentBillingSubscription =
|
||||
await this.billingService.getCurrentBillingSubscription({
|
||||
await this.billingWorkspaceService.getCurrentBillingSubscription({
|
||||
workspaceId: user.defaultWorkspaceId,
|
||||
});
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { WorkspaceManagerService } from 'src/engine/workspace-manager/workspace-manager.service';
|
||||
import { UserWorkspace } from 'src/engine/core-modules/user-workspace/user-workspace.entity';
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { BillingService } from 'src/engine/core-modules/billing/billing.service';
|
||||
import { BillingWorkspaceService } from 'src/engine/core-modules/billing/billing.workspace-service';
|
||||
import { UserWorkspaceService } from 'src/engine/core-modules/user-workspace/user-workspace.service';
|
||||
import { UserService } from 'src/engine/core-modules/user/services/user.service';
|
||||
import { EmailService } from 'src/engine/integrations/email/email.service';
|
||||
@ -46,7 +46,7 @@ describe('WorkspaceService', () => {
|
||||
useValue: {},
|
||||
},
|
||||
{
|
||||
provide: BillingService,
|
||||
provide: BillingWorkspaceService,
|
||||
useValue: {},
|
||||
},
|
||||
{
|
||||
|
||||
@ -14,7 +14,7 @@ import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { ActivateWorkspaceInput } from 'src/engine/core-modules/workspace/dtos/activate-workspace-input';
|
||||
import { UserWorkspace } from 'src/engine/core-modules/user-workspace/user-workspace.entity';
|
||||
import { UserWorkspaceService } from 'src/engine/core-modules/user-workspace/user-workspace.service';
|
||||
import { BillingService } from 'src/engine/core-modules/billing/billing.service';
|
||||
import { BillingWorkspaceService } from 'src/engine/core-modules/billing/billing.workspace-service';
|
||||
import { SendInviteLink } from 'src/engine/core-modules/workspace/dtos/send-invite-link.entity';
|
||||
import { EmailService } from 'src/engine/integrations/email/email.service';
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
@ -30,7 +30,7 @@ export class WorkspaceService extends TypeOrmQueryService<Workspace> {
|
||||
private readonly userWorkspaceRepository: Repository<UserWorkspace>,
|
||||
private readonly workspaceManagerService: WorkspaceManagerService,
|
||||
private readonly userWorkspaceService: UserWorkspaceService,
|
||||
private readonly billingService: BillingService,
|
||||
private readonly billingWorkspaceService: BillingWorkspaceService,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
private readonly emailService: EmailService,
|
||||
private readonly onboardingService: OnboardingService,
|
||||
@ -64,7 +64,7 @@ export class WorkspaceService extends TypeOrmQueryService<Workspace> {
|
||||
assert(workspace, 'Workspace not found');
|
||||
|
||||
await this.userWorkspaceRepository.delete({ workspaceId: id });
|
||||
await this.billingService.deleteSubscription(workspace.id);
|
||||
await this.billingWorkspaceService.deleteSubscription(workspace.id);
|
||||
|
||||
await this.workspaceManagerService.delete(id);
|
||||
|
||||
|
||||
@ -22,7 +22,7 @@ import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { AuthUser } from 'src/engine/decorators/auth/auth-user.decorator';
|
||||
import { ActivateWorkspaceInput } from 'src/engine/core-modules/workspace/dtos/activate-workspace-input';
|
||||
import { BillingSubscription } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
|
||||
import { BillingService } from 'src/engine/core-modules/billing/billing.service';
|
||||
import { BillingWorkspaceService } from 'src/engine/core-modules/billing/billing.workspace-service';
|
||||
import { DemoEnvGuard } from 'src/engine/guards/demo.env.guard';
|
||||
import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.service';
|
||||
import { SendInviteLink } from 'src/engine/core-modules/workspace/dtos/send-invite-link.entity';
|
||||
@ -41,7 +41,7 @@ export class WorkspaceResolver {
|
||||
private readonly workspaceCacheVersionService: WorkspaceCacheVersionService,
|
||||
private readonly userWorkspaceService: UserWorkspaceService,
|
||||
private readonly fileUploadService: FileUploadService,
|
||||
private readonly billingService: BillingService,
|
||||
private readonly billingWorkspaceService: BillingWorkspaceService,
|
||||
) {}
|
||||
|
||||
@Query(() => Workspace)
|
||||
@ -122,7 +122,7 @@ export class WorkspaceResolver {
|
||||
async currentBillingSubscription(
|
||||
@Parent() workspace: Workspace,
|
||||
): Promise<BillingSubscription | null> {
|
||||
return this.billingService.getCurrentBillingSubscription({
|
||||
return this.billingWorkspaceService.getCurrentBillingSubscription({
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user