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

feat: implemented pusher service for real time notifications. #1302

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 0 additions & 22 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,6 @@ todo.txt
.vscode/

# Docker compose
docker-compose.yml
data/
<<<<<<< HEAD
data/
docker-compose.yml
package-lock.json
Expand All @@ -418,22 +415,3 @@ package-lock.json
docker-compose.yml
data/
.dev.env


=======

# Docker compose
docker-compose.yml
data/
>>>>>>> d080450 (chore: created a docker-compose.yml file and updated the gitignore)

data/
docker-compose.yml
package-lock.json
.dev.env


package-lock.json
docker-compose.yml
data/
.dev.env
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"passport-google-oauth20": "^2.0.0",
"passport-jwt": "^4.0.1",
"pg": "^8.13.3",
"pusher": "^5.2.0",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.2",
"sharp": "^0.33.5",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { LanguageGuard } from '@guards/language.guard';
import { ApiStatusModule } from '@modules/api-status/api-status.module';
import { PusherModule } from '@modules/pusher/pusher.module';

@Module({
providers: [
Expand Down Expand Up @@ -174,6 +175,7 @@ import { ApiStatusModule } from '@modules/api-status/api-status.module';
},
}),
ApiStatusModule,
PusherModule,
],
controllers: [HealthController, ProbeController],
})
Expand Down
1 change: 1 addition & 0 deletions src/health.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { skipAuth } from '@shared/helpers/skipAuth';
import { Controller, Get } from '@nestjs/common';
import * as os from 'os';
import { PusherService } from '@modules/pusher/pusher.service';

@Controller()
export default class HealthController {
Expand Down
8 changes: 8 additions & 0 deletions src/modules/pusher/pusher.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { PusherService } from './pusher.service';

@Module({
providers: [PusherService],
exports: [PusherService],
})
export class PusherModule {}
27 changes: 27 additions & 0 deletions src/modules/pusher/pusher.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// src/pusher/pusher.service.ts
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as Pusher from 'pusher';

@Injectable()
export class PusherService {
public pusher: Pusher;

constructor(private readonly configService: ConfigService) {
this.pusher = new Pusher({
appId: this.configService.get<string>('PUSHER_APP_ID'),
key: this.configService.get<string>('PUSHER_APP_KEY'),
secret: this.configService.get<string>('PUSHER_APP_SECRET'),
cluster: this.configService.get<string>('PUSHER_APP_CLUSTER'),
useTLS: true,
});
}

async triggerEvent(channel: string, event: string, data: any) {
try {
await this.pusher.trigger(channel, event, data);
} catch (error) {
throw new Error('Pusher Error');
}
}
}
62 changes: 62 additions & 0 deletions src/modules/pusher/tests/pusher.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PusherService } from '../pusher.service';
import { ConfigService } from '@nestjs/config';
import * as Pusher from 'pusher';

const mockConfigService = {
get: jest.fn((key: string) => {
const config = {
PUSHER_APP_ID: 'testAppId',
PUSHER_APP_KEY: 'testAppKey',
PUSHER_APP_SECRET: 'testAppSecret',
PUSHER_APP_CLUSTER: 'testCluster',
};
return config[key];
}),
};

jest.mock('pusher', () => {
return jest.fn().mockImplementation(() => ({
trigger: jest.fn().mockResolvedValue(true),
}));
});

describe('PusherService', () => {
let pusherService: PusherService;
let pusherInstance: Pusher;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PusherService, { provide: ConfigService, useValue: mockConfigService }],
}).compile();

pusherService = module.get<PusherService>(PusherService);
pusherInstance = module.get<PusherService>(PusherService).pusher;
});

it('should be defined', () => {
expect(pusherService).toBeDefined();
});

it('should trigger event successfully', async () => {
const channel = 'test-channel';
const event = 'test-event';
const data = { message: 'test message' };

await pusherService.triggerEvent(channel, event, data);

expect(pusherInstance.trigger).toHaveBeenCalledWith(channel, event, data);
expect(pusherInstance.trigger).toHaveBeenCalledTimes(1);
});

it('should throw an error if Pusher fails', async () => {
const channel = 'test-channel';
const event = 'test-event';
const data = { message: 'new notification' };

(pusherInstance.trigger as jest.Mock).mockRejectedValue(new Error('Pusher Error'));
await expect(pusherService.triggerEvent(channel, event, data)).rejects.toThrow('Pusher Error');

expect(pusherInstance.trigger).toHaveBeenCalledWith(channel, event, data);
});
});