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:
@ -1,22 +1,22 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
|
||||
import { RunnableSequence } from '@langchain/core/runnables';
|
||||
import { StructuredOutputParser } from '@langchain/core/output_parsers';
|
||||
import { RunnableSequence } from '@langchain/core/runnables';
|
||||
import groupBy from 'lodash.groupby';
|
||||
import { DataSource, QueryFailedError } from 'typeorm';
|
||||
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
|
||||
import { z } from 'zod';
|
||||
import { zodToJsonSchema } from 'zod-to-json-schema';
|
||||
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
|
||||
import groupBy from 'lodash.groupby';
|
||||
|
||||
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
|
||||
import { WorkspaceQueryRunnerService } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-runner.service';
|
||||
import { sqlGenerationPromptTemplate } from 'src/engine/core-modules/ai-sql-query/ai-sql-query.prompt-templates';
|
||||
import { AISQLQueryResult } from 'src/engine/core-modules/ai-sql-query/dtos/ai-sql-query-result.dto';
|
||||
import { LLMChatModelService } from 'src/engine/integrations/llm-chat-model/llm-chat-model.service';
|
||||
import { LLMTracingService } from 'src/engine/integrations/llm-tracing/llm-tracing.service';
|
||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||
import { DEFAULT_LABEL_IDENTIFIER_FIELD_NAME } from 'src/engine/metadata-modules/object-metadata/object-metadata.constants';
|
||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
|
||||
import { StandardObjectFactory } from 'src/engine/workspace-manager/workspace-sync-metadata/factories/standard-object.factory';
|
||||
import { AISQLQueryResult } from 'src/engine/core-modules/ai-sql-query/dtos/ai-sql-query-result.dto';
|
||||
import { sqlGenerationPromptTemplate } from 'src/engine/core-modules/ai-sql-query/ai-sql-query.prompt-templates';
|
||||
|
||||
@Injectable()
|
||||
export class AISQLQueryService {
|
||||
@ -31,9 +31,9 @@ export class AISQLQueryService {
|
||||
|
||||
private getLabelIdentifierName(
|
||||
objectMetadata: ObjectMetadataEntity,
|
||||
dataSourceId,
|
||||
workspaceId,
|
||||
workspaceFeatureFlagsMap,
|
||||
_dataSourceId,
|
||||
_workspaceId,
|
||||
_workspaceFeatureFlagsMap,
|
||||
): string | undefined {
|
||||
const customObjectLabelIdentifierFieldMetadata = objectMetadata.fields.find(
|
||||
(fieldMetadata) =>
|
||||
|
||||
@ -93,10 +93,10 @@ export class GoogleAPIsAuthController {
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
await onboardingServiceInstance.toggleOnboardingConnectAccountCompletion({
|
||||
await onboardingServiceInstance.setOnboardingConnectAccountPending({
|
||||
userId,
|
||||
workspaceId,
|
||||
value: true,
|
||||
value: false,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -1,15 +1,14 @@
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
import { SignInUpService } from 'src/engine/core-modules/auth/services/sign-in-up.service';
|
||||
import { FileUploadService } from 'src/engine/core-modules/file/file-upload/services/file-upload.service';
|
||||
import { UserWorkspaceService } from 'src/engine/core-modules/user-workspace/user-workspace.service';
|
||||
import { WorkspaceService } from 'src/engine/core-modules/workspace/services/workspace.service';
|
||||
import { OnboardingService } from 'src/engine/core-modules/onboarding/onboarding.service';
|
||||
import { UserWorkspaceService } from 'src/engine/core-modules/user-workspace/user-workspace.service';
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
|
||||
describe('SignInUpService', () => {
|
||||
let service: SignInUpService;
|
||||
|
||||
@ -175,6 +175,18 @@ export class SignInUpService {
|
||||
await this.userWorkspaceService.create(user.id, workspace.id);
|
||||
await this.userWorkspaceService.createWorkspaceMember(workspace.id, user);
|
||||
|
||||
await this.onboardingService.setOnboardingConnectAccountPending({
|
||||
userId: user.id,
|
||||
workspaceId: workspace.id,
|
||||
value: true,
|
||||
});
|
||||
|
||||
await this.onboardingService.setOnboardingCreateProfileCompletion({
|
||||
userId: user.id,
|
||||
workspaceId: workspace.id,
|
||||
value: true,
|
||||
});
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@ -222,13 +234,22 @@ export class SignInUpService {
|
||||
|
||||
await this.userWorkspaceService.create(user.id, workspace.id);
|
||||
|
||||
if (user.firstName !== '' || user.lastName === '') {
|
||||
await this.onboardingService.toggleOnboardingCreateProfileCompletion({
|
||||
userId: user.id,
|
||||
workspaceId: workspace.id,
|
||||
value: true,
|
||||
});
|
||||
}
|
||||
await this.onboardingService.setOnboardingConnectAccountPending({
|
||||
userId: user.id,
|
||||
workspaceId: workspace.id,
|
||||
value: true,
|
||||
});
|
||||
|
||||
await this.onboardingService.setOnboardingCreateProfileCompletion({
|
||||
userId: user.id,
|
||||
workspaceId: workspace.id,
|
||||
value: true,
|
||||
});
|
||||
|
||||
await this.onboardingService.setOnboardingInviteTeamPending({
|
||||
workspaceId: workspace.id,
|
||||
value: true,
|
||||
});
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@ -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 {}
|
||||
|
||||
@ -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({
|
||||
|
||||
@ -33,7 +33,7 @@ export class UpdateSubscriptionJob {
|
||||
|
||||
try {
|
||||
const billingSubscriptionItem =
|
||||
await this.billingSubscriptionService.getCurrentBillingSubscriptionItem(
|
||||
await this.billingSubscriptionService.getCurrentBillingSubscriptionItemOrThrow(
|
||||
data.workspaceId,
|
||||
);
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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,
|
||||
},
|
||||
{
|
||||
|
||||
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,16 +1,13 @@
|
||||
import { UseGuards } from '@nestjs/common';
|
||||
import { Query, Args, ArgsType, Field, Int, Resolver } from '@nestjs/graphql';
|
||||
import { Args, ArgsType, Field, Int, Query, Resolver } from '@nestjs/graphql';
|
||||
|
||||
import { Max } from 'class-validator';
|
||||
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { AuthUser } from 'src/engine/decorators/auth/auth-user.decorator';
|
||||
import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard';
|
||||
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
|
||||
import { TIMELINE_CALENDAR_EVENTS_MAX_PAGE_SIZE } from 'src/engine/core-modules/calendar/constants/calendar.constants';
|
||||
import { TimelineCalendarEventsWithTotal } from 'src/engine/core-modules/calendar/dtos/timeline-calendar-events-with-total.dto';
|
||||
import { TimelineCalendarEventService } from 'src/engine/core-modules/calendar/timeline-calendar-event.service';
|
||||
import { UserService } from 'src/engine/core-modules/user/services/user.service';
|
||||
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
|
||||
import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard';
|
||||
|
||||
@ArgsType()
|
||||
class GetTimelineCalendarEventsFromPersonIdArgs {
|
||||
@ -43,12 +40,10 @@ class GetTimelineCalendarEventsFromCompanyIdArgs {
|
||||
export class TimelineCalendarEventResolver {
|
||||
constructor(
|
||||
private readonly timelineCalendarEventService: TimelineCalendarEventService,
|
||||
private readonly userService: UserService,
|
||||
) {}
|
||||
|
||||
@Query(() => TimelineCalendarEventsWithTotal)
|
||||
async getTimelineCalendarEventsFromPersonId(
|
||||
@AuthUser() user: User,
|
||||
@Args()
|
||||
{ personId, page, pageSize }: GetTimelineCalendarEventsFromPersonIdArgs,
|
||||
) {
|
||||
@ -64,7 +59,6 @@ export class TimelineCalendarEventResolver {
|
||||
|
||||
@Query(() => TimelineCalendarEventsWithTotal)
|
||||
async getTimelineCalendarEventsFromCompanyId(
|
||||
@AuthUser() user: User,
|
||||
@Args()
|
||||
{ companyId, page, pageSize }: GetTimelineCalendarEventsFromCompanyIdArgs,
|
||||
) {
|
||||
|
||||
@ -7,7 +7,12 @@ import * as jwt from 'jsonwebtoken';
|
||||
export class JwtWrapperService {
|
||||
constructor(private readonly jwtService: JwtService) {}
|
||||
|
||||
sign(payload: string, options?: JwtSignOptions): string {
|
||||
sign(payload: string | object, options?: JwtSignOptions): string {
|
||||
// Typescript does not handle well the overloads of the sign method, helping it a little bit
|
||||
if (typeof payload === 'object') {
|
||||
return this.jwtService.sign(payload, options);
|
||||
}
|
||||
|
||||
return this.jwtService.sign(payload, options);
|
||||
}
|
||||
|
||||
|
||||
@ -19,10 +19,10 @@ export class OnboardingResolver {
|
||||
@AuthUser() user: User,
|
||||
@AuthWorkspace() workspace: Workspace,
|
||||
): Promise<OnboardingStepSuccess> {
|
||||
await this.onboardingService.toggleOnboardingConnectAccountCompletion({
|
||||
await this.onboardingService.setOnboardingConnectAccountPending({
|
||||
userId: user.id,
|
||||
workspaceId: workspace.id,
|
||||
value: true,
|
||||
value: false,
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
|
||||
@ -1,66 +1,40 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
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 { BillingService } from 'src/engine/core-modules/billing/services/billing.service';
|
||||
import { OnboardingStatus } from 'src/engine/core-modules/onboarding/enums/onboarding-status.enum';
|
||||
import { UserVarsService } from 'src/engine/core-modules/user/user-vars/services/user-vars.service';
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
import { WorkspaceActivationStatus } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
import { isDefined } from 'src/utils/is-defined';
|
||||
|
||||
export enum OnboardingStepKeys {
|
||||
ONBOARDING_CONNECT_ACCOUNT_COMPLETE = 'ONBOARDING_CONNECT_ACCOUNT_COMPLETE',
|
||||
ONBOARDING_INVITE_TEAM_COMPLETE = 'ONBOARDING_INVITE_TEAM_COMPLETE',
|
||||
ONBOARDING_CREATE_PROFILE_COMPLETE = 'ONBOARDING_CREATE_PROFILE_COMPLETE',
|
||||
ONBOARDING_CONNECT_ACCOUNT_PENDING = 'ONBOARDING_CONNECT_ACCOUNT_PENDING',
|
||||
ONBOARDING_INVITE_TEAM_PENDING = 'ONBOARDING_INVITE_TEAM_PENDING',
|
||||
ONBOARDING_CREATE_PROFILE_PENDING = 'ONBOARDING_CREATE_PROFILE_PENDING',
|
||||
}
|
||||
|
||||
export type OnboardingKeyValueTypeMap = {
|
||||
[OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_COMPLETE]: boolean;
|
||||
[OnboardingStepKeys.ONBOARDING_INVITE_TEAM_COMPLETE]: boolean;
|
||||
[OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_COMPLETE]: boolean;
|
||||
[OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_PENDING]: boolean;
|
||||
[OnboardingStepKeys.ONBOARDING_INVITE_TEAM_PENDING]: boolean;
|
||||
[OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_PENDING]: boolean;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class OnboardingService {
|
||||
constructor(
|
||||
private readonly billingSubscriptionService: BillingSubscriptionService,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
private readonly isFeatureEnabledService: IsFeatureEnabledService,
|
||||
private readonly billingService: BillingService,
|
||||
private readonly userVarsService: UserVarsService<OnboardingKeyValueTypeMap>,
|
||||
) {}
|
||||
|
||||
private async isSubscriptionIncompleteOnboardingStatus(user: User) {
|
||||
const isBillingEnabled = this.environmentService.get('IS_BILLING_ENABLED');
|
||||
|
||||
if (!isBillingEnabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const isFreeAccessEnabled =
|
||||
await this.isFeatureEnabledService.isFeatureEnabled(
|
||||
FeatureFlagKey.IsFreeAccessEnabled,
|
||||
const hasSubscription =
|
||||
await this.billingService.hasWorkspaceActiveSubscriptionOrFreeAccess(
|
||||
user.defaultWorkspaceId,
|
||||
);
|
||||
|
||||
if (isFreeAccessEnabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const currentBillingSubscription =
|
||||
await this.billingSubscriptionService.getCurrentBillingSubscription({
|
||||
workspaceId: user.defaultWorkspaceId,
|
||||
});
|
||||
|
||||
return (
|
||||
!isDefined(currentBillingSubscription) ||
|
||||
currentBillingSubscription?.status === SubscriptionStatus.Incomplete
|
||||
);
|
||||
return !hasSubscription;
|
||||
}
|
||||
|
||||
private async isWorkspaceActivationOnboardingStatus(user: User) {
|
||||
private isWorkspaceActivationPending(user: User) {
|
||||
return (
|
||||
user.defaultWorkspace.activationStatus ===
|
||||
WorkspaceActivationStatus.PENDING_CREATION
|
||||
@ -72,7 +46,7 @@ export class OnboardingService {
|
||||
return OnboardingStatus.PLAN_REQUIRED;
|
||||
}
|
||||
|
||||
if (await this.isWorkspaceActivationOnboardingStatus(user)) {
|
||||
if (this.isWorkspaceActivationPending(user)) {
|
||||
return OnboardingStatus.WORKSPACE_ACTIVATION;
|
||||
}
|
||||
|
||||
@ -81,33 +55,33 @@ export class OnboardingService {
|
||||
workspaceId: user.defaultWorkspaceId,
|
||||
});
|
||||
|
||||
const isProfileCreationComplete =
|
||||
userVars.get(OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_COMPLETE) ===
|
||||
const isProfileCreationPending =
|
||||
userVars.get(OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_PENDING) ===
|
||||
true;
|
||||
|
||||
const isConnectAccountComplete =
|
||||
userVars.get(OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_COMPLETE) ===
|
||||
const isConnectAccountPending =
|
||||
userVars.get(OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_PENDING) ===
|
||||
true;
|
||||
|
||||
const isInviteTeamComplete =
|
||||
userVars.get(OnboardingStepKeys.ONBOARDING_INVITE_TEAM_COMPLETE) === true;
|
||||
const isInviteTeamPending =
|
||||
userVars.get(OnboardingStepKeys.ONBOARDING_INVITE_TEAM_PENDING) === true;
|
||||
|
||||
if (!isProfileCreationComplete) {
|
||||
if (isProfileCreationPending) {
|
||||
return OnboardingStatus.PROFILE_CREATION;
|
||||
}
|
||||
|
||||
if (!isConnectAccountComplete) {
|
||||
if (isConnectAccountPending) {
|
||||
return OnboardingStatus.SYNC_EMAIL;
|
||||
}
|
||||
|
||||
if (!isInviteTeamComplete) {
|
||||
if (isInviteTeamPending) {
|
||||
return OnboardingStatus.INVITE_TEAM;
|
||||
}
|
||||
|
||||
return OnboardingStatus.COMPLETED;
|
||||
}
|
||||
|
||||
async toggleOnboardingConnectAccountCompletion({
|
||||
async setOnboardingConnectAccountPending({
|
||||
userId,
|
||||
workspaceId,
|
||||
value,
|
||||
@ -116,29 +90,48 @@ export class OnboardingService {
|
||||
workspaceId: string;
|
||||
value: boolean;
|
||||
}) {
|
||||
if (!value) {
|
||||
await this.userVarsService.delete({
|
||||
userId,
|
||||
workspaceId,
|
||||
key: OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_PENDING,
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await this.userVarsService.set({
|
||||
userId,
|
||||
workspaceId: workspaceId,
|
||||
key: OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_COMPLETE,
|
||||
value,
|
||||
key: OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_PENDING,
|
||||
value: true,
|
||||
});
|
||||
}
|
||||
|
||||
async toggleOnboardingInviteTeamCompletion({
|
||||
async setOnboardingInviteTeamPending({
|
||||
workspaceId,
|
||||
value,
|
||||
}: {
|
||||
workspaceId: string;
|
||||
value: boolean;
|
||||
}) {
|
||||
if (!value) {
|
||||
await this.userVarsService.delete({
|
||||
workspaceId,
|
||||
key: OnboardingStepKeys.ONBOARDING_INVITE_TEAM_PENDING,
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await this.userVarsService.set({
|
||||
workspaceId,
|
||||
key: OnboardingStepKeys.ONBOARDING_INVITE_TEAM_COMPLETE,
|
||||
value,
|
||||
key: OnboardingStepKeys.ONBOARDING_INVITE_TEAM_PENDING,
|
||||
value: true,
|
||||
});
|
||||
}
|
||||
|
||||
async toggleOnboardingCreateProfileCompletion({
|
||||
async setOnboardingCreateProfileCompletion({
|
||||
userId,
|
||||
workspaceId,
|
||||
value,
|
||||
@ -147,11 +140,21 @@ export class OnboardingService {
|
||||
workspaceId: string;
|
||||
value: boolean;
|
||||
}) {
|
||||
if (!value) {
|
||||
await this.userVarsService.delete({
|
||||
userId,
|
||||
workspaceId,
|
||||
key: OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_PENDING,
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await this.userVarsService.set({
|
||||
userId,
|
||||
workspaceId,
|
||||
key: OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_COMPLETE,
|
||||
value,
|
||||
key: OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_PENDING,
|
||||
value: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,6 +47,32 @@ export class WorkspaceService extends TypeOrmQueryService<Workspace> {
|
||||
throw new BadRequestException("'displayName' not provided");
|
||||
}
|
||||
|
||||
const existingWorkspace = await this.workspaceRepository.findOneBy({
|
||||
id: user.defaultWorkspace.id,
|
||||
});
|
||||
|
||||
if (!existingWorkspace) {
|
||||
throw new Error('Workspace not found');
|
||||
}
|
||||
|
||||
if (
|
||||
existingWorkspace.activationStatus ===
|
||||
WorkspaceActivationStatus.ONGOING_CREATION
|
||||
) {
|
||||
throw new Error('Workspace is already being created');
|
||||
}
|
||||
|
||||
if (
|
||||
existingWorkspace.activationStatus !==
|
||||
WorkspaceActivationStatus.PENDING_CREATION
|
||||
) {
|
||||
throw new Error('Worspace is not pending creation');
|
||||
}
|
||||
|
||||
await this.workspaceRepository.update(user.defaultWorkspace.id, {
|
||||
activationStatus: WorkspaceActivationStatus.ONGOING_CREATION,
|
||||
});
|
||||
|
||||
await this.workspaceManagerService.init(user.defaultWorkspace.id);
|
||||
await this.userWorkspaceService.createWorkspaceMember(
|
||||
user.defaultWorkspace.id,
|
||||
@ -142,9 +168,9 @@ export class WorkspaceService extends TypeOrmQueryService<Workspace> {
|
||||
});
|
||||
}
|
||||
|
||||
await this.onboardingService.toggleOnboardingInviteTeamCompletion({
|
||||
await this.onboardingService.setOnboardingInviteTeamPending({
|
||||
workspaceId: workspace.id,
|
||||
value: true,
|
||||
value: false,
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
|
||||
@ -25,9 +25,6 @@ export class WorkspaceWorkspaceMemberListener {
|
||||
async handleUpdateEvent(
|
||||
payload: ObjectRecordUpdateEvent<WorkspaceMemberWorkspaceEntity>,
|
||||
) {
|
||||
const { firstName: firstNameBefore, lastName: lastNameBefore } =
|
||||
payload.properties.before.name;
|
||||
|
||||
const { firstName: firstNameAfter, lastName: lastNameAfter } =
|
||||
payload.properties.after.name;
|
||||
|
||||
@ -39,10 +36,10 @@ export class WorkspaceWorkspaceMemberListener {
|
||||
return;
|
||||
}
|
||||
|
||||
await this.onboardingService.toggleOnboardingCreateProfileCompletion({
|
||||
await this.onboardingService.setOnboardingCreateProfileCompletion({
|
||||
userId: payload.userId,
|
||||
workspaceId: payload.workspaceId,
|
||||
value: true,
|
||||
value: false,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -21,6 +21,7 @@ import { UserWorkspace } from 'src/engine/core-modules/user-workspace/user-works
|
||||
import { User } from 'src/engine/core-modules/user/user.entity';
|
||||
|
||||
export enum WorkspaceActivationStatus {
|
||||
ONGOING_CREATION = 'ONGOING_CREATION',
|
||||
PENDING_CREATION = 'PENDING_CREATION',
|
||||
ACTIVE = 'ACTIVE',
|
||||
INACTIVE = 'INACTIVE',
|
||||
|
||||
@ -118,9 +118,9 @@ export class WorkspaceResolver {
|
||||
async currentBillingSubscription(
|
||||
@Parent() workspace: Workspace,
|
||||
): Promise<BillingSubscription | null> {
|
||||
return this.billingSubscriptionService.getCurrentBillingSubscription({
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
return this.billingSubscriptionService.getCurrentBillingSubscriptionOrThrow(
|
||||
{ workspaceId: workspace.id },
|
||||
);
|
||||
}
|
||||
|
||||
@ResolveField(() => Number)
|
||||
|
||||
Reference in New Issue
Block a user