Refactor onboarding user vars to be absent when user is fully onboarded (#6531)

In this PR:
- take feedbacks from: https://github.com/twentyhq/twenty/pull/6530 /
https://github.com/twentyhq/twenty/pull/6529 /
https://github.com/twentyhq/twenty/pull/6526 /
https://github.com/twentyhq/twenty/pull/6512
- refactor onboarding uservars to be absent when the user is fully
onboarded: isStepComplete ==> isStepIncomplete
- introduce a new workspace.activationStatus: CREATION_ONGOING

I'm retesting the whole flow:
- with/without BILLING
- sign in with/without SSO
- sign up with/without SSO
- another workspaceMembers join the team
- subscriptionCanceled
- access to billingPortal
This commit is contained in:
Charles Bochet
2024-08-04 20:37:36 +02:00
committed by GitHub
parent c543716381
commit 03204021cb
49 changed files with 517 additions and 364 deletions

View File

@ -9,13 +9,16 @@ import { BillingWorkspaceMemberListener } from 'src/engine/core-modules/billing/
import { BillingPortalWorkspaceService } from 'src/engine/core-modules/billing/services/billing-portal.workspace-service';
import { BillingSubscriptionService } from 'src/engine/core-modules/billing/services/billing-subscription.service';
import { BillingWebhookService } from 'src/engine/core-modules/billing/services/billing-webhook.service';
import { BillingService } from 'src/engine/core-modules/billing/services/billing.service';
import { StripeModule } from 'src/engine/core-modules/billing/stripe/stripe.module';
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';
import { UserWorkspaceModule } from 'src/engine/core-modules/user-workspace/user-workspace.module';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
@Module({
imports: [
FeatureFlagModule,
StripeModule,
UserWorkspaceModule,
TypeOrmModule.forFeature(
@ -35,11 +38,13 @@ import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
BillingPortalWorkspaceService,
BillingResolver,
BillingWorkspaceMemberListener,
BillingService,
],
exports: [
BillingSubscriptionService,
BillingPortalWorkspaceService,
BillingWebhookService,
BillingService,
],
})
export class BillingModule {}

View File

@ -1,5 +1,7 @@
import { Field, ObjectType, registerEnumType } from '@nestjs/graphql';
import { IDField } from '@ptc-org/nestjs-query-graphql';
import Stripe from 'stripe';
import {
Column,
CreateDateColumn,
@ -11,12 +13,10 @@ import {
Relation,
UpdateDateColumn,
} from 'typeorm';
import Stripe from 'stripe';
import { IDField } from '@ptc-org/nestjs-query-graphql';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { BillingSubscriptionItem } from 'src/engine/core-modules/billing/entities/billing-subscription-item.entity';
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
import { BillingSubscriptionItem } from 'src/engine/core-modules/billing/entities/billing-subscription-item.entity';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
export enum SubscriptionStatus {
Active = 'active',
@ -76,7 +76,7 @@ export class BillingSubscription {
enum: Object.values(SubscriptionStatus),
nullable: false,
})
status: Stripe.Subscription.Status;
status: SubscriptionStatus;
@Field(() => SubscriptionInterval, { nullable: true })
@Column({

View File

@ -33,7 +33,7 @@ export class UpdateSubscriptionJob {
try {
const billingSubscriptionItem =
await this.billingSubscriptionService.getCurrentBillingSubscriptionItem(
await this.billingSubscriptionService.getCurrentBillingSubscriptionItemOrThrow(
data.workspaceId,
);

View File

@ -70,12 +70,18 @@ export class BillingPortalWorkspaceService {
returnUrlPath?: string,
) {
const currentSubscriptionItem =
await this.billingSubscriptionService.getCurrentBillingSubscription({
workspaceId,
});
await this.billingSubscriptionService.getCurrentBillingSubscriptionOrThrow(
{
workspaceId,
},
);
const stripeCustomerId = currentSubscriptionItem.stripeCustomerId;
if (!stripeCustomerId) {
throw new Error('Error: missing stripeCustomerId');
}
const frontBaseUrl = this.environmentService.get('FRONT_BASE_URL');
const returnUrl = returnUrlPath
? frontBaseUrl + returnUrlPath

View File

@ -79,7 +79,7 @@ export class BillingSubscriptionService {
);
}
async getCurrentBillingSubscription(criteria: {
async getCurrentBillingSubscriptionOrThrow(criteria: {
workspaceId?: string;
stripeCustomerId?: string;
}) {
@ -97,21 +97,15 @@ export class BillingSubscriptionService {
return notCanceledSubscriptions?.[0];
}
async getCurrentBillingSubscriptionItem(
async getCurrentBillingSubscriptionItemOrThrow(
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 billingSubscription = await this.getCurrentBillingSubscriptionOrThrow(
{ workspaceId },
);
const billingSubscriptionItem =
billingSubscription.billingSubscriptionItems.filter(
@ -129,9 +123,10 @@ export class BillingSubscriptionService {
}
async deleteSubscription(workspaceId: string) {
const subscriptionToCancel = await this.getCurrentBillingSubscription({
workspaceId,
});
const subscriptionToCancel =
await this.getCurrentBillingSubscriptionOrThrow({
workspaceId,
});
if (subscriptionToCancel) {
await this.stripeService.cancelSubscription(
@ -142,9 +137,9 @@ export class BillingSubscriptionService {
}
async handleUnpaidInvoices(data: Stripe.SetupIntentSucceededEvent.Data) {
const billingSubscription = await this.getCurrentBillingSubscription({
stripeCustomerId: data.object.customer as string,
});
const billingSubscription = await this.getCurrentBillingSubscriptionOrThrow(
{ stripeCustomerId: data.object.customer as string },
);
if (billingSubscription?.status === 'unpaid') {
await this.stripeService.collectLastInvoice(
@ -154,9 +149,9 @@ export class BillingSubscriptionService {
}
async applyBillingSubscription(user: User) {
const billingSubscription = await this.getCurrentBillingSubscription({
workspaceId: user.defaultWorkspaceId,
});
const billingSubscription = await this.getCurrentBillingSubscriptionOrThrow(
{ workspaceId: user.defaultWorkspaceId },
);
const newInterval =
billingSubscription?.interval === SubscriptionInterval.Year
@ -164,7 +159,9 @@ export class BillingSubscriptionService {
: SubscriptionInterval.Year;
const billingSubscriptionItem =
await this.getCurrentBillingSubscriptionItem(user.defaultWorkspaceId);
await this.getCurrentBillingSubscriptionItemOrThrow(
user.defaultWorkspaceId,
);
const productPrice = await this.stripeService.getStripePrice(
AvailableProduct.BasePlan,

View File

@ -46,7 +46,7 @@ export class BillingWebhookService {
workspaceId: workspaceId,
stripeCustomerId: data.object.customer as string,
stripeSubscriptionId: data.object.id,
status: data.object.status,
status: data.object.status as SubscriptionStatus,
interval: data.object.items.data[0].plan.interval,
},
{

View File

@ -0,0 +1,55 @@
import { Injectable, Logger } from '@nestjs/common';
import { isDefined } from 'class-validator';
import { SubscriptionStatus } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
import { BillingSubscriptionService } from 'src/engine/core-modules/billing/services/billing-subscription.service';
import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum';
import { IsFeatureEnabledService } from 'src/engine/core-modules/feature-flag/services/is-feature-enabled.service';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
@Injectable()
export class BillingService {
protected readonly logger = new Logger(BillingService.name);
constructor(
private readonly environmentService: EnvironmentService,
private readonly billingSubscriptionService: BillingSubscriptionService,
private readonly isFeatureEnabledService: IsFeatureEnabledService,
) {}
isBillingEnabled() {
return this.environmentService.get('IS_BILLING_ENABLED');
}
async hasWorkspaceActiveSubscriptionOrFreeAccess(workspaceId: string) {
const isBillingEnabled = this.isBillingEnabled();
if (!isBillingEnabled) {
return true;
}
const isFreeAccessEnabled =
await this.isFeatureEnabledService.isFeatureEnabled(
FeatureFlagKey.IsFreeAccessEnabled,
workspaceId,
);
if (isFreeAccessEnabled) {
return true;
}
const currentBillingSubscription =
await this.billingSubscriptionService.getCurrentBillingSubscriptionOrThrow(
{ workspaceId },
);
return (
isDefined(currentBillingSubscription) &&
[
SubscriptionStatus.Active,
SubscriptionStatus.Trialing,
SubscriptionStatus.PastDue,
].includes(currentBillingSubscription.status)
);
}
}

View File

@ -141,28 +141,30 @@ export class StripeService {
);
}
formatProductPrices(prices: Stripe.Price[]) {
const result: Record<string, ProductPriceEntity> = {};
formatProductPrices(prices: Stripe.Price[]): ProductPriceEntity[] {
const productPrices: ProductPriceEntity[] = Object.values(
prices
.filter((item) => item.recurring?.interval && item.unit_amount)
.reduce((acc, item: Stripe.Price) => {
const interval = item.recurring?.interval;
prices.forEach((item) => {
const interval = item.recurring?.interval;
if (!interval || !item.unit_amount) {
return acc;
}
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,
};
}
});
if (!acc[interval] || item.created > acc[interval].created) {
acc[interval] = {
unitAmount: item.unit_amount,
recurringInterval: interval,
created: item.created,
stripePriceId: item.id,
};
}
return Object.values(result).sort((a, b) => a.unitAmount - b.unitAmount);
return acc satisfies Record<string, ProductPriceEntity>;
}, {}),
);
return productPrices.sort((a, b) => a.unitAmount - b.unitAmount);
}
}