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

Prevent crisp resolved events #689

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/crisp/crisp.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Injectable, Logger } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import Crisp from 'crisp-api';
import { sendMailchimpUserEvent } from 'src/api/mailchimp/mailchimp-api';
import { MAILCHIMP_CUSTOM_EVENTS } from 'src/api/mailchimp/mailchimp-api.interfaces';
import { EventLoggerService } from 'src/event-logger/event-logger.service';
import { crispPluginId, crispPluginKey, crispWebsiteId } from 'src/utils/constants';
import {
Expand All @@ -15,7 +13,6 @@ import {
import { CrispEventDto } from './dtos/crisp.dto';

const CrispClient = new Crisp();
const logger = new Logger('CrispLogger');

@Injectable()
export class CrispService {
Expand All @@ -25,6 +22,16 @@ export class CrispService {

async handleCrispEvent(message: CrispEventDto, eventName: EVENT_NAME) {
try {
if (
eventName === EVENT_NAME.CHAT_MESSAGE_RECEIVED &&
typeof message.content !== 'string' &&
'namespace' in message.content
) {
// When a conversation is resolved on crisp, the message:received event is fired with
// message.content.namespace = "state:resolved"
// Prevent our events being logged for conversation resolved events
return;
}
const sessionMetaData = await CrispClient.website.getConversationMetas(
message.website_id,
message.session_id,
Expand All @@ -34,14 +41,6 @@ export class CrispService {
event: eventName,
date: new Date(),
});

if (eventName === EVENT_NAME.CHAT_MESSAGE_RECEIVED) {
sendMailchimpUserEvent(
sessionMetaData.email,
MAILCHIMP_CUSTOM_EVENTS.CRISP_MESSAGE_RECEIVED,
);
logger.log('Crisp service: CRISP_MESSAGE_RECEIVED event sent to mailchimp');
}
} catch (error) {
throw new Error(`Failed to handle crisp event for ${eventName}: ${error}`);
}
Expand Down
Loading