From a43750080acb1e9f76c4acfe9407cd256287056b Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Thu, 20 Oct 2022 13:12:53 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20API=20is=20now=20fully=20o?= =?UTF-8?q?n=20Prisma?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/credits/modules/giveaway/index.ts | 65 +++++++++++++++++-- src/models/api.ts | 51 --------------- 2 files changed, 58 insertions(+), 58 deletions(-) delete mode 100644 src/models/api.ts diff --git a/src/commands/manage/modules/credits/modules/giveaway/index.ts b/src/commands/manage/modules/credits/modules/giveaway/index.ts index e32a2ba..db3beb5 100644 --- a/src/commands/manage/modules/credits/modules/giveaway/index.ts +++ b/src/commands/manage/modules/credits/modules/giveaway/index.ts @@ -12,8 +12,9 @@ import { import { v4 as uuidv4 } from "uuid"; import encryption from "../../../../../../helpers/encryption"; // Configurations +import prisma from "../../../../../../handlers/database"; import getEmbedConfig from "../../../../../../helpers/getEmbedData"; -import apiSchema from "../../../../../../models/api"; +import logger from "../../../../../../middlewares/logger"; // Function export default { @@ -51,7 +52,7 @@ export default { const { successColor, footerText, footerIcon } = await getEmbedConfig( interaction.guild ); // Destructure - const { guild, options } = interaction; + const { guild, user, options } = interaction; const uses = options?.getInteger("uses"); const creditAmount = options?.getInteger("credit"); @@ -60,6 +61,7 @@ export default { if (!uses) throw new Error("Amount of uses is required."); if (!creditAmount) throw new Error("Amount of credits is required."); if (!channel) throw new Error("Channel is required."); + if (!guild) throw new Error("Guild is required."); const embed = new EmbedBuilder() .setTitle("[:toolbox:] Giveaway") @@ -67,18 +69,67 @@ export default { const code = uuidv4(); - const apiCredentials = await apiSchema?.findOne({ - guildId: guild?.id, + const createGuildMember = await prisma.guildMember.upsert({ + where: { + userId_guildId: { + userId: user.id, + guildId: guild.id, + }, + }, + update: {}, + create: { + user: { + connectOrCreate: { + create: { + id: user.id, + }, + where: { + id: user.id, + }, + }, + }, + guild: { + connectOrCreate: { + create: { + id: guild.id, + }, + where: { + id: guild.id, + }, + }, + }, + }, + include: { + user: true, + guild: true, + }, }); - if (!apiCredentials) return; + logger.silly(createGuildMember); - const url = encryption.decrypt(apiCredentials?.url); + if ( + !createGuildMember.guild.apiCpggUrlIv || + !createGuildMember.guild.apiCpggUrlContent + ) + throw new Error("No API url available"); + if ( + !createGuildMember.guild.apiCpggTokenIv || + !createGuildMember.guild.apiCpggTokenContent + ) + throw new Error("No API token available"); + + const url = encryption.decrypt({ + iv: createGuildMember.guild.apiCpggUrlIv, + content: createGuildMember.guild.apiCpggUrlContent, + }); const api = axios?.create({ baseURL: `${url}/api/`, headers: { - Authorization: `Bearer ${encryption.decrypt(apiCredentials.token)}`, + Authorization: `Bearer ${encryption.decrypt({ + iv: createGuildMember.guild.apiCpggTokenIv, + content: createGuildMember.guild.apiCpggTokenContent, + })}`, }, }); diff --git a/src/models/api.ts b/src/models/api.ts deleted file mode 100644 index fd5cbd7..0000000 --- a/src/models/api.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { Snowflake } from "discord.js"; -import { model, Schema } from "mongoose"; -import { IEncryptionData } from "../interfaces/EncryptionData"; - -export interface IApi { - guildId: Snowflake; - url: IEncryptionData; - token: IEncryptionData; -} - -const apiSchema = new Schema( - { - guildId: { - type: String, - required: true, - unique: false, - index: true, - }, - url: { - iv: { - type: String, - required: true, - unique: false, - index: true, - }, - content: { - type: String, - required: true, - unique: false, - index: true, - }, - }, - token: { - iv: { - type: String, - required: true, - unique: false, - index: true, - }, - content: { - type: String, - required: true, - unique: false, - index: true, - }, - }, - }, - { timestamps: true } -); - -export default model("api", apiSchema);