Skip to content

Commit 6f1f45a

Browse files
authored
fix: split crisp origin report routes (#695)
1 parent cde6bbc commit 6f1f45a

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

src/crisp/crisp.controller.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, UseGuards } from '@nestjs/common';
1+
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
22
import { ApiTags } from '@nestjs/swagger';
33
import { SuperAdminAuthGuard } from 'src/partner-admin/super-admin-auth.guard';
44
import { CrispService } from './crisp.service';
@@ -8,9 +8,15 @@ import { CrispService } from './crisp.service';
88
export class CrispController {
99
constructor(private readonly crispService: CrispService) {}
1010

11-
@Get('/analytics-message-origin')
11+
@Get('/conversations')
1212
@UseGuards(SuperAdminAuthGuard)
13-
async getCrispMessageOriginAnalytics() {
14-
return this.crispService.getCrispMessageOriginAnalytics();
13+
async getAllConversationSessionIds() {
14+
return this.crispService.getAllConversationSessionIds();
15+
}
16+
17+
@Post('/analytics-message-origin')
18+
@UseGuards(SuperAdminAuthGuard)
19+
async getCrispMessageOriginAnalytics(@Body() sessionIds: string[]) {
20+
return this.crispService.getCrispMessageOriginAnalytics(sessionIds);
1521
}
1622
}

src/crisp/crisp.service.ts

+33-20
Original file line numberDiff line numberDiff line change
@@ -140,44 +140,57 @@ export class CrispService {
140140
}
141141
}
142142

143-
async getCrispMessageOriginAnalytics() {
143+
// Supports getCrispMessageOriginAnalytics by splitting out logic to get all session IDs
144+
// Combining the logic into one request causes a request timeout as it takes >30 seconds
145+
async getAllConversationSessionIds() {
144146
const messageSentEvents = await this.eventLoggerService.getMessageSentEventLogs();
145147
const userEmails = [...new Set(messageSentEvents.flatMap((event) => event.user.email))];
146-
147-
let totalEmailOrigin = 0;
148-
let totalChatOrigin = 0;
149-
148+
const sessionIds: string[] = [];
150149
for (const userEmail of userEmails) {
151150
try {
152151
const conversations = await CrispClient.website.listPeopleConversations(
153152
crispWebsiteId,
154153
userEmail,
155154
);
156-
157-
for (const conversation of conversations) {
158-
const messages = await CrispClient.website.getMessagesInConversation(
159-
crispWebsiteId,
160-
conversation,
161-
);
162-
163-
for (const message of messages) {
164-
if (message.from === 'user') {
165-
if (message.origin === 'chat') totalChatOrigin++;
166-
if (message.origin === 'email') totalEmailOrigin++;
167-
}
168-
}
169-
}
155+
sessionIds.push(...conversations);
170156
} catch (error) {
171157
// skip
172158
console.log(error);
173159
}
174160
}
161+
return sessionIds;
162+
}
163+
164+
// Returns an analytics string containing the number/percentage of crisp messages
165+
// sent by email vs within the chat widget
166+
async getCrispMessageOriginAnalytics(sessionIds) {
167+
let totalEmailOrigin = 0;
168+
let totalChatOrigin = 0;
169+
170+
try {
171+
for (const sessionId of sessionIds) {
172+
const messages = await CrispClient.website.getMessagesInConversation(
173+
crispWebsiteId,
174+
sessionId,
175+
);
176+
177+
for (const message of messages) {
178+
if (message.from === 'user') {
179+
if (message.origin === 'chat') totalChatOrigin++;
180+
if (message.origin === 'email') totalEmailOrigin++;
181+
}
182+
}
183+
}
184+
} catch (error) {
185+
// skip
186+
console.log(error);
187+
}
175188
const totalMessages = totalEmailOrigin + totalChatOrigin;
176189
const chatPercentage =
177190
totalMessages === 0 ? 0 : Math.round((totalChatOrigin / totalMessages) * 100);
178191
const emailPercentage =
179192
totalMessages === 0 ? 0 : Math.round((totalEmailOrigin / totalMessages) * 100);
180193

181-
return `Crisp message origin report: ${totalChatOrigin} (${chatPercentage}%) chat origin, ${totalEmailOrigin} (${emailPercentage}%) email origin`;
194+
return `Crisp message origin report: ${totalChatOrigin} (${chatPercentage}%) chat widget origin, ${totalEmailOrigin} (${emailPercentage}%) email origin`;
182195
}
183196
}

0 commit comments

Comments
 (0)