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
fix: hubspot signup form issue when user is invited #5317
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
365ecab
fix: hubspot signup form issue when user is invited
jainpawan21 e247e13
fix: remove unused imports
jainpawan21 afb26b5
Merge branch 'next' into fix/hubspot-form-bug-when-user-invited
jainpawan21 5a22a1d
fix: cspell error
jainpawan21 5b3704b
fix: failing cypress test
jainpawan21 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsEnum, IsOptional, IsString } from 'class-validator'; | ||
import { JobTitleEnum } from '@novu/shared'; | ||
|
||
export class UpdateProfileDto { | ||
@ApiProperty() | ||
@IsOptional() | ||
@IsString() | ||
firstName?: string; | ||
|
||
@ApiProperty() | ||
@IsOptional() | ||
@IsString() | ||
lastName?: string; | ||
|
||
@ApiProperty() | ||
@IsOptional() | ||
@IsEnum(JobTitleEnum) | ||
jobTitle?: JobTitleEnum; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
apps/api/src/app/user/usecases/update-profile/update-profile.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { IsEnum, IsOptional, IsString } from 'class-validator'; | ||
import { JobTitleEnum } from '@novu/shared'; | ||
import { AuthenticatedCommand } from '../../../shared/commands/authenticated.command'; | ||
|
||
export class UpdateProfileCommand extends AuthenticatedCommand { | ||
@IsOptional() | ||
@IsString() | ||
firstName?: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
lastName?: string; | ||
|
||
@IsOptional() | ||
@IsEnum(JobTitleEnum) | ||
jobTitle?: JobTitleEnum; | ||
} |
64 changes: 64 additions & 0 deletions
64
apps/api/src/app/user/usecases/update-profile/update-profile.usecase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { BadRequestException, forwardRef, Inject, Injectable, NotFoundException } from '@nestjs/common'; | ||
|
||
import { UserRepository } from '@novu/dal'; | ||
import { AnalyticsService, buildUserKey, InvalidateCacheService } from '@novu/application-generic'; | ||
import { IUserEntity } from '@novu/shared'; | ||
|
||
import { UpdateProfileCommand } from './update-profile.command'; | ||
|
||
@Injectable() | ||
export class UpdateProfileUseCase { | ||
constructor( | ||
private invalidateCache: InvalidateCacheService, | ||
private readonly userRepository: UserRepository, | ||
@Inject(forwardRef(() => AnalyticsService)) | ||
private analyticsService: AnalyticsService | ||
) {} | ||
|
||
async execute(command: UpdateProfileCommand) { | ||
const user = await this.userRepository.findById(command.userId); | ||
if (!user) throw new BadRequestException('Invalid UserId'); | ||
|
||
const valuesToUpdate: Partial<IUserEntity> = {}; | ||
|
||
if (command.firstName) { | ||
valuesToUpdate.firstName = command?.firstName?.toLowerCase(); | ||
} | ||
if (command.lastName) { | ||
valuesToUpdate.lastName = command?.lastName?.toLowerCase(); | ||
} | ||
if (command.jobTitle) { | ||
valuesToUpdate.jobTitle = command.jobTitle; | ||
} | ||
await this.userRepository.update( | ||
{ | ||
_id: command.userId, | ||
}, | ||
{ | ||
$set: { | ||
...valuesToUpdate, | ||
}, | ||
} | ||
); | ||
|
||
const updatedUser = await this.userRepository.findById(command.userId); | ||
if (!updatedUser) throw new NotFoundException('User not found'); | ||
await this.invalidateCache.invalidateByKey({ | ||
key: buildUserKey({ | ||
_id: command.userId, | ||
}), | ||
}); | ||
|
||
if (command.firstName) { | ||
this.analyticsService.setValue(updatedUser?._id, 'firstName', command.firstName); | ||
} | ||
if (command.lastName) { | ||
this.analyticsService.setValue(updatedUser?._id, 'lastName', command.lastName); | ||
} | ||
if (command.jobTitle) { | ||
this.analyticsService.setValue(updatedUser?._id, 'jobTitle', command.jobTitle); | ||
} | ||
|
||
return updatedUser; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import decode from 'jwt-decode'; | |
import { useMantineColorScheme } from '@mantine/core'; | ||
|
||
import { JobTitleEnum } from '@novu/shared'; | ||
import type { ProductUseCases, IResponseError, ICreateOrganizationDto, IJwtPayload } from '@novu/shared'; | ||
import type { IResponseError, ICreateOrganizationDto, IJwtPayload, IOrganizationEntity } from '@novu/shared'; | ||
import { HubspotForm, useSegment } from '@novu/shared-web'; | ||
|
||
import { api } from '../../../api/api.client'; | ||
|
@@ -17,39 +17,52 @@ import SetupLoader from './SetupLoader'; | |
|
||
export function HubspotSignupForm() { | ||
const [loading, setLoading] = useState<boolean>(); | ||
const [existingOrganization, setExistingOrganization] = useState<IOrganizationEntity>(); | ||
const navigate = useNavigate(); | ||
const { setToken, token, currentUser } = useAuthContext(); | ||
const { startVercelSetup } = useVercelIntegration(); | ||
const { isFromVercel } = useVercelParams(); | ||
const { colorScheme } = useMantineColorScheme(); | ||
|
||
const segment = useSegment(); | ||
|
||
const { mutateAsync: createOrganizationMutation } = useMutation< | ||
{ _id: string }, | ||
IResponseError, | ||
ICreateOrganizationDto | ||
>((data: ICreateOrganizationDto) => api.post(`/v1/organizations`, data)); | ||
|
||
const { mutateAsync: updateUserMutation } = useMutation<{ _id: string }, IResponseError, IUserUpdateForm>( | ||
(data: IUserUpdateForm) => api.put(`/v1/users/profile`, data) | ||
); | ||
|
||
const fetchExistingOrganization = async () => { | ||
return api.get(`/v1/organizations/me`); | ||
}; | ||
|
||
useEffect(() => { | ||
if (token) { | ||
const userData = decode<IJwtPayload>(token); | ||
|
||
if (userData.environmentId) { | ||
// if user is invited to an organization, fetch that organization name | ||
(async () => { | ||
const org = await fetchExistingOrganization(); | ||
setExistingOrganization(org); | ||
})(); | ||
|
||
if (isFromVercel) { | ||
startVercelSetup(); | ||
|
||
return; | ||
} | ||
|
||
navigate(ROUTES.HOME); | ||
} | ||
} | ||
}, [token, navigate, isFromVercel, startVercelSetup]); | ||
|
||
return () => {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed |
||
}, [token, isFromVercel, startVercelSetup]); | ||
|
||
async function createOrganization(data: IOrganizationCreateForm) { | ||
const { organizationName, jobTitle, ...rest } = data; | ||
const createDto: ICreateOrganizationDto = { ...rest, name: organizationName, jobTitle }; | ||
const { organizationName, ...rest } = data; | ||
const createDto: ICreateOrganizationDto = { ...rest, name: organizationName }; | ||
const organization = await createOrganizationMutation(createDto); | ||
const organizationResponseToken = await api.post(`/v1/auth/organizations/${organization._id}/switch`, {}); | ||
|
||
|
@@ -66,10 +79,6 @@ export function HubspotSignupForm() { | |
const handleCreateOrganization = async (data: IOrganizationCreateForm) => { | ||
if (!data?.organizationName) return; | ||
|
||
segment.track('Button Clicked - [Signup]', { action: 'hubspot questionnaire form submit' }); | ||
|
||
setLoading(true); | ||
|
||
if (!jwtHasKey('organizationId')) { | ||
await createOrganization({ ...data }); | ||
} | ||
|
@@ -80,10 +89,35 @@ export function HubspotSignupForm() { | |
|
||
return; | ||
} | ||
}; | ||
|
||
const handleFormSubmission = async (values) => { | ||
segment.track('Button Clicked - [Signup]', { action: 'hubspot questionnaire form submit' }); | ||
setLoading(true); | ||
|
||
const submissionValues = values?.submissionValues as unknown as { | ||
company: string; | ||
role___onboarding: string; | ||
firstname: string; | ||
lastname: string; | ||
}; | ||
|
||
// update user profile data on form submit | ||
await updateUserMutation({ | ||
firstName: submissionValues?.firstname, | ||
lastName: submissionValues?.lastname, | ||
jobTitle: hubspotRoleToJobTitleMapping[submissionValues?.role___onboarding], | ||
}); | ||
|
||
// skip organization creation if user is signing up with invitation | ||
if (!existingOrganization) { | ||
await handleCreateOrganization({ | ||
organizationName: submissionValues?.company, | ||
}); | ||
} | ||
|
||
navigate(ROUTES.GET_STARTED); | ||
}; | ||
|
||
if (!currentUser || loading) { | ||
return <SetupLoader title="Loading..." />; | ||
} else { | ||
|
@@ -95,26 +129,16 @@ export function HubspotSignupForm() { | |
lastname: currentUser?.lastName as string, | ||
email: currentUser?.email as string, | ||
|
||
company: '', | ||
company: existingOrganization ? existingOrganization.name : '', | ||
role___onboarding: '', | ||
heard_about_novu: '', | ||
use_case___onboarding: '', | ||
role___onboarding__other_: '', | ||
heard_about_novu__other_: '', | ||
}} | ||
readonlyProperties={['email']} | ||
focussedProperty="company" | ||
onFormSubmitted={($form, values) => { | ||
const submissionValues = values?.submissionValues as unknown as { | ||
company: string; | ||
role___onboarding: string; | ||
}; | ||
|
||
handleCreateOrganization({ | ||
organizationName: submissionValues?.company, | ||
jobTitle: hubspotRoleToJobTitleMapping[submissionValues?.role___onboarding], | ||
}); | ||
}} | ||
readonlyProperties={existingOrganization ? ['email', 'company'] : ['email']} | ||
focussedProperty={existingOrganization ? 'role___onboarding' : 'company'} | ||
onFormSubmitted={($form, values) => handleFormSubmission(values)} | ||
colorScheme={colorScheme} | ||
/> | ||
); | ||
|
@@ -123,9 +147,11 @@ export function HubspotSignupForm() { | |
|
||
interface IOrganizationCreateForm { | ||
organizationName: string; | ||
} | ||
interface IUserUpdateForm { | ||
firstName: string; | ||
lastName: string; | ||
jobTitle: JobTitleEnum; | ||
domain?: string; | ||
productUseCases?: ProductUseCases; | ||
} | ||
|
||
const hubspotRoleToJobTitleMapping: Record<string, JobTitleEnum> = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,9 +77,7 @@ export function SignUpForm({ invitationToken, email }: SignUpFormProps) { | |
applyToken(token); | ||
|
||
if (invitationToken) { | ||
submitToken(token, invitationToken); | ||
|
||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why did we remove this |
||
submitToken(token, invitationToken, false, true); | ||
} else { | ||
setToken(token); | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's hard to understand what are there
true/false
arguments, please change it to the object, which will improve readability