Skip to content

Commit

Permalink
fix event emitter triggering event multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
radekm2000 committed Jun 5, 2024
1 parent 8f6f93e commit 71eb17b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 39 deletions.
9 changes: 0 additions & 9 deletions server/ecommerce/src/discord-bot/discord-bot.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ import { DiscordGuildService } from 'src/discord-guild/discord-guild.service';
FollowersService,
ItemNotifierService,
DiscordNotificationsService,
{
provide: DiscordGuildService,
useFactory: (botService: DiscordBotService, usersService: UsersService) =>
new DiscordGuildService({
botClient: botService.bot,
usersService: usersService,
}),
inject: [DiscordBotService, UsersService],
},
],

imports: [
Expand Down
10 changes: 1 addition & 9 deletions server/ecommerce/src/discord-guild/discord-guild.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,7 @@ import { DiscordNotificationsService } from 'src/discord-notifications/discord-n
provide: IProductsService,
useClass: ProductsService,
},
{
provide: DiscordGuildService,
useFactory: (botService: DiscordBotService, usersService: UsersService) =>
new DiscordGuildService({
botClient: botService.bot,
usersService: usersService,
}),
inject: [DiscordBotService, UsersService],
},
DiscordGuildService,
DiscordBotService,

FollowersService,
Expand Down
22 changes: 12 additions & 10 deletions server/ecommerce/src/discord-guild/discord-guild.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
import {
Inject,
Injectable,
Logger,
OnApplicationBootstrap,
} from '@nestjs/common';
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
import { Client, EmbedBuilder, MessageCreateOptions } from 'discord.js';
import 'dotenv/config';
import { DiscordBotService } from 'src/discord-bot/discord-bot.service';
import { DiscordEvents } from 'src/events/constants/events';
import { UsersService } from 'src/users/users.service';

Expand All @@ -10,21 +16,17 @@ export type AssignDiscordRoleEvent = {
discordRoleId: string;
};

type Config = {
botClient: Client;
usersService: UsersService;
};

@Injectable()
export class DiscordGuildService {
private readonly botClient: Client;
private readonly guildId: string;
private readonly logger: Logger;
private readonly usersService: UsersService;

constructor(config: Config) {
this.usersService = config.usersService;
this.botClient = config.botClient;
constructor(
@Inject(UsersService) private usersService: UsersService,
@Inject(DiscordBotService) private discordBotService: DiscordBotService,
) {
this.botClient = this.discordBotService.bot;
this.logger = new Logger(DiscordGuildService.name);
this.guildId = process.env.GUILD_ID ?? '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,8 @@ import { FollowersService } from 'src/followers/followers.service';
{ provide: IProductsService, useClass: ProductsService },
ItemNotifierService,
DiscordNotificationsService,
{
provide: DiscordGuildService,
useFactory: (botService: DiscordBotService, usersService: UsersService) =>
new DiscordGuildService({
botClient: botService.bot,
usersService: usersService,
}),
inject: [DiscordBotService, UsersService],
},
DiscordBotService,

FollowersService,
],
exports: [ProductNotificationService],
Expand Down
8 changes: 7 additions & 1 deletion server/ecommerce/src/tests/reviews/reviews.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('reviews service ADD REVIEW', () => {
reviewRepositoryMock,
usersServiceMock,
{} as any,
{} as any,
);

await expect(
Expand Down Expand Up @@ -66,14 +67,19 @@ describe('reviews service ADD REVIEW', () => {
save: jest.fn().mockResolvedValue(review),
};

const eventEmitter = {
emit: jest.fn().mockResolvedValue(true),
};

const reviewService = new ReviewsService(
reviewRepositoryMock,
usersServiceMock,
eventEmitter as any,
{} as any,
);

expect(
await reviewService.addReview(reviewDto, reviewRecipient.id),
).toEqual(review);
).toEqual(true);
});
});
2 changes: 1 addition & 1 deletion server/ecommerce/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
S3Client,
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { randomUUID } from 'crypto';
import { Profile } from 'passport-google-oauth20';
Expand Down

0 comments on commit 71eb17b

Please sign in to comment.