Skip to content

Commit 14a245e

Browse files
refactor(dashboard): modularize demo workflow initialization and clean up unused code
- Introduced `useInitDemoWorkflow` hook to encapsulate demo workflow initialization logic. - Removed unused imports and code related to workflow fetching in `InboxPlayground`. - Updated `InboxConnectedGuide` to utilize the new hook for demo workflow setup. - Cleaned up `CommunityEditionService` by removing unused member repository and switch organization use case.
1 parent f4c539f commit 14a245e

File tree

4 files changed

+67
-66
lines changed

4 files changed

+67
-66
lines changed

apps/api/src/app/auth/services/system-organization.service.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
2-
import { CommunityOrganizationRepository, MemberRepository, UserEntity, UserRepository } from '@novu/dal';
2+
import { CommunityOrganizationRepository, UserEntity, UserRepository } from '@novu/dal';
33
import { PinoLogger } from '@novu/application-generic';
44
import { ApiServiceLevelEnum } from '@novu/shared';
55
import { CreateOrganization } from '../../organization/usecases/create-organization/create-organization.usecase';
66
import { CreateOrganizationCommand } from '../../organization/usecases/create-organization/create-organization.command';
77
import { UserRegister } from '../usecases/register/user-register.usecase';
88
import { UserRegisterCommand } from '../usecases/register/user-register.command';
9-
import { SwitchOrganization } from '../usecases/switch-organization/switch-organization.usecase';
10-
import { SwitchOrganizationCommand } from '../usecases/switch-organization/switch-organization.command';
119

1210
@Injectable()
1311
export class CommunityEditionService implements OnModuleInit {
@@ -16,12 +14,10 @@ export class CommunityEditionService implements OnModuleInit {
1614
private readonly COMMUNITY_EDITION_USER_EMAIL = '[email protected]';
1715

1816
constructor(
19-
private memberRepository: MemberRepository,
2017
@Inject('ORGANIZATION_REPOSITORY')
2118
private organizationRepository: CommunityOrganizationRepository,
2219
private createOrganizationUsecase: CreateOrganization,
2320
private userRegisterUsecase: UserRegister,
24-
private switchOrganizationUsecase: SwitchOrganization,
2521
private userRepository: UserRepository,
2622
private logger: PinoLogger
2723
) {}
@@ -41,7 +37,7 @@ export class CommunityEditionService implements OnModuleInit {
4137

4238
if (communityEditionOrg) {
4339
this.logger.info(
44-
'Self Hosted is already initialized, skipping Community Edition creation.' +
40+
'Self Hosted is already initialized, skipping Community Edition creation. ' +
4541
`Organization already exists with ID: ${communityEditionOrg._id}`
4642
);
4743

apps/dashboard/src/components/auth/inbox-playground.tsx

+2-60
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { useEnvironment } from '@/context/environment/hooks';
22
import { useTriggerWorkflow } from '@/hooks/use-trigger-workflow';
33
import { zodResolver } from '@hookform/resolvers/zod';
4-
import { IEnvironment, StepTypeEnum, WorkflowCreationSourceEnum } from '@novu/shared';
54
import { useEffect, useState } from 'react';
65
import { useForm } from 'react-hook-form';
76
import { RiNotification2Fill } from 'react-icons/ri';
87
import { useNavigate } from 'react-router-dom';
98
import { z } from 'zod';
10-
import { createWorkflow } from '../../api/workflows';
119
import { ONBOARDING_DEMO_WORKFLOW_ID } from '../../config';
1210
import { useAuth } from '../../context/auth/hooks';
13-
import { useFetchWorkflows } from '../../hooks/use-fetch-workflows';
1411
import { useTelemetry } from '../../hooks/use-telemetry';
12+
import { useInitDemoWorkflow } from '../../hooks/use-init-demo-workflow';
1513
import { ROUTES } from '../../utils/routes';
1614
import { TelemetryEvent } from '../../utils/telemetry';
1715
import { Button } from '../primitives/button';
@@ -92,30 +90,11 @@ export function InboxPlayground() {
9290
});
9391

9492
const { triggerWorkflow, isPending } = useTriggerWorkflow();
95-
const { data } = useFetchWorkflows({ query: ONBOARDING_DEMO_WORKFLOW_ID });
9693
const auth = useAuth();
9794
const [hasNotificationBeenSent, setHasNotificationBeenSent] = useState(false);
9895
const navigate = useNavigate();
9996
const telemetry = useTelemetry();
100-
101-
useEffect(() => {
102-
if (!data) return;
103-
104-
/**
105-
* We only want to create the demo workflow if it doesn't exist yet.
106-
* This workflow will be used by the inbox preview examples
107-
*/
108-
const initializeDemoWorkflow = async () => {
109-
const workflow = data?.workflows.find((workflow) => workflow.workflowId?.includes(ONBOARDING_DEMO_WORKFLOW_ID));
110-
111-
if (!workflow) {
112-
await createDemoWorkflow({ environment: currentEnvironment! });
113-
}
114-
};
115-
116-
initializeDemoWorkflow();
117-
// eslint-disable-next-line react-hooks/exhaustive-deps
118-
}, [data]);
97+
useInitDemoWorkflow(currentEnvironment!);
11998

12099
const handleSendNotification = async () => {
121100
try {
@@ -249,40 +228,3 @@ export function InboxPlayground() {
249228
</div>
250229
);
251230
}
252-
253-
async function createDemoWorkflow({ environment }: { environment: IEnvironment }) {
254-
await createWorkflow({
255-
environment,
256-
workflow: {
257-
name: 'Onboarding Demo Workflow',
258-
description: 'A demo workflow to showcase the Inbox component',
259-
workflowId: ONBOARDING_DEMO_WORKFLOW_ID,
260-
steps: [
261-
{
262-
name: 'Inbox',
263-
type: StepTypeEnum.IN_APP,
264-
controlValues: {
265-
subject: '{{payload.subject}}',
266-
body: '{{payload.body}}',
267-
avatar: window.location.origin + '/images/novu.svg',
268-
primaryAction: {
269-
label: '{{payload.primaryActionLabel}}',
270-
redirect: {
271-
target: '_self',
272-
url: '/onboarding/inbox/embed',
273-
},
274-
},
275-
secondaryAction: {
276-
label: '{{payload.secondaryActionLabel}}',
277-
redirect: {
278-
target: '_self',
279-
url: '/onboarding/inbox/embed',
280-
},
281-
},
282-
},
283-
},
284-
],
285-
__source: WorkflowCreationSourceEnum.DASHBOARD,
286-
},
287-
});
288-
}

apps/dashboard/src/components/welcome/inbox-connected-guide.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ONBOARDING_DEMO_WORKFLOW_ID } from '../../config';
66
import { ROUTES } from '../../utils/routes';
77
import { Button } from '../primitives/button';
88
import { showErrorToast, showSuccessToast } from '../primitives/sonner-helpers';
9+
import { useInitDemoWorkflow } from '../../hooks/use-init-demo-workflow';
910

1011
type InboxConnectedGuideProps = {
1112
subscriberId: string;
@@ -15,6 +16,7 @@ type InboxConnectedGuideProps = {
1516
export function InboxConnectedGuide({ subscriberId, environment }: InboxConnectedGuideProps) {
1617
const navigate = useNavigate();
1718
const { triggerWorkflow, isPending } = useTriggerWorkflow(environment);
19+
useInitDemoWorkflow(environment);
1820

1921
async function handleSendNotification() {
2022
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { useEffect } from 'react';
2+
import { IEnvironment } from '@novu/shared';
3+
import { ONBOARDING_DEMO_WORKFLOW_ID } from '../config';
4+
import { useFetchWorkflows } from './use-fetch-workflows';
5+
import { createWorkflow } from '../api/workflows';
6+
import { StepTypeEnum, WorkflowCreationSourceEnum } from '@novu/shared';
7+
8+
async function createDemoWorkflow({ environment }: { environment: IEnvironment }) {
9+
await createWorkflow({
10+
environment,
11+
workflow: {
12+
name: 'Onboarding Demo Workflow',
13+
description: 'A demo workflow to showcase the Inbox component',
14+
workflowId: ONBOARDING_DEMO_WORKFLOW_ID,
15+
steps: [
16+
{
17+
name: 'Inbox',
18+
type: StepTypeEnum.IN_APP,
19+
controlValues: {
20+
subject: '{{payload.subject}}',
21+
body: '{{payload.body}}',
22+
avatar: window.location.origin + '/images/novu.svg',
23+
primaryAction: {
24+
label: '{{payload.primaryActionLabel}}',
25+
redirect: {
26+
target: '_self',
27+
url: '/onboarding/inbox/embed',
28+
},
29+
},
30+
secondaryAction: {
31+
label: '{{payload.secondaryActionLabel}}',
32+
redirect: {
33+
target: '_self',
34+
url: '/onboarding/inbox/embed',
35+
},
36+
},
37+
},
38+
},
39+
],
40+
__source: WorkflowCreationSourceEnum.DASHBOARD,
41+
},
42+
});
43+
}
44+
45+
export function useInitDemoWorkflow(environment: IEnvironment) {
46+
const { data } = useFetchWorkflows({ query: ONBOARDING_DEMO_WORKFLOW_ID });
47+
48+
useEffect(() => {
49+
if (!data) return;
50+
51+
const initializeDemoWorkflow = async () => {
52+
const workflow = data?.workflows.find((workflow) => workflow.workflowId?.includes(ONBOARDING_DEMO_WORKFLOW_ID));
53+
54+
if (!workflow) {
55+
await createDemoWorkflow({ environment });
56+
}
57+
};
58+
59+
initializeDemoWorkflow();
60+
}, [data, environment]);
61+
}

0 commit comments

Comments
 (0)