41 update subscription when workspace member changes 2 (#4252)

* Add loader and disabling on checkout button

* Add Stripe Subscription Item id to subscriptionItem entity

* Handle create and delete workspace members

* Update billing webhook

* Make stripe attribute private

* Fixing webhook error

* Clean migration

* Cancel subscription when deleting workspace

* Fix test

* Add freetrial

* Update navigate after signup

* Add automatic tax collection
This commit is contained in:
martmull
2024-03-01 17:29:28 +01:00
committed by GitHub
parent aa7ead3e8c
commit 8f6200be7d
22 changed files with 436 additions and 131 deletions

View File

@ -29,7 +29,7 @@ export class BillingController {
@Res() res: Response,
) {
if (!req.rawBody) {
res.status(400).send('Missing raw body');
res.status(400).end();
return;
}
@ -38,27 +38,23 @@ export class BillingController {
req.rawBody,
);
if (event.type === WebhookEvent.CUSTOMER_SUBSCRIPTION_UPDATED) {
if (event.data.object.status !== 'active') {
res.status(402).send('Payment did not succeeded');
return;
}
if (
event.type === WebhookEvent.CUSTOMER_SUBSCRIPTION_CREATED ||
event.type === WebhookEvent.CUSTOMER_SUBSCRIPTION_UPDATED
) {
const workspaceId = event.data.object.metadata?.workspaceId;
if (!workspaceId) {
res.status(404).send('Missing workspaceId in webhook event metadata');
res.status(404).end();
return;
}
await this.billingService.createBillingSubscription(
await this.billingService.upsertBillingSubscription(
workspaceId,
event.data,
);
res.status(200).send('Subscription successfully updated');
}
res.status(200).end();
}
}

View File

@ -8,16 +8,24 @@ import { BillingSubscription } from 'src/core/billing/entities/billing-subscript
import { BillingSubscriptionItem } from 'src/core/billing/entities/billing-subscription-item.entity';
import { Workspace } from 'src/core/workspace/workspace.entity';
import { BillingResolver } from 'src/core/billing/billing.resolver';
import { FeatureFlagEntity } from 'src/core/feature-flag/feature-flag.entity';
import { BillingWorkspaceMemberListener } from 'src/core/billing/listeners/billing-workspace-member.listener';
@Module({
imports: [
StripeModule,
TypeOrmModule.forFeature(
[BillingSubscription, BillingSubscriptionItem, Workspace],
[
BillingSubscription,
BillingSubscriptionItem,
Workspace,
FeatureFlagEntity,
],
'core',
),
],
controllers: [BillingController],
providers: [BillingService, BillingResolver],
providers: [BillingService, BillingResolver, BillingWorkspaceMemberListener],
exports: [BillingService],
})
export class BillingModule {}

View File

@ -11,13 +11,13 @@ import { BillingSubscriptionItem } from 'src/core/billing/entities/billing-subsc
import { Workspace } from 'src/core/workspace/workspace.entity';
import { ProductPriceEntity } from 'src/core/billing/dto/product-price.entity';
import { User } from 'src/core/user/user.entity';
import { assert } from 'src/utils/assert';
export enum AvailableProduct {
BasePlan = 'base-plan',
}
export enum WebhookEvent {
CUSTOMER_SUBSCRIPTION_CREATED = 'customer.subscription.created',
CUSTOMER_SUBSCRIPTION_UPDATED = 'customer.subscription.updated',
}
@ -42,9 +42,8 @@ export class BillingService {
}
async getProductPrices(stripeProductId: string) {
const productPrices = await this.stripeService.stripe.prices.search({
query: `product: '${stripeProductId}'`,
});
const productPrices =
await this.stripeService.getProductPrices(stripeProductId);
return this.formatProductPrices(productPrices.data);
}
@ -74,63 +73,101 @@ export class BillingService {
return Object.values(result).sort((a, b) => a.unitAmount - b.unitAmount);
}
async checkout(user: User, priceId: string, successUrlPath?: string) {
const frontBaseUrl = this.environmentService.getFrontBaseUrl();
const session = await this.stripeService.stripe.checkout.sessions.create({
line_items: [
{
price: priceId,
quantity: 1,
},
],
mode: 'subscription',
subscription_data: {
metadata: {
workspaceId: user.defaultWorkspace.id,
},
},
customer_email: user.email,
success_url: successUrlPath
? frontBaseUrl + successUrlPath
: frontBaseUrl,
cancel_url: frontBaseUrl,
async getBillingSubscription(workspaceId: string) {
return await this.billingSubscriptionRepository.findOneOrFail({
where: { workspaceId },
relations: ['billingSubscriptionItems'],
});
assert(session.url, 'Error: missing checkout.session.url');
this.logger.log(`Stripe Checkout Session Url Redirection: ${session.url}`);
return session.url;
}
async createBillingSubscription(
async getBillingSubscriptionItem(
workspaceId: string,
data: Stripe.CustomerSubscriptionUpdatedEvent.Data,
stripeProductId = this.environmentService.getBillingStripeBasePlanProductId(),
) {
const billingSubscription = this.billingSubscriptionRepository.create({
workspaceId: workspaceId,
stripeCustomerId: data.object.customer as string,
stripeSubscriptionId: data.object.id,
status: data.object.status,
const billingSubscription = await this.getBillingSubscription(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 checkout(user: User, priceId: string, successUrlPath?: string) {
const frontBaseUrl = this.environmentService.getFrontBaseUrl();
const successUrl = successUrlPath
? frontBaseUrl + successUrlPath
: frontBaseUrl;
return await this.stripeService.createCheckoutSession(
user,
priceId,
successUrl,
frontBaseUrl,
);
}
async deleteSubscription(workspaceId: string) {
const subscriptionToCancel =
await this.billingSubscriptionRepository.findOneBy({
workspaceId,
});
if (subscriptionToCancel) {
await this.stripeService.cancelSubscription(
subscriptionToCancel.stripeSubscriptionId,
);
await this.billingSubscriptionRepository.delete(subscriptionToCancel.id);
}
}
async upsertBillingSubscription(
workspaceId: string,
data:
| Stripe.CustomerSubscriptionUpdatedEvent.Data
| Stripe.CustomerSubscriptionCreatedEvent.Data,
) {
await this.billingSubscriptionRepository.upsert(
{
workspaceId: workspaceId,
stripeCustomerId: data.object.customer as string,
stripeSubscriptionId: data.object.id,
status: data.object.status,
},
{
conflictPaths: ['stripeSubscriptionId'],
skipUpdateIfNoValuesChanged: true,
},
);
await this.workspaceRepository.update(workspaceId, {
subscriptionStatus: data.object.status,
});
await this.billingSubscriptionRepository.save(billingSubscription);
const billingSubscription = await this.getBillingSubscription(workspaceId);
for (const item of data.object.items.data) {
const billingSubscriptionItem =
this.billingSubscriptionItemRepository.create({
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,
});
await this.billingSubscriptionItemRepository.save(
billingSubscriptionItem,
);
}
await this.workspaceRepository.update(workspaceId, {
subscriptionStatus: 'active',
});
};
}),
{
conflictPaths: ['stripeSubscriptionItemId', 'billingSubscriptionId'],
skipUpdateIfNoValuesChanged: true,
},
);
}
}

View File

@ -4,12 +4,21 @@ import {
Entity,
ManyToOne,
PrimaryGeneratedColumn,
Unique,
UpdateDateColumn,
} from 'typeorm';
import { BillingSubscription } from 'src/core/billing/entities/billing-subscription.entity';
@Entity({ name: 'billingSubscriptionItem', schema: 'core' })
@Unique('IndexOnBillingSubscriptionIdAndStripeProductIdUnique', [
'billingSubscriptionId',
'stripeProductId',
])
@Unique('IndexOnBillingSubscriptionIdAndStripeSubscriptionItemIdUnique', [
'billingSubscriptionId',
'stripeSubscriptionItemId',
])
export class BillingSubscriptionItem {
@PrimaryGeneratedColumn('uuid')
id: string;
@ -41,6 +50,9 @@ export class BillingSubscriptionItem {
@Column({ nullable: false })
stripePriceId: string;
@Column({ nullable: false })
stripeSubscriptionItemId: string;
@Column({ nullable: false })
quantity: number;
}

View File

@ -0,0 +1,59 @@
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { MessageQueueJob } from 'src/integrations/message-queue/interfaces/message-queue-job.interface';
import { BillingService } from 'src/core/billing/billing.service';
import { UserWorkspaceService } from 'src/core/user-workspace/user-workspace.service';
import {
FeatureFlagEntity,
FeatureFlagKeys,
} from 'src/core/feature-flag/feature-flag.entity';
import { StripeService } from 'src/core/billing/stripe/stripe.service';
export type UpdateSubscriptionJobData = { workspaceId: string };
@Injectable()
export class UpdateSubscriptionJob
implements MessageQueueJob<UpdateSubscriptionJobData>
{
protected readonly logger = new Logger(UpdateSubscriptionJob.name);
constructor(
private readonly billingService: BillingService,
private readonly userWorkspaceService: UserWorkspaceService,
private readonly stripeService: StripeService,
@InjectRepository(FeatureFlagEntity, 'core')
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
) {}
async handle(data: UpdateSubscriptionJobData): Promise<void> {
const isSelfBillingEnabled = await this.featureFlagRepository.findOneBy({
workspaceId: data.workspaceId,
key: FeatureFlagKeys.IsSelfBillingEnabled,
value: true,
});
if (!isSelfBillingEnabled) {
return;
}
const workspaceMembersCount =
await this.userWorkspaceService.getWorkspaceMemberCount(data.workspaceId);
if (workspaceMembersCount <= 0) {
return;
}
const billingSubscriptionItem =
await this.billingService.getBillingSubscriptionItem(data.workspaceId);
await this.stripeService.updateSubscriptionItem(
billingSubscriptionItem.stripeSubscriptionItemId,
workspaceMembersCount,
);
this.logger.log(
`Updating workspace ${data.workspaceId} subscription quantity to ${workspaceMembersCount} members`,
);
}
}

View File

@ -0,0 +1,50 @@
import { Inject, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { OnEvent } from '@nestjs/event-emitter';
import { Repository } from 'typeorm';
import { MessageQueue } from 'src/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/integrations/message-queue/services/message-queue.service';
import {
FeatureFlagEntity,
FeatureFlagKeys,
} from 'src/core/feature-flag/feature-flag.entity';
import { ObjectRecordCreateEvent } from 'src/integrations/event-emitter/types/object-record-create.event';
import { WorkspaceMemberObjectMetadata } from 'src/workspace/workspace-sync-metadata/standard-objects/workspace-member.object-metadata';
import {
UpdateSubscriptionJob,
UpdateSubscriptionJobData,
} from 'src/core/billing/jobs/update-subscription.job';
@Injectable()
export class BillingWorkspaceMemberListener {
constructor(
@Inject(MessageQueue.billingQueue)
private readonly messageQueueService: MessageQueueService,
@InjectRepository(FeatureFlagEntity, 'core')
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
) {}
@OnEvent('workspaceMember.created')
@OnEvent('workspaceMember.deleted')
async handleCreateOrDeleteEvent(
payload: ObjectRecordCreateEvent<WorkspaceMemberObjectMetadata>,
) {
const isSelfBillingFeatureFlag = await this.featureFlagRepository.findOneBy(
{
key: FeatureFlagKeys.IsSelfBillingEnabled,
value: true,
workspaceId: payload.workspaceId,
},
);
if (!isSelfBillingFeatureFlag) {
return;
}
await this.messageQueueService.add<UpdateSubscriptionJobData>(
UpdateSubscriptionJob.name,
{ workspaceId: payload.workspaceId },
);
}
}

View File

@ -1,12 +1,15 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import Stripe from 'stripe';
import { EnvironmentService } from 'src/integrations/environment/environment.service';
import { User } from 'src/core/user/user.entity';
import { assert } from 'src/utils/assert';
@Injectable()
export class StripeService {
public readonly stripe: Stripe;
protected readonly logger = new Logger(StripeService.name);
private readonly stripe: Stripe;
constructor(private readonly environmentService: EnvironmentService) {
this.stripe = new Stripe(
@ -25,4 +28,53 @@ export class StripeService {
webhookSecret,
);
}
async getProductPrices(stripeProductId: string) {
return this.stripe.prices.search({
query: `product: '${stripeProductId}'`,
});
}
async updateSubscriptionItem(stripeItemId: string, quantity: number) {
await this.stripe.subscriptionItems.update(stripeItemId, { quantity });
}
async cancelSubscription(stripeSubscriptionId: string) {
await this.stripe.subscriptions.cancel(stripeSubscriptionId);
}
async createCheckoutSession(
user: User,
priceId: string,
successUrl?: string,
cancelUrl?: string,
) {
const session = await this.stripe.checkout.sessions.create({
line_items: [
{
price: priceId,
quantity: 1,
},
],
mode: 'subscription',
subscription_data: {
metadata: {
workspaceId: user.defaultWorkspace.id,
},
trial_period_days:
this.environmentService.getBillingFreeTrialDurationInDays(),
},
automatic_tax: { enabled: true },
tax_id_collection: { enabled: true },
customer_email: user.email,
success_url: successUrl,
cancel_url: cancelUrl,
});
assert(session.url, 'Error: missing checkout.session.url');
this.logger.log(`Stripe Checkout Session Url Redirection: ${session.url}`);
return session.url;
}
}

View File

@ -42,7 +42,7 @@ export class UserWorkspace {
@UpdateDateColumn({ type: 'timestamp with time zone' })
updatedAt: Date;
@Field()
@Column('timestamp with time zone')
@Field({ nullable: true })
@Column('timestamp with time zone', { nullable: true })
deletedAt: Date;
}

View File

@ -7,6 +7,7 @@ import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { UserWorkspace } from 'src/core/user-workspace/user-workspace.entity';
import { UserWorkspaceService } from 'src/core/user-workspace/user-workspace.service';
import { DataSourceModule } from 'src/metadata/data-source/data-source.module';
import { WorkspaceDataSourceModule } from 'src/workspace/workspace-datasource/workspace-datasource.module';
@Module({
imports: [
@ -15,6 +16,7 @@ import { DataSourceModule } from 'src/metadata/data-source/data-source.module';
NestjsQueryTypeOrmModule.forFeature([UserWorkspace], 'core'),
TypeORMModule,
DataSourceModule,
WorkspaceDataSourceModule,
],
services: [UserWorkspaceService],
}),

View File

@ -1,4 +1,5 @@
import { InjectRepository } from '@nestjs/typeorm';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
import { Repository } from 'typeorm';
@ -7,6 +8,10 @@ import { UserWorkspace } from 'src/core/user-workspace/user-workspace.entity';
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
import { DataSourceService } from 'src/metadata/data-source/data-source.service';
import { User } from 'src/core/user/user.entity';
import { WorkspaceDataSourceService } from 'src/workspace/workspace-datasource/workspace-datasource.service';
import { ObjectRecordCreateEvent } from 'src/integrations/event-emitter/types/object-record-create.event';
import { WorkspaceMemberObjectMetadata } from 'src/workspace/workspace-sync-metadata/standard-objects/workspace-member.object-metadata';
import { assert } from 'src/utils/assert';
export class UserWorkspaceService extends TypeOrmQueryService<UserWorkspace> {
constructor(
@ -14,6 +19,8 @@ export class UserWorkspaceService extends TypeOrmQueryService<UserWorkspace> {
private readonly userWorkspaceRepository: Repository<UserWorkspace>,
private readonly dataSourceService: DataSourceService,
private readonly typeORMService: TypeORMService,
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
private eventEmitter: EventEmitter2,
) {
super(userWorkspaceRepository);
}
@ -43,6 +50,35 @@ export class UserWorkspaceService extends TypeOrmQueryService<UserWorkspace> {
user.id
}', '${user.email}', '${user.defaultAvatarUrl ?? ''}')`,
);
const workspaceMember = await workspaceDataSource?.query(
`SELECT * FROM ${dataSourceMetadata.schema}."workspaceMember" WHERE "userId"='${user.id}'`,
);
assert(
workspaceMember.length === 1,
`Error while creating workspace member ${user.email} on workspace ${workspaceId}`,
);
const payload =
new ObjectRecordCreateEvent<WorkspaceMemberObjectMetadata>();
payload.workspaceId = workspaceId;
payload.createdRecord = new WorkspaceMemberObjectMetadata();
payload.createdRecord = workspaceMember[0];
this.eventEmitter.emit('workspaceMember.created', payload);
}
public async getWorkspaceMemberCount(workspaceId: string): Promise<number> {
const dataSourceSchema =
this.workspaceDataSourceService.getSchemaName(workspaceId);
return (
await this.workspaceDataSourceService.executeRawQuery(
`SELECT * FROM ${dataSourceSchema}."workspaceMember"`,
[],
workspaceId,
)
).length;
}
async findUserWorkspaces(userId: string): Promise<UserWorkspace[]> {

View File

@ -82,24 +82,6 @@ export class UserService extends TypeOrmQueryService<User> {
);
}
async createWorkspaceMember(user: User) {
const dataSourceMetadata =
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceIdOrFail(
user.defaultWorkspace.id,
);
const workspaceDataSource =
await this.typeORMService.connectToDataSource(dataSourceMetadata);
await workspaceDataSource?.query(
`INSERT INTO ${dataSourceMetadata.schema}."workspaceMember"
("nameFirstName", "nameLastName", "colorScheme", "userId", "userEmail", "avatarUrl")
VALUES ('${user.firstName}', '${user.lastName}', 'Light', '${
user.id
}', '${user.email}', '${user.defaultAvatarUrl ?? ''}')`,
);
}
async deleteUser(userId: string): Promise<User> {
const user = await this.userRepository.findOneBy({
id: userId,

View File

@ -3,7 +3,8 @@ import { getRepositoryToken } from '@nestjs/typeorm';
import { Workspace } from 'src/core/workspace/workspace.entity';
import { WorkspaceManagerService } from 'src/workspace/workspace-manager/workspace-manager.service';
import { UserService } from 'src/core/user/services/user.service';
import { BillingService } from 'src/core/billing/billing.service';
import { UserWorkspaceService } from 'src/core/user-workspace/user-workspace.service';
import { WorkspaceService } from './workspace.service';
@ -23,7 +24,11 @@ describe('WorkspaceService', () => {
useValue: {},
},
{
provide: UserService,
provide: UserWorkspaceService,
useValue: {},
},
{
provide: BillingService,
useValue: {},
},
],

View File

@ -9,15 +9,17 @@ import { Repository } from 'typeorm';
import { WorkspaceManagerService } from 'src/workspace/workspace-manager/workspace-manager.service';
import { Workspace } from 'src/core/workspace/workspace.entity';
import { User } from 'src/core/user/user.entity';
import { UserService } from 'src/core/user/services/user.service';
import { ActivateWorkspaceInput } from 'src/core/workspace/dtos/activate-workspace-input';
import { UserWorkspaceService } from 'src/core/user-workspace/user-workspace.service';
import { BillingService } from 'src/core/billing/billing.service';
export class WorkspaceService extends TypeOrmQueryService<Workspace> {
constructor(
@InjectRepository(Workspace, 'core')
private readonly workspaceRepository: Repository<Workspace>,
private readonly workspaceManagerService: WorkspaceManagerService,
private readonly userService: UserService,
private readonly userWorkspaceService: UserWorkspaceService,
private readonly billingService: BillingService,
) {
super(workspaceRepository);
}
@ -30,7 +32,10 @@ export class WorkspaceService extends TypeOrmQueryService<Workspace> {
displayName: data.displayName,
});
await this.workspaceManagerService.init(user.defaultWorkspace.id);
await this.userService.createWorkspaceMember(user);
await this.userWorkspaceService.createWorkspaceMember(
user.defaultWorkspace.id,
user,
);
return user.defaultWorkspace;
}
@ -44,6 +49,8 @@ export class WorkspaceService extends TypeOrmQueryService<Workspace> {
assert(workspace, 'Workspace not found');
await this.billingService.deleteSubscription(workspace.id);
await this.workspaceManagerService.delete(id);
if (shouldDeleteCoreWorkspace) {
await this.workspaceRepository.delete(id);

View File

@ -10,6 +10,7 @@ import {
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import Stripe from 'stripe';
import { User } from 'src/core/user/user.entity';
import { FeatureFlagEntity } from 'src/core/feature-flag/feature-flag.entity';
@ -63,7 +64,7 @@ export class Workspace {
@Field()
@Column({ default: 'incomplete' })
subscriptionStatus: 'incomplete' | 'active' | 'canceled';
subscriptionStatus: Stripe.Subscription.Status;
@Field()
activationStatus: 'active' | 'inactive';

View File

@ -8,7 +8,8 @@ import { WorkspaceManagerModule } from 'src/workspace/workspace-manager/workspac
import { WorkspaceResolver } from 'src/core/workspace/workspace.resolver';
import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { FeatureFlagEntity } from 'src/core/feature-flag/feature-flag.entity';
import { UserModule } from 'src/core/user/user.module';
import { UserWorkspaceModule } from 'src/core/user-workspace/user-workspace.module';
import { BillingModule } from 'src/core/billing/billing.module';
import { Workspace } from './workspace.entity';
import { workspaceAutoResolverOpts } from './workspace.auto-resolver-opts';
@ -20,13 +21,14 @@ import { WorkspaceService } from './services/workspace.service';
TypeORMModule,
NestjsQueryGraphQLModule.forFeature({
imports: [
BillingModule,
FileModule,
NestjsQueryTypeOrmModule.forFeature(
[Workspace, FeatureFlagEntity],
'core',
),
UserWorkspaceModule,
WorkspaceManagerModule,
UserModule,
FileModule,
],
services: [WorkspaceService],
resolvers: workspaceAutoResolverOpts,