Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: onboarding experiment setup #5250

Merged
merged 15 commits into from Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/prod-deploy-api.yml
Expand Up @@ -46,6 +46,26 @@ jobs:
- name: build api
run: pnpm build:api --skip-nx-cache

- uses: crazy-max/ghaction-setup-docker@v2
with:
version: v24.0.6
daemon-config: |
{
"features": {
"containerd-snapshotter": true
}
}

- name: Setup QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: linux/amd64,linux/arm64

- name: Set Up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: 'image=moby/buildkit:v0.12.4'

- name: Set Bull MQ Env variable for EE
if: contains(matrix.name, 'ee')
shell: bash
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/prod-deploy-inbound-mail.yml
Expand Up @@ -36,6 +36,26 @@ jobs:
- name: build api
run: pnpm build:inbound-mail

- uses: crazy-max/ghaction-setup-docker@v2
with:
version: v24.0.6
daemon-config: |
{
"features": {
"containerd-snapshotter": true
}
}

- name: Setup QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: linux/amd64,linux/arm64

- name: Set Up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: 'image=moby/buildkit:v0.12.4'

- name: Set Bull MQ Env variable for EE
if: contains(matrix.name, 'ee')
shell: bash
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/prod-deploy-worker.yml
Expand Up @@ -46,6 +46,26 @@ jobs:
- name: build worker
run: pnpm build:worker --skip-nx-cache

- uses: crazy-max/ghaction-setup-docker@v2
with:
version: v24.0.6
daemon-config: |
{
"features": {
"containerd-snapshotter": true
}
}

- name: Setup QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: linux/amd64,linux/arm64

- name: Set Up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: 'image=moby/buildkit:v0.12.4'

- name: Set Bull MQ Env variable for EE
if: contains(matrix.name, 'ee')
shell: bash
Expand Down
22 changes: 21 additions & 1 deletion .github/workflows/prod-deploy-ws.yml
Expand Up @@ -46,6 +46,26 @@ jobs:
- name: build api
run: pnpm build:ws

- uses: crazy-max/ghaction-setup-docker@v2
with:
version: v24.0.6
daemon-config: |
{
"features": {
"containerd-snapshotter": true
}
}

- name: Setup QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: linux/amd64,linux/arm64

- name: Set Up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: 'image=moby/buildkit:v0.12.4'

- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
Expand All @@ -56,7 +76,7 @@ jobs:
GH_PASSWORD: ${{ secrets.GH_PACKAGES }}
run: |
echo $GH_PASSWORD | docker login ghcr.io -u $GH_ACTOR --password-stdin
BULL_MQ_PRO_NPM_TOKEN=${BULL_MQ_PRO_NPM_TOKEN} docker buildx build --secret id=BULL_MQ_PRO_NPM_TOKEN -t ghcr.io/$REGISTRY_OWNER/$DOCKER_NAME:$IMAGE_TAG --platform=linux/amd64,linux/arm64 --provenance=false -f apps/ws/Dockerfile .
BULL_MQ_PRO_NPM_TOKEN=${BULL_MQ_PRO_NPM_TOKEN} docker buildx build --secret id=BULL_MQ_PRO_NPM_TOKEN -t ghcr.io/$REGISTRY_OWNER/$DOCKER_NAME:$IMAGE_TAG --load --platform=linux/amd64,linux/arm64 --provenance=false -f apps/ws/Dockerfile .
docker run --network=host --name api -dit --env NODE_ENV=test ghcr.io/$REGISTRY_OWNER/$DOCKER_NAME:$IMAGE_TAG
docker run --network=host appropriate/curl --retry 10 --retry-delay 5 --retry-connrefused http://127.0.0.1:1340/v1/health-check | grep 'ok'
docker tag ghcr.io/$REGISTRY_OWNER/$DOCKER_NAME:$IMAGE_TAG ghcr.io/$REGISTRY_OWNER/$DOCKER_NAME:prod
Expand Down
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { MessageTemplateEntity, MessageTemplateRepository } from '@novu/dal';
import { ChangeEntityTypeEnum, IMessageAction } from '@novu/shared';
import { MessageTemplateEntity, MessageTemplateRepository, LayoutEntity, LayoutRepository } from '@novu/dal';
import { ChangeEntityTypeEnum, IMessageAction, StepTypeEnum } from '@novu/shared';

import { CreateMessageTemplateCommand } from './create-message-template.command';
import { sanitizeMessageContent } from '../../shared/sanitizer.service';
Expand All @@ -13,6 +13,7 @@ import { ApiException, CreateChange, CreateChangeCommand } from '@novu/applicati
export class CreateMessageTemplate {
constructor(
private messageTemplateRepository: MessageTemplateRepository,
private layoutRepository: LayoutRepository,
private createChange: CreateChange,
private updateChange: UpdateChange
) {}
Expand All @@ -22,6 +23,14 @@ export class CreateMessageTemplate {
throw new ApiException('Please provide a valid CTA action');
}

let layoutId: string | undefined | null;
if (command.type === StepTypeEnum.EMAIL && !command.layoutId) {
const defaultLayout = await this.layoutRepository.findDefault(command.environmentId, command.organizationId);
layoutId = defaultLayout?._id;
} else {
layoutId = command.layoutId;
}

let item: MessageTemplateEntity = await this.messageTemplateRepository.create({
cta: command.cta,
name: command.name,
Expand All @@ -32,7 +41,7 @@ export class CreateMessageTemplate {
title: command.title,
type: command.type,
_feedId: command.feedId ? command.feedId : null,
_layoutId: command.layoutId || null,
_layoutId: layoutId,
_organizationId: command.organizationId,
_environmentId: command.environmentId,
_creatorId: command.userId,
Expand Down
8 changes: 4 additions & 4 deletions apps/web/cypress/tests/integrations-list-modal.spec.ts
Expand Up @@ -29,10 +29,10 @@ describe('Integrations List Modal', function () {
.as('session');
});

const navigateToGetStarted = () => {
const navigateToGetStarted = (card = 'channel-card-email') => {
cy.visit('/get-started');
cy.location('pathname').should('equal', '/get-started');
cy.getByTestId('channel-card-email').find('button').contains('Change Provider').click();
cy.getByTestId(card).find('button').contains('Change Provider').click();
cy.getByTestId('integrations-list-modal').should('be.visible').contains('Integrations Store');
};

Expand Down Expand Up @@ -128,7 +128,7 @@ describe('Integrations List Modal', function () {
cy.intercept('*/environments', async () => {
await new Promise((resolve) => setTimeout(resolve, 3500));
}).as('getEnvironments');
navigateToGetStarted();
navigateToGetStarted('channel-card-sms');
cy.getByTestId('select-provider-sidebar').should('be.visible');
cy.getByTestId('sidebar-close').should('be.visible').click();

Expand All @@ -152,7 +152,7 @@ describe('Integrations List Modal', function () {
await new Promise((resolve) => setTimeout(resolve, 3500));
}).as('getIntegrations');

navigateToGetStarted();
navigateToGetStarted('channel-card-sms');
cy.getByTestId('select-provider-sidebar').should('be.visible');
cy.getByTestId('sidebar-close').should('be.visible').click();

Expand Down
@@ -0,0 +1,120 @@
import { useCallback } from 'react';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useNavigate } from 'react-router-dom';
import { v4 as uuid4 } from 'uuid';
import { EmailProviderIdEnum, StepTypeEnum } from '@novu/shared';
import type { IResponseError, ICreateNotificationTemplateDto, INotificationTemplate } from '@novu/shared';
import { QueryKeys } from '@novu/shared-web';

import { createTemplate } from '../../notification-templates';
import { parseUrl } from '../../../utils/routeUtils';
import { ROUTES } from '../../../constants/routes.enum';
import { errorMessage } from '../../../utils/notifications';
import { useNotificationGroup, useTemplates, useIntegrations } from '../../../hooks';
import { FIRST_100_WORKFLOWS } from '../../../constants/workflowConstants';
import { IntegrationEntity } from '../../../pages/integrations/types';
import { setIntegrationAsPrimary } from '../../../api/integration';

export const useCreateOnboardingExperimentWorkflow = () => {
const navigate = useNavigate();
const queryClient = useQueryClient();

const { groups, loading: areNotificationGroupLoading } = useNotificationGroup();

const { mutateAsync: createNotificationTemplate, isLoading: isCreating } = useMutation<
INotificationTemplate & { __source?: string },
IResponseError,
{ template: ICreateNotificationTemplateDto; params: { __source?: string } }
>((data) => createTemplate(data.template, data.params), {
onSuccess: (template) => {
navigate(parseUrl(ROUTES.WORKFLOWS_EDIT_TEMPLATEID, { templateId: template._id as string }));
},
onError: () => {
errorMessage('Failed to create onboarding experiment Workflow');
},
});

const { mutate: makePrimaryIntegration, isLoading: isPrimaryEmailIntegrationLoading } = useMutation<
IntegrationEntity,
IResponseError,
{ id: string }
>(({ id }) => setIntegrationAsPrimary(id), {
onSuccess: () => {
queryClient.refetchQueries({
predicate: ({ queryKey }) =>
queryKey.includes(QueryKeys.integrationsList) || queryKey.includes(QueryKeys.activeIntegrations),
});
},
onError: () => {
errorMessage("Failed to update integration's primary status");
},
});

const { templates = [], loading: templatesLoading } = useTemplates(FIRST_100_WORKFLOWS);

const { integrations, loading: isIntegrationsLoading } = useIntegrations();

const onboardingExperimentWorkflow = 'Onboarding Workflow';

const createOnboardingExperimentWorkflow = useCallback(() => {
if (templatesLoading) return;

const onboardingExperimentWorkflowExists = templates.find((template) =>
template.name.includes(onboardingExperimentWorkflow)
);

const novuEmailIntegration = integrations?.find(
(integration) => integration.providerId === EmailProviderIdEnum.Novu
);

if (novuEmailIntegration && !novuEmailIntegration?.primary) {
makePrimaryIntegration({ id: novuEmailIntegration?._id as string });
}

if (onboardingExperimentWorkflowExists) {
navigate(
parseUrl(ROUTES.WORKFLOWS_EDIT_TEMPLATEID, { templateId: onboardingExperimentWorkflowExists._id as string })
);
} else {
const payload = {
name: onboardingExperimentWorkflow,
notificationGroupId: groups[0]._id,
active: true,
draft: false,
critical: false,
tags: ['onboarding'],
steps: [
{
template: {
subject: 'Your first email notification from Novu!',
senderName: 'Novu Onboarding',
type: StepTypeEnum.EMAIL,
contentType: 'customHtml',
content:
// eslint-disable-next-line max-len
'It\'s that simple! <br/>Learn more about creating workflows <a href="https://docs.novu.co/workflows/notification-workflows">here</a>.',
},
uuid: uuid4(),
active: true,
},
],
};

createNotificationTemplate({
template: payload as any,
params: { __source: 'Onboarding Experiment Workflow' },
});
}
}, [templatesLoading, templates, integrations, makePrimaryIntegration, navigate, groups, createNotificationTemplate]);

return {
createOnboardingExperimentWorkflow,
isLoading: isPrimaryEmailIntegrationLoading || isCreating,
isDisabled:
areNotificationGroupLoading ||
templatesLoading ||
isIntegrationsLoading ||
isPrimaryEmailIntegrationLoading ||
isCreating,
};
};
17 changes: 17 additions & 0 deletions apps/web/src/hooks/useOnboardingExperiment.ts
@@ -0,0 +1,17 @@
import { ChannelTypeEnum, EmailProviderIdEnum } from '@novu/shared';

import { useIntegrations } from './integrations';
import { IS_DOCKER_HOSTED } from '../config';

export function useOnboardingExperiment() {
const { integrations, loading: areIntegrationsLoading } = useIntegrations();

const emailIntegrationOtherThanNovu = integrations?.find(
(integration) =>
integration.channel === ChannelTypeEnum.EMAIL && integration.providerId !== EmailProviderIdEnum.Novu
);

return {
isOnboardingExperimentEnabled: !areIntegrationsLoading && !emailIntegrationOtherThanNovu && !IS_DOCKER_HOSTED,
};
}