Skip to content

Commit

Permalink
create crisp listener service
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes committed Oct 28, 2024
1 parent e187925 commit 9b7d5b4
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 37 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AuthModule } from './auth/auth.module';
import { CoursePartnerModule } from './course-partner/course-partner.module';
import { CourseUserModule } from './course-user/course-user.module';
import { CourseModule } from './course/course.module';
import { CrispListenerModule } from './crisp-listener/crisp-listener.module';
import { CrispModule } from './crisp/crisp.module';
import { EventLoggerModule } from './event-logger/event-logger.module';
import { FeatureModule } from './feature/feature.module';
Expand Down Expand Up @@ -45,6 +46,7 @@ import { WebhooksModule } from './webhooks/webhooks.module';
EventLoggerModule,
HealthModule,
CrispModule,
CrispListenerModule,
],
})
export class AppModule {}
13 changes: 13 additions & 0 deletions src/crisp-listener/crisp-listener.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CrispService } from 'src/crisp/crisp.service';
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 { CrispListenerService } from './crisp-listener.service';

@Module({
imports: [TypeOrmModule.forFeature([EventLogEntity, UserEntity])],
providers: [CrispService, CrispListenerService, EventLoggerService],
})
export class CrispListenerModule {}
48 changes: 48 additions & 0 deletions src/crisp-listener/crisp-listener.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import Crisp from 'crisp-api';
import { EVENT_NAME } from 'src/crisp/crisp.interface';
import { CrispService } from 'src/crisp/crisp.service';
import { CrispEventDto } from 'src/crisp/dtos/crisp.dto';
import { crispPluginId, crispPluginKey } from 'src/utils/constants';
const CrispClient = new Crisp();
const logger = new Logger('CrispLogger');

// This service is split from CrispService due to CrispService being imported/initiated multiple times
// To avoid creating duplicate listeners and events, this CrispListenerService was decoupled
@Injectable()
export class CrispListenerService implements OnModuleInit {
constructor(private crispService: CrispService) {
CrispClient.authenticateTier('plugin', crispPluginId, crispPluginKey);
}

onModuleInit() {
logger.log(`Crisp service initiated`);

try {
const handleCrispEvent = async (message, eventName) =>
await this.crispService.handleCrispEvent(message, eventName);

CrispClient.on('message:send', async function (message: CrispEventDto) {
handleCrispEvent(message, EVENT_NAME.CHAT_MESSAGE_SENT);
})
.then(function () {
logger.log('Crisp service listening to sent messages');
})
.catch(function (error) {
logger.error('Crisp service failed listening to sent messages:', error);
});

CrispClient.on('message:received', function (message: CrispEventDto) {
handleCrispEvent(message, EVENT_NAME.CHAT_MESSAGE_RECEIVED);
})
.then(function () {
logger.log('Crisp service listening to received messages');
})
.catch(function (error) {
logger.error('Crisp service failed listening to sent messages:', error);
});
} catch (error) {
logger.error('Crisp service failed to initiate:', error);
}
}
}
4 changes: 2 additions & 2 deletions src/crisp/crisp.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export interface CrispProfileCustomFields {
export interface CrispProfileBase {
email?: string;
person?: {
nickname: string;
locales: string[];
nickname?: string;
locales?: string[];
};
segments?: string[];
notepad?: string;
Expand Down
1 change: 0 additions & 1 deletion src/crisp/crisp.service.spec.ts

This file was deleted.

36 changes: 3 additions & 33 deletions src/crisp/crisp.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { Injectable, Logger } 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';
Expand All @@ -18,42 +18,11 @@ const CrispClient = new Crisp();
const logger = new Logger('CrispLogger');

@Injectable()
export class CrispService implements OnModuleInit {
export class CrispService {
constructor(private eventLoggerService: EventLoggerService) {
CrispClient.authenticateTier('plugin', crispPluginId, crispPluginKey);
}

onModuleInit() {
logger.log(`Crisp service initiated`);

try {
const handleCrispEvent = async (message, eventName) =>
await this.handleCrispEvent(message, eventName);

CrispClient.on('message:send', async function (message: CrispEventDto) {
handleCrispEvent(message, EVENT_NAME.CHAT_MESSAGE_SENT);
})
.then(function () {
logger.log('Crisp service listening to sent messages');
})
.catch(function (error) {
logger.error('Crisp service failed listening to sent messages:', error);
});

CrispClient.on('message:received', function (message: CrispEventDto) {
handleCrispEvent(message, EVENT_NAME.CHAT_MESSAGE_RECEIVED);
})
.then(function () {
logger.log('Crisp service listening to received messages');
})
.catch(function (error) {
logger.error('Crisp service failed listening to sent messages:', error);
});
} catch (error) {
logger.error('Crisp service failed to initiate:', error);
}
}

async handleCrispEvent(message: CrispEventDto, eventName: EVENT_NAME) {
try {
const sessionMetaData = await CrispClient.website.getConversationMetas(
Expand All @@ -71,6 +40,7 @@ export class CrispService implements OnModuleInit {
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
2 changes: 1 addition & 1 deletion src/service-user-profiles/service-user-profiles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class ServiceUserProfilesService {
this.createCompleteMailchimpUserProfile(userWithRelations);
logger.log(`Created and updated service user profiles user. Email: ${email}`);
}
logger.error(`Update service user profiles user error - ${error}`);
logger.error(`Update service user profiles user error - ${JSON.stringify(error)}`);
}
}

Expand Down

0 comments on commit 9b7d5b4

Please sign in to comment.