From 1f5a19f10169c4610374a54009a4c395d8bdd9d4 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 5 Mar 2025 19:05:10 +0000 Subject: [PATCH] WIP: Implement adding to the users inventory on confirm --- src/buttonEvents/Effects/Buy.ts | 28 ++++++++++++++++++++++++++ tests/buttonEvents/Effects/Buy.test.ts | 4 ++++ 2 files changed, 32 insertions(+) diff --git a/src/buttonEvents/Effects/Buy.ts b/src/buttonEvents/Effects/Buy.ts index 1dad272..473eb08 100644 --- a/src/buttonEvents/Effects/Buy.ts +++ b/src/buttonEvents/Effects/Buy.ts @@ -2,6 +2,8 @@ import {ButtonInteraction} from "discord.js"; import AppLogger from "../../client/appLogger"; import EffectHelper from "../../helpers/EffectHelper"; import EmbedColours from "../../constants/EmbedColours"; +import User from "../../database/entities/app/User"; +import {EffectDetails} from "../../constants/EffectDetails"; export default class Buy { public static async Execute(interaction: ButtonInteraction) { @@ -27,6 +29,13 @@ export default class Buy { AppLogger.LogError("Buy Confirm", "Not enough parameters"); return; } + + const effectDetail = EffectDetails.get(id); + + if (!effectDetail) { + AppLogger.LogError("Buy Confirm", "Effect detail not found!"); + return; + } const quantityNumber = Number(quantity); @@ -35,6 +44,25 @@ export default class Buy { return; } + const totalCost = effectDetail.cost * quantityNumber; + + const user = await User.FetchOneById(User, interaction.user.id); + + if (!user) { + AppLogger.LogError("Buy Confirm", "Unable to find user"); + return; + } + + if (user.Currency < totalCost) { + interaction.reply(`You don't have enough currency to buy this! You have \`${user.Currency} Currency\` and need \`${totalCost} Currency\`!`); + return; + } + + user.RemoveCurrency(totalCost); + await user.Save(User, user); + + await EffectHelper.AddEffectToUserInventory(interaction.user.id, id, quantityNumber); + const generatedEmbed = await EffectHelper.GenerateEffectBuyEmbed(interaction.user.id, id, quantityNumber, true); if (typeof generatedEmbed == "string") { diff --git a/tests/buttonEvents/Effects/Buy.test.ts b/tests/buttonEvents/Effects/Buy.test.ts index 06f701c..6c7ae6b 100644 --- a/tests/buttonEvents/Effects/Buy.test.ts +++ b/tests/buttonEvents/Effects/Buy.test.ts @@ -244,6 +244,10 @@ describe("Confirm", () => { expect(interaction.update).not.toHaveBeenCalled(); }); + test.todo("GIVEN user is not found, EXPECT error"); + + test.todo("GIVEN user does not have enough currency, EXPECT error"); + test("GIVEN GenerateEffectBuyEmbed returns with a string, EXPECT error replied", async () => { // Assert interaction.customId += " id 1";