diff --git a/src/crisp/crisp.controller.ts b/src/crisp/crisp.controller.ts new file mode 100644 index 00000000..75ba6ba7 --- /dev/null +++ b/src/crisp/crisp.controller.ts @@ -0,0 +1,16 @@ +import { Controller, Get, UseGuards } from '@nestjs/common'; +import { ApiTags } from '@nestjs/swagger'; +import { SuperAdminAuthGuard } from 'src/partner-admin/super-admin-auth.guard'; +import { CrispService } from './crisp.service'; + +@ApiTags('Crisp') +@Controller('crisp') +export class CrispController { + constructor(private readonly crispService: CrispService) {} + + @Get('/analytics-message-origin') + @UseGuards(SuperAdminAuthGuard) + async getCrispMessageOriginAnalytics() { + return this.crispService.getCrispMessageOriginAnalytics(); + } +} diff --git a/src/crisp/crisp.module.ts b/src/crisp/crisp.module.ts index d240140a..5834c8f2 100644 --- a/src/crisp/crisp.module.ts +++ b/src/crisp/crisp.module.ts @@ -3,10 +3,12 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { EventLogEntity } from 'src/entities/event-log.entity'; import { UserEntity } from 'src/entities/user.entity'; import { EventLoggerService } from 'src/event-logger/event-logger.service'; +import { CrispController } from './crisp.controller'; import { CrispService } from './crisp.service'; @Module({ imports: [TypeOrmModule.forFeature([EventLogEntity, UserEntity])], providers: [CrispService, EventLoggerService], + controllers: [CrispController], }) export class CrispModule {} diff --git a/src/crisp/crisp.service.ts b/src/crisp/crisp.service.ts index 57e47dfd..9d3f1add 100644 --- a/src/crisp/crisp.service.ts +++ b/src/crisp/crisp.service.ts @@ -139,4 +139,40 @@ export class CrispService { throw new Error(`Delete cypress crisp profiles API call failed: ${error}`); } } + + async getCrispMessageOriginAnalytics() { + const messageSentEvents = await this.eventLoggerService.getMessageSentEventLogs(); + const userEmails = [...new Set(messageSentEvents.flatMap((event) => event.user.email))]; + + let totalEmailOrigin = 0; + let totalChatOrigin = 0; + + for (const userEmail of userEmails) { + const conversations = await CrispClient.website.listPeopleConversations( + crispWebsiteId, + userEmail, + ); + + for (const conversation of conversations) { + const messages = await CrispClient.website.getMessagesInConversation( + crispWebsiteId, + conversation, + ); + + for (const message of messages) { + if (message.from === 'user') { + if (message.origin === 'chat') totalChatOrigin++; + if (message.origin === 'email') totalEmailOrigin++; + } + } + } + } + const totalMessages = totalEmailOrigin + totalChatOrigin; + const chatPercentage = + totalMessages === 0 ? 0 : Math.round((totalChatOrigin / totalMessages) * 100); + const emailPercentage = + totalMessages === 0 ? 0 : Math.round((totalEmailOrigin / totalMessages) * 100); + + return `Crisp message origin report: ${totalChatOrigin} (${chatPercentage}%) chat origin, ${totalEmailOrigin} (${emailPercentage}%) email origin`; + } } diff --git a/src/event-logger/event-logger.service.ts b/src/event-logger/event-logger.service.ts index 79f59d5f..710abd9c 100644 --- a/src/event-logger/event-logger.service.ts +++ b/src/event-logger/event-logger.service.ts @@ -3,7 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm'; import { EventLogEntity } from 'src/entities/event-log.entity'; import { UserEntity } from 'src/entities/user.entity'; import { Repository } from 'typeorm'; -import { ICreateEventLog } from './event-logger.interface'; +import { EVENT_NAME, ICreateEventLog } from './event-logger.interface'; const logger = new Logger('EventLogger'); @@ -20,6 +20,13 @@ export class EventLoggerService { return await this.eventLoggerRepository.findOneBy({ id }); } + async getMessageSentEventLogs(): Promise { + return await this.eventLoggerRepository.find({ + where: { event: EVENT_NAME.CHAT_MESSAGE_SENT }, + relations: { user: true }, + }); + } + async createEventLog({ email, userId, event, date }: ICreateEventLog) { try { if (!userId && !email) {