Skip to content

Commit

Permalink
fix(api): Fix 500 error in /identify (#6815)
Browse files Browse the repository at this point in the history
  • Loading branch information
SokratisVidros authored Oct 31, 2024
1 parent 04e0c2f commit 720ff6b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
7 changes: 4 additions & 3 deletions apps/api/src/app/analytics/analytics.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Post, Headers } from '@nestjs/common';
import { Body, Controller, Post, HttpCode, HttpStatus } from '@nestjs/common';
import { SkipThrottle } from '@nestjs/throttler';
import { AnalyticsService, ExternalApiAccessible, UserSession } from '@novu/application-generic';
import { UserSessionData } from '@novu/shared';
Expand Down Expand Up @@ -35,8 +35,9 @@ export class AnalyticsController {
@Post('/identify')
@ExternalApiAccessible()
@UserAuthentication()
async identifyUser(@Body() body: any, @UserSession() user: UserSessionData): Promise<any> {
return this.hubspotIdentifyFormUsecase.execute(
@HttpCode(HttpStatus.NO_CONTENT)
async identifyUser(@Body() body: any, @UserSession() user: UserSessionData) {
await this.hubspotIdentifyFormUsecase.execute(
HubspotIdentifyFormCommand.create({
email: user.email as string,
lastName: user.lastName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';
import { HubspotIdentifyFormCommand } from './hubspot-identify-form.command';
import { Injectable, Logger } from '@nestjs/common';
import { AxiosError } from 'axios';

const LOG_CONTEXT = 'HubspotIdentifyFormUsecase';

@Injectable()
export class HubspotIdentifyFormUsecase {
Expand All @@ -10,28 +12,38 @@ export class HubspotIdentifyFormUsecase {

constructor(private httpService: HttpService) {}

async execute(command: HubspotIdentifyFormCommand): Promise<{ success: boolean; hubspotResponse?: any }> {
const hubspotSubmitUrl = `https://api.hsforms.com/submissions/v3/integration/submit/${this.hubspotPortalId}/${this.hubspotFormId}`;

const hubspotData = {
fields: [
{ name: 'email', value: command.email },
{ name: 'lastname', value: command.lastName || 'Unknown' },
{ name: 'firstname', value: command.firstName || 'Unknown' },
{ name: 'app_organizationid', value: command.organizationId },
],
context: {
hutk: command.hubspotContext,
pageUri: command.pageUri,
pageName: command.pageName,
},
};
async execute(command: HubspotIdentifyFormCommand) {
try {
const hubspotSubmitUrl = `https://api.hsforms.com/submissions/v3/integration/submit/${this.hubspotPortalId}/${this.hubspotFormId}`;

const response = await firstValueFrom(this.httpService.post(hubspotSubmitUrl, hubspotData));
const hubspotData = {
fields: [
{ name: 'email', value: command.email },
{ name: 'lastname', value: command.lastName || 'Unknown' },
{ name: 'firstname', value: command.firstName || 'Unknown' },
{ name: 'app_organizationid', value: command.organizationId },
],
context: {
hutk: command.hubspotContext,
pageUri: command.pageUri,
pageName: command.pageName,
},
};

return {
success: true,
hubspotResponse: response.data,
};
await this.httpService.post(hubspotSubmitUrl, hubspotData);
} catch (error) {
if (error instanceof AxiosError) {
Logger.error(
`Failed to submit to Hubspot message=${error.message}, status=${error.status}`,
{
organizationId: command.organizationId,
response: error.response?.data,
},
LOG_CONTEXT
);
} else {
throw error;
}
}
}
}
4 changes: 1 addition & 3 deletions apps/web/src/api/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { api } from './api.client';

export const identifyUser = async (userData) => {
try {
const response = await api.post('/v1/telemetry/identify', userData);

return response.data;
await api.post('/v1/telemetry/identify', userData);
} catch (error) {
console.error('Error identifying user:', error);
}
Expand Down

0 comments on commit 720ff6b

Please sign in to comment.