All files / src/modules/bot/reactionRoles ReactionRoles.repository.ts

7.5% Statements 3/40
0% Branches 0/22
7.14% Functions 1/14
6.06% Lines 2/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173                  3x 8x                                                                                                                                                                                                                                                                                                                                    
import { Services } from "@/types/Constants";
import { Inject, Injectable } from "@nestjs/common";
import { Guild, TextChannel } from "discord.js";
import { PrismaService } from "../../shared/database/prisma/Prisma.service";
import { ReactionRolesEntity } from "./entities/ReactionRole.entity";
import type { IReactionRolesRepository } from "./interfaces/IReactionRoleRepository";
import type { IReaction, REACTION_OPTIONS } from "./types";
 
@Injectable()
export class ReactionRolesRepository implements IReactionRolesRepository {
	public constructor(@Inject(Services.Prisma) private readonly prisma: PrismaService) {}
 
	public async getAll(guild: Guild): Promise<ReactionRolesEntity[]> {
		return await this.prisma.guildReactionRoles.findMany({
			where: { guildId: guild.id },
		});
	}
 
	public async getOne(
		guild: Guild,
		{ Channel, Message, Role, Emoji, Option }: IReaction,
	): Promise<ReactionRolesEntity> {
		const data = await this.getAll(guild);
		return data.filter(async (reaction) => {
			reaction.Channel === Channel &&
				reaction.Message === Message &&
				reaction.Role === Role &&
				reaction.Emoji === Emoji &&
				reaction.Option === Option;
		})[0];
	}
 
	public async getInChannel(guild: Guild, channel: TextChannel): Promise<ReactionRolesEntity[]> {
		const data = await this.getAll(guild);
		return data.filter(async (reaction) => reaction.Channel === channel.id);
	}
 
	private async checkIfExists(guild: Guild, { Channel, Message, Role, Emoji, Option }: IReaction): Promise<boolean> {
		const GetGuild = await this.getAll(guild);
		let Verify = false;
		for (const reaction of GetGuild) {
			if (
				reaction.Channel === Channel &&
				reaction.Message === Message &&
				reaction.Role === Role &&
				reaction.Emoji === Emoji &&
				reaction.Option === Option
			) {
				Verify = true;
			}
		}
 
		return Verify;
	}
 
	public async create(
		guild: Guild,
		{ Channel, Message, Role, Emoji, Option }: IReaction,
	): Promise<{ status: "UnableToCreate" | "Created" }> {
		const data: IReaction = {
			Channel,
			Message,
			Role,
			Emoji,
			Option,
		};
 
		const Check = await this.checkIfExists(guild, data);
 
		if (Check) return { status: "UnableToCreate" };
		await this.prisma.guildReactionRoles
			.create({
				data: {
					Channel,
					Message,
					Role,
					Emoji,
					Option,
					guild: {
						connectOrCreate: {
							where: {
								id: guild.id,
							},
							create: {
								id: guild.id,
								Name: guild.name,
							},
						},
					},
				},
			})
			.catch((err) => {
				if (err) return { status: "UnableToCreate" };
			});
 
		return {
			status: "Created",
		};
	}
 
	public async delete(
		guild: Guild,
		{ Channel, Message, Role, Emoji }: IReaction,
	): Promise<{ status: "UnableToDelete" | "Deleted" }> {
		await this.prisma.guildReactionRoles
			.deleteMany({
				where: {
					guildId: guild.id,
					Channel,
					Message,
					Role,
					Emoji,
				},
			})
			.catch((err) => {
				if (err) return { status: "UnableToDelete" };
			});
		return { status: "Deleted" };
	}
 
	public async deleteMany(guild: Guild): Promise<{ status: "UnableToDelete" | "Deleted"; count: number }> {
		const count = await this.prisma.guildReactionRoles.count({
			where: { guildId: guild.id },
		});
		await this.prisma.guildReactionRoles
			.deleteMany({
				where: {
					guildId: guild.id,
				},
			})
			.catch((err) => {
				if (err) return { status: "UnableToDelete" };
			});
 
		return { status: "Deleted", count };
	}
 
	public async update(
		guild: Guild,
		{ Channel, Message, Role, Emoji, Option }: IReaction,
		newOption: REACTION_OPTIONS,
	): Promise<{
		status: "UnableToUpdate" | "Updated";
		oldOption?: REACTION_OPTIONS;
	}> {
		const data: IReaction = {
			Channel,
			Message,
			Role,
			Emoji,
			Option,
		};
 
		const Check = await this.checkIfExists(guild, data);
		if (!Check) return { status: "UnableToUpdate" };
 
		data.Option = newOption;
		await this.prisma.guildReactionRoles.updateMany({
			where: {
				id: guild.id,
				Channel,
				Message,
				Role,
				Emoji,
				Option,
			},
			data,
		});
 
		return { status: "Updated", oldOption: Option };
	}
}