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

@ -102,6 +102,7 @@ export const useGenerateCombinedFindManyRecordsQuery = ({
} }
pageInfo { pageInfo {
hasNextPage hasNextPage
hasPreviousPage
startCursor startCursor
endCursor endCursor
} }

View File

@ -29,8 +29,6 @@ import { ShowPageContainer } from '@/ui/layout/page/ShowPageContainer';
import { ShowPageLeftContainer } from '@/ui/layout/show-page/components/ShowPageLeftContainer'; import { ShowPageLeftContainer } from '@/ui/layout/show-page/components/ShowPageLeftContainer';
import { ShowPageRightContainer } from '@/ui/layout/show-page/components/ShowPageRightContainer'; import { ShowPageRightContainer } from '@/ui/layout/show-page/components/ShowPageRightContainer';
import { ShowPageSummaryCard } from '@/ui/layout/show-page/components/ShowPageSummaryCard'; import { ShowPageSummaryCard } from '@/ui/layout/show-page/components/ShowPageSummaryCard';
import { ShowPageRecoilScopeContext } from '@/ui/layout/states/ShowPageRecoilScopeContext';
import { RecoilScope } from '@/ui/utilities/recoil-scope/components/RecoilScope';
import { useIsMobile } from '@/ui/utilities/responsive/hooks/useIsMobile'; import { useIsMobile } from '@/ui/utilities/responsive/hooks/useIsMobile';
import { import {
FieldMetadataType, FieldMetadataType,
@ -302,27 +300,25 @@ export const RecordShowContainer = ({
); );
return ( return (
<RecoilScope CustomRecoilScopeContext={ShowPageRecoilScopeContext}> <ShowPageContainer>
<ShowPageContainer> <ShowPageLeftContainer forceMobile={isInRightDrawer}>
<ShowPageLeftContainer forceMobile={isInRightDrawer}> {!isMobile && summaryCard}
{!isMobile && summaryCard} {!isMobile && fieldsBox}
{!isMobile && fieldsBox} </ShowPageLeftContainer>
</ShowPageLeftContainer> <ShowPageRightContainer
<ShowPageRightContainer targetableObject={{
targetableObject={{ id: objectRecordId,
id: objectRecordId, targetObjectNameSingular: objectMetadataItem?.nameSingular,
targetObjectNameSingular: objectMetadataItem?.nameSingular, }}
}} timeline
timeline tasks
tasks notes
notes emails
emails isInRightDrawer={isInRightDrawer}
isInRightDrawer={isInRightDrawer} summaryCard={isMobile ? summaryCard : <></>}
summaryCard={isMobile ? summaryCard : <></>} fieldsBox={fieldsBox}
fieldsBox={fieldsBox} loading={isPrefetchLoading || loading || recordLoading}
loading={isPrefetchLoading || loading || recordLoading} />
/> </ShowPageContainer>
</ShowPageContainer>
</RecoilScope>
); );
}; };

View File

@ -5,7 +5,6 @@ import { useNavigate, useParams, useSearchParams } from 'react-router-dom';
import { useSetRecoilState } from 'recoil'; import { useSetRecoilState } from 'recoil';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { generateDepthOneRecordGqlFields } from '@/object-record/graphql/utils/generateDepthOneRecordGqlFields';
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords'; import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
import { lastShowPageRecordIdState } from '@/object-record/record-field/states/lastShowPageRecordId'; import { lastShowPageRecordIdState } from '@/object-record/record-field/states/lastShowPageRecordId';
import { useRecordIdsFromFindManyCacheRootQuery } from '@/object-record/record-show/hooks/useRecordIdsFromFindManyCacheRootQuery'; import { useRecordIdsFromFindManyCacheRootQuery } from '@/object-record/record-show/hooks/useRecordIdsFromFindManyCacheRootQuery';
@ -37,10 +36,6 @@ export const useRecordShowPagePagination = (
const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular }); const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular });
const recordGqlFields = generateDepthOneRecordGqlFields({
objectMetadataItem,
});
const { filter, orderBy } = const { filter, orderBy } =
useQueryVariablesFromActiveFieldsOfViewOrDefaultView({ useQueryVariablesFromActiveFieldsOfViewOrDefaultView({
objectMetadataItem, objectMetadataItem,
@ -55,7 +50,7 @@ export const useRecordShowPagePagination = (
orderBy, orderBy,
limit: 1, limit: 1,
objectNameSingular, objectNameSingular,
recordGqlFields, recordGqlFields: { id: true },
}); });
const cursorFromRequest = currentRecordsPageInfo?.endCursor; const cursorFromRequest = currentRecordsPageInfo?.endCursor;
@ -77,7 +72,7 @@ export const useRecordShowPagePagination = (
} }
: undefined, : undefined,
objectNameSingular, objectNameSingular,
recordGqlFields, recordGqlFields: { id: true },
onCompleted: (_, pagination) => { onCompleted: (_, pagination) => {
setTotalCountBefore(pagination?.totalCount ?? 0); setTotalCountBefore(pagination?.totalCount ?? 0);
}, },
@ -97,7 +92,7 @@ export const useRecordShowPagePagination = (
} }
: undefined, : undefined,
objectNameSingular, objectNameSingular,
recordGqlFields, recordGqlFields: { id: true },
onCompleted: (_, pagination) => { onCompleted: (_, pagination) => {
setTotalCountAfter(pagination?.totalCount ?? 0); setTotalCountAfter(pagination?.totalCount ?? 0);
}, },

View File

@ -3,10 +3,10 @@ import { useRecoilValue } from 'recoil';
import { currentUserState } from '@/auth/states/currentUserState'; import { currentUserState } from '@/auth/states/currentUserState';
import { Favorite } from '@/favorites/types/Favorite'; import { Favorite } from '@/favorites/types/Favorite';
import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems';
import { useCombinedFindManyRecords } from '@/object-record/multiple-objects/hooks/useCombinedFindManyRecords'; import { useCombinedFindManyRecords } from '@/object-record/multiple-objects/hooks/useCombinedFindManyRecords';
import { PREFETCH_CONFIG } from '@/prefetch/constants/PrefetchConfig';
import { usePrefetchRunQuery } from '@/prefetch/hooks/internal/usePrefetchRunQuery'; import { usePrefetchRunQuery } from '@/prefetch/hooks/internal/usePrefetchRunQuery';
import { FIND_ALL_FAVORITES_OPERATION_SIGNATURE } from '@/prefetch/query-keys/FindAllFavoritesOperationSignature';
import { FIND_ALL_VIEWS_OPERATION_SIGNATURE } from '@/prefetch/query-keys/FindAllViewsOperationSignature';
import { PrefetchKey } from '@/prefetch/types/PrefetchKey'; import { PrefetchKey } from '@/prefetch/types/PrefetchKey';
import { View } from '@/views/types/View'; import { View } from '@/views/types/View';
import { isDefined } from '~/utils/isDefined'; import { isDefined } from '~/utils/isDefined';
@ -24,11 +24,20 @@ export const PrefetchRunQueriesEffect = () => {
prefetchKey: PrefetchKey.AllFavorites, prefetchKey: PrefetchKey.AllFavorites,
}); });
const { objectMetadataItems } = useObjectMetadataItems();
const operationSignatures = Object.values(PREFETCH_CONFIG).map(
({ objectNameSingular, operationSignatureFactory }) => {
const objectMetadataItem = objectMetadataItems.find(
(item) => item.nameSingular === objectNameSingular,
);
return operationSignatureFactory({ objectMetadataItem });
},
);
const { result } = useCombinedFindManyRecords({ const { result } = useCombinedFindManyRecords({
operationSignatures: [ operationSignatures,
FIND_ALL_VIEWS_OPERATION_SIGNATURE,
FIND_ALL_FAVORITES_OPERATION_SIGNATURE,
],
skip: !currentUser, skip: !currentUser,
}); });

View File

@ -1,10 +1,22 @@
import { RecordGqlOperationSignature } from '@/object-record/graphql/types/RecordGqlOperationSignature'; import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { FIND_ALL_FAVORITES_OPERATION_SIGNATURE } from '@/prefetch/query-keys/FindAllFavoritesOperationSignature'; import { RecordGqlOperationSignatureFactory } from '@/object-record/graphql/types/RecordGqlOperationSignatureFactory';
import { FIND_ALL_VIEWS_OPERATION_SIGNATURE } from '@/prefetch/query-keys/FindAllViewsOperationSignature'; import { findAllFavoritesOperationSignatureFactory } from '@/prefetch/operation-signatures/factories/findAllFavoritesOperationSignatureFactory';
import { findAllViewsOperationSignatureFactory } from '@/prefetch/operation-signatures/factories/findAllViewsOperationSignatureFactory';
import { PrefetchKey } from '@/prefetch/types/PrefetchKey'; import { PrefetchKey } from '@/prefetch/types/PrefetchKey';
export const PREFETCH_CONFIG: Record<PrefetchKey, RecordGqlOperationSignature> = export const PREFETCH_CONFIG: Record<
PrefetchKey,
{ {
ALL_VIEWS: FIND_ALL_VIEWS_OPERATION_SIGNATURE, objectNameSingular: CoreObjectNameSingular;
ALL_FAVORITES: FIND_ALL_FAVORITES_OPERATION_SIGNATURE, operationSignatureFactory: RecordGqlOperationSignatureFactory;
}; }
> = {
ALL_VIEWS: {
objectNameSingular: CoreObjectNameSingular.View,
operationSignatureFactory: findAllViewsOperationSignatureFactory,
},
ALL_FAVORITES: {
objectNameSingular: CoreObjectNameSingular.Favorite,
operationSignatureFactory: findAllFavoritesOperationSignatureFactory,
},
};

View File

@ -2,7 +2,6 @@ import { useSetRecoilState } from 'recoil';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { useUpsertFindManyRecordsQueryInCache } from '@/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache'; import { useUpsertFindManyRecordsQueryInCache } from '@/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache';
import { generateDepthOneRecordGqlFields } from '@/object-record/graphql/utils/generateDepthOneRecordGqlFields';
import { ObjectRecord } from '@/object-record/types/ObjectRecord'; import { ObjectRecord } from '@/object-record/types/ObjectRecord';
import { PREFETCH_CONFIG } from '@/prefetch/constants/PrefetchConfig'; import { PREFETCH_CONFIG } from '@/prefetch/constants/PrefetchConfig';
import { prefetchIsLoadedFamilyState } from '@/prefetch/states/prefetchIsLoadedFamilyState'; import { prefetchIsLoadedFamilyState } from '@/prefetch/states/prefetchIsLoadedFamilyState';
@ -18,10 +17,16 @@ export const usePrefetchRunQuery = <T extends ObjectRecord>({
const setPrefetchDataIsLoaded = useSetRecoilState( const setPrefetchDataIsLoaded = useSetRecoilState(
prefetchIsLoadedFamilyState(prefetchKey), prefetchIsLoadedFamilyState(prefetchKey),
); );
const { operationSignatureFactory, objectNameSingular } =
PREFETCH_CONFIG[prefetchKey];
const { objectMetadataItem } = useObjectMetadataItem({ const { objectMetadataItem } = useObjectMetadataItem({
objectNameSingular: PREFETCH_CONFIG[prefetchKey].objectNameSingular, objectNameSingular,
}); });
const operationSignature = operationSignatureFactory({ objectMetadataItem });
const { upsertFindManyRecordsQueryInCache } = const { upsertFindManyRecordsQueryInCache } =
useUpsertFindManyRecordsQueryInCache({ useUpsertFindManyRecordsQueryInCache({
objectMetadataItem: objectMetadataItem, objectMetadataItem: objectMetadataItem,
@ -30,10 +35,8 @@ export const usePrefetchRunQuery = <T extends ObjectRecord>({
const upsertRecordsInCache = (records: T[]) => { const upsertRecordsInCache = (records: T[]) => {
setPrefetchDataIsLoaded(false); setPrefetchDataIsLoaded(false);
upsertFindManyRecordsQueryInCache({ upsertFindManyRecordsQueryInCache({
queryVariables: PREFETCH_CONFIG[prefetchKey].variables, queryVariables: operationSignature.variables,
recordGqlFields: recordGqlFields: operationSignature.fields,
PREFETCH_CONFIG[prefetchKey].fields ??
generateDepthOneRecordGqlFields({ objectMetadataItem }),
objectRecordsToOverwrite: records, objectRecordsToOverwrite: records,
computeReferences: false, computeReferences: false,
}); });

View File

@ -2,7 +2,6 @@ import { useRecoilValue } from 'recoil';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { RecordGqlOperationFilter } from '@/object-record/graphql/types/RecordGqlOperationFilter'; import { RecordGqlOperationFilter } from '@/object-record/graphql/types/RecordGqlOperationFilter';
import { generateDepthOneRecordGqlFields } from '@/object-record/graphql/utils/generateDepthOneRecordGqlFields';
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords'; import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
import { ObjectRecord } from '@/object-record/types/ObjectRecord'; import { ObjectRecord } from '@/object-record/types/ObjectRecord';
import { PREFETCH_CONFIG } from '@/prefetch/constants/PrefetchConfig'; import { PREFETCH_CONFIG } from '@/prefetch/constants/PrefetchConfig';
@ -17,21 +16,18 @@ export const usePrefetchedData = <T extends ObjectRecord>(
prefetchIsLoadedFamilyState(prefetchKey), prefetchIsLoadedFamilyState(prefetchKey),
); );
const prefetchQueryKey = PREFETCH_CONFIG[prefetchKey]; const { operationSignatureFactory, objectNameSingular } =
PREFETCH_CONFIG[prefetchKey];
const { objectMetadataItem } = useObjectMetadataItem({ const { objectMetadataItem } = useObjectMetadataItem({
objectNameSingular: prefetchQueryKey.objectNameSingular, objectNameSingular,
}); });
const { records } = useFindManyRecords<T>({ const { records } = useFindManyRecords<T>({
skip: !isDataPrefetched, skip: !isDataPrefetched,
objectNameSingular: prefetchQueryKey.objectNameSingular, objectNameSingular: objectNameSingular,
recordGqlFields: recordGqlFields:
prefetchQueryKey.fields ?? operationSignatureFactory({ objectMetadataItem }).fields ?? filter,
generateDepthOneRecordGqlFields({
objectMetadataItem,
}),
filter,
}); });
return { return {

View File

@ -0,0 +1,15 @@
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { RecordGqlOperationSignatureFactory } from '@/object-record/graphql/types/RecordGqlOperationSignatureFactory';
import { generateDepthOneRecordGqlFields } from '@/object-record/graphql/utils/generateDepthOneRecordGqlFields';
export const findAllFavoritesOperationSignatureFactory: RecordGqlOperationSignatureFactory =
({ objectMetadataItem }: { objectMetadataItem: ObjectMetadataItem }) => ({
objectNameSingular: CoreObjectNameSingular.Favorite,
variables: {},
fields: {
...generateDepthOneRecordGqlFields({
objectMetadataItem,
}),
},
});

View File

@ -0,0 +1,24 @@
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { RecordGqlOperationSignatureFactory } from '@/object-record/graphql/types/RecordGqlOperationSignatureFactory';
export const findAllViewsOperationSignatureFactory: RecordGqlOperationSignatureFactory =
() => ({
objectNameSingular: CoreObjectNameSingular.View,
variables: {},
fields: {
id: true,
createdAt: true,
updatedAt: true,
isCompact: true,
objectMetadataId: true,
position: true,
type: true,
kanbanFieldMetadataId: true,
name: true,
icon: true,
key: true,
viewFilters: true,
viewSorts: true,
viewFields: true,
},
});

View File

@ -1,8 +0,0 @@
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { RecordGqlOperationSignature } from '@/object-record/graphql/types/RecordGqlOperationSignature';
export const FIND_ALL_FAVORITES_OPERATION_SIGNATURE: RecordGqlOperationSignature =
{
objectNameSingular: CoreObjectNameSingular.Favorite,
variables: {},
};

View File

@ -1,23 +0,0 @@
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { RecordGqlOperationSignature } from '@/object-record/graphql/types/RecordGqlOperationSignature';
export const FIND_ALL_VIEWS_OPERATION_SIGNATURE: RecordGqlOperationSignature = {
objectNameSingular: CoreObjectNameSingular.View,
variables: {},
fields: {
id: true,
createdAt: true,
updatedAt: true,
isCompact: true,
objectMetadataId: true,
position: true,
type: true,
kanbanFieldMetadataId: true,
name: true,
icon: true,
key: true,
viewFilters: true,
viewSorts: true,
viewFields: true,
},
};

View File

@ -6,5 +6,5 @@ export const prefetchIsLoadedFamilyState = createFamilyState<
PrefetchKey PrefetchKey
>({ >({
key: 'prefetchIsLoadedFamilyState', key: 'prefetchIsLoadedFamilyState',
defaultValue: true, defaultValue: false,
}); });

View File

@ -1,4 +0,0 @@
import { createContext } from 'react';
/* istanbul ignore next */
export const ShowPageRecoilScopeContext = createContext<string | null>(null);

View File

@ -1,3 +1,4 @@
/* eslint-disable no-console */
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { existsSync } from 'fs'; import { existsSync } from 'fs';

View File

@ -1,3 +1,5 @@
import { Logger } from '@nestjs/common';
import { Command, CommandRunner } from 'nest-commander'; import { Command, CommandRunner } from 'nest-commander';
import { EntityManager } from 'typeorm'; import { EntityManager } from 'typeorm';
@ -43,6 +45,7 @@ import { WorkspaceSyncMetadataService } from 'src/engine/workspace-manager/works
}) })
export class DataSeedWorkspaceCommand extends CommandRunner { export class DataSeedWorkspaceCommand extends CommandRunner {
workspaceIds = [SEED_APPLE_WORKSPACE_ID, SEED_TWENTY_WORKSPACE_ID]; workspaceIds = [SEED_APPLE_WORKSPACE_ID, SEED_TWENTY_WORKSPACE_ID];
private readonly logger = new Logger(DataSeedWorkspaceCommand.name);
constructor( constructor(
private readonly dataSourceService: DataSourceService, private readonly dataSourceService: DataSourceService,
@ -86,7 +89,7 @@ export class DataSeedWorkspaceCommand extends CommandRunner {
}); });
} }
} catch (error) { } catch (error) {
console.error(error); this.logger.error(error);
return; return;
} }
@ -197,7 +200,7 @@ export class DataSeedWorkspaceCommand extends CommandRunner {
}, },
); );
} catch (error) { } catch (error) {
console.error(error); this.logger.error(error);
} }
await this.typeORMService.disconnectFromDataSource(dataSourceMetadata.id); await this.typeORMService.disconnectFromDataSource(dataSourceMetadata.id);

View File

@ -9,7 +9,6 @@ import { DataSeedWorkspaceCommand } from 'src/database/commands/data-seed-dev-wo
import { ConfirmationQuestion } from 'src/database/commands/questions/confirmation.question'; import { ConfirmationQuestion } from 'src/database/commands/questions/confirmation.question';
import { UpgradeTo0_23CommandModule } from 'src/database/commands/upgrade-version/0-23/0-23-upgrade-version.module'; import { UpgradeTo0_23CommandModule } from 'src/database/commands/upgrade-version/0-23/0-23-upgrade-version.module';
import { UpgradeVersionModule } from 'src/database/commands/upgrade-version/upgrade-version.module'; import { UpgradeVersionModule } from 'src/database/commands/upgrade-version/upgrade-version.module';
import { WorkspaceAddTotalCountCommand } from 'src/database/commands/workspace-add-total-count.command';
import { TypeORMModule } from 'src/database/typeorm/typeorm.module'; import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { BillingSubscription } from 'src/engine/core-modules/billing/entities/billing-subscription.entity'; import { BillingSubscription } from 'src/engine/core-modules/billing/entities/billing-subscription.entity';
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity'; import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
@ -51,7 +50,6 @@ import { WorkspaceSyncMetadataModule } from 'src/engine/workspace-manager/worksp
providers: [ providers: [
DataSeedWorkspaceCommand, DataSeedWorkspaceCommand,
DataSeedDemoWorkspaceCommand, DataSeedDemoWorkspaceCommand,
WorkspaceAddTotalCountCommand,
ConfirmationQuestion, ConfirmationQuestion,
StartDataSeedDemoWorkspaceCronCommand, StartDataSeedDemoWorkspaceCronCommand,
StopDataSeedDemoWorkspaceCronCommand, StopDataSeedDemoWorkspaceCronCommand,

View File

@ -5,7 +5,6 @@ import chalk from 'chalk';
import { Command, CommandRunner, Option } from 'nest-commander'; import { Command, CommandRunner, Option } from 'nest-commander';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
import { UpdateFileFolderStructureCommand } from 'src/database/commands/upgrade-version/0-23/0-23-update-file-folder-structure.command';
import { OnboardingService } from 'src/engine/core-modules/onboarding/onboarding.service'; import { OnboardingService } from 'src/engine/core-modules/onboarding/onboarding.service';
import { import {
Workspace, Workspace,
@ -21,7 +20,9 @@ interface BackfillNewOnboardingUserVarsCommandOptions {
description: 'Backfill new onboarding user vars for existing workspaces', description: 'Backfill new onboarding user vars for existing workspaces',
}) })
export class BackfillNewOnboardingUserVarsCommand extends CommandRunner { export class BackfillNewOnboardingUserVarsCommand extends CommandRunner {
private readonly logger = new Logger(UpdateFileFolderStructureCommand.name); private readonly logger = new Logger(
BackfillNewOnboardingUserVarsCommand.name,
);
constructor( constructor(
@InjectRepository(Workspace, 'core') @InjectRepository(Workspace, 'core')
private readonly workspaceRepository: Repository<Workspace>, private readonly workspaceRepository: Repository<Workspace>,
@ -43,22 +44,13 @@ export class BackfillNewOnboardingUserVarsCommand extends CommandRunner {
_passedParam: string[], _passedParam: string[],
options: BackfillNewOnboardingUserVarsCommandOptions, options: BackfillNewOnboardingUserVarsCommandOptions,
): Promise<void> { ): Promise<void> {
let workspaces; const workspaces = await this.workspaceRepository.find({
where: {
if (options.workspaceId) { activationStatus: WorkspaceActivationStatus.PENDING_CREATION,
workspaces = await this.workspaceRepository.find({ ...(options.workspaceId && { id: options.workspaceId }),
where: { },
activationStatus: WorkspaceActivationStatus.ACTIVE, relations: ['users'],
id: options.workspaceId, });
},
relations: ['users'],
});
} else {
workspaces = await this.workspaceRepository.find({
where: { activationStatus: WorkspaceActivationStatus.ACTIVE },
relations: ['users'],
});
}
if (!workspaces.length) { if (!workspaces.length) {
this.logger.log(chalk.yellow('No workspace found')); this.logger.log(chalk.yellow('No workspace found'));
@ -75,19 +67,19 @@ export class BackfillNewOnboardingUserVarsCommand extends CommandRunner {
chalk.green(`Running command on workspace ${workspace.id}`), chalk.green(`Running command on workspace ${workspace.id}`),
); );
await this.onboardingService.toggleOnboardingInviteTeamCompletion({ await this.onboardingService.setOnboardingInviteTeamPending({
workspaceId: workspace.id, workspaceId: workspace.id,
value: true, value: true,
}); });
for (const user of workspace.users) { for (const user of workspace.users) {
await this.onboardingService.toggleOnboardingConnectAccountCompletion({ await this.onboardingService.setOnboardingCreateProfileCompletion({
userId: user.id, userId: user.id,
workspaceId: workspace.id, workspaceId: workspace.id,
value: true, value: true,
}); });
await this.onboardingService.toggleOnboardingCreateProfileCompletion({ await this.onboardingService.setOnboardingConnectAccountPending({
userId: user.id, userId: user.id,
workspaceId: workspace.id, workspaceId: workspace.id,
value: true, value: true,

View File

@ -1,49 +0,0 @@
import { Command, CommandRunner } from 'nest-commander';
import chalk from 'chalk';
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
@Command({
name: 'workspace:add-total-count',
description: 'Add pg_graphql total count directive to all workspace tables',
})
export class WorkspaceAddTotalCountCommand extends CommandRunner {
constructor(private readonly typeORMService: TypeORMService) {
super();
}
async run(): Promise<void> {
const mainDataSource = this.typeORMService.getMainDataSource();
try {
await mainDataSource.query(`
DO $$
DECLARE
schema_cursor CURSOR FOR SELECT schema_name FROM information_schema.schemata WHERE schema_name LIKE 'workspace_%';
schema_name text;
table_rec record;
BEGIN
OPEN schema_cursor;
LOOP
FETCH schema_cursor INTO schema_name;
EXIT WHEN NOT FOUND;
FOR table_rec IN SELECT t.table_name FROM information_schema.tables t WHERE t.table_schema = schema_name
LOOP
EXECUTE 'COMMENT ON TABLE ' || quote_ident(schema_name) || '.' || quote_ident(table_rec.table_name) || ' IS e''@graphql({"totalCount": {"enabled": true}})'';';
END LOOP;
END LOOP;
CLOSE schema_cursor;
END $$;
`);
console.log(
chalk.green('Total count directive added to all workspace tables'),
);
} catch (error) {
console.log(
chalk.red('Error adding total count directive to all workspace tables'),
);
}
}
}

View File

@ -0,0 +1,69 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class UpdateActivationStatusEnumPendingCreationStatus1722256203541
implements MigrationInterface
{
name = 'UpdateActivationStatusEnumPendingCreationStatus1722256203541';
public async up(queryRunner: QueryRunner): Promise<void> {
// Set current column as text
await queryRunner.query(
`ALTER TABLE "core"."workspace" ALTER COLUMN "activationStatus" SET DATA TYPE text USING "activationStatus"::text`,
);
// Drop default value
await queryRunner.query(
`ALTER TABLE "core"."workspace" ALTER COLUMN "activationStatus" DROP DEFAULT`,
);
// Drop the old enum type
await queryRunner.query(
`DROP TYPE "core"."workspace_activationStatus_enum"`,
);
await queryRunner.query(
`CREATE TYPE "core"."workspace_activationStatus_enum" AS ENUM('PENDING_CREATION', 'ONGOING_CREATION', 'ACTIVE', 'INACTIVE')`,
);
// Re-apply the enum type
await queryRunner.query(
`ALTER TABLE "core"."workspace" ALTER COLUMN "activationStatus" SET DATA TYPE "core"."workspace_activationStatus_enum" USING "activationStatus"::"core"."workspace_activationStatus_enum"`,
);
// Update default value
await queryRunner.query(
`ALTER TABLE "core"."workspace" ALTER COLUMN "activationStatus" SET DEFAULT 'INACTIVE'`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
// Set current column as text
await queryRunner.query(
`ALTER TABLE "core"."workspace" ALTER COLUMN "activationStatus" SET DATA TYPE text USING "activationStatus"::text`,
);
// Drop default value
await queryRunner.query(
`ALTER TABLE "core"."workspace" ALTER COLUMN "activationStatus" DROP DEFAULT`,
);
// Drop the old enum type
await queryRunner.query(
`DROP TYPE "core"."workspace_activationStatus_enum"`,
);
await queryRunner.query(
`CREATE TYPE "core"."workspace_activationStatus_enum" AS ENUM('PENDING_CREATION', 'ACTIVE', 'INACTIVE')`,
);
// Re-apply the enum type
await queryRunner.query(
`ALTER TABLE "core"."workspace" ALTER COLUMN "activationStatus" SET DATA TYPE "core"."workspace_activationStatus_enum" USING "activationStatus"::"core"."workspace_activationStatus_enum"`,
);
// Update default value
await queryRunner.query(
`ALTER TABLE "core"."workspace" ALTER COLUMN "activationStatus" SET DEFAULT 'INACTIVE'`,
);
}
}

View File

@ -1,22 +1,22 @@
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { RunnableSequence } from '@langchain/core/runnables';
import { StructuredOutputParser } from '@langchain/core/output_parsers'; import { StructuredOutputParser } from '@langchain/core/output_parsers';
import { RunnableSequence } from '@langchain/core/runnables';
import groupBy from 'lodash.groupby';
import { DataSource, QueryFailedError } from 'typeorm'; import { DataSource, QueryFailedError } from 'typeorm';
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
import { z } from 'zod'; import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema'; 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 { 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 { LLMChatModelService } from 'src/engine/integrations/llm-chat-model/llm-chat-model.service';
import { LLMTracingService } from 'src/engine/integrations/llm-tracing/llm-tracing.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 { 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 { 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() @Injectable()
export class AISQLQueryService { export class AISQLQueryService {
@ -31,9 +31,9 @@ export class AISQLQueryService {
private getLabelIdentifierName( private getLabelIdentifierName(
objectMetadata: ObjectMetadataEntity, objectMetadata: ObjectMetadataEntity,
dataSourceId, _dataSourceId,
workspaceId, _workspaceId,
workspaceFeatureFlagsMap, _workspaceFeatureFlagsMap,
): string | undefined { ): string | undefined {
const customObjectLabelIdentifierFieldMetadata = objectMetadata.fields.find( const customObjectLabelIdentifierFieldMetadata = objectMetadata.fields.find(
(fieldMetadata) => (fieldMetadata) =>

View File

@ -93,10 +93,10 @@ export class GoogleAPIsAuthController {
workspaceId, workspaceId,
); );
await onboardingServiceInstance.toggleOnboardingConnectAccountCompletion({ await onboardingServiceInstance.setOnboardingConnectAccountPending({
userId, userId,
workspaceId, workspaceId,
value: true, value: false,
}); });
} }

View File

@ -1,15 +1,14 @@
import { HttpService } from '@nestjs/axios';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm'; 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 { 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 { 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 { 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', () => { describe('SignInUpService', () => {
let service: SignInUpService; let service: SignInUpService;

View File

@ -175,6 +175,18 @@ export class SignInUpService {
await this.userWorkspaceService.create(user.id, workspace.id); await this.userWorkspaceService.create(user.id, workspace.id);
await this.userWorkspaceService.createWorkspaceMember(workspace.id, user); 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; return user;
} }
@ -222,13 +234,22 @@ export class SignInUpService {
await this.userWorkspaceService.create(user.id, workspace.id); await this.userWorkspaceService.create(user.id, workspace.id);
if (user.firstName !== '' || user.lastName === '') { await this.onboardingService.setOnboardingConnectAccountPending({
await this.onboardingService.toggleOnboardingCreateProfileCompletion({ userId: user.id,
userId: user.id, workspaceId: workspace.id,
workspaceId: workspace.id, value: true,
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; return user;
} }

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

View File

@ -1,5 +1,7 @@
import { Field, ObjectType, registerEnumType } from '@nestjs/graphql'; import { Field, ObjectType, registerEnumType } from '@nestjs/graphql';
import { IDField } from '@ptc-org/nestjs-query-graphql';
import Stripe from 'stripe';
import { import {
Column, Column,
CreateDateColumn, CreateDateColumn,
@ -11,12 +13,10 @@ import {
Relation, Relation,
UpdateDateColumn, UpdateDateColumn,
} from 'typeorm'; } 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 { 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 { export enum SubscriptionStatus {
Active = 'active', Active = 'active',
@ -76,7 +76,7 @@ export class BillingSubscription {
enum: Object.values(SubscriptionStatus), enum: Object.values(SubscriptionStatus),
nullable: false, nullable: false,
}) })
status: Stripe.Subscription.Status; status: SubscriptionStatus;
@Field(() => SubscriptionInterval, { nullable: true }) @Field(() => SubscriptionInterval, { nullable: true })
@Column({ @Column({

View File

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

View File

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

View File

@ -79,7 +79,7 @@ export class BillingSubscriptionService {
); );
} }
async getCurrentBillingSubscription(criteria: { async getCurrentBillingSubscriptionOrThrow(criteria: {
workspaceId?: string; workspaceId?: string;
stripeCustomerId?: string; stripeCustomerId?: string;
}) { }) {
@ -97,21 +97,15 @@ export class BillingSubscriptionService {
return notCanceledSubscriptions?.[0]; return notCanceledSubscriptions?.[0];
} }
async getCurrentBillingSubscriptionItem( async getCurrentBillingSubscriptionItemOrThrow(
workspaceId: string, workspaceId: string,
stripeProductId = this.environmentService.get( stripeProductId = this.environmentService.get(
'BILLING_STRIPE_BASE_PLAN_PRODUCT_ID', 'BILLING_STRIPE_BASE_PLAN_PRODUCT_ID',
), ),
) { ) {
const billingSubscription = await this.getCurrentBillingSubscription({ const billingSubscription = await this.getCurrentBillingSubscriptionOrThrow(
workspaceId, { workspaceId },
}); );
if (!billingSubscription) {
throw new Error(
`Cannot find billingSubscriptionItem for product ${stripeProductId} for workspace ${workspaceId}`,
);
}
const billingSubscriptionItem = const billingSubscriptionItem =
billingSubscription.billingSubscriptionItems.filter( billingSubscription.billingSubscriptionItems.filter(
@ -129,9 +123,10 @@ export class BillingSubscriptionService {
} }
async deleteSubscription(workspaceId: string) { async deleteSubscription(workspaceId: string) {
const subscriptionToCancel = await this.getCurrentBillingSubscription({ const subscriptionToCancel =
workspaceId, await this.getCurrentBillingSubscriptionOrThrow({
}); workspaceId,
});
if (subscriptionToCancel) { if (subscriptionToCancel) {
await this.stripeService.cancelSubscription( await this.stripeService.cancelSubscription(
@ -142,9 +137,9 @@ export class BillingSubscriptionService {
} }
async handleUnpaidInvoices(data: Stripe.SetupIntentSucceededEvent.Data) { async handleUnpaidInvoices(data: Stripe.SetupIntentSucceededEvent.Data) {
const billingSubscription = await this.getCurrentBillingSubscription({ const billingSubscription = await this.getCurrentBillingSubscriptionOrThrow(
stripeCustomerId: data.object.customer as string, { stripeCustomerId: data.object.customer as string },
}); );
if (billingSubscription?.status === 'unpaid') { if (billingSubscription?.status === 'unpaid') {
await this.stripeService.collectLastInvoice( await this.stripeService.collectLastInvoice(
@ -154,9 +149,9 @@ export class BillingSubscriptionService {
} }
async applyBillingSubscription(user: User) { async applyBillingSubscription(user: User) {
const billingSubscription = await this.getCurrentBillingSubscription({ const billingSubscription = await this.getCurrentBillingSubscriptionOrThrow(
workspaceId: user.defaultWorkspaceId, { workspaceId: user.defaultWorkspaceId },
}); );
const newInterval = const newInterval =
billingSubscription?.interval === SubscriptionInterval.Year billingSubscription?.interval === SubscriptionInterval.Year
@ -164,7 +159,9 @@ export class BillingSubscriptionService {
: SubscriptionInterval.Year; : SubscriptionInterval.Year;
const billingSubscriptionItem = const billingSubscriptionItem =
await this.getCurrentBillingSubscriptionItem(user.defaultWorkspaceId); await this.getCurrentBillingSubscriptionItemOrThrow(
user.defaultWorkspaceId,
);
const productPrice = await this.stripeService.getStripePrice( const productPrice = await this.stripeService.getStripePrice(
AvailableProduct.BasePlan, AvailableProduct.BasePlan,

View File

@ -46,7 +46,7 @@ export class BillingWebhookService {
workspaceId: workspaceId, workspaceId: workspaceId,
stripeCustomerId: data.object.customer as string, stripeCustomerId: data.object.customer as string,
stripeSubscriptionId: data.object.id, stripeSubscriptionId: data.object.id,
status: data.object.status, status: data.object.status as SubscriptionStatus,
interval: data.object.items.data[0].plan.interval, 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[]) { formatProductPrices(prices: Stripe.Price[]): ProductPriceEntity[] {
const result: Record<string, 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) => { if (!interval || !item.unit_amount) {
const interval = item.recurring?.interval; return acc;
}
if (!interval || !item.unit_amount) { if (!acc[interval] || item.created > acc[interval].created) {
return; acc[interval] = {
} unitAmount: item.unit_amount,
if ( recurringInterval: interval,
!result[interval] || created: item.created,
item.created > (result[interval]?.created || 0) stripePriceId: item.id,
) { };
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); return acc satisfies Record<string, ProductPriceEntity>;
}, {}),
);
return productPrices.sort((a, b) => a.unitAmount - b.unitAmount);
} }
} }

View File

@ -1,16 +1,13 @@
import { UseGuards } from '@nestjs/common'; 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 { Max } from 'class-validator';
import { User } from 'src/engine/core-modules/user/user.entity'; import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
import { AuthUser } from 'src/engine/decorators/auth/auth-user.decorator';
import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard';
import { TIMELINE_CALENDAR_EVENTS_MAX_PAGE_SIZE } from 'src/engine/core-modules/calendar/constants/calendar.constants'; 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 { 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 { TimelineCalendarEventService } from 'src/engine/core-modules/calendar/timeline-calendar-event.service';
import { UserService } from 'src/engine/core-modules/user/services/user.service'; import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard';
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
@ArgsType() @ArgsType()
class GetTimelineCalendarEventsFromPersonIdArgs { class GetTimelineCalendarEventsFromPersonIdArgs {
@ -43,12 +40,10 @@ class GetTimelineCalendarEventsFromCompanyIdArgs {
export class TimelineCalendarEventResolver { export class TimelineCalendarEventResolver {
constructor( constructor(
private readonly timelineCalendarEventService: TimelineCalendarEventService, private readonly timelineCalendarEventService: TimelineCalendarEventService,
private readonly userService: UserService,
) {} ) {}
@Query(() => TimelineCalendarEventsWithTotal) @Query(() => TimelineCalendarEventsWithTotal)
async getTimelineCalendarEventsFromPersonId( async getTimelineCalendarEventsFromPersonId(
@AuthUser() user: User,
@Args() @Args()
{ personId, page, pageSize }: GetTimelineCalendarEventsFromPersonIdArgs, { personId, page, pageSize }: GetTimelineCalendarEventsFromPersonIdArgs,
) { ) {
@ -64,7 +59,6 @@ export class TimelineCalendarEventResolver {
@Query(() => TimelineCalendarEventsWithTotal) @Query(() => TimelineCalendarEventsWithTotal)
async getTimelineCalendarEventsFromCompanyId( async getTimelineCalendarEventsFromCompanyId(
@AuthUser() user: User,
@Args() @Args()
{ companyId, page, pageSize }: GetTimelineCalendarEventsFromCompanyIdArgs, { companyId, page, pageSize }: GetTimelineCalendarEventsFromCompanyIdArgs,
) { ) {

View File

@ -7,7 +7,12 @@ import * as jwt from 'jsonwebtoken';
export class JwtWrapperService { export class JwtWrapperService {
constructor(private readonly jwtService: JwtService) {} 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); return this.jwtService.sign(payload, options);
} }

View File

@ -19,10 +19,10 @@ export class OnboardingResolver {
@AuthUser() user: User, @AuthUser() user: User,
@AuthWorkspace() workspace: Workspace, @AuthWorkspace() workspace: Workspace,
): Promise<OnboardingStepSuccess> { ): Promise<OnboardingStepSuccess> {
await this.onboardingService.toggleOnboardingConnectAccountCompletion({ await this.onboardingService.setOnboardingConnectAccountPending({
userId: user.id, userId: user.id,
workspaceId: workspace.id, workspaceId: workspace.id,
value: true, value: false,
}); });
return { success: true }; return { success: true };

View File

@ -1,66 +1,40 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { SubscriptionStatus } from 'src/engine/core-modules/billing/entities/billing-subscription.entity'; import { BillingService } from 'src/engine/core-modules/billing/services/billing.service';
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 { OnboardingStatus } from 'src/engine/core-modules/onboarding/enums/onboarding-status.enum'; 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 { UserVarsService } from 'src/engine/core-modules/user/user-vars/services/user-vars.service';
import { User } from 'src/engine/core-modules/user/user.entity'; import { User } from 'src/engine/core-modules/user/user.entity';
import { WorkspaceActivationStatus } from 'src/engine/core-modules/workspace/workspace.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 { export enum OnboardingStepKeys {
ONBOARDING_CONNECT_ACCOUNT_COMPLETE = 'ONBOARDING_CONNECT_ACCOUNT_COMPLETE', ONBOARDING_CONNECT_ACCOUNT_PENDING = 'ONBOARDING_CONNECT_ACCOUNT_PENDING',
ONBOARDING_INVITE_TEAM_COMPLETE = 'ONBOARDING_INVITE_TEAM_COMPLETE', ONBOARDING_INVITE_TEAM_PENDING = 'ONBOARDING_INVITE_TEAM_PENDING',
ONBOARDING_CREATE_PROFILE_COMPLETE = 'ONBOARDING_CREATE_PROFILE_COMPLETE', ONBOARDING_CREATE_PROFILE_PENDING = 'ONBOARDING_CREATE_PROFILE_PENDING',
} }
export type OnboardingKeyValueTypeMap = { export type OnboardingKeyValueTypeMap = {
[OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_COMPLETE]: boolean; [OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_PENDING]: boolean;
[OnboardingStepKeys.ONBOARDING_INVITE_TEAM_COMPLETE]: boolean; [OnboardingStepKeys.ONBOARDING_INVITE_TEAM_PENDING]: boolean;
[OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_COMPLETE]: boolean; [OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_PENDING]: boolean;
}; };
@Injectable() @Injectable()
export class OnboardingService { export class OnboardingService {
constructor( constructor(
private readonly billingSubscriptionService: BillingSubscriptionService, private readonly billingService: BillingService,
private readonly environmentService: EnvironmentService,
private readonly isFeatureEnabledService: IsFeatureEnabledService,
private readonly userVarsService: UserVarsService<OnboardingKeyValueTypeMap>, private readonly userVarsService: UserVarsService<OnboardingKeyValueTypeMap>,
) {} ) {}
private async isSubscriptionIncompleteOnboardingStatus(user: User) { private async isSubscriptionIncompleteOnboardingStatus(user: User) {
const isBillingEnabled = this.environmentService.get('IS_BILLING_ENABLED'); const hasSubscription =
await this.billingService.hasWorkspaceActiveSubscriptionOrFreeAccess(
if (!isBillingEnabled) {
return false;
}
const isFreeAccessEnabled =
await this.isFeatureEnabledService.isFeatureEnabled(
FeatureFlagKey.IsFreeAccessEnabled,
user.defaultWorkspaceId, user.defaultWorkspaceId,
); );
if (isFreeAccessEnabled) { return !hasSubscription;
return false;
}
const currentBillingSubscription =
await this.billingSubscriptionService.getCurrentBillingSubscription({
workspaceId: user.defaultWorkspaceId,
});
return (
!isDefined(currentBillingSubscription) ||
currentBillingSubscription?.status === SubscriptionStatus.Incomplete
);
} }
private async isWorkspaceActivationOnboardingStatus(user: User) { private isWorkspaceActivationPending(user: User) {
return ( return (
user.defaultWorkspace.activationStatus === user.defaultWorkspace.activationStatus ===
WorkspaceActivationStatus.PENDING_CREATION WorkspaceActivationStatus.PENDING_CREATION
@ -72,7 +46,7 @@ export class OnboardingService {
return OnboardingStatus.PLAN_REQUIRED; return OnboardingStatus.PLAN_REQUIRED;
} }
if (await this.isWorkspaceActivationOnboardingStatus(user)) { if (this.isWorkspaceActivationPending(user)) {
return OnboardingStatus.WORKSPACE_ACTIVATION; return OnboardingStatus.WORKSPACE_ACTIVATION;
} }
@ -81,33 +55,33 @@ export class OnboardingService {
workspaceId: user.defaultWorkspaceId, workspaceId: user.defaultWorkspaceId,
}); });
const isProfileCreationComplete = const isProfileCreationPending =
userVars.get(OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_COMPLETE) === userVars.get(OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_PENDING) ===
true; true;
const isConnectAccountComplete = const isConnectAccountPending =
userVars.get(OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_COMPLETE) === userVars.get(OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_PENDING) ===
true; true;
const isInviteTeamComplete = const isInviteTeamPending =
userVars.get(OnboardingStepKeys.ONBOARDING_INVITE_TEAM_COMPLETE) === true; userVars.get(OnboardingStepKeys.ONBOARDING_INVITE_TEAM_PENDING) === true;
if (!isProfileCreationComplete) { if (isProfileCreationPending) {
return OnboardingStatus.PROFILE_CREATION; return OnboardingStatus.PROFILE_CREATION;
} }
if (!isConnectAccountComplete) { if (isConnectAccountPending) {
return OnboardingStatus.SYNC_EMAIL; return OnboardingStatus.SYNC_EMAIL;
} }
if (!isInviteTeamComplete) { if (isInviteTeamPending) {
return OnboardingStatus.INVITE_TEAM; return OnboardingStatus.INVITE_TEAM;
} }
return OnboardingStatus.COMPLETED; return OnboardingStatus.COMPLETED;
} }
async toggleOnboardingConnectAccountCompletion({ async setOnboardingConnectAccountPending({
userId, userId,
workspaceId, workspaceId,
value, value,
@ -116,29 +90,48 @@ export class OnboardingService {
workspaceId: string; workspaceId: string;
value: boolean; value: boolean;
}) { }) {
if (!value) {
await this.userVarsService.delete({
userId,
workspaceId,
key: OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_PENDING,
});
return;
}
await this.userVarsService.set({ await this.userVarsService.set({
userId, userId,
workspaceId: workspaceId, workspaceId: workspaceId,
key: OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_COMPLETE, key: OnboardingStepKeys.ONBOARDING_CONNECT_ACCOUNT_PENDING,
value, value: true,
}); });
} }
async toggleOnboardingInviteTeamCompletion({ async setOnboardingInviteTeamPending({
workspaceId, workspaceId,
value, value,
}: { }: {
workspaceId: string; workspaceId: string;
value: boolean; value: boolean;
}) { }) {
if (!value) {
await this.userVarsService.delete({
workspaceId,
key: OnboardingStepKeys.ONBOARDING_INVITE_TEAM_PENDING,
});
return;
}
await this.userVarsService.set({ await this.userVarsService.set({
workspaceId, workspaceId,
key: OnboardingStepKeys.ONBOARDING_INVITE_TEAM_COMPLETE, key: OnboardingStepKeys.ONBOARDING_INVITE_TEAM_PENDING,
value, value: true,
}); });
} }
async toggleOnboardingCreateProfileCompletion({ async setOnboardingCreateProfileCompletion({
userId, userId,
workspaceId, workspaceId,
value, value,
@ -147,11 +140,21 @@ export class OnboardingService {
workspaceId: string; workspaceId: string;
value: boolean; value: boolean;
}) { }) {
if (!value) {
await this.userVarsService.delete({
userId,
workspaceId,
key: OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_PENDING,
});
return;
}
await this.userVarsService.set({ await this.userVarsService.set({
userId, userId,
workspaceId, workspaceId,
key: OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_COMPLETE, key: OnboardingStepKeys.ONBOARDING_CREATE_PROFILE_PENDING,
value, value: true,
}); });
} }
} }

View File

@ -47,6 +47,32 @@ export class WorkspaceService extends TypeOrmQueryService<Workspace> {
throw new BadRequestException("'displayName' not provided"); 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.workspaceManagerService.init(user.defaultWorkspace.id);
await this.userWorkspaceService.createWorkspaceMember( await this.userWorkspaceService.createWorkspaceMember(
user.defaultWorkspace.id, user.defaultWorkspace.id,
@ -142,9 +168,9 @@ export class WorkspaceService extends TypeOrmQueryService<Workspace> {
}); });
} }
await this.onboardingService.toggleOnboardingInviteTeamCompletion({ await this.onboardingService.setOnboardingInviteTeamPending({
workspaceId: workspace.id, workspaceId: workspace.id,
value: true, value: false,
}); });
return { success: true }; return { success: true };

View File

@ -25,9 +25,6 @@ export class WorkspaceWorkspaceMemberListener {
async handleUpdateEvent( async handleUpdateEvent(
payload: ObjectRecordUpdateEvent<WorkspaceMemberWorkspaceEntity>, payload: ObjectRecordUpdateEvent<WorkspaceMemberWorkspaceEntity>,
) { ) {
const { firstName: firstNameBefore, lastName: lastNameBefore } =
payload.properties.before.name;
const { firstName: firstNameAfter, lastName: lastNameAfter } = const { firstName: firstNameAfter, lastName: lastNameAfter } =
payload.properties.after.name; payload.properties.after.name;
@ -39,10 +36,10 @@ export class WorkspaceWorkspaceMemberListener {
return; return;
} }
await this.onboardingService.toggleOnboardingCreateProfileCompletion({ await this.onboardingService.setOnboardingCreateProfileCompletion({
userId: payload.userId, userId: payload.userId,
workspaceId: payload.workspaceId, workspaceId: payload.workspaceId,
value: true, value: false,
}); });
} }

View File

@ -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'; import { User } from 'src/engine/core-modules/user/user.entity';
export enum WorkspaceActivationStatus { export enum WorkspaceActivationStatus {
ONGOING_CREATION = 'ONGOING_CREATION',
PENDING_CREATION = 'PENDING_CREATION', PENDING_CREATION = 'PENDING_CREATION',
ACTIVE = 'ACTIVE', ACTIVE = 'ACTIVE',
INACTIVE = 'INACTIVE', INACTIVE = 'INACTIVE',

View File

@ -118,9 +118,9 @@ export class WorkspaceResolver {
async currentBillingSubscription( async currentBillingSubscription(
@Parent() workspace: Workspace, @Parent() workspace: Workspace,
): Promise<BillingSubscription | null> { ): Promise<BillingSubscription | null> {
return this.billingSubscriptionService.getCurrentBillingSubscription({ return this.billingSubscriptionService.getCurrentBillingSubscriptionOrThrow(
workspaceId: workspace.id, { workspaceId: workspace.id },
}); );
} }
@ResolveField(() => Number) @ResolveField(() => Number)

View File

@ -1,6 +1,7 @@
/* eslint-disable no-console */
import { BaseCallbackHandler } from '@langchain/core/callbacks/base'; import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
import { ConsoleCallbackHandler } from '@langchain/core/tracers/console';
import { Run } from '@langchain/core/tracers/base'; import { Run } from '@langchain/core/tracers/base';
import { ConsoleCallbackHandler } from '@langchain/core/tracers/console';
import { LLMTracingDriver } from 'src/engine/integrations/llm-tracing/drivers/interfaces/llm-tracing-driver.interface'; import { LLMTracingDriver } from 'src/engine/integrations/llm-tracing/drivers/interfaces/llm-tracing-driver.interface';

View File

@ -1,30 +1,32 @@
import { Injectable, NotFoundException } from '@nestjs/common'; import { Injectable, Logger, NotFoundException } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm'; import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource } from 'typeorm'; import { DataSource } from 'typeorm';
import { WorkspaceHealthFixKind } from 'src/engine/workspace-manager/workspace-health/interfaces/workspace-health-fix-kind.interface';
import { WorkspaceHealthIssue } from 'src/engine/workspace-manager/workspace-health/interfaces/workspace-health-issue.interface'; import { WorkspaceHealthIssue } from 'src/engine/workspace-manager/workspace-health/interfaces/workspace-health-issue.interface';
import { import {
WorkspaceHealthMode, WorkspaceHealthMode,
WorkspaceHealthOptions, WorkspaceHealthOptions,
} from 'src/engine/workspace-manager/workspace-health/interfaces/workspace-health-options.interface'; } from 'src/engine/workspace-manager/workspace-health/interfaces/workspace-health-options.interface';
import { WorkspaceHealthFixKind } from 'src/engine/workspace-manager/workspace-health/interfaces/workspace-health-fix-kind.interface';
import { TypeORMService } from 'src/database/typeorm/typeorm.service'; import { TypeORMService } from 'src/database/typeorm/typeorm.service';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service'; import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service'; import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { ObjectMetadataHealthService } from 'src/engine/workspace-manager/workspace-health/services/object-metadata-health.service';
import { FieldMetadataHealthService } from 'src/engine/workspace-manager/workspace-health/services/field-metadata-health.service';
import { RelationMetadataHealthService } from 'src/engine/workspace-manager/workspace-health/services/relation-metadata.health.service';
import { DatabaseStructureService } from 'src/engine/workspace-manager/workspace-health/services/database-structure.service';
import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util';
import { WorkspaceMigrationEntity } from 'src/engine/metadata-modules/workspace-migration/workspace-migration.entity'; import { WorkspaceMigrationEntity } from 'src/engine/metadata-modules/workspace-migration/workspace-migration.entity';
import { WorkspaceMigrationRunnerService } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.service'; import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { DatabaseStructureService } from 'src/engine/workspace-manager/workspace-health/services/database-structure.service';
import { FieldMetadataHealthService } from 'src/engine/workspace-manager/workspace-health/services/field-metadata-health.service';
import { ObjectMetadataHealthService } from 'src/engine/workspace-manager/workspace-health/services/object-metadata-health.service';
import { RelationMetadataHealthService } from 'src/engine/workspace-manager/workspace-health/services/relation-metadata.health.service';
import { WorkspaceFixService } from 'src/engine/workspace-manager/workspace-health/services/workspace-fix.service'; import { WorkspaceFixService } from 'src/engine/workspace-manager/workspace-health/services/workspace-fix.service';
import { WorkspaceMigrationRunnerService } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.service';
@Injectable() @Injectable()
export class WorkspaceHealthService { export class WorkspaceHealthService {
private readonly logger = new Logger(WorkspaceHealthService.name);
constructor( constructor(
@InjectDataSource('metadata') @InjectDataSource('metadata')
private readonly metadataDataSource: DataSource, private readonly metadataDataSource: DataSource,
@ -188,7 +190,7 @@ export class WorkspaceHealthService {
); );
} catch (error) { } catch (error) {
await queryRunner.rollbackTransaction(); await queryRunner.rollbackTransaction();
console.error('Fix of issues failed with:', error); this.logger.error('Fix of issues failed with:', error);
} finally { } finally {
await queryRunner.release(); await queryRunner.release();
} }

View File

@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { import {
QueryRunner, QueryRunner,
@ -32,6 +32,8 @@ import { customTableDefaultColumns } from './utils/custom-table-default-column.u
@Injectable() @Injectable()
export class WorkspaceMigrationRunnerService { export class WorkspaceMigrationRunnerService {
private readonly logger = new Logger(WorkspaceMigrationRunnerService.name);
constructor( constructor(
private readonly workspaceDataSourceService: WorkspaceDataSourceService, private readonly workspaceDataSourceService: WorkspaceDataSourceService,
private readonly workspaceMigrationService: WorkspaceMigrationService, private readonly workspaceMigrationService: WorkspaceMigrationService,
@ -87,7 +89,7 @@ export class WorkspaceMigrationRunnerService {
await queryRunner.commitTransaction(); await queryRunner.commitTransaction();
} catch (error) { } catch (error) {
console.error('Error executing migration', error); this.logger.error('Error executing migration', error);
await queryRunner.rollbackTransaction(); await queryRunner.rollbackTransaction();
throw error; throw error;
} finally { } finally {

View File

@ -4,10 +4,10 @@ import { WorkspaceQueryHookInstance } from 'src/engine/api/graphql/workspace-que
import { FindOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface'; import { FindOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';
import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/decorators/workspace-query-hook.decorator'; import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/decorators/workspace-query-hook.decorator';
import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager'; import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { CanAccessCalendarEventService } from 'src/modules/calendar/common/query-hooks/calendar-event/services/can-access-calendar-event.service'; import { CanAccessCalendarEventService } from 'src/modules/calendar/common/query-hooks/calendar-event/services/can-access-calendar-event.service';
import { CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity'; import { CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';
import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
@WorkspaceQueryHook({ @WorkspaceQueryHook({
key: `calendarEvent.findOne`, key: `calendarEvent.findOne`,
@ -39,13 +39,12 @@ export class CalendarEventFindOnePreQueryHook
'calendarChannelEventAssociation', 'calendarChannelEventAssociation',
); );
// TODO: Re-implement this using twenty ORM
const calendarChannelCalendarEventAssociations = const calendarChannelCalendarEventAssociations =
await calendarChannelEventAssociationRepository.find({ await calendarChannelEventAssociationRepository.find({
where: { where: {
calendarEventId: payload?.filter?.id?.eq, calendarEventId: payload?.filter?.id?.eq,
}, },
relations: ['calendarChannel.connectedAccount'], relations: ['calendarChannel', 'calendarChannel.connectedAccount'],
}); });
if (calendarChannelCalendarEventAssociations.length === 0) { if (calendarChannelCalendarEventAssociations.length === 0) {

View File

@ -1,15 +1,11 @@
import { ForbiddenException, Injectable } from '@nestjs/common'; import { ForbiddenException, Injectable } from '@nestjs/common';
import groupBy from 'lodash.groupby'; import groupBy from 'lodash.groupby';
import { Any } from 'typeorm';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager'; import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity'; import { CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';
import { import { CalendarChannelVisibility } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
CalendarChannelVisibility,
CalendarChannelWorkspaceEntity,
} from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository'; import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity'; import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
import { WorkspaceMemberRepository } from 'src/modules/workspace-member/repositories/workspace-member.repository'; import { WorkspaceMemberRepository } from 'src/modules/workspace-member/repositories/workspace-member.repository';
@ -30,20 +26,9 @@ export class CanAccessCalendarEventService {
workspaceId: string, workspaceId: string,
calendarChannelCalendarEventAssociations: CalendarChannelEventAssociationWorkspaceEntity[], calendarChannelCalendarEventAssociations: CalendarChannelEventAssociationWorkspaceEntity[],
) { ) {
const calendarRepository = const calendarChannels = calendarChannelCalendarEventAssociations.map(
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>( (association) => association.calendarChannel,
'calendarChannel', );
);
const calendarChannels = await calendarRepository.find({
where: {
id: Any(
calendarChannelCalendarEventAssociations.map(
(association) => association.calendarChannel.id,
),
),
},
});
const calendarChannelsGroupByVisibility = groupBy( const calendarChannelsGroupByVisibility = groupBy(
calendarChannels, calendarChannels,

View File

@ -1,6 +1,13 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter'; import { OnEvent } from '@nestjs/event-emitter';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import {
Workspace,
WorkspaceActivationStatus,
} from 'src/engine/core-modules/workspace/workspace.entity';
import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event'; import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event';
import { ObjectRecordUpdateEvent } from 'src/engine/integrations/event-emitter/types/object-record-update.event'; import { ObjectRecordUpdateEvent } from 'src/engine/integrations/event-emitter/types/object-record-update.event';
import { objectRecordChangedProperties as objectRecordUpdateEventChangedProperties } from 'src/engine/integrations/event-emitter/utils/object-record-changed-properties.util'; import { objectRecordChangedProperties as objectRecordUpdateEventChangedProperties } from 'src/engine/integrations/event-emitter/utils/object-record-changed-properties.util';
@ -8,12 +15,12 @@ import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decora
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { import {
MessageParticipantMatchParticipantJobData,
MessageParticipantMatchParticipantJob, MessageParticipantMatchParticipantJob,
MessageParticipantMatchParticipantJobData,
} from 'src/modules/messaging/message-participant-manager/jobs/message-participant-match-participant.job'; } from 'src/modules/messaging/message-participant-manager/jobs/message-participant-match-participant.job';
import { import {
MessageParticipantUnmatchParticipantJobData,
MessageParticipantUnmatchParticipantJob, MessageParticipantUnmatchParticipantJob,
MessageParticipantUnmatchParticipantJobData,
} from 'src/modules/messaging/message-participant-manager/jobs/message-participant-unmatch-participant.job'; } from 'src/modules/messaging/message-participant-manager/jobs/message-participant-unmatch-participant.job';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity'; import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
@ -22,12 +29,25 @@ export class MessageParticipantWorkspaceMemberListener {
constructor( constructor(
@InjectMessageQueue(MessageQueue.messagingQueue) @InjectMessageQueue(MessageQueue.messagingQueue)
private readonly messageQueueService: MessageQueueService, private readonly messageQueueService: MessageQueueService,
@InjectRepository(Workspace, 'core')
private readonly workspaceRepository: Repository<Workspace>,
) {} ) {}
@OnEvent('workspaceMember.created') @OnEvent('workspaceMember.created')
async handleCreatedEvent( async handleCreatedEvent(
payload: ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>, payload: ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>,
) { ) {
const workspace = await this.workspaceRepository.findOneBy({
id: payload.workspaceId,
});
if (
!workspace ||
workspace.activationStatus !== WorkspaceActivationStatus.ACTIVE
) {
return;
}
if (payload.properties.after.userEmail === null) { if (payload.properties.after.userEmail === null) {
return; return;
} }

View File

@ -3,6 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { AnalyticsModule } from 'src/engine/core-modules/analytics/analytics.module'; import { AnalyticsModule } from 'src/engine/core-modules/analytics/analytics.module';
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity'; import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity'; import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module'; import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module'; import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
@ -22,7 +23,7 @@ import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-o
@Module({ @Module({
imports: [ imports: [
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'), TypeOrmModule.forFeature([FeatureFlagEntity, Workspace], 'core'),
AnalyticsModule, AnalyticsModule,
ContactCreationManagerModule, ContactCreationManagerModule,
WorkspaceDataSourceModule, WorkspaceDataSourceModule,

View File

@ -13,6 +13,7 @@ import {
import { BaseWorkspaceEntity } from 'src/engine/twenty-orm/base.workspace-entity'; import { BaseWorkspaceEntity } from 'src/engine/twenty-orm/base.workspace-entity';
import { WorkspaceEntity } from 'src/engine/twenty-orm/decorators/workspace-entity.decorator'; import { WorkspaceEntity } from 'src/engine/twenty-orm/decorators/workspace-entity.decorator';
import { WorkspaceField } from 'src/engine/twenty-orm/decorators/workspace-field.decorator'; import { WorkspaceField } from 'src/engine/twenty-orm/decorators/workspace-field.decorator';
import { WorkspaceIndex } from 'src/engine/twenty-orm/decorators/workspace-index.decorator';
import { WorkspaceIsDeprecated } from 'src/engine/twenty-orm/decorators/workspace-is-deprecated.decorator'; import { WorkspaceIsDeprecated } from 'src/engine/twenty-orm/decorators/workspace-is-deprecated.decorator';
import { WorkspaceIsNotAuditLogged } from 'src/engine/twenty-orm/decorators/workspace-is-not-audit-logged.decorator'; import { WorkspaceIsNotAuditLogged } from 'src/engine/twenty-orm/decorators/workspace-is-not-audit-logged.decorator';
import { WorkspaceIsNullable } from 'src/engine/twenty-orm/decorators/workspace-is-nullable.decorator'; import { WorkspaceIsNullable } from 'src/engine/twenty-orm/decorators/workspace-is-nullable.decorator';
@ -90,6 +91,7 @@ export class OpportunityWorkspaceEntity extends BaseWorkspaceEntity {
], ],
defaultValue: "'NEW'", defaultValue: "'NEW'",
}) })
@WorkspaceIndex()
stage: string; stage: string;
@WorkspaceField({ @WorkspaceField({

View File

@ -1,4 +1,4 @@
import { Global, Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { WorkflowCommonService } from 'src/modules/workflow/common/workflow-common.services'; import { WorkflowCommonService } from 'src/modules/workflow/common/workflow-common.services';