Skip to content

Commit

Permalink
add guild service
Browse files Browse the repository at this point in the history
  • Loading branch information
radekm2000 committed Jun 1, 2024
1 parent ef7ddee commit a24b0ec
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 20 deletions.
2 changes: 2 additions & 0 deletions server/ecommerce/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { FeedbacksModule } from './feedbacks/feedbacks.module';
import { DiscordBotModule } from './discord-bot/discord-bot.module';
import { DiscordNotificationsModule } from './discord-notifications/discord-notifications.module';
import { ItemNotifierModule } from './discord-bot/src/commands/notifiers/item-notifier.module';
import { DiscordGuildModule } from './discord-guild/discord-guild.module';

@Module({
imports: [
Expand All @@ -41,6 +42,7 @@ import { ItemNotifierModule } from './discord-bot/src/commands/notifiers/item-no
DiscordBotModule,
DiscordNotificationsModule,
ItemNotifierModule,
DiscordGuildModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
5 changes: 4 additions & 1 deletion server/ecommerce/src/discord-bot/discord-bot.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ import { ProductNotification } from 'src/utils/entities/product-notification.ent
import { FollowersService } from 'src/followers/followers.service';
import { ItemNotifierService } from './src/commands/notifiers/item-notifier.service';
import { DiscordNotificationsService } from 'src/discord-notifications/discord-notifications.service';
import { IProductsService } from 'src/spi/products';
import { DiscordGuildModule } from 'src/discord-guild/discord-guild.module';
import { DiscordGuildService } from 'src/discord-guild/discord-guild.service';

@Module({
controllers: [DiscordBotController],
providers: [
DiscordBotService,
UsersService,
ProductsService,
{ provide: IProductsService, useClass: ProductsService },
ProductNotificationService,
FollowersService,
ItemNotifierService,
Expand Down
14 changes: 10 additions & 4 deletions server/ecommerce/src/discord-bot/discord-bot.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import {
Inject,
Injectable,
OnModuleDestroy,
OnModuleInit,
} from '@nestjs/common';
import { Client, IntentsBitField } from 'discord.js';
import { DiscordBot } from './src/discordbot';
import 'dotenv/config';
Expand All @@ -13,17 +18,18 @@ import { TrackedUsersCommand } from './src/commands/trackers/tracked-users';
import { FollowersService } from 'src/followers/followers.service';
import { StartTrackingCommand } from './src/commands/trackers/start-tracking';
import { StopTrackingCommand } from './src/commands/trackers/stop-tracking';
import { IProductsService } from 'src/spi/products';

@Injectable()
export class DiscordBotService implements OnModuleInit {
public discordBot: DiscordBot;
private readonly bot: Client;
private readonly botToken: string;
public readonly bot: Client;
public readonly botToken: string;
private readonly botApplicationId: string;

constructor(
private userService: UsersService,
private productsService: ProductsService,
@Inject(IProductsService) private productsService: IProductsService,
private followersService: FollowersService,
) {
this.botToken = process.env.DISCORD_BOT_TOKEN;
Expand Down
5 changes: 3 additions & 2 deletions server/ecommerce/src/discord-bot/src/commands/inventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import { UsersService } from 'src/users/users.service';
import { ProductsService } from 'src/products/products.service';
import { Product } from 'src/utils/entities/product.entity';
import { DiscordEmbedColors } from '../discord-embeds/colors';
import { IProductsService } from 'src/spi/products';

type Config = {
usersService: UsersService;
productsService: ProductsService;
productsService: IProductsService;
};

export class InventoryCommand implements SlashCommand {
private readonly usersService: UsersService;
private readonly productsService: ProductsService;
private readonly productsService: IProductsService;

constructor(config: Config) {
this.usersService = config.usersService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
import { SlashCommand } from './slash-command';
import { UsersService } from 'src/users/users.service';
import { ProductsService } from 'src/products/products.service';
import { IProductsService } from 'src/spi/products';

const PRODUCT_OPTION_NAME = 'product';

type Config = {
usersService: UsersService;
productService: ProductsService;
productService: IProductsService;
};

export class RemoveProduct implements SlashCommand {
Expand All @@ -27,7 +28,7 @@ export class RemoveProduct implements SlashCommand {
);

private readonly usersService: UsersService;
private readonly productService: ProductsService;
private readonly productService: IProductsService;

constructor(config: Config) {
this.usersService = config.usersService;
Expand Down
1 change: 0 additions & 1 deletion server/ecommerce/src/discord-bot/src/discordbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export class DiscordBot {
private handleCommand = async (interaction: ChatInputCommandInteraction) => {
const command = this.commands.get(interaction.commandName);
if (!command) return;

try {
await command.execute(interaction);
} catch (error) {
Expand Down
54 changes: 54 additions & 0 deletions server/ecommerce/src/discord-guild/discord-guild.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Module } from '@nestjs/common';
import { DiscordGuildService } from './discord-guild.service';
import { DiscordBotService } from 'src/discord-bot/discord-bot.service';
import { UsersService } from 'src/users/users.service';
import { IProductsService } from 'src/spi/products';
import { ProductsService } from 'src/products/products.service';
import { FollowersService } from 'src/followers/followers.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Follow } from 'src/utils/entities/followers.entity';
import { User } from 'src/utils/entities/user.entity';
import { Profile } from 'src/utils/entities/profile.entity';
import { Avatar } from 'src/utils/entities/avatar.entity';
import { Image } from 'src/utils/entities/image.entity';
import { ProductNotification } from 'src/utils/entities/product-notification.entity';
import { Product } from 'src/utils/entities/product.entity';
import { ProductNotificationService } from 'src/product-notification/product-notification.service';
import { ItemNotifierService } from 'src/discord-bot/src/commands/notifiers/item-notifier.service';
import { DiscordNotificationsService } from 'src/discord-notifications/discord-notifications.service';

@Module({
imports: [
TypeOrmModule.forFeature([
Follow,
User,
Profile,
Avatar,
Image,
ProductNotification,
Product,
]),
],
providers: [
DiscordBotService,
UsersService,
ProductNotificationService,
ItemNotifierService,
DiscordNotificationsService,
{
provide: IProductsService,
useClass: ProductsService,
},
FollowersService,
{
provide: DiscordGuildService,
useFactory: (botService: DiscordBotService) =>
new DiscordGuildService({
botClient: botService.bot,
}),
inject: [DiscordBotService],
},
],
exports: [DiscordGuildService],
})
export class DiscordGuildModule {}
82 changes: 82 additions & 0 deletions server/ecommerce/src/discord-guild/discord-guild.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
import { Client, MessageCreateOptions } from 'discord.js';
import 'dotenv/config';

type Config = {
botClient: Client;
};

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

constructor(config: Config) {
this.botClient = config.botClient;
this.logger = new Logger(DiscordGuildService.name);
this.guildId = process.env.GUILD_ID ?? '';
}

public assignRoles = async (userId: string, roleIds: string[]) => {
if (roleIds.length === 0) {
return;
}

roleIds = roleIds.filter((roleId) => !this.hasRole(userId, roleId));

const member = await this.getGuildMember(userId);
try {
await member.roles.add(roleIds);
} catch (error) {
this.logger.error(
`Error while trying to assign roles for user ${userId}`,
);
}
};

private hasRole = async (userId: string, roleId: string) => {
try {
const member = await this.getGuildMember(userId);

return !!member && member.roles.cache.some((role) => role.id === roleId);
} catch (error) {
this.logger.error(`Error while getting roles for user ${userId}`);
}
};

private getGuildMember = async (userId: string) => {
const guild = await this.getGuild();

try {
return (
guild.members.cache.get(userId) ?? (await guild.members.fetch(userId))
);
} catch (error) {
this.logger.error('Error when getting guild member');
}
};

private getGuild = async () => {
try {
return (
this.botClient.guilds.cache.get(this.guildId) ??
(await this.botClient.guilds.fetch(this.guildId))
);
} catch (error) {
this.logger.error(`Error when getting guild ${this.guildId}`);
return undefined;
}
};

public sendMessageToMember = async (
userId: string,
message: MessageCreateOptions,
) => {
try {
await this.botClient.users.send(userId, message);
} catch (error) {
this.logger.error(`Error when trying to send message to ${userId}`);
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { Image } from 'src/utils/entities/image.entity';
import { ItemNotifier } from 'src/discord-bot/src/commands/notifiers/item-notifier';
import { ItemNotifierService } from 'src/discord-bot/src/commands/notifiers/item-notifier.service';
import { DiscordNotificationsService } from 'src/discord-notifications/discord-notifications.service';
import { IProductsService } from 'src/spi/products';
import { DiscordGuildService } from 'src/discord-guild/discord-guild.service';
import { DiscordGuildModule } from 'src/discord-guild/discord-guild.module';

@Module({
imports: [
Expand All @@ -31,7 +34,7 @@ import { DiscordNotificationsService } from 'src/discord-notifications/discord-n
providers: [
ProductNotificationService,
UsersService,
ProductsService,
{ provide: IProductsService, useClass: ProductsService },
ItemNotifierService,
DiscordNotificationsService,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
forwardRef,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ProductsService } from 'src/products/products.service';
import { IProductsService } from 'src/spi/products';
import { UsersService } from 'src/users/users.service';
import { ProductNotification } from 'src/utils/entities/product-notification.entity';
import { Product } from 'src/utils/entities/product.entity';
Expand All @@ -18,8 +18,8 @@ export class ProductNotificationService {
@InjectRepository(ProductNotification)
private productNotificationRepository: Repository<ProductNotification>,
private usersService: UsersService,
@Inject(forwardRef(() => ProductsService))
private productsService: ProductsService,
@Inject(forwardRef(() => IProductsService))
private productsService: IProductsService,
) {}
async notifyFollowersAboutNewProduct(product: Product) {
const existingProductWithImage = await this.productsService.findProduct(
Expand Down
4 changes: 3 additions & 1 deletion server/ecommerce/src/products/products.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Controller,
Delete,
Get,
Inject,
Param,
ParseIntPipe,
Post,
Expand All @@ -29,11 +30,12 @@ import { ZodValidationPipe } from 'src/utils/pipes/ZodValidationPipe';
import { Response } from 'express';
import { StripeService } from 'src/stripe/stripe.service';
import { ParseAndValidateProductPipe } from 'src/utils/pipes/ParseAndValidateProductPipe';
import { IProductsService } from 'src/spi/products';

@Controller('products')
export class ProductsController {
constructor(
private productsService: ProductsService,
@Inject(IProductsService) private productsService: IProductsService,
private stripeService: StripeService,
) {}

Expand Down
7 changes: 5 additions & 2 deletions server/ecommerce/src/products/products.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import { NodemailerService } from 'src/nodemailer/nodemailer.service';
import { ItemNotifier } from 'src/discord-bot/src/commands/notifiers/item-notifier';
import { DiscordNotificationsService } from 'src/discord-notifications/discord-notifications.service';
import { ItemNotifierService } from 'src/discord-bot/src/commands/notifiers/item-notifier.service';
import { IProductsService } from 'src/spi/products';
import { DiscordGuildService } from 'src/discord-guild/discord-guild.service';
import { DiscordGuildModule } from 'src/discord-guild/discord-guild.module';

@Module({
controllers: [ProductsController],
providers: [
ProductsService,
{ provide: IProductsService, useClass: ProductsService },
UsersService,
ProductNotificationService,
StripeService,
Expand All @@ -41,6 +44,6 @@ import { ItemNotifierService } from 'src/discord-bot/src/commands/notifiers/item
]),
FollowersModule,
],
exports: [ProductsService],
exports: [IProductsService],
})
export class ProductsModule {}
4 changes: 3 additions & 1 deletion server/ecommerce/src/products/products.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { UsersService } from 'src/users/users.service';
import 'dotenv/config';
import { ItemNotifierService } from 'src/discord-bot/src/commands/notifiers/item-notifier.service';
import { User } from 'src/utils/entities/user.entity';
import { IProductsService } from 'src/spi/products';
import { DiscordGuildService } from 'src/discord-guild/discord-guild.service';

const s3 = new S3Client({
region: process.env.BUCKET_REGION,
Expand All @@ -29,7 +31,7 @@ const s3 = new S3Client({
},
});
@Injectable()
export class ProductsService {
export class ProductsService implements IProductsService {
logger: Logger;

constructor(
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions server/ecommerce/src/tests/products/products.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ProductsService } from 'src/products/products.service';
import { Product } from 'src/utils/entities/product.entity';

describe('ProductsService getMenFilteredProducts method', () => {
let productsService: ProductsService;
let productsService: any;
let productRepository: any;
let productNotificationService: any;
let imageRepository: any;
Expand Down

0 comments on commit a24b0ec

Please sign in to comment.